COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-parallel-collective-broadcast.hpp
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 // this file is en-block included into utest-parallel-collective.cpp
8 // do not include anything here, rather in utest-parallel-collective.cpp
9 
11 
13 {
16  {
17  int i;
18 
19  // rank and proc
20  nproc=PE::Comm::instance().size();
21  irank=PE::Comm::instance().rank();
22 
23  // ptr helpers
24  sndcnt=0;
25  rcvcnt=0;
26  ptr_snddat=new double[nproc];
27  ptr_rcvdat=new double[nproc*nproc];
28  ptr_sndmap=new int[nproc];
29  ptr_rcvmap=new int[nproc];
30  ptr_tmprcv=new double[nproc];
31 
32  // std::Vector helpers
33  vec_snddat.resize(nproc);
34  vec_rcvdat.resize(nproc*nproc);
35  vec_sndmap.resize(nproc);
36  vec_rcvmap.resize(nproc);
37  vec_tmprcv.resize(0);
38  vec_tmprcvchr.resize(nproc*sizeof(double));
39  vec_snddatchr.resize(nproc*sizeof(double));
40  }
41 
44  {
45  delete[] ptr_snddat;
46  delete[] ptr_rcvdat;
47  delete[] ptr_sndmap;
48  delete[] ptr_rcvmap;
49  delete[] ptr_tmprcv;
50  }
51 
53  int nproc;
55  int irank;
56 
58  int sndcnt;
59  int rcvcnt;
60  double* ptr_snddat;
61  double* ptr_rcvdat;
62  int* ptr_sndmap;
63  int* ptr_rcvmap;
64  double* ptr_tmprcv;
65 
67  std::vector<double> vec_snddat;
68  std::vector<double> vec_rcvdat;
69  std::vector<int> vec_sndmap;
70  std::vector<int> vec_rcvmap;
71  std::vector<double> vec_tmprcv;
72  std::vector<char> vec_tmprcvchr;
73  std::vector<char> vec_snddatchr;
74 
77  {
78  int i,j;
79  for (i=0; i<nproc; i++){
80  ptr_snddat[i]=(irank+1)*1000+(i+1);
81  for (j=0; j<nproc; j++) ptr_rcvdat[i*nproc+j]=(i+1)*1000+(j+1);
82  }
83  sndcnt=nproc;
84  rcvcnt=nproc;
85  vec_snddat.assign(ptr_snddat,ptr_snddat+nproc);
86  vec_rcvdat.assign(ptr_rcvdat,ptr_rcvdat+nproc*nproc);
87  vec_snddatchr.assign((char*)(ptr_snddat),(char*)(ptr_snddat+nproc));
88  }
89 
92  {
93  int i,j,k,l;
94  sndcnt=nproc/2;
95  rcvcnt=nproc/2;
96  for(i=0; i<nproc; i++) { // making debugger shut up for uninitialized values
97  ptr_snddat[i]=0.;
98  ptr_sndmap[i]=0;
99  ptr_rcvmap[i]=0;
100  }
101  for(i=0; i<nproc; i++) for(j=0; j<nproc; j++) ptr_rcvdat[i*nproc+j]=0; // making debugger shut up for uninitialized values
102  for (i=0; i<nproc; i++){
103  ptr_snddat[i]=(irank+1)*1000+(i+1);
104  for (j=0; j<nproc; j++) ptr_rcvdat[i*nproc+j]=(i+1)*1000+(2*(rcvcnt-j)-1);
105  }
106  for (i=0; i<sndcnt; i++)
107  ptr_sndmap[i]=2*i; // every second
108  for (i=0; i<rcvcnt; i++)
109  ptr_rcvmap[i]=rcvcnt-1-i; // inverse into contiguous
110  vec_snddat.assign(ptr_snddat,ptr_snddat+nproc);
111  vec_rcvdat.assign(ptr_rcvdat,ptr_rcvdat+nproc*nproc);
112  vec_snddatchr.assign((char*)(ptr_snddat),(char*)(ptr_snddat+nproc));
113  vec_sndmap.assign(ptr_sndmap,ptr_sndmap+sndcnt);
114  vec_rcvmap.assign(ptr_rcvmap,ptr_rcvmap+rcvcnt);
115  }
116 
117 };
118 
120 
121 BOOST_FIXTURE_TEST_SUITE( PEBroadcastSuite, PEBroadcastFixture )
122 
123 
126 {
127  PEProcessSortedExecute(-1,CFinfo << "Testing broadcast " << irank << "/" << nproc << CFendl; );
128 }
129 
131 
132 BOOST_AUTO_TEST_CASE( broadcast_ptr_constant )
133 {
134  int i,r;
135 
136  setup_data_constant();
137 
138  for (r=0; r<nproc; r++) {
139 
140  delete[] ptr_tmprcv;
141  ptr_tmprcv=0;
142 
143  ptr_tmprcv=PE::Comm::instance().broadcast(ptr_snddat, nproc, (double*)0, r);
144  for (i=0; i<nproc; i++) BOOST_CHECK_EQUAL( ptr_tmprcv[i] , ptr_rcvdat[r*nproc+i] );
145 
146  for (i=0; i<nproc; i++) ptr_tmprcv[i]=0.;
147  PE::Comm::instance().broadcast(ptr_snddat, nproc, ptr_tmprcv, r);
148  for (i=0; i<nproc; i++) BOOST_CHECK_EQUAL( ptr_tmprcv[i] , ptr_rcvdat[r*nproc+i] );
149 
150  for (i=0; i<nproc; i++) ptr_tmprcv[i]=ptr_snddat[i];
151  PE::Comm::instance().broadcast(ptr_tmprcv, nproc, ptr_tmprcv, r);
152  for (i=0; i<nproc; i++) BOOST_CHECK_EQUAL( ptr_tmprcv[i] , ptr_rcvdat[r*nproc+i] );
153 
154  delete[] ptr_tmprcv;
155  ptr_tmprcv=0;
156 
157  ptr_tmprcv=(double*)PE::Comm::instance().broadcast((char*)ptr_snddat, nproc, (char*)0, r, sizeof(double));
158  for (i=0; i<nproc; i++) BOOST_CHECK_EQUAL( ptr_tmprcv[i] , ptr_rcvdat[r*nproc+i] );
159 
160  for (i=0; i<nproc; i++) ptr_tmprcv[i]=0.;
161  PE::Comm::instance().broadcast((char*)ptr_snddat, nproc, (char*)ptr_tmprcv, r, sizeof(double));
162  for (i=0; i<nproc; i++) BOOST_CHECK_EQUAL( ptr_tmprcv[i] , ptr_rcvdat[r*nproc+i] );
163 
164  for (i=0; i<nproc; i++) ptr_tmprcv[i]=ptr_snddat[i];
165  PE::Comm::instance().broadcast((char*)ptr_tmprcv, nproc, (char*)ptr_tmprcv, r, sizeof(double));
166  for (i=0; i<nproc; i++) BOOST_CHECK_EQUAL( ptr_tmprcv[i] , ptr_rcvdat[r*nproc+i] );
167  }
168 }
169 
171 
172 BOOST_AUTO_TEST_CASE( broadcast_vector_constant )
173 {
174  int i,r;
175 
176  setup_data_constant();
177 
178  for (r=0; r<nproc; r++) {
179 
180  vec_tmprcv.resize(0);
181  vec_tmprcv.reserve(0);
182  PE::Comm::instance().broadcast(vec_snddat, vec_tmprcv, r);
183  for (i=0; i<nproc; i++) BOOST_CHECK_EQUAL( vec_tmprcv[i] , vec_rcvdat[r*nproc+i] );
184  BOOST_CHECK_EQUAL( (int)vec_tmprcv.size() , rcvcnt );
185 
186  vec_tmprcv.assign(nproc,0.);
187  PE::Comm::instance().broadcast(vec_snddat, vec_tmprcv, r);
188  for (i=0; i<nproc; i++) BOOST_CHECK_EQUAL( vec_tmprcv[i] , vec_rcvdat[r*nproc+i] );
189 
190  vec_tmprcv=vec_snddat;
191  PE::Comm::instance().broadcast(vec_tmprcv, vec_tmprcv, r);
192  for (i=0; i<nproc; i++) BOOST_CHECK_EQUAL( vec_tmprcv[i] , vec_rcvdat[r*nproc+i] );
193 
194  vec_tmprcvchr.resize(0);
195  vec_tmprcvchr.reserve(0);
196  PE::Comm::instance().broadcast(vec_snddatchr, vec_tmprcvchr, r );
197  BOOST_CHECK_EQUAL( vec_tmprcvchr.size() , sizeof(double)*rcvcnt );
198  for (i=0; i<nproc; i++) BOOST_CHECK_EQUAL( ((double*)(&vec_tmprcvchr[0]))[i], vec_rcvdat[r*nproc+i] );
199 
200  for (i=0; i<nproc; i++) ((double*)(&vec_tmprcvchr[0]))[i]=0.;
201  PE::Comm::instance().broadcast(vec_snddatchr, vec_tmprcvchr, r );
202  for (i=0; i<nproc; i++) BOOST_CHECK_EQUAL( ((double*)(&vec_tmprcvchr[0]))[i], vec_rcvdat[r*nproc+i] );
203 
204  vec_tmprcvchr.assign((char*)(ptr_snddat),(char*)(ptr_snddat+nproc));
205  PE::Comm::instance().broadcast(vec_tmprcvchr, vec_tmprcvchr, r );
206  for (i=0; i<nproc; i++) BOOST_CHECK_EQUAL( ((double*)(&vec_tmprcvchr[0]))[i], vec_rcvdat[r*nproc+i] );
207  }
208 }
209 
211 
212 BOOST_AUTO_TEST_CASE( broadcast_ptr_variable )
213 {
214 
215  int i,j,k,r;
216 
217  setup_data_variable();
218 
219  for (r=0; r<nproc; r++) {
220 
221  delete[] ptr_tmprcv;
222  ptr_tmprcv=0;
223 
224  ptr_tmprcv=PE::Comm::instance().broadcast(ptr_snddat, sndcnt, ptr_sndmap, (double*)0, ptr_rcvmap, r);
225  for (i=0; i<sndcnt; i++) BOOST_CHECK_EQUAL( ptr_tmprcv[i] , ptr_rcvdat[nproc*r+i] );
226 
227  for (i=0; i<sndcnt; i++) ptr_tmprcv[i]=0.;
228  PE::Comm::instance().broadcast(ptr_snddat, sndcnt, ptr_sndmap, ptr_tmprcv, ptr_rcvmap, r);
229  for (i=0; i<sndcnt; i++) BOOST_CHECK_EQUAL( ptr_tmprcv[i] , ptr_rcvdat[nproc*r+i] );
230 
231  delete[] ptr_tmprcv;
232  ptr_tmprcv=new double[nproc];
233  for (i=0; i<nproc; i++) ptr_tmprcv[i]=ptr_snddat[i];
234  PE::Comm::instance().broadcast(ptr_tmprcv, sndcnt, ptr_sndmap, ptr_tmprcv, ptr_rcvmap, r);
235  for (i=0; i<sndcnt; i++) BOOST_CHECK_EQUAL( ptr_tmprcv[i] , ptr_rcvdat[nproc*r+i] );
236 
237  delete[] ptr_tmprcv;
238  ptr_tmprcv=0;
239  ptr_tmprcv=(double*)PE::Comm::instance().broadcast((char*)ptr_snddat, sndcnt, ptr_sndmap, (char*)0, ptr_rcvmap, r, sizeof(double));
240  for (i=0; i<sndcnt; i++) BOOST_CHECK_EQUAL( ptr_tmprcv[i] , ptr_rcvdat[nproc*r+i] );
241 
242  for (i=0; i<sndcnt; i++) ptr_tmprcv[i]=0.;
243  PE::Comm::instance().broadcast((char*)ptr_snddat, sndcnt, ptr_sndmap, (char*)ptr_tmprcv, ptr_rcvmap, r, sizeof(double));
244  for (i=0; i<sndcnt; i++) BOOST_CHECK_EQUAL( ptr_tmprcv[i] , ptr_rcvdat[nproc*r+i] );
245 
246  delete[] ptr_tmprcv;
247  ptr_tmprcv=new double[nproc];
248  for (i=0; i<nproc; i++) ptr_tmprcv[i]=ptr_snddat[i];
249  PE::Comm::instance().broadcast((char*)ptr_tmprcv, sndcnt, ptr_sndmap, (char*)ptr_tmprcv, ptr_rcvmap, r, sizeof(double));
250  for (i=0; i<sndcnt; i++) BOOST_CHECK_EQUAL( ptr_tmprcv[i] , ptr_rcvdat[nproc*r+i] );
251  }
252 }
253 
255 
256 BOOST_AUTO_TEST_CASE( broadcast_vector_variable )
257 {
258  int i,j,k,r;
259 
260  setup_data_variable();
261 
262  for (r=0; r<nproc; r++) {
263 
264  vec_tmprcv.resize(0);
265  vec_tmprcv.reserve(0);
266  PE::Comm::instance().broadcast(vec_snddat, vec_sndmap, vec_tmprcv, vec_rcvmap, r);
267  for (i=0; i<sndcnt; i++) BOOST_CHECK_EQUAL( vec_tmprcv[i] , vec_rcvdat[nproc*r+i] );
268 
269  for (i=0; i<sndcnt; i++) vec_tmprcv[i]=0.;
270  PE::Comm::instance().broadcast(vec_snddat, vec_sndmap, vec_tmprcv, vec_rcvmap, r);
271  for (i=0; i<sndcnt; i++) BOOST_CHECK_EQUAL( vec_tmprcv[i] , vec_rcvdat[nproc*r+i] );
272 
273  vec_tmprcv.resize(nproc);
274  vec_tmprcv.reserve(nproc);
275  for (i=0; i<nproc; i++) vec_tmprcv[i]=vec_snddat[i];
276  PE::Comm::instance().broadcast(vec_tmprcv, vec_sndmap, vec_tmprcv, vec_rcvmap, r);
277  for (i=0; i<sndcnt; i++) BOOST_CHECK_EQUAL( vec_tmprcv[i] , vec_rcvdat[nproc*r+i] );
278 
279  vec_tmprcvchr.resize(0);
280  vec_tmprcvchr.reserve(0);
281  PE::Comm::instance().broadcast(vec_snddatchr, vec_sndmap, vec_tmprcvchr, vec_rcvmap, r, sizeof(double));
282  for (i=0; i<sndcnt; i++) BOOST_CHECK_EQUAL( ((double*)&vec_tmprcvchr[0])[i] , vec_rcvdat[nproc*r+i] );
283 
284  for (i=0; i<nproc; i++) ((double*)&vec_tmprcvchr[0])[i]=0.;
285  PE::Comm::instance().broadcast(vec_snddatchr, vec_sndmap, vec_tmprcvchr, vec_rcvmap, r, sizeof(double));
286  for (i=0; i<sndcnt; i++) BOOST_CHECK_EQUAL( ((double*)&vec_tmprcvchr[0])[i] , vec_rcvdat[nproc*r+i] );
287 
288  vec_tmprcvchr.resize(nproc*sizeof(double));
289  vec_tmprcvchr.reserve(nproc*sizeof(double));
290  for (i=0; i<nproc; i++) ((double*)(&vec_tmprcvchr[0]))[i]=vec_snddat[i];
291  PE::Comm::instance().broadcast(vec_tmprcvchr, vec_sndmap, vec_tmprcvchr, vec_rcvmap, r, sizeof(double));
292  for (i=0; i<sndcnt; i++) BOOST_CHECK_EQUAL( ((double*)&vec_tmprcvchr[0])[i] , vec_rcvdat[nproc*r+i] );
293  }
294 }
295 
297 
298 BOOST_AUTO_TEST_SUITE_END()
299 
300 
#define CFinfo
these are always defined
Definition: Log.hpp:104
void setup_data_variable()
helper function for variable size data - setting up input and verification data
PEBroadcastFixture()
common setup for each test case
BOOST_AUTO_TEST_CASE(Broadcast)
std::vector< double > vec_snddat
data for std::vectors
#define CFendl
Definition: Log.hpp:109
void setup_data_constant()
helper function for constant size data - setting up input and verification data
~PEBroadcastFixture()
common tear-down for each test case
#define PEProcessSortedExecute(irank, expression)
Definition: debug.hpp:43
Send comments to:
COOLFluiD Web Admin