libMesh
parallel_point_test.C
Go to the documentation of this file.
1 // Ignore unused parameter warnings coming from cppunit headers
2 #include <libmesh/ignore_warnings.h>
3 #include <cppunit/extensions/HelperMacros.h>
4 #include <cppunit/TestCase.h>
5 #include <libmesh/restore_warnings.h>
6 
7 #include <libmesh/parallel.h>
8 #include <libmesh/parallel_algebra.h>
9 
10 #include "test_comm.h"
11 
12 // THE CPPUNIT_TEST_SUITE_END macro expands to code that involves
13 // std::auto_ptr, which in turn produces -Wdeprecated-declarations
14 // warnings. These can be ignored in GCC as long as we wrap the
15 // offending code in appropriate pragmas. We can't get away with a
16 // single ignore_warnings.h inclusion at the beginning of this file,
17 // since the libmesh headers pull in a restore_warnings.h at some
18 // point. We also don't bother restoring warnings at the end of this
19 // file since it's not a header.
20 #include <libmesh/ignore_warnings.h>
21 
22 using namespace libMesh;
23 
24 class ParallelPointTest : public CppUnit::TestCase {
25 public:
26  CPPUNIT_TEST_SUITE( ParallelPointTest );
27 
28  CPPUNIT_TEST( testAllGatherPoint );
29  CPPUNIT_TEST( testAllGatherPairPointPoint );
30  CPPUNIT_TEST( testAllGatherPairRealPoint );
31  CPPUNIT_TEST( testBroadcastVectorValueInt );
32  CPPUNIT_TEST( testBroadcastVectorValueReal );
33  CPPUNIT_TEST( testBroadcastPoint );
34  CPPUNIT_TEST( testIsendRecv );
35  CPPUNIT_TEST( testIrecvSend );
36 
37  CPPUNIT_TEST_SUITE_END();
38 
39 private:
40 
41 public:
42  void setUp()
43  {}
44 
45  void tearDown()
46  {}
47 
48 
49 
51  {
52  std::vector<Point> vals;
53  Real myrank = TestCommWorld->rank();
54  TestCommWorld->allgather(Point(myrank, myrank+0.25, myrank+0.5),vals);
55 
56  const std::size_t comm_size = TestCommWorld->size();
57  const std::size_t vec_size = vals.size();
58  CPPUNIT_ASSERT_EQUAL( comm_size, vec_size );
59  for (processor_id_type i=0; i<vals.size(); i++)
60  {
61  Real theirrank = i;
62  CPPUNIT_ASSERT_EQUAL( theirrank, vals[i](0) );
63  CPPUNIT_ASSERT_EQUAL( theirrank+Real(0.25), vals[i](1) );
64  CPPUNIT_ASSERT_EQUAL( theirrank+Real(0.5), vals[i](2) );
65  }
66  }
67 
68 
69 
71  {
72  std::vector<std::pair<Point, Point>> vals;
73  Real myrank = TestCommWorld->rank();
75  (std::make_pair(Point(myrank, myrank+0.125, myrank+0.25), Point(myrank+0.5, myrank+0.625, myrank+0.75)), vals);
76 
77  const std::size_t comm_size = TestCommWorld->size();
78  const std::size_t vec_size = vals.size();
79  CPPUNIT_ASSERT_EQUAL( comm_size, vec_size );
80 
81  for (processor_id_type i=0; i<vals.size(); i++)
82  {
83  Real theirrank = i;
84  CPPUNIT_ASSERT_EQUAL( theirrank, vals[i].first(0) );
85  CPPUNIT_ASSERT_EQUAL( theirrank+Real(0.125), vals[i].first(1) );
86  CPPUNIT_ASSERT_EQUAL( theirrank+Real(0.25), vals[i].first(2) );
87  CPPUNIT_ASSERT_EQUAL( theirrank+Real(0.5), vals[i].second(0) );
88  CPPUNIT_ASSERT_EQUAL( theirrank+Real(0.625), vals[i].second(1) );
89  CPPUNIT_ASSERT_EQUAL( theirrank+Real(0.75), vals[i].second(2) );
90  }
91  }
92 
93 
95  {
96  std::vector<std::pair<Real, Point>> vals;
97  Real myrank = TestCommWorld->rank();
99  (std::make_pair(Real(myrank+0.75), Point(myrank, myrank+0.25, myrank+0.5)), vals);
100 
101  const std::size_t comm_size = TestCommWorld->size();
102  const std::size_t vec_size = vals.size();
103  CPPUNIT_ASSERT_EQUAL( comm_size, vec_size );
104 
105  for (processor_id_type i=0; i<vals.size(); i++)
106  {
107  Real theirrank = i;
108  CPPUNIT_ASSERT_EQUAL( theirrank, vals[i].second(0) );
109  CPPUNIT_ASSERT_EQUAL( theirrank+Real(0.25), vals[i].second(1) );
110  CPPUNIT_ASSERT_EQUAL( theirrank+Real(0.5), vals[i].second(2) );
111  CPPUNIT_ASSERT_EQUAL( theirrank+Real(0.75), vals[i].first );
112  }
113  }
114 
115 
116 
117  template <typename T>
119  {
120  std::vector<VectorValue<T>> src(3), dest(3);
121 
122  {
123  T val=T(0);
124  for (unsigned int i=0; i<3; i++)
125  for (unsigned int j=0; j<LIBMESH_DIM; j++)
126  src[i](j) = val++;
127 
128  if (TestCommWorld->rank() == 0)
129  dest = src;
130  }
131 
132  TestCommWorld->broadcast(dest);
133 
134  for (unsigned int i=0; i<3; i++)
135  for (unsigned int j=0; j<LIBMESH_DIM; j++)
136  CPPUNIT_ASSERT_EQUAL (src[i](j), dest[i](j) );
137  }
138 
139 
140 
142  {
143  this->testBroadcastVectorValue<int>();
144  }
145 
146 
147 
149  {
150  this->testBroadcastVectorValue<Real>();
151  }
152 
153 
154 
156  {
157  std::vector<Point> src(3), dest(3);
158 
159  {
160  Real val=0.;
161  for (unsigned int i=0; i<3; i++)
162  for (unsigned int j=0; j<LIBMESH_DIM; j++)
163  src[i](j) = val++;
164 
165  if (TestCommWorld->rank() == 0)
166  dest = src;
167  }
168 
169  TestCommWorld->broadcast(dest);
170 
171  for (unsigned int i=0; i<3; i++)
172  for (unsigned int j=0; j<LIBMESH_DIM; j++)
173  CPPUNIT_ASSERT_EQUAL (src[i](j), dest[i](j) );
174  }
175 
176 
177 
179  {
180  unsigned int procup = (TestCommWorld->rank() + 1) %
181  TestCommWorld->size();
182  unsigned int procdown = (TestCommWorld->size() +
183  TestCommWorld->rank() - 1) %
184  TestCommWorld->size();
185 
186  std::vector<unsigned int> src_val(3), recv_val(3);
187 
188  src_val[0] = 0;
189  src_val[1] = 1;
190  src_val[2] = 2;
191 
193 
194  if (TestCommWorld->size() > 1)
195  {
196  // Default communication
198 
199  TestCommWorld->send (procup,
200  src_val,
201  request);
202 
203  TestCommWorld->receive (procdown,
204  recv_val);
205 
206  Parallel::wait (request);
207 
208  CPPUNIT_ASSERT_EQUAL ( src_val.size() , recv_val.size() );
209 
210  for (std::size_t i=0; i<src_val.size(); i++)
211  CPPUNIT_ASSERT_EQUAL( src_val[i] , recv_val[i] );
212 
213 
214  // Synchronous communication
216  std::fill (recv_val.begin(), recv_val.end(), 0);
217 
218  TestCommWorld->send (procup,
219  src_val,
220  request);
221 
222  TestCommWorld->receive (procdown,
223  recv_val);
224 
225  Parallel::wait (request);
226 
227  CPPUNIT_ASSERT_EQUAL ( src_val.size() , recv_val.size() );
228 
229  for (std::size_t i=0; i<src_val.size(); i++)
230  CPPUNIT_ASSERT_EQUAL( src_val[i] , recv_val[i] );
231 
232  // Restore default communication
234  }
235  }
236 
237 
238 
240  {
241  unsigned int procup = (TestCommWorld->rank() + 1) %
242  TestCommWorld->size();
243  unsigned int procdown = (TestCommWorld->size() +
244  TestCommWorld->rank() - 1) %
245  TestCommWorld->size();
246 
247  std::vector<unsigned int> src_val(3), recv_val(3);
248 
249  src_val[0] = 0;
250  src_val[1] = 1;
251  src_val[2] = 2;
252 
254 
255  if (TestCommWorld->size() > 1)
256  {
257  // Default communication
259 
260  TestCommWorld->receive (procdown,
261  recv_val,
262  request);
263 
264  TestCommWorld->send (procup,
265  src_val);
266 
267  Parallel::wait (request);
268 
269  CPPUNIT_ASSERT_EQUAL ( src_val.size() , recv_val.size() );
270 
271  for (std::size_t i=0; i<src_val.size(); i++)
272  CPPUNIT_ASSERT_EQUAL( src_val[i] , recv_val[i] );
273 
274  // Synchronous communication
276  std::fill (recv_val.begin(), recv_val.end(), 0);
277 
278 
279  TestCommWorld->receive (procdown,
280  recv_val,
281  request);
282 
283  TestCommWorld->send (procup,
284  src_val);
285 
286  Parallel::wait (request);
287 
288  CPPUNIT_ASSERT_EQUAL ( src_val.size() , recv_val.size() );
289 
290  for (std::size_t i=0; i<src_val.size(); i++)
291  CPPUNIT_ASSERT_EQUAL( src_val[i] , recv_val[i] );
292 
293  // Restore default communication
295  }
296  }
297 
298 
299 
300 
301 };
302 
Status wait(Request &r)
Wait for a non-blocking send or receive to finish.
Definition: parallel.h:565
unsigned int size() const
Definition: parallel.h:726
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:28
uint8_t processor_id_type
Definition: id_types.h:99
MPI_Request request
Request object for non-blocking I/O.
Definition: parallel.h:171
void send_mode(const SendMode sm)
Explicitly sets the SendMode type used for send operations.
Definition: parallel.h:761
The libMesh namespace provides an interface to certain functionality in the library.
void send(const unsigned int dest_processor_id, const T &buf, const MessageTag &tag=no_tag) const
Blocking-send to one processor with data-defined type.
void broadcast(T &data, const unsigned int root_id=0) const
Take a local value and broadcast it to all processors.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Encapsulates the MPI_Request.
Definition: parallel.h:517
Status receive(const unsigned int dest_processor_id, T &buf, const MessageTag &tag=any_tag) const
Blocking-receive from one processor with data-defined type.
unsigned int rank() const
Definition: parallel.h:724
CPPUNIT_TEST_SUITE_REGISTRATION(ParallelPointTest)
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38
void allgather(const T &send, std::vector< T > &recv) const
Take a vector of length this->size(), and fill in recv[processor_id] = the value of send on that proc...