-
Notifications
You must be signed in to change notification settings - Fork 9
/
gups_vanilla.c
255 lines (207 loc) · 6.64 KB
/
gups_vanilla.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* ----------------------------------------------------------------------
gups = algorithm for the HPCC RandomAccess (GUPS) benchmark
implements a hypercube-style synchronous all2all
Steve Plimpton, [email protected], Sandia National Laboratories
www.cs.sandia.gov/~sjplimp
Copyright (2006) Sandia Corporation
------------------------------------------------------------------------- */
/* random update GUPS code, power-of-2 number of procs
compile with -DCHECK to check if table updates happen on correct proc */
#include <stdio.h>
#include <stdlib.h>
#include <mpich/mpi.h>
#define MAX(a,b) ((a) > (b) ? (a) : (b))
/* machine defs
compile with -DLONG64 if a "long" is 64 bits
else compile with no setting if "long long" is 64 bit */
#ifdef LONG64
#define POLY 0x0000000000000007UL
#define PERIOD 1317624576693539401L
#define ZERO64B 0L
typedef long s64Int;
typedef unsigned long u64Int;
#define U64INT MPI_UNSIGNED_LONG
#else
#define POLY 0x0000000000000007ULL
#define PERIOD 1317624576693539401LL
#define ZERO64B 0LL
typedef long long s64Int;
typedef unsigned long long u64Int;
#define U64INT MPI_LONG_LONG_INT
#endif
u64Int HPCC_starts(s64Int n);
int main(int narg, char **arg)
{
int me,nprocs;
int i,j,iterate,niterate;
int nlocal,nlocalm1,logtable,index,logtablelocal;
int logprocs,ipartner,ndata,nsend,nkeep,nrecv,maxndata,maxnfinal,nexcess;
int nbad,chunk,chunkbig;
double t0,t0_all,Gups;
u64Int *table,*data,*send;
u64Int ran,datum,procmask,nglobal,offset,nupdates;
u64Int ilong,nexcess_long,nbad_long;
MPI_Status status;
MPI_Init(&narg,&arg);
MPI_Comm_rank(MPI_COMM_WORLD,&me);
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
/* command line args = N M chunk
N = length of global table is 2^N
M = # of update sets per proc
chunk = # of updates in one set */
if (narg != 4) {
if (me == 0) printf("Syntax: gups N M chunk\n");
MPI_Abort(MPI_COMM_WORLD,1);
}
logtable = atoi(arg[1]);
niterate = atoi(arg[2]);
chunk = atoi(arg[3]);
/* insure Nprocs is power of 2 */
i = 1;
while (i < nprocs) i *= 2;
if (i != nprocs) {
if (me == 0) printf("Must run on power-of-2 procs\n");
MPI_Abort(MPI_COMM_WORLD,1);
}
/* nglobal = entire table
nlocal = size of my portion
nlocalm1 = local size - 1 (for index computation)
logtablelocal = log of table size I store
offset = starting index in global table of 1st entry in local table */
logprocs = 0;
while (1 << logprocs < nprocs) logprocs++;
nglobal = ((u64Int) 1) << logtable;
nlocal = nglobal / nprocs;
nlocalm1 = nlocal - 1;
logtablelocal = logtable - logprocs;
offset = (u64Int) nlocal * me;
/* allocate local memory
16 factor insures space for messages that (randomly) exceed chunk size */
chunkbig = 16*chunk;
table = (u64Int *) malloc(nlocal*sizeof(u64Int));
data = (u64Int *) malloc(chunkbig*sizeof(u64Int));
send = (u64Int *) malloc(chunkbig*sizeof(u64Int));
if (!table || !data || !send) {
if (me == 0) printf("Table allocation failed\n");
MPI_Abort(MPI_COMM_WORLD,1);
}
/* initialize my portion of global array
global array starts with table[i] = i */
for (i = 0; i < nlocal; i++) table[i] = i + offset;
/* start my random # partway thru global stream */
nupdates = (u64Int) nprocs * chunk * niterate;
ran = HPCC_starts(nupdates/nprocs*me);
/* loop:
generate chunk random values per proc
communicate datums to correct processor via hypercube routing
use received values to update local table */
maxndata = 0;
maxnfinal = 0;
nexcess = 0;
nbad = 0;
MPI_Barrier(MPI_COMM_WORLD);
t0 = -MPI_Wtime();
for (iterate = 0; iterate < niterate; iterate++) {
for (i = 0; i < chunk; i++) {
ran = (ran << 1) ^ ((s64Int) ran < ZERO64B ? POLY : ZERO64B);
data[i] = ran;
}
ndata = chunk;
for (j = 0; j < logprocs; j++) {
nkeep = nsend = 0;
ipartner = (1 << j) ^ me;
procmask = ((u64Int) 1) << (logtablelocal + j);
if (ipartner > me) {
for (i = 0; i < ndata; i++) {
if (data[i] & procmask) send[nsend++] = data[i];
else data[nkeep++] = data[i];
}
} else {
for (i = 0; i < ndata; i++) {
if (data[i] & procmask) data[nkeep++] = data[i];
else send[nsend++] = data[i];
}
}
MPI_Sendrecv(send,nsend,U64INT,ipartner,0,&data[nkeep],chunkbig,U64INT,
ipartner,0,MPI_COMM_WORLD,&status);
MPI_Get_count(&status,U64INT,&nrecv);
ndata = nkeep + nrecv;
maxndata = MAX(maxndata,ndata);
}
maxnfinal = MAX(maxnfinal,ndata);
if (ndata > chunk) nexcess += ndata - chunk;
for (i = 0; i < ndata; i++) {
datum = data[i];
index = datum & nlocalm1;
table[index] ^= datum;
}
#ifdef CHECK
procmask = ((u64Int) (nprocs-1)) << logtablelocal;
for (i = 0; i < ndata; i++)
if ((data[i] & procmask) >> logtablelocal != me) nbad++;
#endif
}
MPI_Barrier(MPI_COMM_WORLD);
t0 += MPI_Wtime();
/* stats */
MPI_Allreduce(&t0,&t0_all,1,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD);
t0 = t0_all/nprocs;
i = maxndata;
MPI_Allreduce(&i,&maxndata,1,MPI_INT,MPI_MAX,MPI_COMM_WORLD);
i = maxnfinal;
MPI_Allreduce(&i,&maxnfinal,1,MPI_INT,MPI_MAX,MPI_COMM_WORLD);
ilong = nexcess;
MPI_Allreduce(&ilong,&nexcess_long,1,U64INT,MPI_SUM,MPI_COMM_WORLD);
ilong = nbad;
MPI_Allreduce(&ilong,&nbad_long,1,U64INT,MPI_SUM,MPI_COMM_WORLD);
nupdates = (u64Int) niterate * nprocs * chunk;
Gups = nupdates / t0 / 1.0e9;
if (me == 0) {
printf("Number of procs: %d\n",nprocs);
printf("Vector size: %lld\n",nglobal);
printf("Max datums during comm: %d\n",maxndata);
printf("Max datums after comm: %d\n",maxnfinal);
printf("Excess datums (frac): %lld (%g)\n",
nexcess_long,(double) nexcess_long / nupdates);
printf("Bad locality count: %lld\n",nbad_long);
printf("Update time (secs): %9.3f\n",t0);
printf("Gups: %9.6f\n",Gups);
}
/* clean up */
free(table);
free(data);
free(send);
MPI_Finalize();
}
/* start random number generator at Nth step of stream
routine provided by HPCC */
u64Int HPCC_starts(s64Int n)
{
int i, j;
u64Int m2[64];
u64Int temp, ran;
while (n < 0) n += PERIOD;
while (n > PERIOD) n -= PERIOD;
if (n == 0) return 0x1;
temp = 0x1;
for (i=0; i<64; i++) {
m2[i] = temp;
temp = (temp << 1) ^ ((s64Int) temp < 0 ? POLY : 0);
temp = (temp << 1) ^ ((s64Int) temp < 0 ? POLY : 0);
}
for (i=62; i>=0; i--)
if ((n >> i) & 1)
break;
ran = 0x2;
while (i > 0) {
temp = 0;
for (j=0; j<64; j++)
if ((ran >> j) & 1)
temp ^= m2[j];
ran = temp;
i -= 1;
if ((n >> i) & 1)
ran = (ran << 1) ^ ((s64Int) ran < 0 ? POLY : 0);
}
return ran;
}