code example
UPC Unified Parallel C
partitioned
global address space
#include <upc.h>
#include <upc_collective.h>
#include <upc_relaxed.h>
shared float e;
shared float el[THREADS];
int main(int argc, char *argv[]) {
int n = ...;
shared float *x, *y;
x = (shared float*)upc_all_alloc(THREADS, sizeof(float)*(n+1)/THREADS);
y = (shared float*)upc_all_alloc(THREADS, sizeof(float)*(n+1)/THREADS);
... // fill x, y
float e_local = 0;
int i;
// do computation
upc_forall(i=1; i<n; i++; &x[i]) {
x[i] += ( y[i+1] + y[i-1] )*.5;
e_local += y[i] * y[i];
)
el[MYTHREAD] = e_local;
upc_all_reduceF(&e, &el[0], UPC_ADD, THREADS, 1,
NULL, UPC_IN_NOSYNC | UPC_OUT_ALLSYNC);
... // output x, e
if (MYTHREAD == 0) {
upc_free(x);
upc_free(y);
}
return 0;
}
|
[
start]
[
references]
[
download]
[
install]