3

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?

ciembor
  • 7,189
  • 13
  • 59
  • 100
  • Depends on what you are trying to accomplish. You didn't tell us enough to give you a constructive answer. – Ira Baxter Jan 01 '12 at 22:04
  • From whence cometh the '`shared [ HALFSIZE ]`' notation? It is not standard C. Which platform are you working on; which C compiler? – Jonathan Leffler Jan 01 '12 at 22:10
  • 1
    @JonathanLeffler: The OP has tagged this as "UPC", which apparently is a "parallel extension to ISO C" (although not one I'm familiar with)... – Oliver Charlesworth Jan 01 '12 at 22:12
  • 2
    It's in tags - UPC, Unified Parallel C. table is an array shared between threads (in this case 2). Pointers points to start of each thread's part of that array. – ciembor Jan 01 '12 at 22:15
  • Thanks, @Oli. Nor me. It would have been helpful to emphasize this in the question since it is unusual. Or omit the C tag since the question is not related to C per se, but all about the extensions to C in UPC. – Jonathan Leffler Jan 01 '12 at 22:20

2 Answers2

1

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.

Dan Bonachea
  • 2,408
  • 5
  • 16
  • 31
0

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];
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278