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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
|
/*@@
@file Reduction.c
@date Thu Apr 3 11:54:53 1997
@author Thomas Radke, Paul Walker
@desc
Various MPI reduction operators.
@enddesc
@version $Id$
@@*/
#include <stdlib.h>
#include "pugh_reductions.h"
static const char *rcsid = "$Header$";
CCTK_FILEVERSION(CactusPUGH_PUGHReduce_Reduction_c)
/********************************************************************
********************* Local Routine Prototypes *********************
********************************************************************/
static int ReductionGA (const cGH *GH,
int vindex,
int proc,
CCTK_REAL *outval,
reduction_fn_t reduction_fn);
static int ReductionScalar (const cGH *GH, int vindex, int proc,
CCTK_REAL *outval,
reduction_fn_t reduction_fn);
static int copy_real_to_outtype (int num_elems,
CCTK_REAL inarray [/* num_elems */],
int outtype,
void *outarray /* [num_elems] */);
/********************************************************************
********************* External Routines **********************
********************************************************************/
/*@@
@routine PUGH_ReductionArrays
@author Thomas Radke
@date 19 Aug 1999
@desc
Wrapper to reduce a list of arrays.
Just calls the appropriate reduction operator and does
the type conversion of the results.
@enddesc
@calls copy_real_to_outtype
@var GH
@vdesc Pointer to CCTK grid hierarchy
@vtype const cGH *
@vio in
@endvar
@var proc
@vdesc processor that should receive the result of operation
(negative value means all processors receive the result)
@vtype int
@vio in
@endvar
@var num_dims
@vdesc number of dimensions of input arrays
@vtype int
@vio in
@endvar
@var dims
@vdesc dimensions of input arrays
@vtype int *
@vio in
@endvar
@var intype
@vdesc (common) variable type of input arrays
@vtype int
@vio in
@endvar
@var num_inarrays
@vdesc number of input arrays
@vtype int
@vio in
@endvar
@var inarrays
@vdesc field of input arrays
@vtype const void *const *
@vio in
@endvar
@var outtype
@vdesc (common) variable type of output arrays
@vtype int
@vio in
@endvar
@var num_outvals
@vdesc number of values per output array
@vtype int
@vio in
@endvar
@var outvals
@vdesc pointer to buffer holding the output values
@vtype void *
@vio in
@endvar
@var reduction_fn
@vdesc reduction operator callback
@vtype reduction_fn_t
@vio in
@endvar
@returntype int
@returndesc
the return code of the reduction operator, or<BR>
-1 if array size is zero<BR>
-2 if <num_outvals> is invalid
@endreturndesc
@@*/
int PUGH_ReductionArrays (const cGH *GH,
int proc,
int num_dims,
const int dims[/* num_dims */],
int intype,
int num_inarrays,
const void *const inarrays[/* num_inarrays */],
int outtype,
int num_outvals,
void *outvals /* [num_outvals] */,
reduction_fn_t reduction_fn)
{
int i, num_points, retval;
int from[1], to[1], iterator[1], points_per_dim[1];
int *intypes;
CCTK_REAL *buffer;
points_per_dim[0] = 1;
from[0] = 0;
to[0] = dims[0];
/* get the total number of array elements */
for (i = 1; i < num_dims; i++)
{
to[0] *= dims[i];
}
/* check for zero-sized arrays */
if (to[0] <= 0)
{
CCTK_WARN (2, "PUGH_ReductionArrays: Cannot reduce zero-sized arrays");
return (-1);
}
if (num_outvals != 1)
{
if (num_outvals != to[0])
{
CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
"PUGH_ReductionArrays: Don't know how to reduce "
"a %d-dimensional array with %d elements "
"to an output array of %d elements",
num_dims, to[0], num_outvals);
return (-2);
}
to[0] = 1;
}
num_points = to[0] * CCTK_nProcs (GH);
/* set the array types to intype */
/* FIXME: could allow to pass in arrays of different types now !!! */
intypes = malloc (num_inarrays * sizeof (int));
for (i = 0; i < num_inarrays; i++)
{
intypes[i] = intype;
}
buffer = malloc (num_outvals * sizeof (CCTK_REAL));
/* do the reduction on the input arrays */
retval = reduction_fn (GH,
proc,
num_dims,
from,
to,
iterator,
points_per_dim,
num_points,
num_inarrays,
intypes,
inarrays,
num_outvals,
buffer);
if (retval == 0 && (proc < 0 || proc == CCTK_MyProc (GH)))
{
/* type-cast the result to the requested datatype */
retval = copy_real_to_outtype (num_inarrays * num_outvals,
buffer,
outtype,
outvals);
}
free (intypes);
free (buffer);
return (retval);
}
/*@@
@routine PUGH_ReductionGVs
@author Thomas Radke
@date 19 Aug 1999
@desc
Wrapper to reduce a list of grid variables.
Just calls the appropriate reduction operator and does
the type conversion of the results.
@enddesc
@calls CCTK_VarTypeSize
CCTK_GroupTypeFromVarI
ReductionGA
ReductionScalar
copy_real_to_outtype
@var GH
@vdesc Pointer to CCTK grid hierarchy
@vtype const cGH *
@vio in
@endvar
@var proc
@vdesc processor that should receive the result of operation
(negative value means all processors receive the result)
@vtype int
@vio in
@endvar
@var num_invars
@vdesc number of input arrays
@vtype int
@vio in
@endvar
@var invars
@vdesc field of input arrays
@vtype const int *
@vio in
@endvar
@var outtype
@vdesc (common) variable type of output arrays
@vtype int
@vio in
@endvar
@var num_outvals
@vdesc number of values per output array
@vtype int
@vio in
@endvar
@var outvals
@vdesc pointer to buffer holding the output values
@vtype void *
@vio in
@endvar
@var reduction_fn
@vdesc reduction operator callback
@vtype reduction_fn_t
@vio in
@endvar
@returntype int
@returndesc
the return code of the reduction operator
@endreturndesc
@@*/
int PUGH_ReductionGVs (const cGH *GH,
int proc,
int num_invars,
const int invars[/* num_invars */],
int outtype,
int num_outvals,
void *outvals /* [num_outvals] */,
reduction_fn_t reduction_fn)
{
int i, myproc, outtypesize, this_retval, retval;
CCTK_REAL result;
if (num_outvals != 1)
{
CCTK_WARN (1, "PUGH_ReductionGVs: Only one output value per "
"input array allowed");
return (-1);
}
outtypesize = CCTK_VarTypeSize (outtype);
if (outtypesize <= 0)
{
CCTK_WARN (1, "PUGH_ReductionGVs: Invalid output variable type");
return (-1);
}
retval = 0;
myproc = CCTK_MyProc (GH);
for (i = 0; i < num_invars; i++)
{
switch (CCTK_GroupTypeFromVarI (invars[i]))
{
case CCTK_GF:
case CCTK_ARRAY:
this_retval = ReductionGA (GH, invars[i], proc, &result,
reduction_fn);
break;
case CCTK_SCALAR:
this_retval = ReductionScalar (GH, invars[i], proc, &result,
reduction_fn);
break;
default:
CCTK_WARN (1, "PUGH_ReductionGVs: Invalid variable index");
this_retval = -1;
break;
}
if (this_retval == 0 && (proc < 0 || proc == myproc))
{
/* type-cast the result to the requested datatype */
this_retval = copy_real_to_outtype (1, &result, outtype,
(char *) outvals + i*outtypesize);
}
retval |= this_retval;
}
return (retval);
}
/********************************************************************
********************* Local Routines *************************
********************************************************************/
/*@@
@routine ReductionGA
@date Fri Jun 29 15:35:01 2001
@author Tom Goodale
@desc
Reduction of a grid array variable.
@enddesc
@var GH
@vdesc Pointer to CCTK grid hierarchy
@vtype const cGH *
@vio in
@endvar
@var vindex
@vdesc The GV index
@vtype int
@vio in
@endvar
@var proc
@vdesc The processor at which we desire the result
@vtype int
@vio in
@endvar
@var outval
@vdesc buffer to store the reduction result in
@vtype CCTK_REAL *
@vio out
@endvar
@var reduction_fn
@vdesc The function which does the actual reduction
@vtype reduction_fn_t
@vio in
@endvar
@returntype int
@returndesc
The return code of the reduction operator.
@endreturndesc
@@*/
static int ReductionGA (const cGH *GH, int vindex, int proc, CCTK_REAL *outval,
reduction_fn_t reduction_fn)
{
int i, stagger_index, num_points, dir_points, retval;
pGA *GA;
const void *data;
char *fullname;
int *from, *to, *iterator, *points_per_dim;
/* get the GA structure for the current timelevel */
GA = ((pGA ***) PUGH_pGH (GH)->variables)[vindex][0];
data = GA->data;
/* check for zero-sized arrays */
if (GA->extras->npoints <= 0)
{
fullname = CCTK_FullName (vindex);
CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
"ReductionGA: Cannot reduce zero-sized grid array '%s'",
fullname);
free (fullname);
return (-1);
}
/* allocate temporary vectors */
from = malloc (4 * GA->connectivity->dim * sizeof (int));
to = from + 1*GA->connectivity->dim;
iterator = from + 2*GA->connectivity->dim;
points_per_dim = from + 3*GA->connectivity->dim;
/* get the start- and endpoint to iterate over, the total number of points,
and the points_per_dim[] vector */
num_points = 1;
points_per_dim[0] = 1;
for (i = 0; i < GA->connectivity->dim; i++)
{
/* find out whether the variable is staggered */
stagger_index = CCTK_StaggerDirIndex (i, GA->stagger);
/* get the start- and endpoint to iterate over in this dimension */
from[i] = GA->extras->ownership[stagger_index][0][i];
to[i] = GA->extras->ownership[stagger_index][1][i];
/* get the number of points in this dimension (excluding ghostzones) */
dir_points = GA->extras->nsize[i];
if (GA->connectivity->perme[i])
{
dir_points -= 2*GA->extras->nghostzones[i];
}
if (stagger_index)
{
dir_points--;
}
/* accumulate the total number of points the reduce */
num_points *= dir_points;
if (i > 0)
{
points_per_dim[i] = points_per_dim[i-1] * GA->extras->lnsize[i-1];
}
}
/* now do the reduction */
retval = reduction_fn (GH, proc, GA->connectivity->dim, from, to, iterator,
points_per_dim, num_points, 1, &GA->vtype, &data,
1, outval);
/* free temporary vectors */
free (from);
return (retval);
}
/*@@
@routine ReductionScalar
@date Fri Jun 29 15:35:01 2001
@author Tom Goodale
@desc
Reduction of a scalar GV.
Basically a copy of ReductionGA cut down to the scalar case.
@enddesc
@var GH
@vdesc Pointer to CCTK grid hierarchy
@vtype const cGH *
@vio in
@endvar
@var vindex
@vdesc The GV index
@vtype int
@vio in
@endvar
@var proc
@vdesc The processor at which we desire the result
@vtype int
@vio in
@endvar
@var outval
@vdesc buffer to store the reduction result in
@vtype CCTK_REAL *
@vio out
@endvar
@var reduction_fn
@vdesc The function which does the actual reduction
@vtype reduction_fn_t
@vio in
@endvar
@returntype int
@returndesc
The return code of the reduction operator.
@endreturndesc
@@*/
static int ReductionScalar (const cGH *GH,
int vindex,
int proc,
CCTK_REAL *outval,
reduction_fn_t reduction_fn)
{
int retval;
int num_points, from, to, iterator, points_per_dim, type;
const void *data;
/* get the data for the current timelevel */
data = ((const void ***) PUGH_pGH (GH)->variables)[vindex][0];
from = 0;
to = 1;
iterator = 1;
points_per_dim = 1;
type = CCTK_VarTypeI (vindex);
num_points = 1;
/* now do the reduction */
retval = reduction_fn (GH, proc, 1, &from, &to, &iterator,
&points_per_dim, num_points, 1, &type, &data,
1, outval);
return retval;
}
/*@@
@routine copy_real_to_outtype
@author Thomas Radke
@date 19 Aug 1999
@desc
Does the type conversion from CCTK_REAL into the requested
datatype.
@enddesc
@calls CCTK_VarTypeSize
CCTK_GroupTypeFromVarI
ReductionGA
ReductionScalar
copy_real_to_outtype
@var num_elems
@vdesc number of elements to convert
@vtype int
@vio in
@endvar
@var inarray
@vdesc input array with results of reductions
@vtype CCTK_REAL *
@vio in
@endvar
@var outtype
@vdesc requested output datatype
@vtype int
@vio in
@endvar
@var outarray
@vdesc pointer to output buffer to store the converted results
@vtype void *
@vio out
@endvar
@returntype int
@returndesc
0 for success, or<BR>
-1 if conversion into target datatype is not supported.
@endreturndesc
@@*/
static int copy_real_to_outtype (int num_elems,
CCTK_REAL inarray[/* num_elems */],
int outtype,
void *outarray /* [num_elems] */)
{
int i, retval;
retval = 0;
if (outtype == CCTK_VARIABLE_CHAR)
{
CCTK_BYTE *_outarray = (CCTK_CHAR *) outarray;
for (i = 0; i < num_elems; i++)
{
_outarray[i] = (CCTK_BYTE) inarray[i];
}
}
else if (outtype == CCTK_VARIABLE_INT)
{
CCTK_INT *_outarray = (CCTK_INT *) outarray;
for (i = 0; i < num_elems; i++)
{
_outarray[i] = (CCTK_INT) inarray[i];
}
}
#ifdef CCTK_INT2
else if (outtype == CCTK_VARIABLE_INT2)
{
CCTK_INT2 *_outarray = (CCTK_INT2 *) outarray;
for (i = 0; i < num_elems; i++)
{
_outarray[i] = (CCTK_INT2) inarray[i];
}
}
#endif
#ifdef CCTK_INT4
else if (outtype == CCTK_VARIABLE_INT4)
{
CCTK_INT4 *_outarray = (CCTK_INT4 *) outarray;
for (i = 0; i < num_elems; i++)
{
_outarray[i] = (CCTK_INT4) inarray[i];
}
}
#endif
#ifdef CCTK_INT8
else if (outtype == CCTK_VARIABLE_INT8)
{
CCTK_INT8 *_outarray = (CCTK_INT8 *) outarray;
for (i = 0; i < num_elems; i++)
{
_outarray[i] = (CCTK_INT8) inarray[i];
}
}
#endif
else if (outtype == CCTK_VARIABLE_REAL)
{
CCTK_REAL *_outarray = (CCTK_REAL *) outarray;
for (i = 0; i < num_elems; i++)
{
_outarray[i] = (CCTK_REAL) inarray[i];
}
}
#ifdef CCTK_REAL4
else if (outtype == CCTK_VARIABLE_REAL4)
{
CCTK_REAL4 *_outarray = (CCTK_REAL4 *) outarray;
for (i = 0; i < num_elems; i++)
{
_outarray[i] = (CCTK_REAL4) inarray[i];
}
}
#endif
#ifdef CCTK_REAL8
else if (outtype == CCTK_VARIABLE_REAL8)
{
CCTK_REAL8 *_outarray = (CCTK_REAL8 *) outarray;
for (i = 0; i < num_elems; i++)
{
_outarray[i] = (CCTK_REAL8) inarray[i];
}
}
#endif
#ifdef CCTK_REAL16
else if (outtype == CCTK_VARIABLE_REAL16)
{
CCTK_REAL16 *_outarray = (CCTK_REAL16 *) outarray;
for (i = 0; i < num_elems; i++)
{
_outarray[i] = (CCTK_REAL16) inarray[i];
}
}
#endif
else
{
CCTK_WARN (1, "copy_real_to_outtype: Unsupported output type");
retval = -1;
}
return (retval);
}
|