-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_5.upc
53 lines (39 loc) · 1.2 KB
/
ex_5.upc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <upc_relaxed.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BLOCKSIZE 16
//==> declare the x, x_new and b arrays in the shared space with size of
// BLOCKSIZE*THREADS and with blocking size of BLOCKSIZE
shared [BLOCKSIZE] double x[BLOCKSIZE*THREADS];
shared [BLOCKSIZE] double x_new[BLOCKSIZE*THREADS];
shared [BLOCKSIZE] double b[BLOCKSIZE*THREADS];
void init();
int main(int argc, char **argv){
int j;
init();
upc_barrier;
//==> insert a upc_forall statement to do work sharing while
// respecting the affinity of the x_new array
upc_forall( j=0; j<(BLOCKSIZE*THREADS)-1; j++; &x_new[j]){
x_new[j] = 0.5*( x[j-1] + x[j+1] + b[j] );
}
upc_barrier;
if( MYTHREAD == 0 ){
printf(" b | x | x_new\n");
printf("=============================\n");
for( j=0; j<BLOCKSIZE*THREADS; j++ )
printf("%1.4f | %1.4f | %1.4f \n", b[j], x[j], x_new[j]);
}
return 0;
}
void init(){
int i;
if( MYTHREAD == 0 ){
srand(time(NULL));
for( i=0; i<BLOCKSIZE*THREADS; i++ ){
b[i] = (double)rand() / RAND_MAX;
x[i] = (double)rand() / RAND_MAX;
}
}
}