libMesh
packing_types_test.C
Go to the documentation of this file.
1 #include <libmesh/parallel.h>
2 #include <libmesh/parallel_eigen.h>
3 #include <libmesh/int_range.h>
4 
5 #include "timpi/parallel_sync.h"
6 
7 #include <vector>
8 
9 #include "test_comm.h"
10 #include "libmesh_cppunit.h"
11 
12 using namespace libMesh;
13 
14 #ifdef LIBMESH_HAVE_EIGEN
15 
16 class PackingTypesTest : public CppUnit::TestCase {
17 public:
18  LIBMESH_CPPUNIT_TEST_SUITE( PackingTypesTest );
19 
20  CPPUNIT_TEST( testDynamicEigenMatrix );
21  CPPUNIT_TEST( testDynamicEigenVector );
22  CPPUNIT_TEST( testNonFixedScalar );
23 
24  CPPUNIT_TEST_SUITE_END();
25 
26 private:
27 
28 public:
29  void setUp()
30  {}
31 
32  void tearDown()
33  {}
34 
35 
36 
38  {
39  LOG_UNIT_TEST;
40 
41  typedef Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic> DynamicEigenMatrix;
42  typedef std::vector<DynamicEigenMatrix> DynamicMatrixVector;
43  std::map<processor_id_type, DynamicMatrixVector> send_data;
44 
45  // prepare unique data
46  for (const auto i : index_range(*TestCommWorld))
47  {
48  const auto j = TestCommWorld->rank();
49  send_data[i] = DynamicMatrixVector{ DynamicEigenMatrix(i+1, i+2) };
50  for (const auto row: make_range(i+1))
51  for (const auto col: make_range(i+2))
52  send_data[i][0](row, col) = row * 100000.0 + col * 1000.0 + 10.0 * i + j;
53  }
54 
55  // verification
56  auto verify_data =
57  [](processor_id_type j, const DynamicMatrixVector & recv_data)
58  {
59  const auto i = TestCommWorld->rank();
60  const std::size_t rows = recv_data[0].rows();
61  const std::size_t cols = recv_data[0].cols();
62 
63  CPPUNIT_ASSERT_EQUAL( rows, static_cast<std::size_t>(i+1) );
64  CPPUNIT_ASSERT_EQUAL( cols, static_cast<std::size_t>(i+2) );
65 
66  for (const auto row: make_range(rows))
67  for (const auto col: make_range(cols))
68  CPPUNIT_ASSERT_EQUAL( recv_data[0](row, col), static_cast<Real>(row * 100000.0 + col * 1000.0 + j + 10.0 * i) );
69  };
70 
71  // communicate
72  Parallel::push_parallel_vector_data(*TestCommWorld, send_data, verify_data);
73  }
74 
76  {
77  LOG_UNIT_TEST;
78 
79  typedef Eigen::Matrix<Real, Eigen::Dynamic, 1> DynamicEigenVector;
80  typedef std::vector<DynamicEigenVector> DynamicVectorVector;
81  std::map<processor_id_type, DynamicVectorVector> send_data;
82 
83  // prepare unique data
84  for (const auto i : index_range(*TestCommWorld))
85  {
86  const auto j = TestCommWorld->rank();
87  send_data[i] = DynamicVectorVector{ DynamicEigenVector(i+1, 1) };
88  for (const auto row: make_range(i+1))
89  send_data[i][0](row) = row * 1000.0 + 10.0 * i + j;
90  }
91 
92  // verification
93  auto verify_data =
94  [](processor_id_type j, const DynamicVectorVector & recv_data)
95  {
96  const auto i = TestCommWorld->rank();
97  const std::size_t rows = recv_data[0].rows();
98  const std::size_t cols = recv_data[0].cols();
99 
100  CPPUNIT_ASSERT_EQUAL( rows, static_cast<std::size_t>(i+1) );
101  CPPUNIT_ASSERT_EQUAL( cols, 1lu );
102 
103  for (const auto row: make_range(rows))
104  CPPUNIT_ASSERT_EQUAL( recv_data[0](row), static_cast<Real>(row * 1000.0 + j + 10.0 * i) );
105  };
106 
107  // communicate
108  Parallel::push_parallel_vector_data(*TestCommWorld, send_data, verify_data);
109  }
110 
112  {
113  LOG_UNIT_TEST;
114 
115  typedef Eigen::Matrix<std::vector<Real>, 3, 3> VectorEigenMatrix;
116  typedef std::vector<VectorEigenMatrix> VectorMatrixVector;
117  std::map<processor_id_type, VectorMatrixVector> send_data;
118 
119  // prepare unique data
120  for (const auto i : index_range(*TestCommWorld))
121  {
122  const auto j = TestCommWorld->rank();
123  send_data[i] = VectorMatrixVector(1);
124  for (const auto row: make_range(3))
125  for (const auto col: make_range(3))
126  send_data[i][0](row, col).assign(row + col + 1, 10.0 * i + j);
127  }
128 
129  // verification
130  auto verify_data =
131  [](processor_id_type j, const VectorMatrixVector & recv_data)
132  {
133  const auto i = TestCommWorld->rank();
134  const std::size_t rows = recv_data[0].rows();
135  const std::size_t cols = recv_data[0].cols();
136 
137  CPPUNIT_ASSERT_EQUAL( rows, static_cast<std::size_t>(3) );
138  CPPUNIT_ASSERT_EQUAL( cols, static_cast<std::size_t>(3) );
139 
140  for (const auto row: make_range(rows))
141  for (const auto col: make_range(cols))
142  {
143  CPPUNIT_ASSERT_EQUAL( recv_data[0](row, col).size(), static_cast<std::size_t>(row + col + 1) );
144  for (const auto & val : recv_data[0](row, col))
145  CPPUNIT_ASSERT_EQUAL( val, static_cast<Real>(j + 10.0 * i) );
146  }
147  };
148 
149  // communicate
150  Parallel::push_parallel_vector_data(*TestCommWorld, send_data, verify_data);
151  }
152 
153 
154 };
155 
157 
158 #endif // LIBMESH_HAVE_EIGEN
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:171
processor_id_type rank() const
The libMesh namespace provides an interface to certain functionality in the library.
CPPUNIT_TEST_SUITE_REGISTRATION(PackingTypesTest)
uint8_t processor_id_type
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
Definition: int_range.h:140
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition: int_range.h:117