File: btas.cpp

package info (click to toggle)
tiledarray 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,568 kB
  • sloc: cpp: 53,449; javascript: 1,599; sh: 393; ansic: 226; python: 223; xml: 195; makefile: 36
file content (382 lines) | stat: -rw-r--r-- 12,211 bytes parent folder | download
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
/*
 *  This file is a part of TiledArray.
 *  Copyright (C) 2018  Virginia Tech
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  btas.cpp
 *  January 18, 2018
 *
 */

#include <TiledArray/config.h>

#ifdef TILEDARRAY_HAS_BTAS

#include <TiledArray/conversions/btas.h>
#include <TiledArray/external/btas.h>
#include "range_fixture.h"
#include "tiledarray.h"
#include "unit_test_config.h"

using namespace TiledArray;

// test both bare (deep-copy) BTAS tensor as well as its shallow-copy wrap in
// Tile<>, using both btas::RangeNd<> and TiledArray::Range as the range type
typedef boost::mpl::list<
    DistArray<
        Tile<btas::Tensor<double, TiledArray::Range, btas::varray<double>>>,
        DensePolicy>,
    DistArray<btas::Tensor<double, TiledArray::Range, btas::varray<double>>,
              DensePolicy>
    // DistArray<Tile<btas::Tensor<double>> , DensePolicy>, DistArray<
    // btas::Tensor<double>                                          ,
    // DensePolicy>
    >
    array_types;

typedef boost::mpl::list<
    btas::Tensor<double, TiledArray::Range, btas::varray<double>>
    // btas::Tensor<double>
    >
    tensor_types;

struct BTASFixture : public TiledRangeFixture {
  BTASFixture()
      : a0(*GlobalFixture::world, trange2),
        b0(*GlobalFixture::world, trange3),
        c0(*GlobalFixture::world, trange4),
        a1(*GlobalFixture::world, trange2),
        b1(*GlobalFixture::world, trange3),
        c1(*GlobalFixture::world, trange4),
        a2(*GlobalFixture::world, trange2),
        b2(*GlobalFixture::world, trange3),
        c2(*GlobalFixture::world, trange4),
        a3(*GlobalFixture::world, trange2),
        b3(*GlobalFixture::world, trange3),
        c3(*GlobalFixture::world, trange4) {
    random_fill(a0);
    random_fill(b0);
    random_fill(c0);
    random_fill(a1);
    random_fill(b1);
    random_fill(c1);
    random_fill(a2);
    random_fill(b2);
    random_fill(c2);
    random_fill(a3);
    random_fill(b3);
    random_fill(c3);
    GlobalFixture::world->gop.fence();
  }

  template <typename Tile>
  static void random_fill(DistArray<Tile>& array) {
    using Range = typename DistArray<Tile>::range_type;
    typename DistArray<Tile>::pmap_interface::const_iterator it =
        array.pmap()->begin();
    typename DistArray<Tile>::pmap_interface::const_iterator end =
        array.pmap()->end();
    for (; it != end; ++it) {
      array.set(
          *it, array.world().taskq.add(BTASFixture::make_rand_tile<Tile, Range>,
                                       array.trange().make_tile_range(*it)));
    }
  }

  template <typename T>
  static void set_random(T& t) {
    // with 50% generate nonzero integer value in [0,101)
    auto rand_int = GlobalFixture::world->rand();
    t = (rand_int < 0x8fffff) ? rand_int % 101 : 0;
  }

  template <typename T>
  static void set_random(std::complex<T>& t) {
    // with 50% generate nonzero value
    auto rand_int1 = GlobalFixture::world->rand();
    if (rand_int1 < 0x8ffffful) {
      t = std::complex<T>{T(rand_int1 % 101),
                          T(GlobalFixture::world->rand() % 101)};
    } else
      t = std::complex<T>{0, 0};
  }

  // Fill a tile with random data
  template <typename Tile, typename Range>
  static Tile make_rand_tile(const Range& r) {
    Tile tile(r);
    for (auto& v : tile) {
      set_random(v);
    }
    return tile;
  }

  ~BTASFixture() { GlobalFixture::world->gop.fence(); }

  const static TiledRange trange1;
  const static TiledRange trange2;
  const static TiledRange trange3;
  const static TiledRange trange4;

  using TArrayDSB = DistArray<
      Tile<btas::Tensor<double, TiledArray::Range, btas::varray<double>>>,
      DensePolicy>;
  TArrayDSB a0, b0, c0;
  using TArrayDB =
      DistArray<btas::Tensor<double, TiledArray::Range, btas::varray<double>>,
                DensePolicy>;
  TArrayDB a1, b1, c1;
  using TArrayDSB0 = DistArray<Tile<btas::Tensor<double>>, DensePolicy>;
  TArrayDSB0 a2, b2, c2;
  using TArrayDB0 = DistArray<btas::Tensor<double>, DensePolicy>;
  TArrayDB0 a3, b3, c3;

  template <typename Array>
  Array& array(size_t idx);

};  // BTASFixture

// Instantiate static variables for fixture
const TiledRange BTASFixture::trange1{{0, 2, 5, 10, 17, 28, 41}};
const TiledRange BTASFixture::trange2{{0, 2, 5, 10, 17, 28, 41},
                                      {0, 3, 6, 11, 18, 29, 42}};
const TiledRange BTASFixture::trange3{{0, 2, 5, 10, 17, 28, 41},
                                      {0, 3, 6, 11, 18, 29, 42},
                                      {0, 4, 5, 12, 17, 30, 41}};
const TiledRange BTASFixture::trange4{trange2.data()[0], trange2.data()[1],
                                      trange2.data()[0], trange2.data()[1]};

template <>
BTASFixture::TArrayDSB& BTASFixture::array<BTASFixture::TArrayDSB>(size_t idx) {
  if (idx == 0)
    return a0;
  else if (idx == 1)
    return b0;
  else if (idx == 2)
    return c0;
  else
    throw std::range_error("idx out of range");
}

template <>
BTASFixture::TArrayDB& BTASFixture::array<BTASFixture::TArrayDB>(size_t idx) {
  if (idx == 0)
    return a1;
  else if (idx == 1)
    return b1;
  else if (idx == 2)
    return c1;
  else
    throw std::range_error("idx out of range");
}

template <>
BTASFixture::TArrayDSB0& BTASFixture::array<BTASFixture::TArrayDSB0>(
    size_t idx) {
  if (idx == 0)
    return a2;
  else if (idx == 1)
    return b2;
  else if (idx == 2)
    return c2;
  else
    throw std::range_error("idx out of range");
}

template <>
BTASFixture::TArrayDB0& BTASFixture::array<BTASFixture::TArrayDB0>(size_t idx) {
  if (idx == 0)
    return a3;
  else if (idx == 1)
    return b3;
  else if (idx == 2)
    return c3;
  else
    throw std::range_error("idx out of range");
}

BOOST_FIXTURE_TEST_SUITE(btas_suite, BTASFixture)

BOOST_AUTO_TEST_CASE_TEMPLATE(copy, Array, array_types) {
  const auto& a = array<Array>(0);
  TArrayD b;
  BOOST_REQUIRE_NO_THROW(b("i,j") = a("i,j"));
}

BOOST_AUTO_TEST_CASE_TEMPLATE(contract, Array, array_types) {
  // contract 2 tensors
  const auto& a = array<Array>(0);
  const auto& b = array<Array>(1);
  Array c;
  c("j,k,l") = a("i,j") * b("i,k,l");

  // copy result to standard tensor, to be able to compare with the reference
  TArrayD c_copy(c);

  // compute the reference result using standard tensor
  TArrayD a_copy(a);
  TArrayD b_copy(b);
  TArrayD c_ref;
  c_ref("j,k,l") = a_copy("i,j") * b_copy("i,k,l");

  BOOST_CHECK(
      std::sqrt((c_copy("i,j,k") - c_ref("i,j,k")).squared_norm().get()) <
      1e-10);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(from_btas_subtensor, bTensor, tensor_types) {
  using range_type = typename bTensor::range_type;
  bTensor src = make_rand_tile<bTensor>(range_type({4, 5}));

  Tensor<double> dst(TiledArray::Range({1, 1}, {3, 4}));
  BOOST_REQUIRE_NO_THROW(btas_subtensor_to_tensor(src, dst));

  //  btas_subtensor_to_tensor(src, dst);

  for (const auto& i : dst.range()) {
    BOOST_CHECK_EQUAL(src(i), dst(i));
  }
}

BOOST_AUTO_TEST_CASE_TEMPLATE(to_btas_subtensor, bTensor, tensor_types) {
  Tensor<double> src =
      make_rand_tile<Tensor<double>>(TiledArray::Range({1, 1}, {3, 3}));

  using range_type = typename bTensor::range_type;
  bTensor dst(range_type({4, 5}), 0.0);

  BOOST_REQUIRE_NO_THROW(tensor_to_btas_subtensor(src, dst));

  for (const auto& i : src.range()) {
    BOOST_CHECK_EQUAL(src(i), dst(i));
  }
}

BOOST_AUTO_TEST_CASE_TEMPLATE(dense_array_conversion, bTensor, tensor_types) {
  // make random btas::Tensor on World rank 0, and replicate
  const auto root = 0;
  bTensor src;
  if (GlobalFixture::world->rank() == root)
    src = make_rand_tile<bTensor>(typename bTensor::range_type({20, 22, 24}));
  if (GlobalFixture::world->size() != 0)
    GlobalFixture::world->gop.broadcast_serializable(src, root);

  // make tiled range
  using trange1_t = TiledArray::TiledRange1;
  TiledArray::TiledRange trange(
      {trange1_t(0, 10, 20), trange1_t(0, 11, 22), trange1_t(0, 12, 24)});

  // convert to a replicated DistArray
  using T = typename bTensor::value_type;
  using TArray = TiledArray::TArray<T>;
  TArray dst;
  const auto replicated = true;
#if !defined(TA_USER_ASSERT_DISABLED)
  if (GlobalFixture::world->size() > 1)
    BOOST_REQUIRE_THROW(dst = btas_tensor_to_array<TArray>(
                            *GlobalFixture::world, trange, src, not replicated),
                        TiledArray::Exception);
#endif
  BOOST_REQUIRE_NO_THROW(dst = btas_tensor_to_array<TArray>(
                             *GlobalFixture::world, trange, src, replicated));

  // check the array contents
  for (const auto& t : dst) {
    const auto& tile = t.get();
    const auto& tile_range = tile.range();
    auto src_blk_range = TiledArray::BlockRange(
        trange.elements_range(), tile_range.lobound(), tile_range.upbound());
    using std::data;
    auto src_view = TiledArray::make_const_map(data(src), src_blk_range);

    for (const auto& i : tile_range) {
      BOOST_CHECK_EQUAL(src_view(i), tile(i));
    }
  }

  // convert the replicated DistArray back to a btas::Tensor
  btas::Tensor<T> src_copy;
  BOOST_REQUIRE_NO_THROW(src_copy = array_to_btas_tensor(dst));
  for (const auto& i : src.range()) {
    BOOST_CHECK_EQUAL(src(i), src_copy(i));
  }

  // convert the replicated DistArray to a btas::Tensor on rank 0 only
  {
    btas::Tensor<T> src_copy;
    BOOST_REQUIRE_NO_THROW(src_copy = array_to_btas_tensor(dst, 0));
    if (GlobalFixture::world->rank() == 0) {
      for (const auto& i : src.range()) {
        BOOST_CHECK_EQUAL(src(i), src_copy(i));
      }
    } else {
      BOOST_CHECK(src_copy == btas::Tensor<T>{});
    }
  }
}

BOOST_AUTO_TEST_CASE_TEMPLATE(sparse_array_conversion, bTensor, tensor_types) {
  // make random btas::Tensor on World rank 0, and replicate
  const auto root = 0;
  bTensor src;
  if (GlobalFixture::world->rank() == root)
    src = make_rand_tile<bTensor>(typename bTensor::range_type({20, 22, 24}));
  if (GlobalFixture::world->size() != 0)
    GlobalFixture::world->gop.broadcast_serializable(src, root);

  // make tiled range
  using trange1_t = TiledArray::TiledRange1;
  TiledArray::TiledRange trange(
      {trange1_t(0, 10, 20), trange1_t(0, 11, 22), trange1_t(0, 12, 24)});

  // convert to a replicated sparse policy DistArray
  using T = typename bTensor::value_type;
  using TSpArray = TiledArray::TSpArray<T>;
  TSpArray dst;
  const auto replicated = true;
#if !defined(TA_USER_ASSERT_DISABLED)
  if (GlobalFixture::world->size() > 1)
    BOOST_REQUIRE_THROW(dst = btas_tensor_to_array<TSpArray>(
                            *GlobalFixture::world, trange, src, not replicated),
                        TiledArray::Exception);
#endif
  BOOST_REQUIRE_NO_THROW(dst = btas_tensor_to_array<TSpArray>(
                             *GlobalFixture::world, trange, src, replicated));

  // check the array contents
  for (const auto& t : dst) {
    const auto& tile = t.get();
    const auto& tile_range = tile.range();
    auto src_blk_range = TiledArray::BlockRange(
        trange.elements_range(), tile_range.lobound(), tile_range.upbound());
    using std::data;
    auto src_view = TiledArray::make_const_map(data(src), src_blk_range);

    for (const auto& i : tile_range) {
      BOOST_CHECK_EQUAL(src_view(i), tile(i));
    }
  }

  // convert to the replicated DistArray back to a btas::Tensor
  btas::Tensor<T> src_copy;
  BOOST_REQUIRE_NO_THROW(src_copy = array_to_btas_tensor(dst));
  for (const auto& i : src.range()) {
    BOOST_CHECK_EQUAL(src(i), src_copy(i));
  }
}

BOOST_AUTO_TEST_SUITE_END()

#endif  // TILEDARRAY_HAS_BLAS