COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-parallel-commwrapper.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2013 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6 //
7 // IMPORTANT:
8 // run it both on 1 and many cores
9 // for example: mpirun -np 4 ./test-parallel-environment --report_level=confirm or --report_level=detailed
10 
11 #define BOOST_TEST_DYN_LINK
12 #define BOOST_TEST_MODULE "Test module for cf3::common 's parallel environment - part of testing the commwrapper."
13 
15 
16 #include <boost/test/unit_test.hpp>
17 #include <boost/shared_ptr.hpp>
18 
19 #include "common/Log.hpp"
21 #include "common/Component.hpp"
22 #include "common/PE/Comm.hpp"
26 #include "common/PE/debug.hpp"
27 #include "common/Group.hpp"
28 
29 
31 
32 using namespace cf3;
33 using namespace cf3::common;
34 using namespace cf3::common::PE;
35 
37 
39 {
42  {
43  m_argc = boost::unit_test::framework::master_test_suite().argc;
44  m_argv = boost::unit_test::framework::master_test_suite().argv;
45  }
46 
49  {
50  }
51 
54  {
55  BOOST_CHECK_EQUAL( w1->needs_update() , true );
56  BOOST_CHECK_EQUAL( w2->needs_update() , false );
57 
58  BOOST_CHECK_EQUAL( w1->is_data_type_Uint() , true );
59  BOOST_CHECK_EQUAL( w2->is_data_type_Uint() , false );
60 
61  BOOST_CHECK_EQUAL( w1->size() , 16 );
62  BOOST_CHECK_EQUAL( w2->size() , 8 );
63 
64  BOOST_CHECK_EQUAL( w1->stride() , 1 );
65  BOOST_CHECK_EQUAL( w2->stride() , 3 );
66 
67  BOOST_CHECK_EQUAL( w1->size_of() , (int)sizeof(Uint) );
68  BOOST_CHECK_EQUAL( w2->size_of() , (int)sizeof(double) );
69  }
70 
73  {
74  int i;
75  Uint *itest1=(Uint*) w1->pack();
76  double *dtest2=(double*)w2->pack();
77 
78  for(i=0; i<16; i++) { BOOST_CHECK_EQUAL(itest1[i],16+i); itest1[i]+=16; }
79  for(i=0; i<24; i++) { BOOST_CHECK_EQUAL(dtest2[i],24.+(double)i); dtest2[i]+=24.; }
80 
81  w1->unpack(itest1);
82  w2->unpack(dtest2);
83 
84  for (i=0; i<16; i++) itest1[i]=0;
85  for (i=0; i<24; i++) dtest2[i]=0.;
86 
87  w1->pack(itest1);
88  w2->pack(dtest2);
89 
90  for(i=0; i<16; i++) { BOOST_CHECK_EQUAL(itest1[i],32+i); itest1[i]+=16; }
91  for(i=0; i<24; i++) { BOOST_CHECK_EQUAL(dtest2[i],48.+(double)i); dtest2[i]+=24.; }
92 
93  std::vector<Uint> iv1; iv1.assign(itest1,itest1+16);
94  std::vector<double> dv2; dv2.assign(dtest2,dtest2+24);
95 
96  w1->unpack(iv1);
97  w2->unpack(dv2);
98 
99  iv1.assign(100,0);
100  dv2.assign(100,0.);
101 
102  w1->pack(iv1);
103  w2->pack(dv2);
104 
105  BOOST_CHECK_EQUAL(iv1.size(),16);
106  BOOST_CHECK_EQUAL(dv2.size(),24);
107  for(i=0; i<16; i++) { BOOST_CHECK_EQUAL(iv1[i],48+i); iv1[i]+=16; }
108  for(i=0; i<24; i++) { BOOST_CHECK_EQUAL(dv2[i],72.+(double)i); dv2[i]+=24.; }
109 
110  std::vector<unsigned char> cv1; cv1.assign((unsigned char*)&iv1[0],(unsigned char*)&iv1[0]+16*sizeof(Uint));
111  std::vector<unsigned char> cv2; cv2.assign((unsigned char*)&dv2[0],(unsigned char*)&dv2[0]+24*sizeof(double));
112 
113  w1->unpack(cv1);
114  w2->unpack(cv2);
115 
116  cv1.assign(1000,0x00);
117  cv2.assign(1000,0x00);
118 
119  w1->pack(cv1);
120  w2->pack(cv2);
121 
122  BOOST_CHECK_EQUAL(cv1.size(),16*sizeof(Uint));
123  BOOST_CHECK_EQUAL(cv2.size(),24*sizeof(double));
124  for(i=0; i<16; i++) { BOOST_CHECK_EQUAL( ((Uint*)(&cv1[0]))[i] , 64+i ); ((Uint*)(&cv1[0]))[i]+=16; }
125  for(i=0; i<24; i++) { BOOST_CHECK_EQUAL( ((double*)(&cv2[0]))[i] , (double)(96+i) ); ((double*)(&cv2[0]))[i]+=24.; }
126 
127  w1->unpack(cv1);
128  w2->unpack(cv2);
129 
130  delete[] itest1;
131  delete[] dtest2;
132 
133  w1->pack(iv1);
134  w2->pack(dv2);
135 
136  for(i=0; i<16; i++) iv1[i]-=64;
137  for(i=0; i<24; i++) dv2[i]-=96.;
138  for(i=0; i<16; i++) BOOST_CHECK_EQUAL(iv1[i],16+i);
139  for(i=0; i<24; i++) BOOST_CHECK_EQUAL(dv2[i],24.+(double)i);
140 
141  w1->unpack(iv1);
142  w2->unpack(dv2);
143  }
144 
147  {
148  int i,j;
149  Uint *itest1=(Uint*) w1->pack(map);
150  double *dtest2=(double*)w2->pack(map);
151 
152  for(i=0; i<4; i++) { BOOST_CHECK_EQUAL( itest1[i] , 16+map[i] ); itest1[i]+=16; }
153  for(i=0; i<4; i++) for (j=0; j<3; j++) { BOOST_CHECK_EQUAL( dtest2[i*3+j] , (double)(24+3*map[i]+j) ); dtest2[i*3+j]+=24.; }
154 
155  w1->unpack(itest1,map);
156  w2->unpack(dtest2,map);
157 
158  for (i=0; i<4; i++) itest1[i]=0;
159  for (i=0; i<12; i++) dtest2[i]=0.;
160 
161  w1->pack(map,itest1);
162  w2->pack(map,dtest2);
163 
164  for(i=0; i<4; i++) { BOOST_CHECK_EQUAL( itest1[i] , 32+map[i] ); itest1[i]+=16; }
165  for(i=0; i<4; i++) for (j=0; j<3; j++) { BOOST_CHECK_EQUAL( dtest2[i*3+j] , (double)(48+3*map[i]+j) ); dtest2[i*3+j]+=24.; }
166 
167  std::vector<Uint> iv1; iv1.assign(itest1,itest1+4);
168  std::vector<double> dv2; dv2.assign(dtest2,dtest2+12);
169 
170  w1->unpack(iv1,map);
171  w2->unpack(dv2,map);
172 
173  iv1.assign(100,0);
174  dv2.assign(100,0.);
175 
176  w1->pack(iv1,map);
177  w2->pack(dv2,map);
178 
179  BOOST_CHECK_EQUAL(iv1.size(),4);
180  BOOST_CHECK_EQUAL(dv2.size(),12);
181  for(i=0; i<4; i++) { BOOST_CHECK_EQUAL( iv1[i] , 48+map[i] ); iv1[i]+=16; }
182  for(i=0; i<4; i++) for (j=0; j<3; j++) { BOOST_CHECK_EQUAL( dv2[i*3+j] , (double)(72+3*map[i]+j) ); dv2[i*3+j]+=24.; }
183 
184  std::vector<unsigned char> cv1; cv1.assign((unsigned char*)&iv1[0],(unsigned char*)&iv1[0]+4*sizeof(Uint));
185  std::vector<unsigned char> cv2; cv2.assign((unsigned char*)&dv2[0],(unsigned char*)&dv2[0]+12*sizeof(double));
186 
187  w1->unpack(cv1,map);
188  w2->unpack(cv2,map);
189 
190  cv1.assign(1000,0x00);
191  cv2.assign(1000,0x00);
192 
193  w1->pack(cv1,map);
194  w2->pack(cv2,map);
195 
196  BOOST_CHECK_EQUAL(cv1.size(),4*sizeof(Uint));
197  BOOST_CHECK_EQUAL(cv2.size(),12*sizeof(double));
198  for(i=0; i<4; i++) { BOOST_CHECK_EQUAL( ((Uint*)(&cv1[0]))[i] , 64+map[i] ); ((Uint*)(&cv1[0]))[i]+=16; }
199  for(i=0; i<4; i++) for (j=0; j<3; j++) { BOOST_CHECK_EQUAL( ((double*)(&cv2[0]))[i*3+j] , (double)(96+3*map[i]+j) ); ((double*)(&cv2[0]))[i*3+j]+=24.; }
200 
201  w1->unpack(cv1,map);
202  w2->unpack(cv2,map);
203 
204  delete[] itest1;
205  delete[] dtest2;
206 
207  w1->pack(iv1);
208  w2->pack(dv2);
209 
210  for(i=0; i<4; i++) iv1[map[i]]-=64;
211  for(i=0; i<4; i++) for (j=0; j<3; j++) dv2[3*map[i]+j]-=96.;
212  for(i=0; i<16; i++) BOOST_CHECK_EQUAL(iv1[i],16+i);
213  for(i=0; i<24; i++) BOOST_CHECK_EQUAL(dv2[i],24.+(double)i);
214 
215  w1->unpack(iv1);
216  w2->unpack(dv2);
217  }
218 
221  {
222  int i;
223 
224  CommWrapperView<Uint> iwv1(w1);
225  CommWrapperView<double> dwv2(w2);
226 
227  BOOST_CHECK_EQUAL(iwv1.size(),16);
228  BOOST_CHECK_EQUAL(dwv2.size(),24);
229 
230  Uint* ip=iwv1.get_ptr(); // here get_ptr function is used
231  double* dp=dwv2.get_ptr();
232 
233  for(i=0; i<16; i++) BOOST_CHECK_EQUAL(ip[i],16+i);
234  for(i=0; i<24; i++) BOOST_CHECK_EQUAL(dp[i],24.+(double)i);
235 
238 
239  BOOST_CHECK_EQUAL(cwv1.size(),16*sizeof(Uint));
240  BOOST_CHECK_EQUAL(cwv2.size(),24*sizeof(double));
241 
242  Uint* ia=(Uint*)cwv1(); // here operator () is used
243  double* da=(double*)cwv2();
244 
245  for(i=0; i<16; i++) BOOST_CHECK_EQUAL(ia[i],16+i);
246  for(i=0; i<24; i++) BOOST_CHECK_EQUAL(da[i],24.+(double)i);
247  }
248 
251  {
252  int i;
253 
254  w1->resize(w1->size()+10);
255  w2->resize(w2->size()+10);
256 
257  BOOST_CHECK_EQUAL( w1->size() , 26 );
258  BOOST_CHECK_EQUAL( w2->size() , 18 );
259  BOOST_CHECK_EQUAL( w1->stride() , 1 );
260  BOOST_CHECK_EQUAL( w2->stride() , 3 );
261 
262  w1->resize(w1->size()-15);
263  w2->resize(w2->size()-15);
264 
265  BOOST_CHECK_EQUAL( w1->size() , 11 );
266  BOOST_CHECK_EQUAL( w2->size() , 3 );
267  BOOST_CHECK_EQUAL( w1->stride() , 1 );
268  BOOST_CHECK_EQUAL( w2->stride() , 3 );
269 
270  CommWrapperView<Uint> iwv1(w1);
271  CommWrapperView<double> dwv2(w2);
272 
273  BOOST_CHECK_EQUAL(iwv1.size(),11);
274  BOOST_CHECK_EQUAL(dwv2.size(),9);
275 
276  Uint* ia;
277  double*da;
278  for (ia=iwv1(), i=0; i<(const int)iwv1.size(); i++) BOOST_CHECK_EQUAL(ia[i],16+i);
279  for (da=dwv2(), i=0; i<(const int)dwv2.size(); i++) BOOST_CHECK_EQUAL(da[i],24.+(double)i);
280  }
281 
283  int m_argc;
284  char** m_argv;
285 };
286 
288 
289 BOOST_FIXTURE_TEST_SUITE( CommWrapperSuite, CommWrapperFixture )
290 
291 
294 {
295  PE::Comm::instance().init(m_argc,m_argv);
296  BOOST_CHECK_EQUAL( PE::Comm::instance().is_active() , true );
297  CFinfo.setFilterRankZero(false);
298  PEProcessSortedExecute(-1,CFinfo << "Proccess " << PE::Comm::instance().rank() << "/" << PE::Comm::instance().size() << " reports in." << CFendl;);
299 }
300 
302 
303 BOOST_AUTO_TEST_CASE( ObjectWrapperPtr )
304 {
305  int i;
306  Uint *i1= new Uint[16];
307  double *d2=new double[24];
308  std::vector<int> map(4);
309 
310  for(i=0; i<16; i++) i1[i]=16+i;
311  for(i=0; i<24; i++) d2[i]=24.+(double)i;
312  for(i=0; i<4; i++) map[i]=1+2*i;
313 
314  boost::shared_ptr< CommWrapperPtr<Uint> > wptr1= allocate_component< CommWrapperPtr<Uint> >("Ptr1");
315  boost::shared_ptr< CommWrapperPtr<double> > wptr2=allocate_component< CommWrapperPtr<double> >("Ptr2");
316  Handle<CommWrapper> w1(wptr1);
317  Handle<CommWrapper> w2(wptr2);
318 
319  wptr1->setup(i1,16,1,true);
320  wptr2->setup(d2,24,3,false);
321 
322  test_setup(w1,w2);
323  test_pack_unpack(w1,w2);
324  test_mapped_pack_unpack(w1,w2,map);
325  test_view(w1,w2);
326  try
327  {
328  w1->resize(5);
329  }
330  catch (...)
331  {
332  BOOST_CHECK_EQUAL(w1->size(),5);
333  }
334  try
335  {
336  w1->resize(6);
337  }
338  catch (...)
339  {
340  BOOST_CHECK_EQUAL(w1->size(),6);
341  }
342 
343  delete[] i1;
344  delete[] d2;
345 
346 }
347 
349 
350 BOOST_AUTO_TEST_CASE( ObjectWrapperVector )
351 {
352  int i;
353  std::vector<Uint> i1(16);
354  std::vector<double> d2(24);
355  std::vector<int> map(4);
356 
357  for(i=0; i<16; i++) i1[i]=16+i;
358  for(i=0; i<24; i++) d2[i]=24.+(double)i;
359  for(i=0; i<4; i++) map[i]=1+2*i;
360 
361  boost::shared_ptr< CommWrapperVector<Uint> > wptr1= allocate_component< CommWrapperVector<Uint> >("Ptr1");
362  boost::shared_ptr< CommWrapperVector<double> > wptr2=allocate_component< CommWrapperVector<double> >("Ptr2");
363  Handle<CommWrapper> w1(wptr1);
364  Handle<CommWrapper> w2(wptr2);
365 
366  wptr1->setup(i1,1,true);
367  wptr2->setup(d2,3,false);
368 
369  test_setup(w1,w2);
370  test_pack_unpack(w1,w2);
371  test_mapped_pack_unpack(w1,w2,map);
372  test_view(w1,w2);
373  test_resize(w1,w2);
374 }
375 
377 
378 BOOST_AUTO_TEST_CASE( ObjectWrapperMultiArray )
379 {
380  int i,j;
381  boost::multi_array<Uint,1> i1;
382  boost::multi_array<double,2> d2;
383  std::vector<int> map(4);
384  i1.resize(boost::extents[16]);
385  d2.resize(boost::extents[8][3]);
386 
387  for(i=0; i<16; i++) i1[i]=16+i;
388  for(i=0; i<8; i++)
389  for (j=0; j<3; j++)
390  d2[i][j]=24.+(double)(3*i+j);
391  for(i=0; i<4; i++) map[i]=1+2*i;
392 
393  boost::shared_ptr< CommWrapperMArray<Uint,1> > wptr1= allocate_component< CommWrapperMArray<Uint,1> >("Ptr1");
394  boost::shared_ptr< CommWrapperMArray<double,2> > wptr2=allocate_component< CommWrapperMArray<double,2> >("Ptr2");
395  Handle<CommWrapper> w1(wptr1);
396  Handle<CommWrapper> w2(wptr2);
397 
398  wptr1->setup(i1,true);
399  wptr2->setup(d2,false);
400 
401  test_setup(w1,w2);
402  test_pack_unpack(w1,w2);
403  test_mapped_pack_unpack(w1,w2,map);
404  test_view(w1,w2);
405  test_resize(w1,w2);
406 }
407 
409 
411 {
412  PEProcessSortedExecute(-1,CFinfo << "Proccess " << PE::Comm::instance().rank() << "/" << PE::Comm::instance().size() << " says good bye." << CFendl;);
413  CFinfo.setFilterRankZero(true);
415  BOOST_CHECK_EQUAL( PE::Comm::instance().is_active() , false );
416 }
417 
419 
420 BOOST_AUTO_TEST_SUITE_END()
421 
422 
424 
#define CFinfo
these are always defined
Definition: Log.hpp:104
void test_view(Handle< CommWrapper > &w1, Handle< CommWrapper > &w2)
helper function for testing views
Safe pointer to an object. This is the supported method for referring to components.
Definition: Handle.hpp:39
Parallel Communication Pattern. This class provides functionality to collect communication. For efficiency it works such a way that you submit your request via the constructor or the add/remove/move magic triangle and then call setup to modify the commpattern. The data needed to be kept synchronous can be registered via the insert function. The word node here means any kind of "point of storage", in this context it is not directly related with the computational mesh.
void test_setup(Handle< CommWrapper > &w1, Handle< CommWrapper > &w2)
helper function to test setup
void test_resize(Handle< CommWrapper > &w1, Handle< CommWrapper > &w2)
helper function for testing resize
#define CFendl
Definition: Log.hpp:109
virtual void resize(const int size)=0
void test_pack_unpack(Handle< CommWrapper > &w1, Handle< CommWrapper > &w2)
helper function to test pack and unpack
Holds the Component class, as well as the ComponentIterator class plus some functions related to comp...
virtual int stride() const =0
virtual bool is_data_type_Uint() const =0
~CommWrapperFixture()
common tear-down for each test case
void init(int argc=0, char **args=0)
Definition: Comm.cpp:80
Top-level namespace for coolfluid.
Definition: Action.cpp:18
BOOST_AUTO_TEST_CASE(init)
virtual const void * pack(std::vector< int > &map, void *buf=nullptr) const =0
CommWrapperFixture()
common setup for each test case
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
Classes offering a MPI interface for COOLFluiD.
Definition: all_gather.hpp:39
void test_mapped_pack_unpack(Handle< CommWrapper > &w1, Handle< CommWrapper > &w2, std::vector< int > &map)
helper function to test mapped pack and unpack
virtual int size_of() const =0
virtual int size() const =0
virtual void unpack(void *buf, std::vector< int > &map) const =0
static Comm & instance()
Return a reference to the current PE.
Definition: Comm.cpp:44
void map(Field &field, Eigen::Map< Derived > &v, const Uint row_idx, const Uint var_idx)
Most basic kernel library.
Definition: Action.cpp:19
int size()
accessor to the length of the array, measured in items of sizeof(T)
#define PEProcessSortedExecute(irank, expression)
Definition: debug.hpp:43
Send comments to:
COOLFluiD Web Admin