Question

I am programming in UPC and have an array shared between two threads. Each thread has private pointers to these shared areas:

#define SIZE 10000
#define HALFSIZE (SIZE/2)

shared [ HALFSIZE ] int table [ SIZE ]; /* all areas */
shared [ HALFSIZE ] int *first_area_pt; /* points to first thread area */
shared [ HALFSIZE ] int *second_area_pt; /* points to second thread area */

Now I want not 2, but N threads, N areas and N pointers. So I need an array of these pointers:

shared [ HALFSIZE ] int *first_area_pt;
shared [ HALFSIZE ] int *second_area_pt;

How should I define it?

Was it helpful?

Solution

Since the notation you're using is non-standard (although I gather it follows the UPC - Unified Parallel C - specification), we can only guess at what you might need. It would be helpful to highlight what you're using because (when) it is unusual.

This looks superficially plausible, under one possible interpretation of the shared [ N ] notation.

#define SIZE 10000
#define NUMTHREADS 25
#define FRACSIZE (SIZE/NUMTHREADS)

shared [ FRACSIZE ] int table[SIZE];
shared [ 1 ]        int *area_ptr[NUMTHREADS];

OTHER TIPS

Assuming you are using the static-threads compilation environment, a more succinct way to declare your array would be something like this:

#define SIZE 10000

shared [*] int table [ SIZE ]; /* data automatically blocked across THREADS */

shared [1] int *base = (shared [1] int *)&table;

Then you can use the cyclic base pointer to create a pointer-to-shared referencing the data with affinity to thread X with an expression like:

  shared [] int *threadXdata = (shared [] int *)(base+X);

With this approach you never need to instantiate storage for an array of THREADS pointers. However if you really want that you can certainly declare and initialize one with something like this:

  shared [] int *threadptr[THREADS];
  for (int i=0; i < THREADS; i++) threadptr[i] = (shared [] int *)(base+i);
  ...
  threadptr[4][10] = 42; /* example access to element 10 on thread 4*/

Here threadptr is a local array of pointers serving as a directory of references to each thread's data.

Note all of the above use an indefinitely-blocked pointer-to-shared for accessing the elements with affinity to a single thread, since each block is logically one contiguous chunk of data with no wrapping across threads.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top