aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/vect.cc
blob: 9fcaf0e4f6713a7d3a7276c92104d4dfc1a62ace (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
#include <cctk.h>

#include <cassert>
#include <iostream>
#include <typeinfo>

#include "defs.hh"
#include "bboxset.hh"

#include "vect.hh"

using namespace std;



// Input
template<typename T,int D>
void vect<T,D>::input (istream& is) {
  skipws (is);
  consume (is, '[');
  for (int d=0; d<D; ++d) {
    is >> (*this)[d];
    assert (is.good());
    if (d<D-1) {
      skipws (is);
      consume (is, ',');
    }
  }
  skipws (is);
  consume (is, ']');
}



// Output
template<typename T,int D>
void vect<T,D>::output (ostream& os) const {
  os << "[";
  for (int d=0; d<D; ++d) {
    os << (*this)[d];
    if (d<D-1) os << ",";
  }
  os << "]";
}



// MPI
template<typename T,int D>
MPI_Datatype vect<T,D>::mpi_datatype () const
{
  static bool initialised = false;
  static MPI_Datatype newtype;
  if (not initialised) {
    vect<T,D> const& s = *this;
#define ENTRY(type, name)                                               \
    {                                                                   \
      sizeof s.name / sizeof(type), /* count elements */                \
        (char*)&s.name - (char*)&s, /* offsetof doesn't work (why?) */  \
        dist::mpi_datatype<type>(), /* find MPI datatype */             \
        STRINGIFY(name),    /* field name */                            \
        STRINGIFY(type),    /* type name */                             \
        }
    dist::mpi_struct_descr_t const descr[] = {
      ENTRY(T, elt),
      {1, sizeof s, MPI_UB, "MPI_UB", "MPI_UB"}
    };
#undef ENTRY
    ostringstream buf;
    buf << "vect<" << typeid(T).name() << "," << D << ">";
    newtype =
      dist::create_mpi_datatype (sizeof descr / sizeof descr[0], descr,
                                 buf.str().c_str(), sizeof s);
    initialised = true;
  }
  return newtype;
}



// Specialise some constructors for lower dimensions
// These functions are declared, but must not be used.

template<> vect<int,0>::vect (const int& x, const int& y) { assert(0); }
template<> vect<int,1>::vect (const int& x, const int& y) { assert(0); }
template<> vect<int,3>::vect (const int& x, const int& y) { assert(0); }
template<> vect<int,4>::vect (const int& x, const int& y) { assert(0); }

template<> vect<int,0>::vect (const int& x, const int& y, const int& z) { assert(0); }
template<> vect<int,1>::vect (const int& x, const int& y, const int& z) { assert(0); }
template<> vect<int,2>::vect (const int& x, const int& y, const int& z) { assert(0); }
template<> vect<int,4>::vect (const int& x, const int& y, const int& z) { assert(0); }

template<> vect<int,0>::vect (const int& x, const int& y, const int& z, const int& t) { assert(0); }
template<> vect<int,1>::vect (const int& x, const int& y, const int& z, const int& t) { assert(0); }
template<> vect<int,2>::vect (const int& x, const int& y, const int& z, const int& t) { assert(0); }
template<> vect<int,3>::vect (const int& x, const int& y, const int& z, const int& t) { assert(0); }



// Note: We need all dimensions all the time.
template class vect<int,0>;
template class vect<int,1>;
template class vect<int,2>;
template class vect<int,3>;
template class vect<int,4>;

template void vect<unsigned long long,dim>::input (istream& is);
template void vect<CCTK_REAL,dim>::input (istream& is);
template void vect<vect<bool,2>,dim>::input (istream& is);
template void vect<vect<bool,dim>,2>::input (istream& is);
template void vect<vect<int,dim>,2>::input (istream& is);

template void vect<bool,2>::output (ostream& os) const;
template void vect<bool,dim>::output (ostream& os) const;
template void vect<unsigned long long,dim>::output (ostream& os) const;
template void vect<CCTK_REAL,2>::output (ostream& os) const;
template void vect<CCTK_REAL,dim>::output (ostream& os) const;
template void vect<vect<bool,2>,dim>::output (ostream& os) const;
template void vect<vect<int,2>,dim>::output (ostream& os) const;
template void vect<vect<bool,dim>,2>::output (ostream& os) const;
template void vect<vect<int,dim>,2>::output (ostream& os) const;
template void vect<vect<CCTK_REAL,dim>,2>::output (ostream& os) const;
template void vect<vect<vector<int>,2>,dim>::output (ostream& os) const;



// Instantiate for bboxset class

#define DEFINE_FAKE_VECT_OPERATIONS(T,D)                                \
template<> vect<T,D> vect<T,D>::dir (const int d) { assert(0); }        \
template<> vect<T,D> vect<T,D>::seq () { assert(0); }                   \
template<> vect<T,D> vect<T,D>::seq (const int n) { assert(0); }        \
template<> vect<T,D> vect<T,D>::seq (const int n, const int s) { assert(0); } \
template<> vect<T,D>& vect<T,D>::operator*= (const vect<T,D>&) { assert(0); } \
template<> vect<T,D>& vect<T,D>::operator*= (const T&) { assert(0); }   \
template<> vect<T,D>& vect<T,D>::operator/= (const vect<T,D>&) { assert(0); } \
template<> vect<T,D>& vect<T,D>::operator/= (const T&) { assert(0); }   \
template<> vect<T,D>& vect<T,D>::operator%= (const vect<T,D>&) { assert(0); } \
template<> vect<T,D>& vect<T,D>::operator%= (const T&) { assert(0); }   \
template<> vect<T,D>& vect<T,D>::operator^= (const vect<T,D>&) { assert(0); } \
template<> vect<T,D>& vect<T,D>::operator^= (const T&) { assert(0); }   \
template<> vect<T,D> vect<T,D>::operator+ () const { assert(0); }       \
template<> vect<T,D> vect<T,D>::operator- () const { assert(0); }       \
template<> vect<T,D> vect<T,D>::operator~ () const { assert(0); }       \
template class vect<T,D>;                                               \
template size_t memoryof (const vect<T,D>&);                            \
template istream& operator>> (istream& is, vect<T,D>&);                 \
template ostream& operator<< (ostream& os, const vect<T,D>&);

typedef bboxset<int,dim> T1;
typedef vect<bboxset<int,dim>,2> T2;

DEFINE_FAKE_VECT_OPERATIONS(T1,dim)
DEFINE_FAKE_VECT_OPERATIONS(T2,dim)

#undef DEFINE_FAKE_VECT_OPERATIONS