File: eigen.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 (404 lines) | stat: -rw-r--r-- 15,422 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 *  This file is a part of TiledArray.
 *  Copyright (C) 2013  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/>.
 *
 */

#include "TiledArray/conversions/eigen.h"
#include "range_fixture.h"
#include "tiledarray.h"
#include "unit_test_config.h"

using namespace TiledArray;

struct EigenFixture : public TiledRangeFixture {
  EigenFixture()
      : trange(dims.begin(), dims.begin() + 2),
        trange1(dims.begin(), dims.begin() + 1),
        array(*GlobalFixture::world, trange),
        array1(*GlobalFixture::world, trange1),
        matrix(dims[0].elements_range().second,
               dims[1].elements_range().second),
        rmatrix(dims[0].elements_range().second,
                dims[1].elements_range().second),
        vector(dims[0].elements_range().second) {}

  TiledRange trange;
  TiledRange trange1;
  TArrayI array;
  TArrayI array1;
  Eigen::MatrixXi matrix;
  EigenMatrixXi rmatrix;
  Eigen::VectorXi vector;
};

BOOST_FIXTURE_TEST_SUITE(eigen_suite, EigenFixture)

BOOST_AUTO_TEST_CASE(tile_map) {
  // Make a tile with random data
  Tensor<int> tensor(trange.make_tile_range(0));
  const Tensor<int>& ctensor = tensor;
  GlobalFixture::world->srand(27);
  for (Tensor<int>::iterator it = tensor.begin(); it != tensor.end(); ++it)
    *it = GlobalFixture::world->rand();

  Eigen::Map<EigenMatrixXi> map =
      eigen_map(tensor, tensor.range().extent(0), tensor.range().extent(1));

  // Check the map dimensions
  BOOST_CHECK_EQUAL(map.rows(), tensor.range().extent(0));
  BOOST_CHECK_EQUAL(map.cols(), tensor.range().extent(1));

  for (Range::const_iterator it = tensor.range().begin();
       it != tensor.range().end(); ++it) {
    BOOST_CHECK_EQUAL(map((*it)[0], (*it)[1]), tensor[*it]);
  }

  Eigen::Map<const EigenMatrixXi> cmap =
      eigen_map(ctensor, ctensor.range().extent(0), ctensor.range().extent(1));

  // Check the map dimensions
  BOOST_CHECK_EQUAL(cmap.rows(), ctensor.range().extent(0));
  BOOST_CHECK_EQUAL(cmap.cols(), ctensor.range().extent(1));

  for (Range::const_iterator it = tensor.range().begin();
       it != tensor.range().end(); ++it) {
    BOOST_CHECK_EQUAL(cmap((*it)[0], (*it)[1]), ctensor[*it]);
  }
}

BOOST_AUTO_TEST_CASE(auto_tile_map) {
  // Make a tile with random data
  Tensor<int> tensor(trange.make_tile_range(0));
  const Tensor<int>& ctensor = tensor;
  GlobalFixture::world->srand(27);
  for (Tensor<int>::iterator it = tensor.begin(); it != tensor.end(); ++it)
    *it = GlobalFixture::world->rand();

  Eigen::Map<EigenMatrixXi> map = eigen_map(tensor);

  // Check the map dimensions
  BOOST_CHECK_EQUAL(map.rows(), tensor.range().extent(0));
  BOOST_CHECK_EQUAL(map.cols(), tensor.range().extent(1));

  for (Range::const_iterator it = tensor.range().begin();
       it != tensor.range().end(); ++it) {
    BOOST_CHECK_EQUAL(map((*it)[0], (*it)[1]), tensor[*it]);
  }

  Eigen::Map<const EigenMatrixXi> cmap = eigen_map(ctensor);

  // Check the map dimensions
  BOOST_CHECK_EQUAL(cmap.rows(), ctensor.range().extent(0));
  BOOST_CHECK_EQUAL(cmap.cols(), ctensor.range().extent(1));

  for (Range::const_iterator it = tensor.range().begin();
       it != tensor.range().end(); ++it) {
    BOOST_CHECK_EQUAL(cmap((*it)[0], (*it)[1]), ctensor[*it]);
  }
}

BOOST_AUTO_TEST_CASE(submatrix_to_tensor) {
  // Fill the matrix with random data
  matrix = decltype(matrix)::Random(matrix.rows(), matrix.cols());
  // Make a target tensor
  Tensor<int> tensor(trange.make_tile_range(0));

  // Copy the sub matrix to the tensor objects
  BOOST_CHECK_NO_THROW(eigen_submatrix_to_tensor(matrix, tensor));

  // Get the target submatrix block
  auto block =
      matrix.block(tensor.range().lobound(0), tensor.range().lobound(1),
                   tensor.range().extent(0), tensor.range().extent(1));

  // Check that the block contains the same values as the tensor
  for (Range::const_iterator it = tensor.range().begin();
       it != tensor.range().end(); ++it) {
    BOOST_CHECK_EQUAL(tensor[*it], block((*it)[0], (*it)[1]));
  }
}

BOOST_AUTO_TEST_CASE(tensor_to_submatrix) {
  // Fill a tensor with data
  Tensor<int> tensor(trange.make_tile_range(0));
  GlobalFixture::world->srand(27);
  for (Tensor<int>::iterator it = tensor.begin(); it != tensor.end(); ++it)
    *it = GlobalFixture::world->rand();

  // Copy the tensor to the submatrix block
  BOOST_CHECK_NO_THROW(tensor_to_eigen_submatrix(tensor, matrix));

  // Get the source submatrix block
  auto block =
      matrix.block(tensor.range().lobound(0), tensor.range().lobound(1),
                   tensor.range().extent(0), tensor.range().extent(1));

  // Check that the block contains the same values as the tensor
  for (Range::const_iterator it = tensor.range().begin();
       it != tensor.range().end(); ++it) {
    BOOST_CHECK_EQUAL(block((*it)[0], (*it)[1]), tensor[*it]);
  }
}

BOOST_AUTO_TEST_CASE(matrix_to_array) {
  // Fill the matrix with random data
  matrix = decltype(matrix)::Random(matrix.rows(), matrix.cols());

  if (GlobalFixture::world->size() == 1) {
    // Copy matrix to array
    BOOST_CHECK_NO_THROW((array = eigen_to_array<TArrayI>(*GlobalFixture::world,
                                                          trange, matrix)));
  } else {
    // Check that eigen_to_array does not work in distributed environments
#if !defined(TA_USER_ASSERT_DISABLED)
    BOOST_CHECK_THROW(
        (eigen_to_array<TArrayI>(*GlobalFixture::world, trange, matrix)),
        TiledArray::Exception);
#endif

    // Note: The following tests constructs a replicated array, but the data may
    // not be identical. That is OK here since we are check the local data, but
    // in real applications the data should be identical.

    // Copy matrix to a replicated array
    BOOST_CHECK_NO_THROW((array = eigen_to_array<TArrayI>(
                              *GlobalFixture::world, trange, matrix, true)));
  }

  // Check that the data in array is equal to that in matrix
  for (Range::const_iterator it = array.range().begin();
       it != array.range().end(); ++it) {
    Future<TArrayI::value_type> tile = array.find(*it);
    for (Range::const_iterator tile_it = tile.get().range().begin();
         tile_it != tile.get().range().end(); ++tile_it) {
      BOOST_CHECK_EQUAL(tile.get()[*tile_it],
                        matrix((*tile_it)[0], (*tile_it)[1]));
    }
  }
}

BOOST_AUTO_TEST_CASE(vector_to_array) {
  // Fill the vector with random data
  vector = Eigen::VectorXi::Random(vector.size());

  if (GlobalFixture::world->size() == 1) {
    // Convert the vector to an array
    BOOST_CHECK_NO_THROW((array1 = eigen_to_array<TArrayI>(
                              *GlobalFixture::world, trange1, vector)));

  } else {
    // Check that eigen_to_array does not work in distributed environments
#if !defined(TA_USER_ASSERT_DISABLED)
    BOOST_CHECK_THROW(
        (eigen_to_array<TArrayI>(*GlobalFixture::world, trange1, vector)),
        TiledArray::Exception);
#endif

    // Note: The following tests constructs a replicated array, but the data may
    // not be identical. That is OK here since we are check the local data, but
    // in real applications the data should be identical.

    // Convert the vector to an array
    BOOST_CHECK_NO_THROW((array1 = eigen_to_array<TArrayI>(
                              *GlobalFixture::world, trange1, vector, true)));
  }

  // Check that the data in array matches the data in vector
  for (Range::const_iterator it = array1.range().begin();
       it != array1.range().end(); ++it) {
    Future<TArrayI::value_type> tile = array1.find(*it);
    for (Range::const_iterator tile_it = tile.get().range().begin();
         tile_it != tile.get().range().end(); ++tile_it) {
      BOOST_CHECK_EQUAL(tile.get()[*tile_it], vector((*tile_it)[0]));
    }
  }
}

BOOST_AUTO_TEST_CASE(array_to_matrix) {
  auto a_to_e_rowmajor = [](const TArrayI& array) -> EigenMatrixXi {
    return array_to_eigen<Tensor<int>, DensePolicy, Eigen::RowMajor>(array);
  };

  if (GlobalFixture::world->size() == 1) {
    // Fill the array with random data
    GlobalFixture::world->srand(27);
    for (Range::const_iterator it = array.range().begin();
         it != array.range().end(); ++it) {
      TArrayI::value_type tile(array.trange().make_tile_range(*it));
      for (TArrayI::value_type::iterator tile_it = tile.begin();
           tile_it != tile.end(); ++tile_it) {
        *tile_it = GlobalFixture::world->rand();
      }
      array.set(*it, tile);
    }

    // Convert the array to an Eigen matrices: column-major (matrix) and
    // row-major (rmatrix)
    BOOST_CHECK_NO_THROW(matrix = array_to_eigen(array));
    BOOST_CHECK_NO_THROW(rmatrix = a_to_e_rowmajor(array));

    // Check that the matrix dimensions are the same as the array
    BOOST_CHECK_EQUAL(matrix.rows(), array.trange().elements_range().extent(0));
    BOOST_CHECK_EQUAL(matrix.cols(), array.trange().elements_range().extent(1));
    BOOST_CHECK_EQUAL(rmatrix.rows(),
                      array.trange().elements_range().extent(0));
    BOOST_CHECK_EQUAL(rmatrix.cols(),
                      array.trange().elements_range().extent(1));

    // Check that the data in matrix matches the data in array
    for (Range::const_iterator it = array.range().begin();
         it != array.range().end(); ++it) {
      Future<TArrayI::value_type> tile = array.find(*it);
      for (Range::const_iterator tile_it = tile.get().range().begin();
           tile_it != tile.get().range().end(); ++tile_it) {
        BOOST_CHECK_EQUAL(matrix((*tile_it)[0], (*tile_it)[1]),
                          tile.get()[*tile_it]);
        BOOST_CHECK_EQUAL(rmatrix((*tile_it)[0], (*tile_it)[1]),
                          tile.get()[*tile_it]);
      }
    }
  } else {
    // Check that eigen_to_array throws when there is more than one node
#if !defined(TA_USER_ASSERT_DISABLED)
    BOOST_CHECK_THROW(array_to_eigen(array), TiledArray::Exception);
#endif

    // Fill local tiles with data
    GlobalFixture::world->srand(27);
    TArrayI::pmap_interface::const_iterator it = array.pmap()->begin();
    TArrayI::pmap_interface::const_iterator end = array.pmap()->end();
    for (; it != end; ++it) {
      TArrayI::value_type tile(array.trange().make_tile_range(*it));
      for (TArrayI::value_type::iterator tile_it = tile.begin();
           tile_it != tile.end(); ++tile_it) {
        *tile_it = GlobalFixture::world->rand();
      }
      array.set(*it, tile);
    }

    // Distribute the data of array1 to all nodes
    array.make_replicated();

    BOOST_CHECK(array.pmap()->is_replicated());

    // Convert the array to an Eigen matrix
    BOOST_CHECK_NO_THROW(matrix = array_to_eigen(array));
    BOOST_CHECK_NO_THROW(rmatrix = a_to_e_rowmajor(array));

    // Check that the matrix dimensions are the same as the array
    BOOST_CHECK_EQUAL(matrix.rows(), array.trange().elements_range().extent(0));
    BOOST_CHECK_EQUAL(matrix.cols(), array.trange().elements_range().extent(1));
    BOOST_CHECK_EQUAL(rmatrix.rows(),
                      array.trange().elements_range().extent(0));
    BOOST_CHECK_EQUAL(rmatrix.cols(),
                      array.trange().elements_range().extent(1));

    // Check that the data in vector matches the data in array
    for (Range::const_iterator it = array.range().begin();
         it != array.range().end(); ++it) {
      BOOST_CHECK(array.is_local(*it));

      Future<TArrayI::value_type> tile = array.find(*it);
      for (Range::const_iterator tile_it = tile.get().range().begin();
           tile_it != tile.get().range().end(); ++tile_it) {
        BOOST_CHECK_EQUAL(matrix((*tile_it)[0], (*tile_it)[1]),
                          tile.get()[*tile_it]);
        BOOST_CHECK_EQUAL(rmatrix((*tile_it)[0], (*tile_it)[1]),
                          tile.get()[*tile_it]);
      }
    }
  }
}

BOOST_AUTO_TEST_CASE(array_to_vector) {
  if (GlobalFixture::world->size() == 1) {
    // Fill the array with random data
    GlobalFixture::world->srand(27);
    for (Range::const_iterator it = array1.range().begin();
         it != array1.range().end(); ++it) {
      TArrayI::value_type tile(array1.trange().make_tile_range(*it));
      for (TArrayI::value_type::iterator tile_it = tile.begin();
           tile_it != tile.end(); ++tile_it) {
        *tile_it = GlobalFixture::world->rand();
      }
      array1.set(*it, tile);
    }

    // Convert the array to an Eigen vector
    BOOST_CHECK_NO_THROW(vector = array_to_eigen(array1));

    // Check that the matrix dimensions are the same as the array
    BOOST_CHECK_EQUAL(vector.rows(),
                      array1.trange().elements_range().extent(0));
    BOOST_CHECK_EQUAL(vector.cols(), 1);

    // Check that the data in vector matches the data in array
    for (Range::const_iterator it = array1.range().begin();
         it != array1.range().end(); ++it) {
      Future<TArrayI::value_type> tile = array1.find(*it);
      for (Range::const_iterator tile_it = tile.get().range().begin();
           tile_it != tile.get().range().end(); ++tile_it) {
        BOOST_CHECK_EQUAL(vector((*tile_it)[0]), tile.get()[*tile_it]);
      }
    }
  } else {
    // Check that eigen_to_array throws when there is more than one node
#if !defined(TA_USER_ASSERT_DISABLED)
    BOOST_CHECK_THROW(array_to_eigen(array1), TiledArray::Exception);
#endif

    // Fill local tiles with data
    GlobalFixture::world->srand(27);
    TArrayI::pmap_interface::const_iterator it = array1.pmap()->begin();
    TArrayI::pmap_interface::const_iterator end = array1.pmap()->end();
    for (; it != end; ++it) {
      TArrayI::value_type tile(array1.trange().make_tile_range(*it));
      for (TArrayI::value_type::iterator tile_it = tile.begin();
           tile_it != tile.end(); ++tile_it) {
        *tile_it = GlobalFixture::world->rand();
      }
      array1.set(*it, tile);
    }

    // Distribute the data of array1 to all nodes
    array1.make_replicated();

    BOOST_CHECK(array1.pmap()->is_replicated());

    // Convert the array to an Eigen vector
    BOOST_CHECK_NO_THROW(vector = array_to_eigen(array1));

    // Check that the matrix dimensions are the same as the array
    BOOST_CHECK_EQUAL(vector.rows(),
                      array1.trange().elements_range().extent(0));
    BOOST_CHECK_EQUAL(vector.cols(), 1);

    // Check that the data in vector matches the data in array
    for (Range::const_iterator it = array1.range().begin();
         it != array1.range().end(); ++it) {
      BOOST_CHECK(array1.is_local(*it));

      Future<TArrayI::value_type> tile = array1.find(*it);
      for (Range::const_iterator tile_it = tile.get().range().begin();
           tile_it != tile.get().range().end(); ++tile_it) {
        BOOST_CHECK_EQUAL(vector((*tile_it)[0]), tile.get()[*tile_it]);
      }
    }
  }
}

BOOST_AUTO_TEST_SUITE_END()