From: Dan Bonachea (bonachea_at_cs_dot_berkeley_dot_edu)
Date: Tue Jun 22 2004 - 17:58:24 PDT
At 10:20 AM 6/21/2004, P Mehta wrote:
>Hello,
>
> Another implementation based question for UPC. I am trying to build a
>simple queue that can be shared among threads, but I'm running into some
>issues. The queue is a structure of (for simplicity) one pointer to a
>(possible shared) array of integers, and three other integers.
>
> I want to be able to allocate the structure dynamically in one thread,
>and have it accessible by all threads later. From what I understand (and
>please correct me if I'm wrong), this would require a upc_global_alloc
>call and that a shared pointer-to-shared (ie. shared struct queue
>*shared).
>
> Unfortunately there are a number of complications that I cannot seem to
>resolve. For example, in this case, should the pointer within the
>structure be a pointer to shared, or is the sharing resolved by making the
>pointer to the structure a shared pointer? I've found that I can only
>make the pointer a shared variable, and I am not allowed to make the other
>three integers 'shared int's. Additionally, I've found that to create a
>shared pointer-to-shared, the declaration must be made statically. Is
>this the case; am I limited to static declarations?
You cannot place a "shared int" inside a struct - when you allocate a struct,
either all the fields are shared or none of them are, and this is determined
by the call or declaration which allocates the struct, not by placing shared
qualifiers on the fields. For example in this code:
struct S {
int x;
int y;
shared int *p;
}
shared struct S myStruct;
struct S myLocalStruct;
myStruct.x and myStruct.y are shared ints, and myStruct.p is a shared
pointer-to-shared.
myLocalStruct.x and myLocalStruct.y are private ints, and myLocalStruct.p is a
private pointer-to-shared.
The equivalent dynamic allocations can also be accomplished with any of the
following:
shared struct S *pmyStruct1 = upc_all_alloc(1, sizeof(struct S));
shared struct S *pmyStruct2 = upc_global_alloc(1, sizeof(struct S));
shared struct S *pmyStruct3 = upc_alloc(sizeof(struct S));
struct S *myLocalStruct = malloc(sizeof(struct S))
You cannot declare automatic variables to be shared - specifically, local
stack variables can never themselves be shared (although they may be
pointers-to-shared). Any global scope or static local variables are permitted
to be shared.
> Also, are there some pointer declaration cases in which one of the
>allocation functions will work and another will not? For example, is
>there a case where the allocation must be a upc_all_alloc, and making an
>analogous (eg. single thread) upc_global_alloc will not work?
upc_global_alloc can be used anywhere that upc_all_alloc is used - the only
semantic difference between the two is that upc_all_alloc is collective and
all threads get a pointer to the same shared allocation, whereas
upc_global_alloc is non-collective and each calling thread creates its own
shared allocation.
See section 6.4.3 of the language spec for details.
Hope this helps..
Dan