File: block_range.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 (239 lines) | stat: -rw-r--r-- 9,224 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
/*
 *  This file is a part of TiledArray.
 *  Copyright (C) 2015  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
 *
 *  block_range.cpp
 *  May 29, 2015
 *
 */

#include <TiledArray/util/eigen.h>
#include <boost/range/combine.hpp>
#ifdef TILEDARRAY_HAS_RANGEV3
#include <range/v3/view/zip.hpp>
#endif

#include "TiledArray/block_range.h"
#include "range_fixture.h"
#include "unit_test_config.h"

struct BlockRangeFixture {
  BlockRangeFixture() {}

  ~BlockRangeFixture() {}

  static const Range r0;
  static const Range r;
};  // BlockRangeFixture

const Range BlockRangeFixture::r0(std::array<int, 3>{{5, 11, 8}});
const Range BlockRangeFixture::r(std::array<int, 3>{{0, 1, 2}},
                                 std::array<int, 3>{{5, 11, 8}});

BOOST_FIXTURE_TEST_SUITE(block_range_suite, BlockRangeFixture)

BOOST_AUTO_TEST_CASE(block_zero_lower_bound) {
  BlockRange block_range;

  for (auto lower_it = r0.begin(); lower_it != r0.end(); ++lower_it) {
    const auto lower = *lower_it;
    for (auto upper_it = r0.begin(); upper_it != r0.end(); ++upper_it) {
      auto upper = *upper_it;
      for (unsigned int i = 0u; i < upper.size(); ++i) ++(upper[i]);

      if (std::equal(lower.begin(), lower.end(), upper.begin(),
                     [](std::size_t l, std::size_t r0) { return l < r0; })) {
        // Check that the sub-block is constructed without exceptions
        BOOST_CHECK_NO_THROW(block_range = BlockRange(r0, lower, upper));

        // Check that the data of the block range is correct
        std::size_t volume = 1ul;
        for (unsigned int i = 0u; i < r0.rank(); ++i) {
          // Check that the range data is correct
          BOOST_CHECK_EQUAL(block_range.lobound(i), lower[i]);
          BOOST_CHECK_EQUAL(block_range.upbound(i), upper[i]);
          BOOST_CHECK_EQUAL(block_range.extent(i), upper[i] - lower[i]);
          BOOST_CHECK_EQUAL(block_range.stride(i), r0.stride(i));
          volume *= upper[i] - lower[i];
        }
        // Check for the correct volume
        BOOST_CHECK_EQUAL(block_range.volume(), volume);

        // Check that the subrange ordinal calculation returns the same offset
        // as the original range.
        Range::size_type index = 0ul;
        for (auto it = block_range.begin(); it != block_range.end();
             ++it, ++index) {
          // Check that the ordinal offset returned for an ordianl offset and a
          // coordinate index agree.
          BOOST_CHECK_EQUAL(block_range.ordinal(*it), r0.ordinal(*it));

          // Check that the ordinal function returns the correct offset in the
          // parent index space.
          BOOST_CHECK_EQUAL(block_range.ordinal(index), r0.ordinal(*it));

          // Check that the index returned by idx is correct
          BOOST_CHECK_EQUAL(block_range.idx(index), *it);
        }
      }
#ifdef TA_EXCEPTION_ERROR
      else {
        // Check for exception with invalid input
        BOOST_CHECK_THROW(BlockRange(r0, lower, upper), TiledArray::Exception);
      }
#endif  // TA_EXCEPTION_ERROR
    }
  }
}

BOOST_AUTO_TEST_CASE(block) {
  BlockRange block_range;

  for (auto lower_it = r.begin(); lower_it != r.end(); ++lower_it) {
    const auto lower = *lower_it;
    for (auto upper_it = r.begin(); upper_it != r.end(); ++upper_it) {
      auto upper = *upper_it;
      for (unsigned int i = 0u; i < r.rank(); ++i) ++(upper[i]);

      if (std::equal(lower.begin(), lower.end(), upper.begin(),
                     [](std::size_t l, std::size_t r) { return l < r; })) {
        // Check that the sub-block is constructed without exceptions
        BOOST_CHECK_NO_THROW(block_range = BlockRange(r, lower, upper));

        // Check that the data of the block range is correct
        std::size_t volume = 1ul;
        for (unsigned int i = 0u; i < r.rank(); ++i) {
          // Check that the range data is correct
          BOOST_CHECK_EQUAL(block_range.lobound(i), lower[i]);
          BOOST_CHECK_EQUAL(block_range.upbound(i), upper[i]);
          BOOST_CHECK_EQUAL(block_range.extent(i), upper[i] - lower[i]);
          BOOST_CHECK_EQUAL(block_range.stride(i), r.stride(i));
          volume *= upper[i] - lower[i];
        }
        // Check for the correct volume
        BOOST_CHECK_EQUAL(block_range.volume(), volume);

        // Check that the subrange ordinal calculation returns the same
        // offset as the original range.
        Range::size_type index = 0ul;
        for (auto it = block_range.begin(); it != block_range.end();
             ++it, ++index) {
          // Check that the ordinal offset returned for an ordianl offset and a
          // coordinate index agree.
          BOOST_CHECK_EQUAL(block_range.ordinal(*it), r.ordinal(*it));

          // Check that the ordinal function returns the correct offset in the
          // parent index space.
          BOOST_CHECK_EQUAL(block_range.ordinal(index), r.ordinal(*it));
          // Check that the index returned by idx is correct
          BOOST_CHECK_EQUAL(block_range.idx(index), *it);
        }

        // check that can also construct using sequence of bound pairs
        std::vector<std::pair<std::size_t, std::size_t>> bounds;
        bounds.reserve(r.rank());
        for (unsigned int i = 0u; i < r.rank(); ++i) {
          bounds.emplace_back(lower[i], upper[i]);
        }
        BlockRange br2;
        BOOST_CHECK_NO_THROW(br2 = BlockRange(r, bounds));
        BOOST_CHECK_EQUAL(br2, block_range);

        // test the rest of ctors
        {
          Range r(10, 10, 10);
          std::vector<size_t> lobounds = {0, 1, 2};
          std::vector<size_t> upbounds = {4, 6, 8};
          BlockRange bref(r, lobounds, upbounds);

          // using vector of pairs
          std::vector<std::pair<size_t, size_t>> vpbounds{
              {0, 4}, {1, 6}, {2, 8}};
          BOOST_CHECK_NO_THROW(BlockRange br0(r, vpbounds));
          BlockRange br0(r, vpbounds);
          BOOST_CHECK_EQUAL(br0, bref);

          // using initializer_list of pairs
          BOOST_CHECK_NO_THROW(BlockRange br0a(
              r, {std::make_pair(0, 4), std::pair{1, 6}, std::pair(2, 8)}));
          BlockRange br0a(
              r, {std::make_pair(0, 4), std::pair{1, 6}, std::pair(2, 8)});
          BOOST_CHECK_EQUAL(br0a, bref);

          // using vector of tuples
          std::vector<std::tuple<size_t, size_t>> vtbounds{
              {0, 4}, {1, 6}, {2, 8}};
          BOOST_CHECK_NO_THROW(BlockRange br1(r, vtbounds));
          BlockRange br1(r, vpbounds);
          BOOST_CHECK_EQUAL(br1, bref);

          // using initializer_list of tuples
          BOOST_CHECK_NO_THROW(BlockRange br1a(
              r, {std::make_tuple(0, 4), std::tuple{1, 6}, std::tuple(2, 8)}));
          BlockRange br1a(
              r, {std::make_tuple(0, 4), std::tuple{1, 6}, std::tuple(2, 8)});
          BOOST_CHECK_EQUAL(br1a, bref);

          // using zipped ranges of bounds (using Boost.Range)
          // need to #include <boost/range/combine.hpp>
          BOOST_CHECK_NO_THROW(
              BlockRange br2(r, boost::combine(lobounds, upbounds)));
          BlockRange br2(r, boost::combine(lobounds, upbounds));
          BOOST_CHECK_EQUAL(br2, bref);

#ifdef TILEDARRAY_HAS_RANGEV3
          // using zipped ranges of bounds (using Ranges-V3)
          // need to #include <range/v3/view/zip.hpp>
          BOOST_CHECK_NO_THROW(
              BlockRange br3(r, ranges::views::zip(lobounds, upbounds)));
          BlockRange br3(r, ranges::views::zip(lobounds, upbounds));
          BOOST_CHECK_EQUAL(br3, bref);
#endif

          // using nested initializer_list
          BOOST_CHECK_NO_THROW(BlockRange br4(r, {{0, 4}, {1, 6}, {2, 8}}));
          BlockRange br4(r, {{0, 4}, {1, 6}, {2, 8}});
          BOOST_CHECK_EQUAL(br4, bref);

          // using Eigen
          {
            using TiledArray::eigen::iv;

            BOOST_CHECK_NO_THROW(BlockRange br5(r, iv(0, 1, 2), iv(4, 6, 8)));
            BlockRange br5(r, iv(0, 1, 2), iv(4, 6, 8));
            BOOST_CHECK_EQUAL(br5, bref);

            BOOST_CHECK_NO_THROW(
                BlockRange br6(r, boost::combine(iv(0, 1, 2), iv(4, 6, 8))));
            BlockRange br6(r, boost::combine(iv(0, 1, 2), iv(4, 6, 8)));
            BOOST_CHECK_EQUAL(br6, bref);
          }
        }
      }
#ifdef TA_EXCEPTION_ERROR
      else {
        // Check for exception with invalid input
        BOOST_CHECK_THROW(BlockRange(r, lower, upper), TiledArray::Exception);
      }
#endif  // TA_EXCEPTION_ERROR
    }
  }
}

BOOST_AUTO_TEST_SUITE_END()