summaryrefslogtreecommitdiff
path: root/src/comm/Reduction.c
blob: 0cc25db8e1084279c4604188901bc7ef6d39a6fa (plain)
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
 /*@@
   @file      Reduction.c
   @date      
   @author    Gabrielle Allen
   @desc 
   This file contains routines to deal with registering and 
   using functions providing reduction operations.
   @enddesc 
 @@*/

/*#define DEBUG_REDUCTION*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <stdarg.h>

#include "cctk.h"
#include "flesh.h"
#include "FortranString.h"
#include "Groups.h"
#include "StoreHandledData.h"
#include "Reduction.h"
#include "WarnLevel.h"

static char *rcsid = "$Header$";

static cHandledData *ReductionOperators = NULL;
static int num_reductions = 0;


 /*@@
   @routine    CCTK_RegisterReductionOperator
   @date       April 28 1999
   @author     Gabrielle Allen
   @desc 
   Registers "function" as a reduction operator called "name"
   @enddesc 
   @var     function
   @vdesc   Routine containing reduction operator
   @vtype   (void (*))
   @vio     
   @vcomment 
   @endvar 
   @var     name
   @vdesc   String containing name of reduction operator
   @vtype   const char *
   @vio     in
   @vcomment 
   @endvar 
@@*/
 
int CCTK_RegisterReductionOperator(void (*function)(REGISTER_ARGLIST),
				  const char *name)
{
  int handle;

  /* Check that the method hasn't already been registered */
  handle = Util_GetHandle(ReductionOperators, name, NULL);

  if(handle < 0)
  {
    /* Get a handle for it. */
    handle = Util_NewHandle(&ReductionOperators, name, (void *)function);
    
    /* Remember how many reduction operators there are */
    num_reductions++;
   }
  else
  {
    /* Reduction operator with this name already exists. */
    CCTK_WARN(1,"Reduction operator with this name already exists");
    handle = -1;
  }

#ifdef DEBUG_REDUCTION
  CCTK_PRINTSEPARATOR
  printf("In CCTK_RegisterReductionOperator\n");
  printf("---------------------------------\n");
  printf("  Registering %s with handle %d\n",name,handle);
  CCTK_PRINTSEPARATOR
#endif
    
  return handle;

}


 /*@@
   @routine    CCTK_ReductionHandle
   @date       April 28 1999
   @author     Gabrielle Allen
   @desc 
   Returns the handle of a given reduction operator
   @enddesc 
   @var     reduction
   @vdesc   String containing name of reduction operator
   @vtype   const char *
   @vio     in
   @vcomment 
   @endvar 
@@*/

int CCTK_ReductionHandle(const char *reduction)
{

  int handle;
  void **data=NULL; /* isn't used here */

  handle = Util_GetHandle(ReductionOperators, reduction, data);

#ifdef DEBUG_REDUCTION
  CCTK_PRINTSEPARATOR
  printf("In CCTK_ReductionHandle\n");
  printf("-----------------------\n");
  printf("  Got handle %d for %s\n",handle,reduction);
  CCTK_PRINTSEPARATOR
#endif

  if (handle < 0)
    CCTK_WARN(1,"No handle found for this reduction operator");

  return handle;

}  

void FMODIFIER FORTRAN_NAME(CCTK_ReductionHandle)(int *handle, ONE_FORTSTRING_ARG)
{
  ONE_FORTSTRING_CREATE(reduction)
  *handle = CCTK_ReductionHandle(reduction);
  free(reduction);
}




int CCTK_Reduce(  cGH *GH, 
		  int proc,
		  int operation_handle,
                  int num_out_vals,
		  int type_out_vals,
		  void *out_vals,
		  int num_in_fields,
		  ... )
{
 
  va_list indices;
  int i;
  int *in_fields = malloc(num_in_fields*sizeof(int));
  void (*function)(REGISTER_ARGLIST)=NULL; 

  /* Get the pointer to the reduction operator */


  if (operation_handle < 0)

    CCTK_WARN(3,"Invalid handle passed to CCTK_Reduce");

  else
  {
    function = (void (*)(REGISTER_ARGLIST))
      Util_GetHandledData(ReductionOperators,operation_handle);
    
    if (function)
      {
	
	/* Fill in the array of variable indices from the variable argument list */
	va_start(indices, num_in_fields);
	for (i=0; i<num_in_fields; i++)
	  in_fields[i] = va_arg(indices,int);
	va_end(indices);
	
	function(GH,proc,num_out_vals,type_out_vals,out_vals,num_in_fields,in_fields);
	
	if (in_fields) free(in_fields);
	
      }
    else
      CCTK_WARN(3,"Reduction operation is not registered and cannot be called");
  }

  return 1;

}

void FMODIFIER FORTRAN_NAME(CCTK_Reduce)(cGH *GH, 
                  int *fortranreturn,
		  int *proc,
		  int *operation_handle,
                  int *num_out_vals,
		  int *type_out_vals,
		  void *out_vals,
		  int *num_in_fields,
		  ... )
{ 
  va_list indices;
  int i;
  int *in_fields = malloc(*num_in_fields*sizeof(int));
  void (*function)(REGISTER_ARGLIST)=NULL; 

  /* Get the pointer to the reduction operator */


  if (*operation_handle < 0)

    CCTK_WARN(3,"Invalid handle passed to CCTK_Reduce");

  else
  {
    function = (void (*)(REGISTER_ARGLIST))
      Util_GetHandledData(ReductionOperators,*operation_handle);
    
    if (function)
      {
	
	/* Fill in the array of variable indices from the variable argument list */
	va_start(indices, num_in_fields);
	for (i=0; i<*num_in_fields; i++)
	  in_fields[i] = *va_arg(indices,int *);
	va_end(indices);
	
	function(GH,*proc,*num_out_vals,*type_out_vals,out_vals,*num_in_fields,in_fields);
	
	if (in_fields) free(in_fields);
	
      }
    else
      CCTK_WARN(3,"Reduction operation is not registered and cannot be called");
  }

  *fortranreturn= 1;

}