File: array_impl.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 (241 lines) | stat: -rw-r--r-- 8,740 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
/*
 *  This file is a part of TiledArray.
 *  Copyright (C) 2014  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/>.

 *  Justus Calvin
 *  Department of Chemistry, Virginia Tech
 *
 *  array_impl.cpp
 *  Oct 24, 2014
 *
 */

#include "TiledArray/array_impl.h"
#include "sparse_shape_fixture.h"
#include "tiledarray.h"
#include "unit_test_config.h"

using namespace TiledArray;

struct ArrayImplBaseFixture : public SparseShapeFixture {
  typedef Tensor<int> value_type;
  typedef detail::ArrayImpl<value_type, DensePolicy> array_impl_base;
  ArrayImplBaseFixture()
      : pmap(new detail::HashPmap(*GlobalFixture::world,
                                  tr.tiles_range().volume())) {}

  std::shared_ptr<array_impl_base::pmap_interface> pmap;
};  // struct TensorImplBaseFixture

struct ArrayImplFixture : public ArrayImplBaseFixture {
  typedef TiledRange trange_type;
  typedef Tensor<int> value_type;
  typedef detail::ArrayImpl<value_type, DensePolicy> array_impl_base;
  typedef detail::ArrayImpl<value_type, SparsePolicy> sp_array_impl_base;
  typedef array_impl_base::shape_type dense_shape_type;
  typedef sp_array_impl_base::shape_type sparse_shape_type;

  ArrayImplFixture()
      : impl(*GlobalFixture::world, tr, dense_shape_type(), pmap),
        sp_impl(*GlobalFixture::world, tr, make_shape(tr, 0.5, 42), pmap) {}

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

  array_impl_base impl;
  sp_array_impl_base sp_impl;
};  // struct TensorImplFixture

BOOST_FIXTURE_TEST_SUITE(array_impl_suite, ArrayImplFixture)

BOOST_AUTO_TEST_CASE(constructor_dense_policy) {
  BOOST_REQUIRE_NO_THROW(
      array_impl_base(*GlobalFixture::world, tr, dense_shape_type(), pmap));
  array_impl_base x(*GlobalFixture::world, tr, dense_shape_type(), pmap);

  // Check that the initial conditions are correct after constructution.
  BOOST_CHECK_EQUAL(&x.world(), GlobalFixture::world);
  BOOST_CHECK(x.pmap() == pmap);
  BOOST_CHECK_EQUAL(x.tiles_range(), tr.tiles_range());
  BOOST_CHECK_EQUAL(x.trange(), tr);
  BOOST_CHECK_EQUAL(x.size(), tr.tiles_range().volume());
  BOOST_CHECK(x.is_dense());
  for (std::size_t i = 0; i < tr.tiles_range().volume(); ++i)
    BOOST_CHECK(!x.is_zero(i));
}

BOOST_AUTO_TEST_CASE(constructor_shape_policy) {
  BOOST_REQUIRE_NO_THROW(sp_array_impl_base(*GlobalFixture::world, tr,
                                            make_shape(tr, 0.5, 23), pmap));
  sp_array_impl_base x(*GlobalFixture::world, tr, make_shape(tr, 0.5, 23),
                       pmap);

  // Check that the initial conditions are correct after constructution.
  BOOST_CHECK_EQUAL(&x.world(), GlobalFixture::world);
  BOOST_CHECK(x.pmap() == pmap);
  BOOST_CHECK_EQUAL(x.tiles_range(), tr.tiles_range());
  BOOST_CHECK_EQUAL(x.trange(), tr);
  BOOST_CHECK_EQUAL(x.size(), tr.tiles_range().volume());
  BOOST_CHECK(!x.is_dense());
  for (std::size_t i = 0; i < tr.tiles_range().volume(); ++i) {
    if (x.shape()[i] < SparseShape<float>::threshold()) {
      BOOST_CHECK(x.is_zero(i));
    } else {
      BOOST_CHECK(!x.is_zero(i));
    }
  }
}

BOOST_AUTO_TEST_CASE(tile_get_and_set_w_value) {
  // Get each tile before it is set
  for (std::size_t i = 0; i < tr.tiles_range().volume(); ++i) {
    if (GlobalFixture::world->rank() == 0) {
      const ProcessID owner = impl.owner(i);

      // Set each tile on node 0
      BOOST_CHECK_NO_THROW(
          impl.set(i, value_type(impl.trange().make_tile_range(i), owner)));

      // Get the tile future (may or may not be remote) and wait for the data to
      // arrive.
      Future<value_type> tile = impl.get(i);
      GlobalFixture::world->gop.fence();

      // Check that the future has been set and the data is what we expect.
      BOOST_CHECK(tile.probe());
      for (std::size_t j = 0ul; j < tile.get().size(); ++j)
        BOOST_CHECK_EQUAL(tile.get()[j], owner);
    } else {
      GlobalFixture::world->gop.fence();
      if (impl.is_local(i)) {
        // Get the local tile
        Future<value_type> tile = impl.get(i);

        // Check that the future has been set and the data is what we expect.
        BOOST_CHECK(tile.probe());
        for (std::size_t j = 0ul; j < tile.get().size(); ++j)
          BOOST_CHECK_EQUAL(tile.get()[j], GlobalFixture::world->rank());
      }
    }
  }
}

BOOST_AUTO_TEST_CASE(tile_get_and_set_w_future) {
  // Get each tile before it is set
  for (std::size_t i = 0; i < tr.tiles_range().volume(); ++i) {
    if (GlobalFixture::world->rank() == 0) {
      const ProcessID owner = impl.owner(i);
      Future<value_type> tile;

      // Set each tile on node 0
      BOOST_CHECK_NO_THROW(impl.set(i, tile));

      // Get the tile future (may or may not be remote) and wait for the data to
      // arrive.
      tile.set(value_type(impl.trange().make_tile_range(i), owner));
      GlobalFixture::world->gop.fence();

      // Check that the future has been set and the data is what we expect.
      BOOST_CHECK(tile.probe());
      for (std::size_t j = 0ul; j < tile.get().size(); ++j)
        BOOST_CHECK_EQUAL(tile.get()[j], owner);
    } else {
      GlobalFixture::world->gop.fence();
      if (impl.is_local(i)) {
        // Get the local tile
        Future<value_type> tile = impl.get(i);

        // Check that the future has been set and the data is what we expect.
        BOOST_CHECK(tile.probe());
        for (std::size_t j = 0ul; j < tile.get().size(); ++j)
          BOOST_CHECK_EQUAL(tile.get()[j], GlobalFixture::world->rank());
      }
    }
  }
}

BOOST_AUTO_TEST_CASE(tile_iterator_w_value) {
  // Set local tiles via iterators
  for (array_impl_base::iterator it = impl.begin(); it != impl.end(); ++it) {
    BOOST_CHECK_NO_THROW(*it = value_type(
                             impl.trange().make_tile_range(it.ordinal()),
                             impl.owner(it.ordinal())));
  }

  GlobalFixture::world->gop.fence();

  // Get each tile before it is set
  for (std::size_t i = 0; i < tr.tiles_range().volume(); ++i) {
    if (GlobalFixture::world->rank() == 0) {
      // Get the tile future (may or may not be remote) and wait for the data to
      // arrive.
      value_type tile = impl.get(i);
      GlobalFixture::world->gop.fence();

      // Check that the future has been set and the data is what we expect.
      for (std::size_t j = 0ul; j < tile.size(); ++j)
        BOOST_CHECK_EQUAL(tile[j], impl.owner(i));
    } else {
      GlobalFixture::world->gop.fence();
      if (impl.is_local(i)) {
        // Get the local tile
        value_type tile = impl.get(i);

        // Check that the future has been set and the data is what we expect.
        for (std::size_t j = 0ul; j < tile.size(); ++j)
          BOOST_CHECK_EQUAL(tile[j], GlobalFixture::world->rank());
      }
    }
  }
}

BOOST_AUTO_TEST_CASE(tile_iterator_w_future) {
  // Set local tiles via iterators
  for (array_impl_base::iterator it = impl.begin(); it != impl.end(); ++it) {
    Future<value_type> tile;
    BOOST_CHECK_NO_THROW(*it = tile);
    tile.set(value_type(impl.trange().make_tile_range(it.ordinal()),
                        GlobalFixture::world->rank()));
  }

  GlobalFixture::world->gop.fence();

  // Get each tile before it is set
  for (std::size_t i = 0; i < tr.tiles_range().volume(); ++i) {
    if (GlobalFixture::world->rank() == 0) {
      // Get the tile, which may be local or remote.
      Future<value_type> tile = impl.get(i);
      GlobalFixture::world->gop.fence();

      // Check that the future has been set and the data is what we expect.
      BOOST_CHECK(tile.probe());
      for (std::size_t j = 0ul; j < tile.get().size(); ++j)
        BOOST_CHECK_EQUAL(tile.get()[j], impl.owner(i));
    } else {
      GlobalFixture::world->gop.fence();
      if (impl.is_local(i)) {
        // Get the local tile
        Future<value_type> tile = impl.get(i);

        // Check that the future has been set and the data is what we expect.
        BOOST_CHECK(tile.probe());
        for (std::size_t j = 0ul; j < tile.get().size(); ++j)
          BOOST_CHECK_EQUAL(tile.get()[j], GlobalFixture::world->rank());
      }
    }
  }
}

BOOST_AUTO_TEST_SUITE_END()