-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_6.upc
57 lines (45 loc) · 1.29 KB
/
ex_6.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
53
54
55
56
57
#include <upc_relaxed.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BLOCKSIZE 70
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;
int iter;
init();
upc_barrier;
// add two barrier statements, to ensure all threads finished computing
// x_new[] and to ensure that all threads have completed the array
// swapping.
for( iter=0; iter<10000; iter++ ){
upc_forall( j=1; j<BLOCKSIZE*THREADS-1; j++; &x_new[j] ){
x_new[j] = 0.5*( x[j-1] + x[j+1] + b[j] );
}
upc_barrier;
upc_forall( j=0; j<BLOCKSIZE*THREADS; j++; &x_new[j] ){
x[j] = x_new[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;
}
}
}