aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/data.cc
blob: e3feae50f18f982498125c89bfd87a9e04b7997e (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
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
#include <algorithm>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
#include <vector>

#include <mpi.h>

#include "cctk.h"
#include "cctk_Parameters.h"

#include "bbox.hh"
#include "defs.hh"
#include "dist.hh"
#include "timestat.hh"
#include "vect.hh"

#include "data.hh"
#include "operator_prototypes.hh"

using namespace std;

using namespace CarpetLib;



// Fortran wrappers

template <typename T>
void
prolongate_3d_eno (T const * restrict const src,
                   ivect3 const & srcext,
                   T * restrict const dst,
                   ivect3 const & dstext,
                   ibbox3 const & srcbbox,
                   ibbox3 const & dstbbox,
                   ibbox3 const & regbbox)
{
  CCTK_WARN (0, "Data type not supported");
}

#ifndef OMIT_F90
extern "C"
void
CCTK_FCALL CCTK_FNAME(prolongate_3d_real8_eno)
  (const CCTK_REAL8* src,
   const int& srciext, const int& srcjext, const int& srckext,
   CCTK_REAL8* dst,
   const int& dstiext, const int& dstjext, const int& dstkext,
   const int srcbbox[3][3],
   const int dstbbox[3][3],
   const int regbbox[3][3]);

template <>
void
prolongate_3d_eno (CCTK_REAL8 const * restrict const src,
                   ivect3 const & srcext,
                   CCTK_REAL8 * restrict const dst,
                   ivect3 const & dstext,
                   ibbox3 const & srcbbox,
                   ibbox3 const & dstbbox,
                   ibbox3 const & regbbox)
{
  CCTK_FNAME(prolongate_3d_real8_eno)
    (src,
     srcext[0], srcext[1], srcext[2],
     dst,
     dstext[0], dstext[1], dstext[2],
     reinterpret_cast <int const (*) [3]> (& srcbbox),
     reinterpret_cast <int const (*) [3]> (& dstbbox),
     reinterpret_cast <int const (*) [3]> (& regbbox));
}
#endif



template <typename T>
void
prolongate_3d_weno (T const * restrict const src,
                    ivect3 const & srcext,
                    T * restrict const dst,
                    ivect3 const & dstext,
                    ibbox3 const & srcbbox,
                    ibbox3 const & dstbbox,
                    ibbox3 const & regbbox)
{
  CCTK_WARN (0, "Data type not supported");
}

#ifndef OMIT_F90
extern "C"
void
CCTK_FCALL CCTK_FNAME(prolongate_3d_real8_weno)
  (const CCTK_REAL8* src,
   const int& srciext, const int& srcjext, const int& srckext,
   CCTK_REAL8* dst,
   const int& dstiext, const int& dstjext, const int& dstkext,
   const int srcbbox[3][3],
   const int dstbbox[3][3],
   const int regbbox[3][3]);

template <>
void
prolongate_3d_weno (CCTK_REAL8 const * restrict const src,
                    ivect3 const & srcext,
                    CCTK_REAL8 * restrict const dst,
                    ivect3 const & dstext,
                    ibbox3 const & srcbbox,
                    ibbox3 const & dstbbox,
                    ibbox3 const & regbbox)
{
  CCTK_FNAME(prolongate_3d_real8_weno)
    (src,
     srcext[0], srcext[1], srcext[2],
     dst,
     dstext[0], dstext[1], dstext[2],
     reinterpret_cast <int const (*) [3]> (& srcbbox),
     reinterpret_cast <int const (*) [3]> (& dstbbox),
     reinterpret_cast <int const (*) [3]> (& regbbox));
}
#endif



static const CCTK_REAL eps = 1.0e-10;

// Constructors
template<typename T>
data<T>::data (const int varindex_,
               const centering cent_, const operator_type transport_operator_,
               const int vectorlength_, const int vectorindex_,
               data* const vectorleader_,
               const int tag_)
  : gdata(varindex_, cent_, transport_operator_, tag_),
    _memory(NULL),
    vectorlength(vectorlength_), vectorindex(vectorindex_),
    vectorleader(vectorleader_)
{
  assert (vectorlength>=1);
  assert (vectorindex>=0 && vectorindex<vectorlength);
  assert ((vectorindex==0 && not vectorleader)
          || (vectorindex!=0 && vectorleader));
}

template<typename T>
data<T>::data (const int varindex_,
               const centering cent_, const operator_type transport_operator_,
               const int vectorlength_, const int vectorindex_,
               data* const vectorleader_,
               const ibbox& extent_, const int proc_)
  : gdata(varindex_, cent_, transport_operator_),
    _memory(NULL),
    vectorlength(vectorlength_), vectorindex(vectorindex_),
    vectorleader(vectorleader_)
{
  assert (vectorlength>=1);
  assert (vectorindex>=0 && vectorindex<vectorlength);
  assert ((vectorindex==0 && not vectorleader)
          || (vectorindex!=0 && vectorleader));
  allocate(extent_, proc_);
}

// Destructors
template<typename T>
data<T>::~data ()
{
  if (_memory) free();
}
  
// Pseudo constructors
template<typename T>
data<T>* data<T>::make_typed (const int varindex_,
                              const centering cent_,
                              const operator_type transport_operator_,
                              const int tag_)
  const
{
  return new data(varindex_, cent_, transport_operator_, 1, 0, NULL, tag_);
}



// Storage management
template<typename T>
void data<T>::allocate (const ibbox& extent_,
                        const int proc_,
                        void* const memptr)
{
  assert (not _has_storage);
  _has_storage = true;
  // prevent accidental wrap-around
  assert (all(extent_.lower() < numeric_limits<int>::max() / 2));
  assert (all(extent_.lower() > numeric_limits<int>::min() / 2));
  assert (all(extent_.upper() < numeric_limits<int>::max() / 2));
  assert (all(extent_.upper() > numeric_limits<int>::min() / 2));
  // data
  _extent = extent_;
  _shape = max(ivect(0), _extent.shape() / _extent.stride());
  _size = 1;
  for (int d=0; d<dim; ++d) {
    _stride[d] = _size;
    assert (_shape[d]==0 || _size <= INT_MAX / _shape[d]);
    _size *= _shape[d];
  }
  _proc = proc_;
  if (dist::rank() == _proc) {
    if (vectorindex == 0) {
      assert (not vectorleader);
      _memory = new mem<T> (vectorlength, _size, (T*)memptr);
    } else {
      assert (vectorleader);
      _memory = vectorleader->_memory;
      assert (_memory);
    }
    _memory->register_client (vectorindex);
  } else {
    assert (not memptr);
  }
}

template<typename T>
void data<T>::free ()
{
  assert (_has_storage);
  assert (_memory);
  _memory->unregister_client (vectorindex);
  if (not _memory->has_clients()) delete _memory;
  _memory = NULL;
  _has_storage = false;
}



// Processor management
template<typename T>
void data<T>::change_processor_recv (comm_state& state,
                                     const int newproc,
                                     void* const memptr)
{
  DECLARE_CCTK_PARAMETERS;
  
  assert (not comm_active);
  comm_active = true;
  
  if (newproc == _proc) {
    assert (not memptr);
    return;
  }
  
  wtime_changeproc_recv.start();
  
  assert (vectorlength == 1);
  
  if (_has_storage) {
    if (dist::rank() == newproc) {
      // copy from other processor
      
      assert (not _memory);
      _memory = new mem<T> (1, _size, (T*)memptr);
      _memory->register_client (0);
      
      wtime_irecv.start();
      T dummy;
      MPI_Irecv (_memory->storage(0),
                 _size, dist::datatype(dummy), proc(),
                 tag, dist::comm(), &request);
      wtime_irecv.stop(_size * sizeof(T));
      if (use_waitall) {
        state.requests.push_back (request);
      }
      
    } else if (dist::rank() == _proc) {
      // copy to other processor
      
    } else {
      assert (not memptr);
      assert (not _memory);
    }
  }
  
  wtime_changeproc_recv.stop();
}



template<typename T>
void data<T>::change_processor_send (comm_state& state,
                                     const int newproc,
                                     void* const memptr)
{
  DECLARE_CCTK_PARAMETERS;
  
  assert (comm_active);
  
  if (newproc == _proc) {
    assert (not memptr);
    return;
  }
  
  wtime_changeproc_send.start();
  
  assert (vectorlength == 1);
  
  if (_has_storage) {
    if (dist::rank() == newproc) {
      // copy from other processor
      
    } else if (dist::rank() == _proc) {
      // copy to other processor
      
      assert (not memptr);
      assert (_memory);
      
      wtime_isend.start();
      T dummy;
      MPI_Isend (_memory->storage(0),
                 _size, dist::datatype(dummy), newproc,
                 tag, dist::comm(), &request);
      wtime_isend.stop(_size * sizeof(T));
      if (use_waitall) {
        state.requests.push_back (request);
      }
      
    } else {
      assert (not memptr);
      assert (not _memory);
    }
  }
  
  wtime_changeproc_send.stop();
}



template<typename T>
void data<T>::change_processor_wait (comm_state& state,
                                     const int newproc,
                                     void* const memptr)
{
  DECLARE_CCTK_PARAMETERS;
  
  assert (comm_active);
  comm_active = false;
  
  if (newproc == _proc) {
    assert (not memptr);
    return;
  }
  
  wtime_changeproc_wait.start();
  
  assert (vectorlength == 1);
  
  if (use_waitall) {
    if (not state.requests.empty()) {
      // wait for all requests at once
      wtime_irecvwait.start();
      MPI_Waitall
        (state.requests.size(), &state.requests.front(), MPI_STATUSES_IGNORE);
      wtime_irecvwait.stop();
      state.requests.clear();
    }
  }
  
  if (_has_storage) {
    if (dist::rank() == newproc) {
      // copy from other processor
      
      if (not use_waitall) {
        wtime_irecvwait.start();
        MPI_Wait (&request, MPI_STATUS_IGNORE);
        wtime_irecvwait.stop();
      }
      
    } else if (dist::rank() == _proc) {
      // copy to other processor
      
      assert (not memptr);
      assert (_memory);
      
      if (not use_waitall) {
        wtime_isendwait.start();
        MPI_Wait (&request, MPI_STATUS_IGNORE);
        wtime_isendwait.stop();
      }
      
      _memory->unregister_client (0);
      if (not _memory->has_clients()) delete _memory;
      _memory = 0;
      
    } else {
      assert (not memptr);
      assert (not _memory);
    }
  }
  
  _proc = newproc;
  
  wtime_changeproc_wait.stop();
}



// Data manipulators
template<typename T>
comm_state::gcommbuf *
data<T>::
make_typed_commbuf (const ibbox & box)
  const
{
  return new comm_state::commbuf<T> (box);
}



template <typename T>
void data <T>
::copy_from_innerloop (gdata const * const gsrc,
                       ibbox const & box)
{
  data const * const src = dynamic_cast <data const *> (gsrc);
  assert (has_storage() and src->has_storage());
  
  assert (proc() == src->proc());
  assert (dist::rank() == proc());
  
  copy_3d (static_cast <T const *> (src->storage()),
           src->shape(),
           static_cast <T *> (this->storage()),
           this->shape(),
           src->extent(),
           this->extent(),
           box);
}



template <typename T>
void data <T>
::interpolate_from_innerloop (vector <gdata const *> const & gsrcs,
                              vector <CCTK_REAL> const & times,
			      ibbox const & box,
                              CCTK_REAL const time,
                              int const order_space,
			      int const order_time)
{
  assert (has_storage());
  
  vector <data const *> srcs (gsrcs.size());
  for (size_t t=0; t<srcs.size(); ++t) {
    srcs.AT(t) = dynamic_cast <data const *> (gsrcs.AT(t));
  }
  assert (srcs.size() == times.size() and srcs.size() > 0);
  
  for (size_t t=0; t<srcs.size(); ++t) {
    assert (srcs.AT(t)->has_storage());
    assert (proc() == srcs.AT(t)->proc());
  }
  
  assert (dist::rank() == proc());
  
  interpolate_time (srcs, times, box, time, order_space, order_time);
}



template <typename T>
void data <T>
::interpolate_time (vector <data const *> const & srcs,
                    vector <CCTK_REAL> const & times,
                    ibbox const & box,
                    CCTK_REAL const time,
                    int const order_space,
                    int const order_time)
{
  // Ensure that the times are consistent
  assert (times.size() > 0);
  CCTK_REAL const min_time = * min_element (times.begin(), times.end());
  CCTK_REAL const max_time = * max_element (times.begin(), times.end());
  if (transport_operator != op_copy) {
    if (time < min_time - eps or time > max_time + eps) {
      ostringstream buf;
      buf << "Internal error: extrapolation in time."
          << "  time=" << time
          << "  times=" << times;
      CCTK_WARN (0, buf.str().c_str());
    }
  }
  
  // Use this timelevel, or interpolate in time if set to -1
  int timelevel = -1;
  
  // Try to avoid time interpolation if possible
  if (timelevel == -1) {
    if (times.size() == 1) {
      timelevel = 0;
    }
  }
  if (timelevel == -1) {
    if (transport_operator == op_copy) {
      timelevel = 0;
    }
  }
  if (timelevel == -1) {
    for (size_t tl=0; tl<times.size(); ++tl) {
      if (abs (times.AT(tl) - time) < eps) {
        timelevel = tl;
        break;
      }
    }
  }
  
  if (timelevel == -1) {
    // Time interpolation is necessary
    
    vector <data *> tmps (times.size());
    
    for (size_t tl=0; tl<times.size(); ++tl) {
      
      tmps.AT(tl) =
        new data (this->varindex, this->cent, this->transport_operator);
      tmps.AT(tl)->allocate (box, this->proc());
      
      tmps.AT(tl)->interpolate_p_r (srcs.AT(tl), box, order_space);
      
    }
    
    time_interpolate (tmps, box, times, time, order_time);
    
    for (size_t tl=0; tl<times.size(); ++tl) {
      delete tmps.AT(tl);
    }
    
  } else {
    // No time interpolation
    
    interpolate_p_r (srcs.AT(timelevel), box, order_space);
    
  } // if
}



template <typename T>
void data <T>
::interpolate_p_r (data const * const src,
                   ibbox const & box,
                   int const order_space)
{
  if (all (src->extent().stride() > this->extent().stride())) {
    // Prolongate
    interpolate_p_vc_cc (src, box, order_space);
  } else if (all (src->extent().stride() < this->extent().stride())) {
    // Restrict
    interpolate_restrict (src, box, order_space);
  } else {
    assert (0);
  }
}



template <typename T>
void data <T>
::interpolate_p_vc_cc (data const * const src,
                       ibbox const & box,
                       int const order_space)
{
  if (cent == vertex_centered) {
    // Vertex centred
    
    interpolate_prolongate (src, box, order_space);
    
  } else if (cent == cell_centered) {
    // Cell centred
    
    // Destination region
    assert (all (box.stride() % 2 == 0));
    ibbox const newdstbox (box.lower() - box.stride() / 2,
                           box.upper() + box.stride() / 2,
                           box.stride());
    
    // Source region
    ibbox const & srcbox = src->extent();
    
    assert (all (srcbox.stride() % 2 == 0));
    ibbox const tmpsrcbox (srcbox.lower() - srcbox.stride() / 2,
                           srcbox.upper() + srcbox.stride() / 2,
                           srcbox.stride());
    
    assert (all (srcbox.stride() % box.stride() == 0));
    ivect const reffact = srcbox.stride() / box.stride();
    
    ivect const regext = newdstbox.shape() / newdstbox.stride();
    assert (all ((newdstbox.lower() - srcbox.lower()) % box.stride() == 0));
    ivect const srcoff = (newdstbox.lower() - srcbox.lower()) / box.stride();
    
    bvect const needoffsetlo =
      srcoff % reffact != 0 or regext > 1;
    bvect const needoffsethi =
      (srcoff + regext - 1) % reffact != 0 or regext > 1;
    
    assert (order_space % 2 == 1);
    int const stencil_size = (order_space + 1) / 2;
    
    ivect const offsetlo = either (needoffsetlo, stencil_size, 0);
    ivect const offsethi = either (needoffsethi, stencil_size, 0);
    
    ibbox const newsrcbox =
      newdstbox .contracted_for (tmpsrcbox) .expand (offsetlo, offsethi);
    
    // Allocate temporary storage
    data * const newsrc =
      new data (src->varindex, vertex_centered, src->transport_operator);
    newsrc->allocate (newsrcbox, src->proc());
    
    data * const newdst =
      new data (this->varindex, vertex_centered, this->transport_operator);
    newdst->allocate (newdstbox, this->proc());
    
    // Convert source to primitive representation
    prolongate_3d_cc_rf2_std2prim
      (static_cast <T const *> (src->storage()),
       src->shape(),
       static_cast <T *> (newsrc->storage()),
       newsrc->shape(),
       src->extent(),
       newsrc->extent(),
       newsrc->extent());
    
    // Interpolate
    newdst->interpolate_prolongate (newsrc, newdstbox, order_space);
    
    // Convert destination to standard representation
    prolongate_3d_cc_rf2_prim2std
      (static_cast <T const *> (newdst->storage()),
       newdst->shape(),
       static_cast <T *> (this->storage()),
       this->shape(),
       newdst->extent(),
       this->extent(),
       box);
    
    delete newsrc;
    delete newdst;
    
  } else {
    assert (0);
  }
}

template <>
void data <CCTK_INT>
::interpolate_p_vc_cc (data const * const src,
                       ibbox const & box,
                       int const order_space)
{
  CCTK_WARN (0, "Data type not supported");
}



template <typename T>
void data <T>
::interpolate_prolongate (data const * const src,
                          ibbox const & box,
                          int const order_space)
{
  wtime_prolongate.start();
  
  switch (transport_operator) {
    
  case op_copy:
  case op_Lagrange:
    wtime_prolongate_Lagrange.start();
    switch (order_space) {
    case 1:
      prolongate_3d_o1_rf2 (static_cast <T const *> (src->storage()),
                            src->shape(),
                            static_cast <T *> (this->storage()),
                            this->shape(),
                            src->extent(),
                            this->extent(),
                            box);
      break;
    case 3:
      prolongate_3d_o3_rf2 (static_cast <T const *> (src->storage()),
                            src->shape(),
                            static_cast <T *> (this->storage()),
                            this->shape(),
                            src->extent(),
                            this->extent(),
                            box);
      break;
    case 5:
      prolongate_3d_o5_rf2 (static_cast <T const *> (src->storage()),
                            src->shape(),
                            static_cast <T *> (this->storage()),
                            this->shape(),
                            src->extent(),
                            this->extent(),
                            box);
      break;
    default:
      assert (0);
    }
    wtime_prolongate_Lagrange.stop();
    break;
    
  case op_ENO:
    wtime_prolongate_ENO.start();
    switch (order_space) {
    case 1:
      CCTK_WARN (0, "There is no stencil for op=\"ENO\" with order_space=1");
      break;
    case 3:
      prolongate_3d_eno (static_cast <T const *> (src->storage()),
                         src->shape(),
                         static_cast <T *> (this->storage()),
                         this->shape(),
                         src->extent(),
                         this->extent(),
                         box);
      break;
    default:
      assert (0);
    }
    wtime_prolongate_ENO.stop();
    break;
    
  case op_WENO:
    wtime_prolongate_WENO.start();
    switch (order_space) {
    case 1:
      CCTK_WARN (0, "There is no stencil for op=\"WENO\" with order_space=1");
      break;
    case 3:
      CCTK_WARN (0, "There is no stencil for op=\"WENO\" with order_space=3");
      break;
    case 5:
      prolongate_3d_eno (static_cast <T const *> (src->storage()),
                         src->shape(),
                         static_cast <T *> (this->storage()),
                         this->shape(),
                         src->extent(),
                         this->extent(),
                         box);
      break;
    default:
      assert (0);
    }
    wtime_prolongate_WENO.stop();
    break;
    
  default:
    assert(0);
  } // switch (transport_operator)
  
  wtime_prolongate.stop();
}

template <>
void data <CCTK_INT>
::interpolate_prolongate (data const * const src,
                          ibbox const & box,
                          int const order_space)
{
  CCTK_WARN (0, "Data type not supported");
}



template <typename T>
void data <T>
::interpolate_restrict (data const * const src,
                        ibbox const & box,
                        int const order_space)
{
  wtime_restrict.start();
  
  switch (transport_operator) {
    
  case op_copy:
  case op_Lagrange:
  case op_ENO:
  case op_WENO:
    // enum centering { vertex_centered, cell_centered };
    switch (cent) {
    case vertex_centered:
      restrict_3d_rf2 (static_cast <T const *> (src->storage()),
                       src->shape(),
                       static_cast <T *> (this->storage()),
                       this->shape(),
                       src->extent(),
                       this->extent(),
                       box);
      break;
    case cell_centered:
      restrict_3d_cc_rf2 (static_cast <T const *> (src->storage()),
                          src->shape(),
                          static_cast <T *> (this->storage()),
                          this->shape(),
                          src->extent(),
                          this->extent(),
                          box);
      break;
    default:
      assert (0);
    }
    break;
    
  default:
    assert(0);
  }
  
  wtime_restrict.stop();
}

template <>
void data <CCTK_INT>
::interpolate_restrict (data const * const src,
                        ibbox const & box,
                        int const order_space)
{
  CCTK_WARN (0, "Data type not supported");
}



template <typename T>
void data <T>
::time_interpolate (vector <data *> const & srcs,
                    ibbox const & box,
                    vector <CCTK_REAL> const & times,
                    CCTK_REAL const time,
                    int const order_time)
{
  switch (order_time) {
    
  case 0:
    // We could handle this, but this points to an inefficiency
    assert (0);
    
  case 1:
    assert (times.size() >= 2);
    interpolate_3d_2tl (static_cast <T const *> (srcs.AT(0)->storage()),
                        times.AT(0),
                        static_cast <T const *> (srcs.AT(1)->storage()),
                        times.AT(1),
                        srcs.AT(0)->shape(),
                        static_cast <T *> (this->storage()),
                        time,
                        this->shape(),
                        srcs.AT(0)->extent(),
                        this->extent(),
                        box);
    break;
    
  case 2:
    assert (times.size() >= 3);
    interpolate_3d_3tl (static_cast <T const *> (srcs.AT(0)->storage()),
                        times.AT(0),
                        static_cast <T const *> (srcs.AT(1)->storage()),
                        times.AT(1),
                        static_cast <T const *> (srcs.AT(2)->storage()),
                        times.AT(2),
                        srcs.AT(0)->shape(),
                        static_cast <T *> (this->storage()),
                        time,
                        this->shape(),
                        srcs.AT(0)->extent(),
                        this->extent(),
                        box);
    break;
    
  default:
    assert (0);
  }
}

template <>
void data <CCTK_INT>
::time_interpolate (vector <data *> const & srcs,
                    ibbox const & box,
                    vector <CCTK_REAL> const & times,
                    CCTK_REAL const time,
                    int const order_time)
{
  CCTK_WARN (0, "Data type not supported");
}



// Output
template<typename T>
ostream& data<T>::output (ostream& os) const
{
  T Tdummy;
  os << "data<" << typestring(Tdummy) << ">:"
     << "extent=" << extent() << ","
     << "stride=" << stride() << ",size=" << size();
  return os;
}

template<typename T>
ostream & operator << (ostream & os, const data<T> & d)
{
  char const * space = "";
  for (int i = 0; i < d.vectorlength; i++) {
    os << space << d[i];
    space = " ";
  }
  return os;
}



#define INSTANTIATE(T)				\
template class data<T>;
template 
ostream & operator << <CCTK_REAL> (ostream & os, data<CCTK_REAL> const & d);
#include "instantiate"

#undef INSTANTIATE