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
|
/*@@
@file ReductionAvg.c
@date Thu Apr 3 11:54:53 1997
@author Thomas Radke, Paul Walker, Erik Schnetter
@desc
Defines the reduction operator to get the average
of an arbitrary array.
@enddesc
@version $Id$
@@*/
#include <stdlib.h>
#include <string.h>
#include "local_reductions.h"
static const char *rcsid = "$Id$";
CCTK_FILEVERSION(CCTDevelopment_LocalReduce_ReductionAvg_c);
/* local function prototypes */
static int ReductionAvg (int N_dims, int operator_handle,
int param_table_handle, int N_input_arrays,
const CCTK_INT input_array_dims[],
const CCTK_INT input_array_type_codes[],
const void *const input_arrays[],
int M_output_numbers,
const CCTK_INT output_number_type_codes[],
void * const output_numbers[]);
/*@@
@routine LocalReduce_Mean
@author Thomas Radke
@date 19 Aug 1999
@desc
@enddesc
@history
@endhistory
@var N_dims
@vdesc number of dimensions in the *reduction*
@vtype int
@vio in
@endvar
@var operator_handle
@vdesc operator handle specificies the type of reduction we will perform
@vtype int
@vio in
@endvar
@var param_table_handle
@vdesc handle to "parameter table", a key-value table
@vtype int
@vio in
@endvar
@var N_input_arrays
@vdesc number of input arrays
@vtype int
@vio in
@endvar
@var input_array_dims
@vdesc array of input array dimensions (common to all input arrays)
@vtype const CCTK_INT
@vio in
@endvar
@var input_array_type_codes
@vdesc array of CCTK_VARIABLE_* codes giving data types of input arrays
@vtype const CCTK_INT
@vio in
@endvar
@var input_arrays
@vdesc array of pointers to input arrays
@vtype const void *const
@vio in
@endvar
@var M_output_numbers
@vdesc
@vtype int
@vio in
@endvar
@var output_number_type_codes
@vdesc array of CCTK_VARIABLE_* codes giving data types of output numbers
@vtype const CCTK_INT
@vio in
@endvar
@var output_numbers
@vdesc array[M_output_numbers] of pointers to output numbers[M_reduce_numbers]
@vtype void *const
@vio in
@endvar
@@*/
int LocalReduce_Mean (int N_dims, int operator_handle,
int param_table_handle, int N_input_arrays,
const CCTK_INT input_array_dims[],
const CCTK_INT input_array_type_codes[],
const void *const input_arrays[],
int M_output_numbers,
const CCTK_INT output_number_type_codes[],
void * const output_numbers[])
{
return (LocalReduce_Reduce (N_dims, operator_handle,
param_table_handle, N_input_arrays,
input_array_dims, input_array_type_codes,
input_arrays, M_output_numbers,
output_number_type_codes, output_numbers,
ReductionAvg));
}
/*****************************************************************************/
/* local functions */
/*****************************************************************************/
/*@@
@routine ReductionAvg
@date Aug 19 1999
@author Thomas Radke
@desc Returns the average of a distributed array with
'num_points' elements. Global reduction is done element-wise
(num_outvals == 1) or on the results of the local reductions.
@enddesc
@@*/
static int ReductionAvg (int N_dims, int operator_handle,
int param_table_handle, int N_input_arrays,
const CCTK_INT input_array_dims[],
const CCTK_INT input_array_type_codes[],
const void *const input_arrays[],
int M_output_numbers,
const CCTK_INT output_number_type_codes[],
void * const output_numbers[])
{
/* utility variables */
int i, j, k, flag, product, num_points;
int ierr;
int * iters_per_dim;
void * data_pointer;
void * output_buffer;
output_buffer = (CCTK_REAL *) malloc (M_output_numbers*sizeof(CCTK_REAL));
/* indices to hold the temp indices of size N_dims and iteration indices*/
int * indices;
int * actual_indices;
int * actual_iters_per_dim;
int index = 0;
int max_iter = 0;;
int iter = 0;
int sum_indices = 0;
int max_index = 1;
/* data pointer offset and strides declared here */
CCTK_INT * input_array_offsets;
CCTK_INT * input_array_strides;
CCTK_INT * input_array_min_subscripts;
CCTK_INT * input_array_max_subscripts;
/* excesion variables declared here */
int mask_on = 1; /* mask is by default off=1 */
void * mask_array; /* same dimensions/indexing as input arrays */
CCTK_INT mask_type_code; /* one of the CCTK_VARIABLE_* codes */
CCTK_INT mask_offset;
CCTK_INT mask_time_level;
/* set the number of points */
num_points = 0;
/* allocate memory for iters_per_dim */
iters_per_dim = (int *)malloc(N_dims * sizeof(int));
/* allocate then initialize the values of the strides and subscripts */
indices = (int *)malloc (N_dims * sizeof(int));
actual_indices = (int *)malloc (N_dims * sizeof(int));
actual_iters_per_dim = (int *)malloc (N_dims * sizeof(int));
/* allocate then initialize the values of the strides and subscripts */
input_array_offsets = (CCTK_INT *)malloc (N_input_arrays * sizeof(CCTK_INT));
input_array_strides = (CCTK_INT *) malloc (N_dims * sizeof(CCTK_INT));
input_array_min_subscripts = (CCTK_INT *) malloc (N_dims * sizeof(CCTK_INT));
input_array_max_subscripts = (CCTK_INT *) malloc (N_dims * sizeof(CCTK_INT));
for (i = 0; i<N_input_arrays; i++)
{
input_array_offsets[i] = 0;
}
for (i = 0; i<N_dims; i++)
{
input_array_strides[i] = 1;
input_array_min_subscripts[i] = 0;
input_array_max_subscripts[i] = input_array_dims[i];
max_index *= input_array_max_subscripts[i];
}
/* for strides and subscripts get values from param table (it they exist) */
if ( Util_TableQueryNKeys(param_table_handle) != 0)
{
ierr = Util_TableGetGenericArray(param_table_handle, CCTK_VARIABLE_INT,
N_input_arrays, input_array_offsets, "input_array_offsets");
ierr = Util_TableGetGenericArray(param_table_handle, CCTK_VARIABLE_INT,
N_dims, input_array_strides, "input_array_strides");
ierr = Util_TableGetGenericArray(param_table_handle, CCTK_VARIABLE_INT,
N_dims, input_array_min_subscripts, "input_array_min_subscripts");
ierr = Util_TableGetGenericArray(param_table_handle, CCTK_VARIABLE_INT,
N_dims, input_array_min_subscripts, "input_array_min_subscripts");
}
/* for masks get values from param table (it they exist) */
if ( Util_TableQueryNKeys(param_table_handle) != 0)
{
ierr = 0;
ierr = Util_TableGetInt(param_table_handle, mask_type_code, "mask_type_code");
/* mask_valid_min, mask_valid_max;
ierr = Util_TableGetGeneric(param_table_handle, mask_type_code,
mask_range, "mask_valid_min");
ierr = Util_TableGetGeneric(param_table_handle, mask_type_code,
mask_range, "mask_valid_max");
ierr = Util_TableGetGeneric(param_table_handle, mask_type_code,
mask_array, "mask_array"); */
}
/* reduction maps an array to a single value of the same type */
if (M_output_numbers != N_input_arrays)
{
CCTK_WARN (1, "Average reduction returns a single value\n \
for each input array\n");
return (-1);
}
/* set the indices to their minimum values */
max_iter = 1;
for (j = 0; j <N_dims; j++)
{
indices [j] = 0;
actual_indices[j] = input_array_min_subscripts[j];
actual_iters_per_dim [j] = (int) (input_array_max_subscripts[j] - input_array_min_subscripts[j]);
iters_per_dim [j] = (int) ((input_array_max_subscripts[j] - input_array_min_subscripts[j])/input_array_strides[j]);
max_iter *= iters_per_dim [j];
}
for (i = 0; i < N_input_arrays; i++)
{
/*
switch (output_number_type_codes[i])
{
case CCTK_VARIABLE_CHAR:
output_buffer = (CCTK_BYTE *) output_buffer;
output_buffer = (CCTK_BYTE *) malloc (M_output_numbers*sizeof(CCTK_BYTE));
break;
case CCTK_VARIABLE_INT:
output_buffer = (CCTK_INT *) output_buffer;
output_buffer = (CCTK_INT *) malloc (M_output_numbers*sizeof(CCTK_INT));
break;
#ifdef CCTK_INT1
case CCTK_VARIABLE_INT1:
output_buffer = (CCTK_INT1 *) output_buffer;
output_buffer = (CCTK_INT1 *) malloc (M_output_numbers*sizeof(CCTK_INT1));
break;
#endif
#ifdef CCTK_INT2
case CCTK_VARIABLE_INT2:
output_buffer = (CCTK_INT2 *) output_buffer;
output_buffer = (CCTK_INT2 *) malloc (M_output_numbers*sizeof(CCTK_INT2));
break;
#endif
#ifdef CCTK_INT4
case CCTK_VARIABLE_INT4:
output_buffer = (CCTK_INT4 *) output_buffer;
output_buffer = (CCTK_INT4 *) malloc (M_output_numbers*sizeof(CCTK_INT4));
break;
#endif
#ifdef CCTK_INT8
case CCTK_VARIABLE_INT8:
output_buffer = (CCTK_INT8 *) output_buffer;
output_buffer = (CCTK_INT8 *) malloc (M_output_numbers*sizeof(CCTK_INT8));
break;
#endif
case CCTK_VARIABLE_REAL:
#define type CCTK_REAL
output_buffer = (CCTK_REAL *) output_buffer;
output_buffer = (CCTK_REAL *) malloc (M_output_numbers*sizeof(CCTK_REAL));
break;
#ifdef CCTK_REAL4
case CCTK_VARIABLE_REAL4:
output_buffer = (CCTK_REAL4 *) output_buffer;
output_buffer = (CCTK_REAL4 *) malloc (M_output_numbers*sizeof(CCTK_REAL4));
break;
#endif
#ifdef CCTK_REAL8
case CCTK_VARIABLE_REAL8:
output_buffer = (CCTK_REAL8 *) output_buffer;
output_buffer = (CCTK_REAL8 *) malloc (M_output_numbers*sizeof(CCTK_REAL8));
break;
#endif
#ifdef CCTK_REAL6
case CCTK_VARIABLE_REAL6:
output_buffer = (CCTK_REAL6 *) output_buffer;
output_buffer = (CCTK_REAL6 *) malloc (M_output_numbers*sizeof(CCTK_REAL6));
break;
#endif
default:
CCTK_WARN (1, "Average reduction: Unknown variable type");
return (-1);
}*/
if ( mask_on == 1)
{
if ( input_array_offsets[i] == 0)
{
output_buffer = 0;
iter = 0;
sum_indices = 0;
while (iter < max_iter)
{
((CCTK_REAL *)output_buffer)[i] += ((CCTK_REAL* )data_pointer)[sum_indices];
num_points++;
printf("\nvalue is:%f",((CCTK_REAL* )data_pointer)[sum_indices]);
flag = 0;
for (k=0;k<N_dims;k++)
{
if( indices[k] < iters_per_dim[k]-1)
{
if (flag==1)
{
actual_indices[k] += input_array_strides[k-1];
indices[k]++;
iter++;
flag = 0;
break;
}
indices[k]++;
actual_indices[k] += input_array_strides[k];
iter++;
break;
}
else if (indices[k] == iters_per_dim[k]-1)
{
indices[k] = 0;
actual_indices[k] = input_array_min_subscripts[k];
flag = 1;
continue;
}
else
{
CCTK_WARN(1,"indices out of bounds, this should not happen");
return -1;
}
}
sum_indices = actual_indices[0];
for (k=N_dims-1;k>0;k--)
{
product = 1;
for (j=k-1;j>=0;j--)
{
product *= actual_iters_per_dim[j];
}
sum_indices += actual_indices[k]*product;
}
}
}
else
{
output_buffer = 0;
iter = 0;
sum_indices = 0;
while (iter < max_iter)
{
/* prevent offset from giving segfaults */
if (sum_indices >= max_iter)
{
CCTK_WARN(1,"offsets and strides access unallocated memory");
return -1;
}
((CCTK_REAL *)output_buffer)[i] += ((CCTK_REAL* )data_pointer)[sum_indices];
num_points++;
printf("\nvalue is:%f",((CCTK_REAL* )data_pointer)[sum_indices]);
flag = 0;
for (k=0;k<N_dims;k++)
{
if( indices[k] < iters_per_dim[k]-1)
{
if (flag==1)
{
actual_indices[k] += input_array_strides[k-1];
indices[k]++;
iter++;
flag = 0;
break;
}
indices[k]++;
actual_indices[k] += input_array_strides[k];
iter++;
break;
}
else if (indices[k] == iters_per_dim[k]-1)
{
indices[k] = 0;
actual_indices[k] = input_array_min_subscripts[k];
flag = 1;
continue;
}
else
{
CCTK_WARN(1,"indices out of bounds, this should not happen");
return -1;
}
}
sum_indices = actual_indices[0]+input_array_offsets[i];
for (k=N_dims-1;k>0;k--)
{
product = 1;
for (j=k-1;j>=0;j--)
{
product *= actual_iters_per_dim[j];
}
sum_indices += actual_indices[k]*product;
}
}
}
}
else if (mask_on == 0)
{
if ( input_array_offsets[i] == 0)
{
output_buffer = 0;
iter = 0;
sum_indices = 0;
while (iter < max_iter)
{
((CCTK_REAL *)output_buffer)[i] += ((CCTK_REAL* )data_pointer)[sum_indices];
num_points++;
printf("\nvalue is:%f",((CCTK_REAL* )data_pointer)[sum_indices]);
flag = 0;
for (k=0;k<N_dims;k++)
{
if( indices[k] < iters_per_dim[k]-1)
{
if (flag==1)
{
actual_indices[k] += input_array_strides[k-1];
indices[k]++;
iter++;
flag = 0;
break;
}
indices[k]++;
actual_indices[k] += input_array_strides[k];
iter++;
break;
}
else if (indices[k] == iters_per_dim[k]-1)
{
indices[k] = 0;
actual_indices[k] = input_array_min_subscripts[k];
flag = 1;
continue;
}
else
{
CCTK_WARN(1,"indices out of bounds, this should not happen");
return -1;
}
}
sum_indices = actual_indices[0];
for (k=N_dims-1;k>0;k--)
{
product = 1;
for (j=k-1;j>=0;j--)
{
product *= actual_iters_per_dim[j];
}
sum_indices += actual_indices[k]*product;
}
}
}
else
{
output_buffer = 0;
iter = 0;
sum_indices = 0;
while (iter < max_iter)
{
/* prevent offset from giving segfaults */
if (sum_indices >= max_iter)
{
CCTK_WARN(1,"offsets and strides access unallocated memory");
return -1;
}
((CCTK_REAL *)output_buffer)[i] += ((CCTK_REAL* )data_pointer)[sum_indices];
num_points++;
printf("\nvalue is:%f",((CCTK_REAL* )data_pointer)[sum_indices]);
flag = 0;
for (k=0;k<N_dims;k++)
{
if( indices[k] < iters_per_dim[k]-1)
{
if (flag==1)
{
actual_indices[k] += input_array_strides[k-1];
indices[k]++;
iter++;
flag = 0;
break;
}
indices[k]++;
actual_indices[k] += input_array_strides[k];
iter++;
break;
}
else if (indices[k] == iters_per_dim[k]-1)
{
indices[k] = 0;
actual_indices[k] = input_array_min_subscripts[k];
flag = 1;
continue;
}
else
{
CCTK_WARN(1,"indices out of bounds, this should not happen");
return -1;
}
}
sum_indices = actual_indices[0]+input_array_offsets[i];
for (k=N_dims-1;k>0;k--)
{
product = 1;
for (j=k-1;j>=0;j--)
{
product *= actual_iters_per_dim[j];
}
sum_indices += actual_indices[k]*product;
}
}
}
}
else
{
CCTK_WARN(1, "mask_on is not set to a valid value");
}
}
return (0);
}
|