File: tensor_test.cc

package info (click to toggle)
btas 1.0.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,132 kB
  • sloc: cpp: 26,486; ansic: 1,545; makefile: 5
file content (347 lines) | stat: -rw-r--r-- 10,871 bytes parent folder | download | duplicates (2)
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 "btas/tensor.h"
#include <btas/btas.h>
#include <btas/tarray.h>
#include "btas/tarray.h"
#include "btas/tensorview.h"
#include "btas/varray/allocators.h"
#include "test.h"

#include <ctime>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <set>

#ifdef BTAS_HAS_BOOST_SERIALIZATION
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/complex.hpp>
#endif  // BTAS_HAS_BOOST_SERIALIZATION

using std::cout;
using std::endl;

using btas::Range;
using btas::Tensor;

using DTensor = Tensor<double>;
using ZTensor = Tensor<std::complex<double>>;
template <typename T, typename A = std::allocator<T>> using ValTensor = Tensor<T, btas::DEFAULT::range, btas::varray<T, A>>;
using DValTensor = ValTensor<double>;
using ZValTensor = ValTensor<std::complex<double>>;
using namespace btas;
using namespace std;

double static rng() {
  static boost::random::mt19937 rng(random_seed_accessor());
  static auto dist = boost::random::uniform_real_distribution<double>{0., 1.};
  return dist(rng);
}

static std::ostream& operator<<(std::ostream& s, const DTensor& X) {
  for (auto i : X.range()) s << i << " " << X(i) << "\n";
  return s;
}

// Set the elements of a Tensor T such that
// T(i,j,k) = 1ijk
// assuming individual dimensions are all less than 10.
// (The 1 in front is just a placeholder.)
void static fillEls(DTensor& T) {
  if (T.rank() == 0) return;
  const double base = pow(10., T.rank());
  const size_t max_ii = T.rank() - 1;
  for (auto I : T.range()) {
    double& val = T(I);
    val = base;
    for (size_t ii = 0; ii <= max_ii; ++ii) {
      val += I[ii] * pow(10., max_ii - ii);
    }
  }
}

template <typename T>
T randomReal() {
  static boost::random::mt19937 rng(random_seed_accessor());
  static auto dist = boost::random::uniform_real_distribution<T>{0., 1.};
  return dist(rng);
}

TEST_CASE("Tensor Constructors") {
  SECTION("Default Constructor") {
    Tensor<double> T0;
    CHECK(T0.size() == 0);
    CHECK(T0.empty());
    Tensor<int> T1;
    CHECK(T1.size() == 0);
    CHECK(T1.empty());
    Tensor<bool> T2;
    CHECK(T2.size() == 0);
    CHECK(T2.empty());
    Tensor<std::complex<double>> T3;
    CHECK(T3.size() == 0);
    CHECK(T3.empty());
  }

  SECTION("Extent Constructor") {
    DTensor T0(2, 3, 4);
    CHECK(T0.rank() == 3);
    CHECK(T0.extent(0) == 2);
    CHECK(T0.extent(1) == 3);
    CHECK(T0.extent(2) == 4);
    CHECK(T0.size() == 2 * 3 * 4);
    CHECK(!T0.empty());

    DTensor T1(6, 1, 2, 7, 9);
    CHECK(T1.rank() == 5);
    CHECK(T1.extent(0) == 6);
    CHECK(T1.extent(1) == 1);
    CHECK(T1.extent(2) == 2);
    CHECK(T1.extent(3) == 7);
    CHECK(T1.extent(4) == 9);
    CHECK(T1.size() == 6 * 1 * 2 * 7 * 9);
    CHECK(!T1.empty());

    Tensor<int> Ti(2, 4, 5);
    CHECK(Ti.rank() == 3);
    CHECK(Ti.extent(0) == 2);
    CHECK(Ti.extent(1) == 4);
    CHECK(Ti.extent(2) == 5);
    CHECK(Ti.size() == 2 * 4 * 5);

    Tensor<std::complex<double>> Tc(3, 2, 9);
    CHECK(Tc.rank() == 3);
    CHECK(Tc.extent(0) == 3);
    CHECK(Tc.extent(1) == 2);
    CHECK(Tc.extent(2) == 9);
    CHECK(Tc.size() == 3 * 2 * 9);
  }

  SECTION("Range Constructor") {
    Range r0(2, 5, 3);
    DTensor T0(r0);
    CHECK(T0.rank() == 3);
    CHECK(T0.extent(0) == 2);
    CHECK(T0.extent(1) == 5);
    CHECK(T0.extent(2) == 3);

    Range r1(2, 5, 3, 9, 18, 6);
    DTensor T1(r1);
    CHECK(T1.rank() == 6);

    // range + value
    CHECK_NOTHROW(DTensor(r1, 1.2));

    // range + vector of values
    CHECK_NOTHROW(DTensor(r1, T1.data()));
  }

  SECTION("Fixed Rank Tensor") {
    TArray<double, 3> T0(2, 4, 3);
    CHECK(T0.rank() == 3);
    CHECK(T0.size() == 24);
    CHECK(T0.extent(0) == 2);
    CHECK(T0.extent(1) == 4);
    CHECK(T0.extent(2) == 3);

    Range r1(2, 5, 3);
    TArray<double, 3> T1(r1);

    typedef TArray<double, 3>::range_type Range3;  // rank-3 Range
    Range3 r2(2, 5, 3);
    TArray<double, 3> T2(r2);

    CHECK(T1 == T2);

    typedef TArray<double, 4>::range_type Range4;  // rank-3 Range
    Range4 r3(2, 5, 3, 4);
    // TArray<double,3> T3(r3); // error: mismatched rank
  }
}

TEST_CASE("Custom Tensor") {
  SECTION("Storage") {
    {
      typedef Tensor<double, btas::DEFAULT::range, std::vector<double>> Tensor;
      static_assert(std::is_same_v<DTensor::rebind_storage_t<std::vector<double>>, Tensor>);
      Tensor T0;
      Tensor T1(2, 3, 4);
    }
    {
      typedef Tensor<double, btas::DEFAULT::range, btas::varray<double>> Tensor;
      static_assert(std::is_same_v<DTensor::rebind_storage_t<btas::varray<double>>, Tensor>);
      Tensor T0;
      Tensor T1(2, 3, 4);
    }
    {
      typedef Tensor<double, btas::DEFAULT::range, std::array<double, 24>>
          Tensor;
      static_assert(std::is_same_v<DTensor::rebind_storage_t<std::array<double, 24>>, Tensor>);
      Tensor T0;
      Tensor T1(2, 3, 4);
    }
    {
      typedef Tensor<double, btas::DEFAULT::range, std::valarray<double>>
          Tensor;
      static_assert(std::is_same_v<DTensor::rebind_storage_t<std::valarray<double>>, Tensor>);
      Tensor T0;
      Tensor T1(2, 3, 4);
    }
  }
}

TEST_CASE("Tensor Operations") {
  DTensor T0(1);
  fillEls(T0);

  DTensor T2(3, 2);
  fillEls(T2);

  DTensor T3(3, 2, 4);
  fillEls(T3);

  SECTION("Fill") {
    T3.fill(1.);
    for (auto x : T3) CHECK(x == 1);
  }

  SECTION("Element Access") {
    CHECK(T3(2,1,3) == 1213);
    CHECK(T3({2,1,3}) == 1213);
    CHECK(T3.at(2,1,3) == 1213);
    CHECK(T3.at(std::array{2,1,3}) == 1213);
    CHECK(T3[2 * T3.range().stride_data()[0] + 1*T3.range().stride_data()[1] + 3 * T3.range().stride_data()[2]] == 1213);
    CHECK(T3.at_ordinal(2 * T3.range().stride_data()[0] + 1*T3.range().stride_data()[1] + 3 * T3.range().stride_data()[2]) == 1213);
  }

  SECTION("Conversion to Scalar") {
    CHECK_THROWS_AS( static_cast<double>(T2), btas::exception);
    CHECK_NOTHROW( static_cast<double>(T0) == 11);
  }

  SECTION("Generate") {
    std::vector<double> data(T3.size());
    for (auto& x : data) x = rng();

    size_t count = 0;
    T3.generate([&]() { return data[count++]; });

    auto it = T3.cbegin();
    size_t j = 0;
    for (; it != T3.cend(); ++j, ++it) {
      CHECK(*it == data[j]);
    }
  }

  SECTION("Tensor of Tensor") {
    Tensor<Tensor<double>> A(4, 3);
    Tensor<double> aval(2, 3);
    aval.generate([]() { return randomReal<double>(); });
    A.fill(aval);
    Tensor<Tensor<double>> B(3, 2);
    Tensor<double> bval(3, 4);
    bval.generate([]() { return randomReal<double>(); });
    B.fill(bval);
    Tensor<Tensor<double>> C(4, 2);
    C.fill(Tensor<double>(
        2, 4));  // rank info is required to determine contraction ranks at gemm
    btas::gemm(blas::Op::NoTrans, blas::Op::NoTrans, 1.0, A, B, 1.0, C);
    Tensor<Tensor<double>> Ctest(4, 2);
    Ctest.fill(Tensor<double>(2, 4));
    for (size_t i0 = 0; i0 < A.extent(0); i0++)
      for (size_t i1 = 0; i1 < A.extent(1); i1++)
        for (size_t i2 = 0; i2 < B.extent(1); i2++)
          btas::gemm(blas::Op::NoTrans, blas::Op::NoTrans, 1.0, A(i0, i1), B(i1, i2), 1.0,
                     Ctest(i0, i2));

    const auto eps_double = 1.e4 * std::numeric_limits<double>::epsilon();
    Ctest -= C;
    CHECK(dot(Ctest, Ctest) < eps_double);

    Tensor<Tensor<double>> Ctest1(4, 2);
    Ctest1.fill(Tensor<double>(2, 4));
    for (size_t i0 = 0; i0 < A.extent(0); i0++)
      for (size_t i1 = 0; i1 < A.extent(1); i1++)
        for (size_t i2 = 0; i2 < B.extent(1); i2++)
          for (size_t j0 = 0; j0 < A(i0, i1).extent(0); j0++)
            for (size_t j1 = 0; j1 < A(i0, i1).extent(1); j1++)
              for (size_t j2 = 0; j2 < B(i1, i2).extent(1); j2++)
                Ctest1(i0, i2)(j0, j2) += A(i0, i1)(j0, j1) * B(i1, i2)(j1, j2);
    Ctest1 -= C;
    CHECK(dot(Ctest1, Ctest1) < eps_double);
  }

  SECTION("Rebind") {
    using TD = DTensor;
    using TZ = ZTensor;
    static_assert(std::is_same_v<TD::rebind_t<std::complex<double>>, TZ>);
    static_assert(std::is_same_v<TD::rebind_numeric_t<std::complex<double>>, TZ>);
    using TTD = Tensor<TD>;
    using TTZ = Tensor<TZ>;
    static_assert(std::is_same_v<TTD::rebind_t<std::complex<double>>, TZ>);
    static_assert(std::is_same_v<TTD::rebind_numeric_t<std::complex<double>>, TTZ>);

    // now with custom storage
    static_assert(std::is_same_v<DValTensor::rebind_t<std::complex<double>>, ZValTensor>);
    static_assert(std::is_same_v<DValTensor::rebind_numeric_t<std::complex<double>>, ZValTensor>);
    static_assert(std::is_same_v<ValTensor<DValTensor>::rebind_t<std::complex<double>>, ZValTensor>);
    static_assert(std::is_same_v<ValTensor<DValTensor>::rebind_numeric_t<std::complex<double>>, ValTensor<ZValTensor>>);

    // now with custom storage and allocator
    {
      using TD = ValTensor<double, btas::stack_allocator<double>>;
      using TZ = ValTensor<std::complex<double>, btas::stack_allocator<std::complex<double>>>;
      static_assert(std::is_same_v<TD::rebind_t<std::complex<double>>, TZ>);
      static_assert(std::is_same_v<TD::rebind_numeric_t<std::complex<double>>, TZ>);
      using TTD = Tensor<TD>;
      using TTZ = Tensor<TZ>;
      static_assert(std::is_same_v<TTD::rebind_t<std::complex<double>>, Tensor<std::complex<double>>>);  // N.B. outer Tensor != ValTensor
      static_assert(std::is_same_v<TTD::rebind_numeric_t<std::complex<double>>, TTZ>);
    }
  }

#ifdef BTAS_HAS_BOOST_SERIALIZATION
  SECTION("Serialization") {
    const auto archive_fname = "tensor_operations.serialization.archive";

    Tensor<std::array<complex<double>, 3>> T1(2, 3, 4);
    T1.fill({{{1.0, 2.0}, {2.0, 1.0}, {2.0, 3.0}}});
    // Tensor<double,3> t({-1,0,1},{2,4,3});
    TArray<double, 3> t(2, 4, 3);
    t.generate([]() { return randomReal<double>(); });
    Tensor<Tensor<double>> A(4, 4);
    Tensor<double> aval(2, 2);
    aval.fill(1.0);
    A.fill(aval);
    // write
    {
      std::ofstream os(archive_fname);
      assert(os.good());
      boost::archive::xml_oarchive ar(os);
      CHECK_NOTHROW(ar << BOOST_SERIALIZATION_NVP(t));   // fixed-size Tensor
      CHECK_NOTHROW(ar << BOOST_SERIALIZATION_NVP(A));   // Tensor of Tensor
      CHECK_NOTHROW(ar << BOOST_SERIALIZATION_NVP(T1));  // Tensor of complex datatypes
    }
    // read
    {
      std::ifstream is(archive_fname);
      assert(is.good());
      boost::archive::xml_iarchive ar(is);

      TArray<double, 3> tcopy;
      CHECK_NOTHROW(ar >> BOOST_SERIALIZATION_NVP(tcopy));
      CHECK(t == tcopy);

      Tensor<Tensor<double>> Acopy;
      CHECK_NOTHROW(ar >> BOOST_SERIALIZATION_NVP(Acopy));
      CHECK(A == Acopy);

      Tensor<std::array<complex<double>, 3>> T1copy;
      CHECK_NOTHROW(ar >> BOOST_SERIALIZATION_NVP(T1copy));
      CHECK(T1 == T1copy);
    }
    std::remove(archive_fname);
  }
#endif  // BTAS_HAS_BOOST_SERIALIZATION

}