aboutsummaryrefslogtreecommitdiff
path: root/Carpet/Carpet/src/CallFunction.cc
blob: 054ea6808dedcec29f5e4872c923143caad7e745 (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
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
#include <algorithm>
#include <cassert>
#include <cstdlib>

#include <cctk.h>
#include <cctk_Parameters.h>
#include <cctki_GHExtensions.h>

#include <gh.hh>

#include <carpet.hh>
#include <Timers.hh>



namespace Carpet {
  
  using namespace std;
  
  
  
  static void
  CallScheduledFunction (char const * restrict time_and_mode,
                         void * function,
                         cFunctionData * attribute,
                         void * data,
                         Timer & user_timer);
  
  static void
  SyncGroupsInScheduleBlock (cFunctionData * attribute, cGH * cctkGH);
  
  /// Traverse one function on all components of one refinement level
  /// of one multigrid level.
  int CallFunction (void * function, ///< the function to call
                    cFunctionData * attribute, ///< attributes of the function
                    void * data) ///< private data for CCTK_CallFunction
  {
    DECLARE_CCTK_PARAMETERS;
    
    static Timer total_timer ("CallFunction");
    static Timer user_timer  ("CallFunction::thorns");
    static Timer sync_timer  ("CallFunction::syncs");
    
    total_timer.start();
    
    cGH * cctkGH = static_cast<cGH *> (data);
    
    assert (int (not not attribute->meta) +
            int (not not attribute->meta_early) +
            int (not not attribute->meta_late) +
            int (not not attribute->global) +
            int (not not attribute->global_early) +
            int (not not attribute->global_late) +
            int (not not attribute->level) +
            int (not not attribute->singlemap) +
            int (not not attribute->local)
            <= 1);
    
    assert (not not attribute->loop_global +
            not not attribute->loop_level +
            not not attribute->loop_singlemap +
            not not attribute->loop_local
            <= 1);
    
    if (attribute->meta or attribute->meta_early or attribute->meta_late or
        is_meta_mode())
    {
      // Convtest operation
      
      if ((attribute->meta and do_meta_mode) or
          (attribute->meta_early and do_early_meta_mode) or
          (attribute->meta_late and do_late_meta_mode) or
          is_meta_mode())
      {
        if (attribute->loop_local) {
          BEGIN_META_MODE(cctkGH) {
            BEGIN_MGLEVEL_LOOP(cctkGH) {
              BEGIN_REFLEVEL_LOOP(cctkGH) {
                BEGIN_LOCAL_MAP_LOOP(cctkGH, CCTK_GF) {
                  BEGIN_LOCAL_COMPONENT_LOOP(cctkGH, CCTK_GF) {
                    CallScheduledFunction
                      ("Meta time local mode",
                       function, attribute, data, user_timer);
                  } END_LOCAL_COMPONENT_LOOP;
                } END_LOCAL_MAP_LOOP;
                sync_timer.start();
                SyncGroupsInScheduleBlock (attribute, cctkGH) ;
                sync_timer.stop();
              } END_REFLEVEL_LOOP;
            } END_MGLEVEL_LOOP;
          } END_META_MODE;
        } else if (attribute->loop_singlemap) {
          BEGIN_META_MODE(cctkGH) {
            BEGIN_MGLEVEL_LOOP(cctkGH) {
              BEGIN_REFLEVEL_LOOP(cctkGH) {
                BEGIN_MAP_LOOP(cctkGH, CCTK_GF) {
                  CallScheduledFunction
                    ("Meta time singlemap mode",
                     function, attribute, data, user_timer);
                } END_MAP_LOOP;
                sync_timer.start();
                SyncGroupsInScheduleBlock (attribute, cctkGH) ;
                sync_timer.stop();
              } END_REFLEVEL_LOOP;
            } END_MGLEVEL_LOOP;
          } END_META_MODE;
        } else if (attribute->loop_level) {
          BEGIN_META_MODE(cctkGH) {
            BEGIN_MGLEVEL_LOOP(cctkGH) {
              BEGIN_REFLEVEL_LOOP(cctkGH) {
                CallScheduledFunction
                  ("Meta time level mode",
                   function, attribute, data, user_timer);
                sync_timer.start();
                SyncGroupsInScheduleBlock (attribute, cctkGH) ;
                sync_timer.stop();
              } END_REFLEVEL_LOOP;
            } END_MGLEVEL_LOOP;
          } END_META_MODE;
        } else if (attribute->loop_global) {
          BEGIN_META_MODE(cctkGH) {
            BEGIN_MGLEVEL_LOOP(cctkGH) {
              CallScheduledFunction
                ("Meta time global mode",
                 function, attribute, data, user_timer);
              BEGIN_REFLEVEL_LOOP(cctkGH) {
                sync_timer.start();
                SyncGroupsInScheduleBlock (attribute, cctkGH) ;
                sync_timer.stop();
              } END_REFLEVEL_LOOP;
            } END_MGLEVEL_LOOP;
          } END_META_MODE;
        } else {
          BEGIN_META_MODE(cctkGH) {
            CallScheduledFunction
              ("Meta mode",
               function, attribute, data, user_timer);
            BEGIN_MGLEVEL_LOOP(cctkGH) {
              BEGIN_REFLEVEL_LOOP(cctkGH) {
                sync_timer.start();
                SyncGroupsInScheduleBlock (attribute, cctkGH) ;
                sync_timer.stop();
              } END_REFLEVEL_LOOP;
            } END_MGLEVEL_LOOP;
          } END_META_MODE;
        }
      }
      
    } else if (attribute->global or attribute->global_early or
               attribute->global_late or is_global_mode())
    {
      // Global operation: call once
      
      if ((attribute->global and do_global_mode) or
          (attribute->global_early and do_early_global_mode) or
          (attribute->global_late and do_late_global_mode) or
          is_global_mode())
      {
        if (attribute->loop_local) {
          BEGIN_GLOBAL_MODE(cctkGH) {
            BEGIN_REFLEVEL_LOOP(cctkGH) {
              BEGIN_LOCAL_MAP_LOOP(cctkGH, CCTK_GF) {
                BEGIN_LOCAL_COMPONENT_LOOP(cctkGH, CCTK_GF) {
                  CallScheduledFunction
                    ("Global time local mode",
                     function, attribute, data, user_timer);
                } END_LOCAL_COMPONENT_LOOP;
              } END_LOCAL_MAP_LOOP;
              sync_timer.start();
              SyncGroupsInScheduleBlock (attribute, cctkGH) ;
              sync_timer.stop();
            } END_REFLEVEL_LOOP;
          } END_GLOBAL_MODE;
        } else if (attribute->loop_singlemap) {
          BEGIN_GLOBAL_MODE(cctkGH) {
            BEGIN_REFLEVEL_LOOP(cctkGH) {
              BEGIN_MAP_LOOP(cctkGH, CCTK_GF) {
                CallScheduledFunction
                  ("Global time singlemap mode",
                   function, attribute, data, user_timer);
              } END_MAP_LOOP;
              sync_timer.start();
              SyncGroupsInScheduleBlock (attribute, cctkGH) ;
              sync_timer.stop();
            } END_REFLEVEL_LOOP;
          } END_GLOBAL_MODE;
        } else if (attribute->loop_level) {
          BEGIN_GLOBAL_MODE(cctkGH) {
            BEGIN_REFLEVEL_LOOP(cctkGH) {
              CallScheduledFunction
                ("Global time level mode",
                 function, attribute, data, user_timer);
              sync_timer.start();
              SyncGroupsInScheduleBlock (attribute, cctkGH) ;
              sync_timer.stop();
            } END_REFLEVEL_LOOP;
          } END_GLOBAL_MODE;
        } else {
          BEGIN_GLOBAL_MODE(cctkGH) {
            CallScheduledFunction
              ("Global mode",
               function, attribute, data, user_timer);
            BEGIN_REFLEVEL_LOOP(cctkGH) {
              sync_timer.start();
              SyncGroupsInScheduleBlock (attribute, cctkGH) ;
              sync_timer.stop();
            } END_REFLEVEL_LOOP;
          } END_GLOBAL_MODE;
        }
      }
      
    } else if (attribute->level) {
      // Level operation: call once per refinement level
      
      if (attribute->loop_local) {
        BEGIN_LOCAL_MAP_LOOP(cctkGH, CCTK_GF) {
          BEGIN_LOCAL_COMPONENT_LOOP(cctkGH, CCTK_GF) {
            CallScheduledFunction
              ("Level time local mode",
               function, attribute, data, user_timer);
          } END_LOCAL_COMPONENT_LOOP;
        } END_LOCAL_MAP_LOOP;
      } else if (attribute->loop_singlemap) {
        BEGIN_MAP_LOOP(cctkGH, CCTK_GF) {
          CallScheduledFunction
            ("Level time singlemap mode",
             function, attribute, data, user_timer);
        } END_MAP_LOOP;
      } else {
        CallScheduledFunction
          ("Level mode",
           function, attribute, data, user_timer);
      }
      sync_timer.start();
      SyncGroupsInScheduleBlock (attribute, cctkGH) ;
      sync_timer.stop();
      
    } else if (attribute->singlemap) {
      // Single map operation: call once per refinement level and map
      
      if (attribute->loop_local) {
        BEGIN_LOCAL_MAP_LOOP(cctkGH, CCTK_GF) {
          BEGIN_LOCAL_COMPONENT_LOOP(cctkGH, CCTK_GF) {
            CallScheduledFunction
              ("Singlemap time local mode",
               function, attribute, data, user_timer);
          } END_LOCAL_COMPONENT_LOOP;
        } END_LOCAL_MAP_LOOP;
      } else {
        BEGIN_MAP_LOOP(cctkGH, CCTK_GF) {
          CallScheduledFunction
            ("Singlemap mode",
             function, attribute, data, user_timer);
        } END_MAP_LOOP;
      }
      sync_timer.start();
      SyncGroupsInScheduleBlock (attribute, cctkGH) ;
      sync_timer.stop();
      
    } else {
      // Local operation: call once per component
      
      BEGIN_LOCAL_MAP_LOOP(cctkGH, CCTK_GF) {
        BEGIN_LOCAL_COMPONENT_LOOP(cctkGH, CCTK_GF) {
          CallScheduledFunction
            ("Local mode",
             function, attribute, data, user_timer);
        } END_LOCAL_COMPONENT_LOOP;
      }	END_LOCAL_MAP_LOOP;
      sync_timer.start();
      SyncGroupsInScheduleBlock (attribute, cctkGH) ;
      sync_timer.stop();
      
    }
    
    if (schedule_barriers) {
#if 0
      static unsigned int magic = 0xe8932329UL; // a random starting value
      unsigned int recv = magic;
      Checkpoint ("About to Bcast %u", magic);
      MPI_Bcast (& recv, 1, MPI_UNSIGNED, 0, dist::comm());
      Checkpoint ("Finished Bcast");
      if (recv != magic) {
        CCTK_WARN (CCTK_WARN_ABORT,
                   "Inconsistent communication schedule: not all processes return to CallFunction at the same time");
      }
      ++ magic;
      Checkpoint ("About to Barrier");
      MPI_Barrier (dist::comm());
      Checkpoint ("Finished Barrier");
#endif
      static int id = 513400912; // arbitrary starting value
      CCTK_NamedBarrier (NULL, id++);
    }
    
    total_timer.stop();
    
    // The return value indicates whether the grid functions have been
    // synchronised.
    // 0: let the flesh do the synchronisation
    // 1: we did the synchronisation
    return 1;
  }
  
  
  
  void
  CallScheduledFunction (char const * restrict const time_and_mode,
                         void * const function,
                         cFunctionData * const attribute,
                         void * const data,
                         Timer & user_timer)
  {
    cGH const * const cctkGH = static_cast <cGH const *> (data);
    Checkpoint ("%s call at %s to %s::%s",
                time_and_mode,
                attribute->where,
                attribute->thorn, attribute->routine);
    CallBeforeRoutines (cctkGH, function, attribute, data);
    user_timer.start();
    int const res = CCTK_CallFunction (function, attribute, data);
    user_timer.stop();
    CallAfterRoutines (cctkGH, function, attribute, data);
    assert (res==0);
  }
  
  
  
  void SyncGroupsInScheduleBlock (cFunctionData* attribute, cGH* cctkGH)
  {
    // check if there is anything to do
    if (attribute->n_SyncGroups <= 0) return;

    // remove all empty groups from the list
    vector<int> groups;
    groups.reserve (attribute->n_SyncGroups);
    for (int g = 0; g < attribute->n_SyncGroups; g++) {
      const int group = attribute->SyncGroups[g];
      if (CCTK_NumVarsInGroupI (group) > 0) {
        groups.push_back (group);
      }
    }

    SyncProlongateGroups (cctkGH, groups);
  }
  
} // namespace Carpet