File: allreduce_maxminloc.java

package info (click to toggle)
mpj 0.44%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 7,592 kB
  • sloc: java: 49,853; ansic: 2,508; xml: 596; sh: 311; perl: 156; makefile: 27
file content (413 lines) | stat: -rw-r--r-- 11,512 bytes parent folder | download | duplicates (3)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package mpi.ccl;

/****************************************************************************

 MPI-Java version :
 Sung-Hoon Ko(shko@npac.syr.edu)
 Northeast Parallel Architectures Center at Syracuse University
 09/14/99

 ****************************************************************************/
import mpi.*;

public class allreduce_maxminloc {
  static public void main(String[] args) throws Exception {
    try {
      allreduce_maxminloc a = new allreduce_maxminloc(args);
    }
    catch (Exception e) {
    }
  }

  public allreduce_maxminloc() {
  }

  public allreduce_maxminloc(String[] args) throws Exception {

    MPI.Init(args);

    int count, errcnt = 0, gerr = 0, size, rank;
    Comm comm;

    Comm comms[] = new Comm[10];
    int ncomm, i, world_rank;

    world_rank = MPI.COMM_WORLD.Rank();

    comm = MPI.COMM_WORLD;
    size = comm.Size();
    rank = comm.Rank();

    count = 10;

    /* Test Maxloc */

    // int //////////////////////////////////////////
    if (world_rank == 0)
      System.out.println("Testing MPI.MAXLOC with MPI.INT2...");

    int in[] = new int[2 * count];
    int out[] = new int[2 * count];
    int sol[] = new int[2 * count];

    int fnderr = 0;

    for (i = 0; i < count; i++) {
      in[i * 2] = (rank + i);
      in[i * 2 + 1] = rank;

      sol[i * 2] = size - 1 + i;
      sol[i * 2 + 1] = size - 1;

      out[i * 2] = 0;
      out[i * 2 + 1] = -1;
    }

    MPI.COMM_WORLD.Allreduce(in, 0, out, 0, count, MPI.INT2, MPI.MAXLOC);

    for (i = 0; i < count; i++) {
      if (out[i * 2] != sol[i * 2] || out[i * 2 + 1] != sol[i * 2 + 1]) {
	errcnt++;
	fnderr++;
	System.out.println(world_rank + " Expected (" + sol[i * 2] + ","
	    + sol[i * 2 + 1] + ") got (" + out[i * 2] + "," + out[i * 2 + 1]
	    + ")");
      }
    }

    if (fnderr > 0)
      System.out.println(world_rank
	  + " Error for type MPI.INT2 and op MPI.MAXLOC (" + fnderr + " of "
	  + count + " wrong)");
    // long //////////////////////////////////////////
    if (world_rank == 0)
      System.out.println("Testing MPI.MAXLOC with MPI.LONG2...");

    fnderr = 0;

    long in_l[] = new long[2 * count];
    long out_l[] = new long[2 * count];
    long sol_l[] = new long[2 * count];

    for (i = 0; i < count; i++) {
      in_l[i * 2] = (rank + i);
      in_l[i * 2 + 1] = rank;

      sol_l[i * 2] = (size - 1 + i);
      sol_l[i * 2 + 1] = (size - 1);

      out_l[i * 2] = 0;
      out_l[i * 2 + 1] = -1;
    }
    MPI.COMM_WORLD.Allreduce(in_l, 0, out_l, 0, count, MPI.LONG2, MPI.MAXLOC);

    for (i = 0; i < count; i++) {
      if (out_l[i * 2] != sol_l[i * 2] || out_l[i * 2 + 1] != sol_l[i * 2 + 1]) {
	errcnt++;
	fnderr++;
	System.out.println(world_rank + " Expected (" + sol_l[i * 2] + ","
	    + sol_l[i * 2 + 1] + ") got (" + out_l[i * 2] + ","
	    + out_l[i * 2 + 1] + ")");
      }
    }

    if (fnderr > 0)
      System.out.println(world_rank
	  + " Error for type MPI.LONG2 and op MPI.MAXLOC (" + fnderr + " of "
	  + count + " wrong)");

    // short //////////////////////////////////////////
    MPI.COMM_WORLD.Barrier();
    if (world_rank == 0)
      System.out.println("Testing MPI.MAXLOC with MPI.SHORT2...");

    fnderr = 0;

    short in_s[] = new short[2 * count];
    short out_s[] = new short[2 * count];
    short sol_s[] = new short[2 * count];

    for (i = 0; i < count; i++) {
      in_s[i * 2] = (short) (rank + i);
      in_s[i * 2 + 1] = (short) rank;

      sol_s[i * 2] = (short) (size - 1 + i);
      sol_s[i * 2 + 1] = (short) (size - 1);

      out_s[i * 2] = 0;
      out_s[i * 2 + 1] = -1;
    }
    MPI.COMM_WORLD.Allreduce(in_s, 0, out_s, 0, count, MPI.SHORT2, MPI.MAXLOC);

    for (i = 0; i < count; i++) {
      if (out_s[i * 2] != sol_s[i * 2] || out_s[i * 2 + 1] != sol_s[i * 2 + 1]) {
	errcnt++;
	fnderr++;
	System.out.println(world_rank + " Expected (" + sol_s[i * 2] + ","
	    + sol_s[i * 2 + 1] + ")got (" + out_s[i * 2] + ","
	    + out_s[i * 2 + 1] + ")");
      }
    }

    if (fnderr > 0)
      System.out.println(world_rank
	  + " Error for type MPI.SHORT2 and op MPI.MAXLOC (" + fnderr + " of "
	  + count + " wrong)");

    // float //////////////////////////////////////////
    MPI.COMM_WORLD.Barrier();
    if (world_rank == 0)
      System.out.println("Testing MPI.MAXLOC with MPI.FLOAT2...");

    fnderr = 0;

    float in_f[] = new float[2 * count];
    float out_f[] = new float[2 * count];
    float sol_f[] = new float[2 * count];

    for (i = 0; i < count; i++) {
      in_f[i * 2] = (rank + i);
      in_f[i * 2 + 1] = rank;

      sol_f[i * 2] = (size - 1 + i);
      sol_f[i * 2 + 1] = (size - 1);

      out_f[i * 2] = 0;
      out_f[i * 2 + 1] = -1;
    }
    MPI.COMM_WORLD.Allreduce(in_f, 0, out_f, 0, count, MPI.FLOAT2, MPI.MAXLOC);

    for (i = 0; i < count; i++) {
      if (out_f[i * 2] != sol_f[i * 2] || out_f[i * 2 + 1] != sol_f[i * 2 + 1]) {
	errcnt++;
	fnderr++;
	System.out.println(world_rank + " Expected (" + sol_f[i * 2] + ","
	    + sol_f[i * 2 + 1] + ")got (" + out_f[i * 2] + ","
	    + out_f[i * 2 + 1] + ")");
      }
    }

    if (fnderr > 0)
      System.out.println(world_rank
	  + " Error for type MPI.FLOAT and op MPI.MAXLOC (" + fnderr + " of "
	  + count + " wrong)");

    // double //////////////////////////////////////////
    MPI.COMM_WORLD.Barrier();
    if (world_rank == 0)
      System.out.println("Testing MPI.MAXLOC with MPI.DOUBLE2...");

    fnderr = 0;

    double in_d[] = new double[2 * count];
    double out_d[] = new double[2 * count];
    double sol_d[] = new double[2 * count];

    for (i = 0; i < count; i++) {
      in_d[i * 2] = (rank + i);
      in_d[i * 2 + 1] = rank;

      sol_d[i * 2] = (size - 1 + i);
      sol_d[i * 2 + 1] = (size - 1);

      out_d[i * 2] = 0;
      out_d[i * 2 + 1] = -1;
    }
    MPI.COMM_WORLD.Allreduce(in_d, 0, out_d, 0, count, MPI.DOUBLE2, MPI.MAXLOC);

    for (i = 0; i < count; i++) {
      if (out_d[i * 2] != sol_d[i * 2] || out_d[i * 2 + 1] != sol_d[i * 2 + 1]) {
	errcnt++;
	fnderr++;
	System.out.println(world_rank + " Expected (" + sol_d[i * 2] + ","
	    + sol_d[i * 2 + 1] + ")got (" + out_d[i * 2] + ","
	    + out_d[i * 2 + 1] + ")");
      }
    }

    if (fnderr > 0)
      System.out.println(world_rank
	  + " Error for type MPI.DOUBLE and op MPI.MAXLOC (" + fnderr + " of "
	  + count + " wrong)");

    gerr += errcnt;
    if (errcnt > 0)
      System.out.println("Found " + errcnt + " errors on " + rank
	  + " for MPI_MAXLOC\n");
    errcnt = 0;

    // /////////////////////////////////////////////////////
    // Test minloc
    // ////////////////////////////////////////////////////

    // int //////////////////////////////////////////
    if (world_rank == 0)
      System.out.println("Testing MPI.MINLOC with MPI.INT2...");

    // struct int_test { int a; int b; } *in, *out, *sol;
    int in_mn_i[] = new int[2 * count];
    int out_mn_i[] = new int[2 * count];
    int sol_mn_i[] = new int[2 * count];

    fnderr = 0;

    for (i = 0; i < count; i++) {
      in_mn_i[i * 2] = (rank + i);
      in_mn_i[i * 2 + 1] = rank;

      sol_mn_i[i * 2] = i;
      sol_mn_i[i * 2 + 1] = 0;

      out_mn_i[i * 2] = 0;
      out_mn_i[i * 2 + 1] = -1;
    }
    MPI.COMM_WORLD.Allreduce(in_mn_i, 0, out_mn_i, 0, count, MPI.INT2,
	MPI.MINLOC);

    for (i = 0; i < count; i++) {
      if (out_mn_i[i * 2] != sol_mn_i[i * 2]
	  || out_mn_i[i * 2 + 1] != sol_mn_i[i * 2 + 1]) {
	errcnt++;
	fnderr++;
	System.out.println(world_rank + " Expected (" + sol_mn_i[i * 2] + ","
	    + sol_mn_i[i * 2 + 1] + ") got (" + out_mn_i[i * 2] + ","
	    + out_mn_i[i * 2 + 1] + ")");
      }
    }

    if (fnderr > 0)
      System.out.println(world_rank
	  + " Error for type MPI.INT2 and op MPI.MINLOC (" + fnderr + " of "
	  + count + " wrong)");

    // long //////////////////////////////////////////
    if (world_rank == 0)
      System.out.println("Testing MPI.MINLOC with MPI.LONG2...");

    long in_mn_l[] = new long[2 * count];
    long out_mn_l[] = new long[2 * count];
    long sol_mn_l[] = new long[2 * count];

    fnderr = 0;

    for (i = 0; i < count; i++) {
      in_mn_l[i * 2] = (rank + i);
      in_mn_l[i * 2 + 1] = rank;

      sol_mn_l[i * 2] = i;
      sol_mn_l[i * 2 + 1] = 0;

      out_mn_l[i * 2] = 0;
      out_mn_l[i * 2 + 1] = -1;
    }
    MPI.COMM_WORLD.Allreduce(in_mn_l, 0, out_mn_l, 0, count, MPI.LONG2,
	MPI.MINLOC);

    for (i = 0; i < count; i++) {
      if (out_mn_l[i * 2] != sol_mn_l[i * 2]
	  || out_mn_l[i * 2 + 1] != sol_mn_l[i * 2 + 1]) {
	errcnt++;
	fnderr++;
	System.out.println(world_rank + " Expected (" + sol_mn_l[i * 2] + ","
	    + sol_mn_l[i * 2 + 1] + ") got (" + out_mn_l[i * 2] + ","
	    + out_mn_l[i * 2 + 1] + ")");
      }
    }

    if (fnderr > 0)
      System.out.println(world_rank
	  + " Error for type MPI.INT2 and op MPI.MINLOC (" + fnderr + " of "
	  + count + " wrong)");

    // short //////////////////////////////////////////
    if (world_rank == 0)
      System.out.println("Testing MPI.MINLOC with MPI.SHORT2...");

    short in_mn_s[] = new short[2 * count];
    short out_mn_s[] = new short[2 * count];
    short sol_mn_s[] = new short[2 * count];

    fnderr = 0;

    for (i = 0; i < count; i++) {
      in_mn_s[i * 2] = (short) (rank + i);
      in_mn_s[i * 2 + 1] = (short) rank;

      sol_mn_s[i * 2] = (short) i;
      sol_mn_s[i * 2 + 1] = 0;

      out_mn_s[i * 2] = 0;
      out_mn_s[i * 2 + 1] = -1;
    }
    MPI.COMM_WORLD.Allreduce(in_mn_s, 0, out_mn_s, 0, count, MPI.SHORT2,
	MPI.MINLOC);

    for (i = 0; i < count; i++) {
      if (out_mn_s[i * 2] != sol_mn_s[i * 2]
	  || out_mn_s[i * 2 + 1] != sol_mn_s[i * 2 + 1]) {
	errcnt++;
	fnderr++;
	System.out.println(world_rank + " Expected (" + sol_mn_s[i * 2] + ","
	    + sol_mn_s[i * 2 + 1] + ") got (" + out_mn_s[i * 2] + ","
	    + out_mn_s[i * 2 + 1] + ")");
      }
    }

    if (fnderr > 0)
      System.out.println(world_rank
	  + " Error for type MPI.SHORT2 and op MPI.MINLOC (" + fnderr + " of "
	  + count + " wrong)");

    // double //////////////////////////////////////////
    if (world_rank == 0)
      System.out.println("Testing MPI.MINLOC with MPI.DOUBLE2...");

    double in_mn_d[] = new double[2 * count];
    double out_mn_d[] = new double[2 * count];
    double sol_mn_d[] = new double[2 * count];

    fnderr = 0;

    for (i = 0; i < count; i++) {
      in_mn_d[i * 2] = (rank + i);
      in_mn_d[i * 2 + 1] = rank;

      sol_mn_d[i * 2] = i;
      sol_mn_d[i * 2 + 1] = 0;

      out_mn_d[i * 2] = 0;
      out_mn_d[i * 2 + 1] = -1;
    }
    MPI.COMM_WORLD.Allreduce(in_mn_d, 0, out_mn_d, 0, count, MPI.DOUBLE2,
	MPI.MINLOC);

    for (i = 0; i < count; i++) {
      if (out_mn_d[i * 2] != sol_mn_d[i * 2]
	  || out_mn_d[i * 2 + 1] != sol_mn_d[i * 2 + 1]) {
	errcnt++;
	fnderr++;
	System.out.println(world_rank + " Expected (" + sol_mn_d[i * 2] + ","
	    + sol_mn_d[i * 2 + 1] + ") got (" + out_mn_d[i * 2] + ","
	    + out_mn_d[i * 2 + 1] + ")");
      }
    }

    if (fnderr > 0)
      System.out.println(world_rank
	  + " Error for type MPI.DOUBLE2 and op MPI.MINLOC (" + fnderr + " of "
	  + count + " wrong)");

    gerr += errcnt;
    if (errcnt > 0)
      System.out.println("Found " + errcnt + " errors on " + rank
	  + " for MPI_MINLOC");
    errcnt = 0;
    if (gerr > 0) {
      System.out.println("Found " + gerr + " errors overall on " + rank);
    } else if (world_rank == 0)
      System.out.println("Allreduce Max-Min-Loc TEST COMPLETE.");

    MPI.COMM_WORLD.Barrier();
    MPI.Finalize();
  }
}