1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
27 #include "MPIAccessDECTest.hxx"
28 #include <cppunit/TestAssert.h>
30 //#include "CommInterface.hxx"
31 //#include "ProcessorGroup.hxx"
32 //#include "MPIProcessorGroup.hxx"
33 #include "MPIAccessDEC.hxx"
34 #include "LinearTimeInterpolator.hxx"
36 // use this define to enable lines, execution of which leads to Segmentation Fault
39 // use this define to enable CPPUNIT asserts and fails, showing bugs
40 #define ENABLE_FORCED_FAILURES
43 using namespace ParaMEDMEM;
45 void MPIAccessDECTest::test_AllToAllvTimeDECSynchronousNative() {
46 test_AllToAllvTimeDEC( false , true ) ;
48 void MPIAccessDECTest::test_AllToAllvTimeDECSynchronousPointToPoint() {
49 test_AllToAllvTimeDEC( false , false ) ;
51 void MPIAccessDECTest::test_AllToAllvTimeDECAsynchronousPointToPoint() {
52 test_AllToAllvTimeDEC( true , false ) ;
55 static void chksts( int sts , int myrank , ParaMEDMEM::MPIAccess * mpi_access ) {
56 char msgerr[MPI_MAX_ERROR_STRING] ;
58 if ( sts != MPI_SUCCESS ) {
59 mpi_access->errorString(sts, msgerr, &lenerr) ;
60 cout << "test_AllToAllvTimeDEC" << myrank << " lenerr " << lenerr << " "
62 ostringstream strstream ;
63 strstream << "==========================================================="
64 << "test_AllToAllvTimeDEC" << myrank << " KO"
65 << "==========================================================="
67 cout << strstream.str() << endl ;
68 CPPUNIT_FAIL( strstream.str() ) ;
73 void MPIAccessDECTest::test_AllToAllvTimeDEC( bool Asynchronous , bool UseMPINative ) {
75 cout << "test_AllToAllvTimeDEC" << endl ;
77 // MPI_Init(&argc, &argv) ;
81 MPI_Comm_size(MPI_COMM_WORLD,&size) ;
82 MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
84 if ( size < 2 || size > 11 ) {
85 ostringstream strstream ;
86 strstream << "usage :" << endl
87 << "mpirun -np <nbprocs> test_AllToAllTimeDEC" << endl
88 << " (nbprocs >=2)" << endl
89 << "test must be runned with more than 1 proc and less than 12 procs"
91 cout << strstream.str() << endl ;
92 CPPUNIT_FAIL( strstream.str() ) ;
95 // int Asynchronous = atoi(argv[1]) ;
96 int UseMPI_Alltoallv = UseMPINative ;
98 // UseMPI_Alltoallv = atoi(argv[2]) ;
101 cout << "test_AllToAllvTimeDEC" << myrank << " Asynchronous " << Asynchronous
102 << " UseMPI_Alltoallv " << UseMPI_Alltoallv << endl ;
104 ParaMEDMEM::CommInterface interface ;
105 std::set<int> sourceprocs;
106 std::set<int> targetprocs;
108 for ( i = 0 ; i < size/2 ; i++ ) {
109 sourceprocs.insert(i);
111 for ( i = size/2 ; i < size ; i++ ) {
112 targetprocs.insert(i);
115 ParaMEDMEM::MPIProcessorGroup* sourcegroup = new ParaMEDMEM::MPIProcessorGroup(interface,sourceprocs) ;
116 ParaMEDMEM::MPIProcessorGroup* targetgroup = new ParaMEDMEM::MPIProcessorGroup(interface,targetprocs) ;
118 // TimeInterpolator * aLinearInterpDEC = new LinearTimeInterpolator( 0.5 ) ;
119 MPIAccessDEC * MyMPIAccessDEC = new MPIAccessDEC( *sourcegroup , *targetgroup ,
121 // Asynchronous , LinearInterp , 0.5 ) ;
122 MyMPIAccessDEC->setTimeInterpolator( LinearTimeInterp , 0.5 ) ;
123 MPIAccess * mpi_access = MyMPIAccessDEC->getMPIAccess() ;
125 cout << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
126 mpi_access->barrier() ;
127 cout << "test_AllToAllvTimeDEC" << myrank << " Barrier done" << endl ;
131 #define datamsglength 10
134 int *sendcounts = new int[size] ;
135 int *sdispls = new int[size] ;
136 int *recvcounts = new int[size] ;
137 int *rdispls = new int[size] ;
138 int *sendtimecounts = new int[size] ;
139 int *stimedispls = new int[size] ;
140 int *recvtimecounts = new int[size] ;
141 int *rtimedispls = new int[size] ;
142 for ( i = 0 ; i < size ; i++ ) {
143 sendcounts[i] = datamsglength-i ;
144 sdispls[i] = i*datamsglength ;
145 recvcounts[i] = datamsglength-myrank ;
146 rdispls[i] = i*datamsglength ;
147 sendtimecounts[i] = 1 ;
149 recvtimecounts[i] = 1 ;
151 //rtimedispls[i] = i*mpi_access->TimeExtent() ;
155 double deltatime[maxproc] = {1.,2.1,3.2,4.3,5.4,6.5,7.6,8.7,9.8,10.9,11.} ;
157 double nextdeltatime = deltatime[myrank] ;
158 if ( UseMPI_Alltoallv ) {
159 maxtime = maxreq*nextdeltatime - 0.1 ;
163 // MyMPIAccessDEC->InitTime( time , nextdeltatime , maxtime ) ;
165 time_t begintime = time(NULL) ;
166 // for ( time = 0 ; time <= maxtime ; time+=deltatime[myrank] ) {
167 for ( timeLoc = 0 ; timeLoc <= maxtime && nextdeltatime != 0 ; timeLoc+=nextdeltatime ) {
168 nextdeltatime = deltatime[myrank] ;
169 if ( timeLoc != 0 ) {
170 nextdeltatime = deltatime[myrank] ;
171 if ( timeLoc+nextdeltatime > maxtime ) {
174 // MyMPIAccessDEC->NextTime( nextdeltatime ) ;
176 MyMPIAccessDEC->setTime( timeLoc , nextdeltatime ) ;
177 cout << "test_AllToAllvTimeDEC" << myrank << "=====TIME " << time << "=====DELTATIME "
178 << nextdeltatime << "=====MAXTIME " << maxtime << " ======" << endl ;
179 int * sendbuf = new int[datamsglength*size] ;
180 // int * sendbuf = (int *) malloc(sizeof(int)*datamsglength*size) ;
181 int * recvbuf = new int[datamsglength*size] ;
183 for ( j = 0 ; j < datamsglength*size ; j++ ) {
184 sendbuf[j] = myrank*1000000 + (j/datamsglength)*1000 + j ;
188 if ( UseMPI_Alltoallv ) {
189 const MPI_Comm* comm = MyMPIAccessDEC->getComm();
190 TimeMessage * aSendTimeMessage = new TimeMessage ;
191 aSendTimeMessage->time = timeLoc ;
192 // aSendTimeMessage->deltatime = deltatime[myrank] ;
193 aSendTimeMessage->deltatime = nextdeltatime ;
194 // aSendTimeMessage->maxtime = maxtime ;
195 aSendTimeMessage->tag = (int ) (timeLoc/deltatime[myrank]) ;
196 TimeMessage * aRecvTimeMessage = new TimeMessage[size] ;
197 interface.allToAllV(aSendTimeMessage, sendtimecounts , stimedispls ,
198 mpi_access->timeType() ,
199 aRecvTimeMessage, recvtimecounts , rtimedispls ,
200 mpi_access->timeType() , *comm ) ;
201 // for ( j = 0 ; j < size ; j++ ) {
202 // cout << "test_AllToAllvTimeDEC" << myrank << " TimeMessage received " << j << " "
203 // << aRecvTimeMessage[j] << endl ;
205 delete aSendTimeMessage ;
206 delete [] aRecvTimeMessage ;
207 interface.allToAllV(sendbuf, sendcounts , sdispls , MPI_INT ,
208 recvbuf, recvcounts , rdispls , MPI_INT , *comm ) ;
213 int sts = MyMPIAccessDEC->allToAllvTime( sendbuf, sendcounts , sdispls , MPI_INT ,
214 recvbuf, recvcounts , rdispls , MPI_INT ) ;
215 chksts( sts , myrank , mpi_access ) ;
218 // cout << "test_AllToAllvTimeDEC" << myrank << " recvbuf before CheckSent" ;
219 // for ( i = 0 ; i < datamsglength*size ; i++ ) {
220 // cout << " " << recvbuf[i] ;
224 // cout << "test_AllToAllvTimeDEC" << myrank << " sendbuf " << sendbuf << endl ;
225 // MyMPIAccessDEC->CheckSent() ;
227 int nRecvReq = mpi_access->recvRequestIdsSize() ;
228 if ( nRecvReq != 0 ) {
229 ostringstream strstream ;
230 strstream << "=============================================================" << endl
231 << "test_AllToAllvTimeDEC" << myrank << " WaitAllRecv " << nRecvReq << " Requests # 0 ERROR"
232 << endl << "============================================================="
234 int *ArrayOfRecvRequests = new int[nRecvReq] ;
235 int nReq = mpi_access->recvRequestIds( nRecvReq, ArrayOfRecvRequests ) ;
236 mpi_access->waitAll( nReq , ArrayOfRecvRequests ) ;
237 delete [] ArrayOfRecvRequests ;
238 cout << strstream.str() << endl ;
239 CPPUNIT_FAIL( strstream.str() ) ;
242 // cout << "test_AllToAllvTimeDEC" << myrank << " check of recvbuf" << endl ;
243 bool badrecvbuf = false ;
244 for ( i = 0 ; i < size ; i++ ) {
246 for ( j = 0 ; j < datamsglength ; j++ ) {
247 int index = i*datamsglength+j ;
248 if ( j < recvcounts[i] ) {
249 if ( recvbuf[index] != (index/datamsglength)*1000000 + myrank*1000 +
250 myrank*datamsglength+(index%datamsglength) ) {
252 cout << "test_AllToAllvTimeDEC" << myrank << " recvbuf[" << index << "] "
253 << recvbuf[index] << " # " << (index/datamsglength)*1000000 +
255 myrank*datamsglength+(index%datamsglength) << endl ;
257 else if ( badrecvbuf ) {
258 cout << "test_AllToAllvTimeDEC" << myrank << " recvbuf[" << index << "] "
259 << recvbuf[index] << " == " << (index/datamsglength)*1000000 +
261 myrank*datamsglength+(index%datamsglength) << endl ;
264 else if ( recvbuf[index] != -1 ) {
266 cout << "test_AllToAllvTimeDEC" << myrank << " recvbuf[" << index << "] "
267 << recvbuf[index] << " # -1" << endl ;
272 ostringstream strstream ;
273 strstream << "==============================================================" << endl
274 << "test_AllToAllvTimeDEC" << myrank << " badrecvbuf"
275 << endl << "============================================================="
277 cout << strstream.str() << endl ;
278 CPPUNIT_FAIL( strstream.str() ) ;
283 cout << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
284 mpi_access->barrier() ;
285 cout << "test_AllToAllvTimeDEC" << myrank << " Barrier done" << endl ;
287 cout << "test_AllToAllvTimeDEC" << myrank << " CheckFinalSent" << endl ;
288 sts = MyMPIAccessDEC->checkFinalSent() ;
289 if ( sts != MPI_SUCCESS ) {
290 ostringstream strstream ;
291 strstream << "================================================================" << endl
292 << "test_AllToAllvTimeDEC" << myrank << " final CheckSent ERROR"
293 << endl << "================================================================"
295 cout << strstream.str() << endl ;
296 CPPUNIT_FAIL( strstream.str() ) ;
299 cout << "test_AllToAllvTimeDEC" << myrank << " CheckFinalRecv" << endl ;
300 sts = MyMPIAccessDEC->checkFinalRecv() ;
301 if ( sts != MPI_SUCCESS ) {
302 ostringstream strstream ;
303 strstream << "================================================================" << endl
304 << "test_AllToAllvTimeDEC" << myrank << " CheckFinalRecv ERROR"
305 << endl << "================================================================"
307 cout << strstream.str() << endl ;
308 CPPUNIT_FAIL( strstream.str() ) ;
311 int nRecvReq = mpi_access->recvRequestIdsSize() ;
313 ostringstream strstream ;
314 strstream << "===============================================================" << endl
315 << "test_AllToAllvTimeDEC" << myrank << " RecvRequestIds " << nRecvReq
316 << " RecvRequests # 0 Error"
317 << endl << "==============================================================="
319 cout << strstream.str() << endl ;
320 CPPUNIT_FAIL( strstream.str() ) ;
323 cout << "test_AllToAllvTimeDEC" << myrank << " RecvRequestIds " << nRecvReq
324 << " RecvRequests = 0 OK" << endl ;
327 time_t endtime = time(NULL) ;
328 cout << "test_AllToAllvTimeDEC" << myrank << " begintime " << begintime << " endtime " << endtime
329 << " elapse " << endtime-begintime << " " << maxtime/deltatime[myrank]
330 << " calls to AllToAll" << endl ;
332 cout << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
333 mpi_access->barrier() ;
334 cout << "test_AllToAllvTimeDEC" << myrank << " Barrier done" << endl ;
338 delete MyMPIAccessDEC ;
339 // delete aLinearInterpDEC ;
341 delete [] sendcounts ;
343 delete [] recvcounts ;
345 delete [] sendtimecounts ;
346 delete [] stimedispls ;
347 delete [] recvtimecounts ;
348 delete [] rtimedispls ;
352 endtime = time(NULL) ;
354 cout << "test_AllToAllvTimeDEC" << myrank << " OK begintime " << begintime << " endtime " << endtime
355 << " elapse " << endtime-begintime << " " << maxtime/deltatime[myrank]
356 << " calls to AllToAll" << endl ;