-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel_random_walk.c
235 lines (178 loc) · 5.65 KB
/
parallel_random_walk.c
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <math.h>
#include <time.h>
#include <limits.h>
#include <errno.h>
#include "pcg.h"
#define GRID_SIZE 10240
#define NSTEPS 320000
#define D_NSTEPS 0
#define NMARKERS 1000000
#define TOLERANCE 0.01
struct mrk{
int id;
int thread;
int team;
int stopAt;
pcg32_random_t rng;
int location;
float integral;
};
typedef struct mrk mrk_t;
#pragma omp declare target
int stepMarker(mrk_t *, float *, int);
#pragma omp end declare target
int stepMarker(mrk_t *marker, float *grid, int gridsize){
marker->location = (int) pcg32_boundedrand_r( &(marker->rng), gridsize);
marker->integral += grid[marker->location];
return 0;
}
int initField(int gridSize, float *grid){
int i;
/* Init the field s.t. the average = 1.0 */
for(i=0;i<gridSize;i++){
grid[i] = 2.0 * ( (float) i ) / ( (float) gridSize ) ;
}
return 0;
}
int initMarkers(mrk_t *markers, int nMarks, int nsteps, int d_nsteps){
int i;
for(i=0;i<nMarks;i++){
pcg32_srandom_r( &(markers[i].rng), 0x853c49e6748fea9bULL, i);
markers[i].id=i;
markers[i].integral=0;
markers[i].stopAt = nsteps + rand() / (RAND_MAX / (2*d_nsteps + 1) + 1) - d_nsteps;
}
return 0;
}
int printMarker(mrk_t marker){
if ( fabs(marker.integral - 1.000 ) < TOLERANCE ) {
printf("M%05d: @%06d by tm%04d:th%04d s%d integral %f\n",
marker.id, marker.location,
marker.team, marker.thread,
marker.stopAt, marker.integral);
} else {
printf("M%05d: @%06d by tm%04d:th%04d s%d integral %f !!!!!!!!!!!!\n",
marker.id, marker.location,
marker.team, marker.thread,
marker.stopAt, marker.integral);
}
return 0;
}
int parse_arguments( int argc, char *argv[], int *gridsize, int *nsteps, int *d_nsteps){
char *endptr;
/* Get the gridsize as input */
if(argc >= 2 ){
*gridsize = strtol(argv[1],&endptr,10);
if(errno == EINVAL || *gridsize <= 0 ){
printf("Interpreting '%s' as %d, which is not reasonable gridsize.\n", argv[1], *gridsize);
return 1;
}
} else {
*gridsize = GRID_SIZE;
}
/* Get the number of steps and its variability as input */
if(argc >= 3 ){
*nsteps = strtol(argv[2],&endptr,10);
if(errno == EINVAL || *nsteps <= 0 ){
printf("Interpreting '%s' as %d, which is not reasonable gridsize.\n", argv[2], *nsteps);
return 2;
}
} else {
*nsteps = NSTEPS;
}
if(argc >= 4 ){
*d_nsteps = strtol(argv[3],&endptr,10);
if(errno == EINVAL || *d_nsteps < 0 ){
printf("Interpreting '%s' as %d, which is not reasonable variability of steps.\n", argv[3], *nsteps);
return 3;
}
} else {
*d_nsteps = D_NSTEPS;
}
return 0;
}
int main( int argc, char *argv[] ){
int i;
int resval;
mrk_t *markers;
const int nMarks = NMARKERS;
float *grid;
int gridsize;
int nsteps, d_nsteps;
int maxTeam,maxThread;
struct timespec wc_begin,wc_end,cpu_begin,cpu_end;
int *nFinished; /* This variable is here to test atomic/critical pragmas */
int N;
resval = parse_arguments( argc, argv, &gridsize, &nsteps, &d_nsteps);
if(resval != 0) {
return resval;
}
printf("Markers %d; Steps %d pm %d; Gridsize %d.\n", nMarks, nsteps, d_nsteps, gridsize );
markers = (mrk_t *) malloc( nMarks * sizeof(mrk_t));
grid = (float *) malloc( gridsize * sizeof(float));
initMarkers(markers,nMarks,nsteps,d_nsteps);
initField(gridsize,grid);
nFinished = &N;
*nFinished = -1; /* Just mark this with something non-default*/
printf("Finished %d/%d markers.\n",*nFinished,nMarks);
clock_gettime( CLOCK_REALTIME, &wc_begin );
clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &cpu_begin );
/*Move all the data to the target on one go*/
#pragma omp target data map(tofrom:markers[0:nMarks]) map(to:grid[0:gridsize]) map(tofrom:nFinished[0:1])
{
/*
if ( omp_is_initial_device() ) {
printf("Running on host (target data map).\n");
} else {
printf("Running on target (target data map)\n");
}
*/
#pragma omp target
{
#pragma omp atomic write
(*nFinished) = 0; /* Test the atomic save.*/
}
#pragma omp target teams distribute parallel for shared(nFinished)
for(i=0;i<nMarks;i++){
if(i==0){
if ( omp_is_initial_device() ) {
printf("Running on host (ottdpf)\n");
}
else {
printf("Running on target (ottdpf)\n");
}
}
markers[i].thread = omp_get_thread_num();
markers[i].team = omp_get_team_num();
int j;
for(j=0;j<markers[i].stopAt;j++){
stepMarker( &(markers[i]), grid, gridsize );
}
/* Scale the integral by number of steps */
markers[i].integral /= (float) markers[i].stopAt;
#pragma omp atomic update
(*nFinished)++;
/* printf("i=%5d ready=%5d\n",i,*nFinished); */
}
/* printf("** i=----- ready=%5d **\n",*nFinished); */
}
clock_gettime( CLOCK_REALTIME, &wc_end );
clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &cpu_end );
printf("Finished %d/%d markers.\n",*nFinished,nMarks);
printf("Wall clock time: %lf s\n", (double) ( wc_end.tv_sec- wc_begin.tv_sec) + ((double) ( wc_end.tv_nsec- wc_begin.tv_nsec ))*1.0e-9 );
printf(" CPU time: %lf s\n", (double) (cpu_end.tv_sec-cpu_begin.tv_sec) + ((double) (cpu_end.tv_nsec-cpu_begin.tv_nsec ))*1.0e-9 );
for (i=0; i<nMarks; i+=19331){
printMarker(markers[i]);
}
maxThread = -1;
maxTeam = -1;
for (i=0; i<nMarks; i++){
if ( markers[i].team > maxTeam ) maxTeam = markers[i].team;
if ( markers[i].thread > maxThread) maxThread = markers[i].thread;
}
printf("Teams %d Threads %d\n", maxTeam+1, maxThread+1);
return 0;
}