aboutsummaryrefslogtreecommitdiff
path: root/Carpet/Carpet/src/Requirements.cc
blob: 630d1057d907cafce4d74c99704500b6115ca233 (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
348
349
350
351
352
353
#include <algorithm>
#include <set>
#include <string>

#include <cctk.h>
#include <cctk_Schedule.h>

#include <cctki_GHExtensions.h>
#include <cctki_Schedule.h>

#include <carpet.hh>

using namespace std;



#ifdef CACTUS_HAS_REQUIRES_CLAUSES

// Illegally copied from ScheduleInterface.c:

typedef enum {sched_none, sched_group, sched_function} iSchedType;
typedef enum {schedpoint_misc, schedpoint_analysis} iSchedPoint;

typedef struct t_timer
{
  struct t_timer *next;
  int timer_handle;
  char *schedule_bin;
  int has_been_output;
} t_timer;

typedef struct
{
  /* Static data */
  char *description;

  /*char *thorn; MOVED TO FunctionData */
  char *implementation;

  iSchedType type;

  cFunctionData FunctionData;

  int n_mem_groups;
  int *mem_groups;
  int *timelevels;

  int n_comm_groups;
  int *comm_groups;

  /* Timer data */
  t_timer *timers;

  /* Dynamic data */
  int *CommOnEntry;
  int *StorageOnEntry;

  int done_entry;
  int synchronised;

} t_attribute;



namespace Carpet {
  namespace Requirements {
    
    
    
    // Rules:
    //
    // 1. Everything that is required by a routine must be provided by
    //    another routine which is scheduled earlier.
    //
    // 2. Things can be provided only once, not multiple times.
    //    Except when they are also provided.
    
    // Keep track of which time levels contain good data; modify this
    // while time level cycling; routine specify how many time levels
    // they require/provide
    
    
    
    int CheckEntry (void * attribute, void * data);
    int CheckExit  (void * attribute, void * data);
    int CheckWhile (int n_whiles, char ** whiles, void * attribute, void * data, int first);
    int CheckIf    (int n_ifs, char ** ifs, void * attribute, void * data);
    int CheckCall  (void * function, void * attribute, void * data);
    
    void CheckOneGroup (cGH const * cctkGH, char const * where);
    
    
    
    int
    CheckEntry (void * const attribute,
                void * const data)
    {
      DECLARE_CCTK_PARAMETERS;
      
      if (not attribute) {
        // Nothing to check
        return 1;
      }
      
      int (*const warn) (char const *thorn, char const *format, ...) =
        requirement_inconsistencies_are_fatal ? CCTK_VParamWarn : CCTK_VInfo;
      
      // Convert argument types
      cFunctionData & function_data =
        (static_cast <t_attribute *> (attribute))->FunctionData;
      set <string> & active_provisions = * static_cast <set <string> *> (data);
      
      // Gather all required items
      set <string> requires;
      for (int n = 0; n < function_data.n_RequiresClauses; ++ n) {
        requires.insert (string (function_data.RequiresClauses[n]));
      }
      
      // Check whether all required items have already been provided
      set <string> required_but_not_provided;
      set_difference
        (requires.begin(), requires.end(),
         active_provisions.begin(), active_provisions.end(),
         insert_iterator <set <string> >
         (required_but_not_provided, required_but_not_provided.begin()));
      
      // Are there unmet requirements?
      if (not required_but_not_provided.empty()) {
        for (set<string>::const_iterator ri = required_but_not_provided.begin();
             ri != required_but_not_provided.end(); ++ ri)
        {
          string const req = * ri;
          warn (CCTK_THORNSTRING,
                "Requirement inconsistency:\n"
                "   Group %s, function %s::%s requires \"%s\" which has not been provided",
                function_data.where,
                function_data.thorn, function_data.routine,
                req.c_str());
        }
      }
      
      // Do traverse this schedule item
      return 1;
    }
    
    
    
    int
    CheckExit (void * const attribute,
               void * const data)
    {
      DECLARE_CCTK_PARAMETERS;
      
      if (not attribute) {
        // Nothing to check
        return 1;
      }
      
      int (*const warn) (char const *thorn, char const *format, ...) =
        requirement_inconsistencies_are_fatal ? CCTK_VParamWarn : CCTK_VInfo;
      
      // Convert argument types
      cFunctionData & function_data =
        (static_cast <t_attribute *> (attribute))->FunctionData;
      set <string> & active_provisions = * static_cast <set <string> *> (data);
      
      // Gather all required and provided items
      set <string> requires;
      for (int n = 0; n < function_data.n_RequiresClauses; ++ n) {
        requires.insert (string (function_data.RequiresClauses[n]));
      }
      set <string> provides;
      for (int n = 0; n < function_data.n_ProvidesClauses; ++ n) {
        provides.insert (string (function_data.ProvidesClauses[n]));
      }
      
      // Check whether any of the providions have already been
      // provided.  (We disallow this as well, so that a routine
      // cannot overwrite what another routine has already set up.)
      set <string> provided_twice;
      set_intersection
        (provides.begin(), provides.end(),
         active_provisions.begin(), active_provisions.end(),
         insert_iterator <set <string> >
         (provided_twice, provided_twice.begin()));
      // But we do allow to provide things which are also required
      set <string> provided_too_often;
      set_difference
        (provided_twice.begin(), provided_twice.end(),
         requires.begin(), requires.end(),
         insert_iterator <set <string> >
         (provided_too_often, provided_too_often.begin()));
      
      // Are there things provided twice?
      if (not provided_too_often.empty()) {
        for (set<string>::const_iterator pi = provided_too_often.begin();
             pi != provided_too_often.end(); ++ pi)
        {
          string const prov = * pi;
          warn (CCTK_THORNSTRING,
                "Requirement inconsistency:\n"
                "   Group %s, function %s::%s provides (and does not require) \"%s\" which has already been provided",
                function_data.where,
                function_data.thorn, function_data.routine,
                prov.c_str());
        }
      }
      
      // Add the new provisions
      for (set<string>::const_iterator pi =
             provides.begin(); pi != provides.end(); ++ pi)
      {
        string const prov = * pi;
        active_provisions.insert (prov);
      }
      
      // ???
      return 1;
    }
    
    
    
    int
    CheckWhile (int const n_whiles, char ** const whiles,
                void * const attribute,
                void * const data,
                int const first)
    {
      // Execute item once
      return first;
    }
    
    
    
    int
    CheckIf (int const n_ifs, char ** const ifs,
             void * const attribute,
             void * const data)
    {
      // Execute item
      return 1;
    }
    
    
    
    int CheckCall (void * const function,
                   void * const attribute,
                   void * const data)
    {
      // Do nothing
      return 0;
    }
    
    
    
    // Check one schedule bin
    void
    CheckOneGroup (cGH const * const cctkGH,
                   char const * const where)
    {
      CCTK_VInfo (CCTK_THORNSTRING,
                  "Checking requirements of schedule bin %s", where);
      
      // Set up initial provision (none at the moment)
      set <string> active_provisions;
      
      // Output initial provisions
      CCTK_VInfo (CCTK_THORNSTRING,
                  "   Initial provisions:");
      for (set<string>::const_iterator pi =
             active_provisions.begin(); pi != active_provisions.end(); ++ pi)
      {
        string const prov = * pi;
        CCTK_VInfo (CCTK_THORNSTRING,
                    "      %s", prov.c_str());
      }
      
      // Check the schedule bin
      CCTKi_DoScheduleTraverse (where,
                                CheckEntry, CheckExit,
                                CheckWhile, CheckIf,
                                CheckCall,
                                & active_provisions);
      
      // Output the final provisions
      CCTK_VInfo (CCTK_THORNSTRING,
                  "   Final provisions:");
      for (set<string>::const_iterator pi =
             active_provisions.begin(); pi != active_provisions.end(); ++ pi)
      {
        string const prov = * pi;
        CCTK_VInfo (CCTK_THORNSTRING,
                    "      %s", prov.c_str());
      }
    }
    
    
    
    // Check everything
    void
    CheckRequirements (cGH const * const cctkGH)
    {
      Checkpoint ("Checking schedule requirements");
      
      // Check some bins
      CheckOneGroup (cctkGH, "CCTK_WRAGH");
      CheckOneGroup (cctkGH, "CCTK_BASEGRID");
      
      CheckOneGroup (cctkGH, "CCTK_RECOVER_VARIABLES");
      CheckOneGroup (cctkGH, "CCTK_POST_RECOVER_VARIABLES");
      
      CheckOneGroup (cctkGH, "CCTK_PREREGRIDINITIAL");
      CheckOneGroup (cctkGH, "CCTK_POSTREGRIDINITIAL");
      CheckOneGroup (cctkGH, "CCTK_INITIAL");
      CheckOneGroup (cctkGH, "CCTK_POSTRESTRICTINITIAL");
      CheckOneGroup (cctkGH, "CCTK_POSTINITIAL");
      CheckOneGroup (cctkGH, "CCTK_CPINITIAL");
      
      CheckOneGroup (cctkGH, "CCTK_PREREGRID");
      CheckOneGroup (cctkGH, "CCTK_POSTREGRID");
      CheckOneGroup (cctkGH, "CCTK_PRESTEP");
      CheckOneGroup (cctkGH, "CCTK_EVOL");
      CheckOneGroup (cctkGH, "CCTK_POSTSTEP");
      CheckOneGroup (cctkGH, "CCTK_CHECKPOINT");
      CheckOneGroup (cctkGH, "CCTK_ANALYSIS");
      
      CheckOneGroup (cctkGH, "CCTK_TERMINATE");
    }
    
  } // namespace Carpet
}   // namespace Requirements



#else // #ifndef CACTUS_HAS_REQUIRES_CLAUSES



namespace Carpet {
  namespace Requirements {
    // Check one schedule bin
    void
    CheckRequirements (cGH const * const cctkGH)
    {
      Checkpoint ("Skipping check of schedule requirements (no flesh support)");
      // do nothing
    }
  } // namespace Carpet
}   // namespace Requirements



#endif // #ifdef CACTUS_HAS_REQUIRES_CLAUSES