aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/restrict_3d_vc_rf2.cc
blob: 2ff64032d3c7e54b23df8b9e76b7bb4e6e361998 (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <cctk.h>
#include <cctk_Parameters.h>

#include <algorithm>
#include <cassert>
#include <cmath>

#include "gdata.hh"
#include "operator_prototypes_3d.hh"
#include "typeprops.hh"

using namespace std;



namespace CarpetLib {


  
#define SRCIND3(i,j,k)                                  \
  index3 (srcioff + (i), srcjoff + (j), srckoff + (k),  \
          srciext, srcjext, srckext)
#define DSTIND3(i,j,k)                                  \
  index3 (dstioff + (i), dstjoff + (j), dstkoff + (k),  \
          dstiext, dstjext, dstkext)
#define SRCOFF3(i,j,k)                                  \
  offset3 (srcioff + (i), srcjoff + (j), srckoff + (k),  \
          srciext, srcjext, srckext)
#define DSTOFF3(i,j,k)                                  \
  offset3 (dstioff + (i), dstjoff + (j), dstkoff + (k),  \
          dstiext, dstjext, dstkext)
  
  
  
  // 0D "restriction"
  template <typename T>
  struct restrict0 {
    static inline T call (T const * restrict const p)
    {
      return * p;
    }
  };
  
  // 1D restriction
  template <typename T, int centi>
  struct restrict1 {
    static inline T call (T const * restrict const p,
                          size_t const d1);
  };
  template <typename T>
  struct restrict1<T,0> {
    static inline T call (T const * restrict const p,
                          size_t const d1)
    {
      T const res = restrict0<T>::call (p);
      return res;
    }
  };
  template <typename T>
  struct restrict1<T,1> {
    static inline T call (T const * restrict const p,
                          size_t const d1)
    {
      typedef typename typeprops<T>::real RT;
      RT const one  = 1;
      RT const half = one/2;
      T const res =
        half * restrict0<T>::call (p     ) +
        half * restrict0<T>::call (p + d1);
      return res;
    }
  };
  
  // 2D restriction
  template <typename T, int centi, int centj>
  struct restrict2 {
    static inline T call (T const * restrict const p,
                          size_t const d1, size_t const d2);
  };
  template <typename T, int centi>
  struct restrict2<T,centi,0> {
    static inline T call (T const * restrict const p,
                          size_t const d1, size_t const d2)
    {
      T const res = restrict1<T,centi>::call (p, d1);
      return res;
    }
  };
  template <typename T, int centi>
  struct restrict2<T,centi,1> {
    static inline T call (T const * restrict const p,
                          size_t const d1, size_t const d2)
    {
      typedef typename typeprops<T>::real RT;
      RT const one  = 1;
      RT const half = one/2;
      T const res =
        half * restrict1<T,centi>::call (p     , d1) +
        half * restrict1<T,centi>::call (p + d2, d1);
      return res;
    }
  };
  
  // 3D restriction
  template <typename T, int centi, int centj, int centk>
  struct restrict3 {
    static inline T call (T const * restrict const p,
                          size_t const d1, size_t const d2, size_t const d3);
  };
  template <typename T, int centi, int centj>
  struct restrict3<T,centi,centj,0> {
    static inline T call (T const * restrict const p,
                          size_t const d1, size_t const d2, size_t const d3)
    {
      T const res = restrict2<T,centi,centj>::call (p, d1, d2);
      return res;
    }
  };
  template <typename T, int centi, int centj>
  struct restrict3<T,centi,centj,1> {
    static inline T call (T const * restrict const p,
                          size_t const d1, size_t const d2, size_t const d3)
    {
      typedef typename typeprops<T>::real RT;
      RT const one  = 1;
      RT const half = one/2;
      T const res =
        half * restrict2<T,centi,centj>::call (p     , d1, d2) +
        half * restrict2<T,centi,centj>::call (p + d3, d1, d2);
      return res;
    }
  };
  
  
  
  template <typename T, int centi, int centj, int centk>
  void
  restrict_3d_vc_rf2 (T const * restrict const src,
                      ivect3 const & restrict srcext,
                      T * restrict const dst,
                      ivect3 const & restrict dstext,
                      ibbox3 const & restrict srcbbox,
                      ibbox3 const & restrict dstbbox,
                      ibbox3 const & restrict srcregbbox,
                      ibbox3 const & restrict regbbox,
                      void * extraargs)
  {
    assert (not extraargs);
    
    DECLARE_CCTK_PARAMETERS;
    
    typedef typename typeprops<T>::real RT;
    
    // false: vertex centered, true: cell centered
    ivect const icent (centi, centj, centk);
    assert (all (icent == 0 or icent == 1));
    
    if (any (srcbbox.stride() >= regbbox.stride() or
             dstbbox.stride() != regbbox.stride()))
    {
      CCTK_WARN (0, "Internal error: strides disagree");
    }
    
    if (any (reffact2 * srcbbox.stride() != dstbbox.stride())) {
      CCTK_WARN (0, "Internal error: destination strides are not twice the source strides");
    }
    
    // This could be handled, but is likely to point to an error
    // elsewhere
    if (regbbox.empty()) {
      CCTK_WARN (0, "Internal error: region extent is empty");
    }

    // shift vertex centered directions to lower interface (see
    // Refluxing for conventions)
    ivect const ivert(icent == 0);
    ibbox3 const unshifted_srcbbox = srcbbox.shift(-ivert,2);
    ibbox3 const unshifted_dstbbox = dstbbox.shift(-ivert,2);
    // ibbox3 const unshifted_srcregbbox = srcregbbox.shift(-ivert,2);
    ibbox3 const unshifted_regbbox = regbbox.shift(-ivert,2);
    
    if (not unshifted_regbbox.expanded_for(unshifted_srcbbox).is_contained_in(unshifted_srcbbox) or
        not unshifted_regbbox.is_contained_in(unshifted_dstbbox))
    {
      cerr << "unshifted_srcbbox: " << unshifted_srcbbox << endl
           << "unshifted_dstbbox: " << unshifted_dstbbox << endl
           << "unshifted_regbbox: " << unshifted_regbbox << endl;
      CCTK_WARN (0, "Internal error: region extent is not contained in array extent");
    }
    
    if (any (srcext != gdata::allocated_memory_shape(srcbbox.shape() / srcbbox.stride()) or
             dstext != gdata::allocated_memory_shape(dstbbox.shape() / dstbbox.stride())))
    {
      CCTK_WARN (0, "Internal error: array sizes don't agree with bounding boxes");
    }
    
    
    
    ivect3 const regext = regbbox.shape() / regbbox.stride();
    assert (all (srcbbox.stride() % 2 == 0));
    if (not (all ((regbbox.lower() - srcbbox.lower() - srcbbox.stride() / 2) %
                  srcbbox.stride() == 0)))
    {
      cout << "restrict_3d_vc_rf2.cc\n";
      cout << "regbbox=" << regbbox << "\n";
      cout << "srcbbox=" << srcbbox << "\n";
      cout << "icent=" << icent << "\n";
    }
    assert (all ((regbbox.lower() - srcbbox.lower() - srcbbox.stride() / 2) %
                 srcbbox.stride() == 0));
    ivect3 const srcoff =
      (regbbox.lower() - srcbbox.lower() - srcbbox.stride() / 2) /
      srcbbox.stride();
    assert (all ((regbbox.lower() - dstbbox.lower()) % dstbbox.stride() == 0));
    ivect3 const dstoff =
      (regbbox.lower() - dstbbox.lower()) / dstbbox.stride();
    
    
    
    int const srciext = srcext[0];
    int const srcjext = srcext[1];
    int const srckext = srcext[2];
    
    int const dstiext = dstext[0];
    int const dstjext = dstext[1];
    int const dstkext = dstext[2];
    
    int const regiext = regext[0];
    int const regjext = regext[1];
    int const regkext = regext[2];
    
    int const srcioff = srcoff[0];
    int const srcjoff = srcoff[1];
    int const srckoff = srcoff[2];
    
    int const dstioff = dstoff[0];
    int const dstjoff = dstoff[1];
    int const dstkoff = dstoff[2];
    
    // size_t const srcdi == SRCOFF3(1,0,0) - SRCOFF3(0,0,0);
    size_t const srcdi = 1;
    assert (srcdi == SRCOFF3(1,0,0) - SRCOFF3(0,0,0));
    size_t const srcdj = SRCOFF3(0,1,0) - SRCOFF3(0,0,0);
    size_t const srcdk = SRCOFF3(0,0,1) - SRCOFF3(0,0,0);
    
    
    // Loop over coarse region
#pragma omp parallel for //collapse(3)
    for (int k=0; k<regkext; ++k) {
      for (int j=0; j<regjext; ++j) {
        for (int i=0; i<regiext; ++i) {
#ifdef CARPET_DEBUG
    if(not (2 * k + centk < srckext and
            2 * j + centj < srcjext and
            2 * i + centi < srciext))
    {
      cout << "restrict_3d_vc_rf2.cc\n";
      cout << "regext " << regext << "\n";
      cout << "srcext " << srcext << "\n";
      cout << "srcbbox=" << srcbbox << "\n";
      cout << "dstbbox=" << dstbbox << "\n";
      cout << "regbbox=" << regbbox << "\n";
      cout << "srcregbbox=" << srcregbbox << "\n";
      cout << "icent=" << icent << "\n";
    }
    assert(2 * k + centk < srckext and
           2 * j + centj < srcjext and
           2 * i + centi < srciext);
#endif    
          
          dst [DSTIND3(i, j, k)] =
            restrict3<T,centi,centj,centk>::call
            (& src[SRCIND3(2*i, 2*j, 2*k)], srcdi, srcdj, srcdk);
          
        }
      }
    }
    
  }
  
  
  
#define TYPECASE(N,T)                                                   \
  template                                                              \
  void                                                                  \
  restrict_3d_vc_rf2<T,0,0,0> (T const * restrict const src,            \
                               ivect3 const & restrict srcext,          \
                               T * restrict const dst,                  \
                               ivect3 const & restrict dstext,          \
                               ibbox3 const & restrict srcbbox,         \
                               ibbox3 const & restrict dstbbox,         \
                               ibbox3 const & restrict,                 \
                               ibbox3 const & restrict regbbox,         \
                               void * extraargs);                       \
  template                                                              \
  void                                                                  \
  restrict_3d_vc_rf2<T,0,0,1> (T const * restrict const src,            \
                               ivect3 const & restrict srcext,          \
                               T * restrict const dst,                  \
                               ivect3 const & restrict dstext,          \
                               ibbox3 const & restrict srcbbox,         \
                               ibbox3 const & restrict dstbbox,         \
                               ibbox3 const & restrict,                 \
                               ibbox3 const & restrict regbbox,         \
                               void * extraargs);                       \
  template                                                              \
  void                                                                  \
  restrict_3d_vc_rf2<T,0,1,0> (T const * restrict const src,            \
                               ivect3 const & restrict srcext,          \
                               T * restrict const dst,                  \
                               ivect3 const & restrict dstext,          \
                               ibbox3 const & restrict srcbbox,         \
                               ibbox3 const & restrict dstbbox,         \
                               ibbox3 const & restrict,                 \
                               ibbox3 const & restrict regbbox,         \
                               void * extraargs);                       \
  template                                                              \
  void                                                                  \
  restrict_3d_vc_rf2<T,0,1,1> (T const * restrict const src,            \
                               ivect3 const & restrict srcext,          \
                               T * restrict const dst,                  \
                               ivect3 const & restrict dstext,          \
                               ibbox3 const & restrict srcbbox,         \
                               ibbox3 const & restrict dstbbox,         \
                               ibbox3 const & restrict,                 \
                               ibbox3 const & restrict regbbox,         \
                               void * extraargs);                       \
  template                                                              \
  void                                                                  \
  restrict_3d_vc_rf2<T,1,0,0> (T const * restrict const src,            \
                               ivect3 const & restrict srcext,          \
                               T * restrict const dst,                  \
                               ivect3 const & restrict dstext,          \
                               ibbox3 const & restrict srcbbox,         \
                               ibbox3 const & restrict dstbbox,         \
                               ibbox3 const & restrict,                 \
                               ibbox3 const & restrict regbbox,         \
                               void * extraargs);                       \
  template                                                              \
  void                                                                  \
  restrict_3d_vc_rf2<T,1,0,1> (T const * restrict const src,            \
                               ivect3 const & restrict srcext,          \
                               T * restrict const dst,                  \
                               ivect3 const & restrict dstext,          \
                               ibbox3 const & restrict srcbbox,         \
                               ibbox3 const & restrict dstbbox,         \
                               ibbox3 const & restrict,                 \
                               ibbox3 const & restrict regbbox,         \
                               void * extraargs);                       \
  template                                                              \
  void                                                                  \
  restrict_3d_vc_rf2<T,1,1,0> (T const * restrict const src,            \
                               ivect3 const & restrict srcext,          \
                               T * restrict const dst,                  \
                               ivect3 const & restrict dstext,          \
                               ibbox3 const & restrict srcbbox,         \
                               ibbox3 const & restrict dstbbox,         \
                               ibbox3 const & restrict,                 \
                               ibbox3 const & restrict regbbox,         \
                               void * extraargs);                       \
  template                                                              \
  void                                                                  \
  restrict_3d_vc_rf2<T,1,1,1> (T const * restrict const src,            \
                               ivect3 const & restrict srcext,          \
                               T * restrict const dst,                  \
                               ivect3 const & restrict dstext,          \
                               ibbox3 const & restrict srcbbox,         \
                               ibbox3 const & restrict dstbbox,         \
                               ibbox3 const & restrict,                 \
                               ibbox3 const & restrict regbbox,         \
                               void * extraargs);
#include "typecase.hh"
#undef TYPECASE
  
  
  
} // namespace CarpetLib