1 // Copyright (C) 2007-2020 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, or (at your option) any later version.
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 MEDCoupling;
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 , MEDCoupling::MPIAccess * mpi_access ) {
56 char msgerr[MPI_MAX_ERROR_STRING] ;
58 if ( sts != MPI_SUCCESS ) {
59 mpi_access->errorString(sts, msgerr, &lenerr) ;
60 debugStream << "test_AllToAllvTimeDEC" << myrank << " lenerr " << lenerr << " "
62 ostringstream strstream ;
63 strstream << "==========================================================="
64 << "test_AllToAllvTimeDEC" << myrank << " KO"
65 << "==========================================================="
67 debugStream << strstream.str() << endl ;
68 CPPUNIT_FAIL( strstream.str() ) ;
73 void MPIAccessDECTest::test_AllToAllvTimeDEC( bool Asynchronous , bool UseMPINative ) {
75 debugStream << "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 run with more than 1 proc and less than 12 procs"
91 cerr << 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 debugStream << "test_AllToAllvTimeDEC" << myrank << " Asynchronous " << Asynchronous
102 << " UseMPI_Alltoallv " << UseMPI_Alltoallv << endl ;
104 MEDCoupling::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 MEDCoupling::MPIProcessorGroup* sourcegroup = new MEDCoupling::MPIProcessorGroup(interface,sourceprocs) ;
116 MEDCoupling::MPIProcessorGroup* targetgroup = new MEDCoupling::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 debugStream << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
126 mpi_access->barrier() ;
127 debugStream << "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 debugStream << "test_AllToAllvTimeDEC" << myrank << "=====TIME " << timeLoc << "=====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 // debugStream << "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 sts2 = MyMPIAccessDEC->allToAllvTime( sendbuf, sendcounts , sdispls , MPI_INT ,
214 recvbuf, recvcounts , rdispls , MPI_INT ) ;
215 chksts( sts2 , myrank , mpi_access ) ;
218 // debugStream << "test_AllToAllvTimeDEC" << myrank << " recvbuf before CheckSent" ;
219 // for ( i = 0 ; i < datamsglength*size ; i++ ) {
220 // debugStream << " " << recvbuf[i] ;
222 // debugStream << endl ;
224 // debugStream << "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 debugStream << strstream.str() << endl ;
239 CPPUNIT_FAIL( strstream.str() ) ;
242 // debugStream << "test_AllToAllvTimeDEC" << myrank << " check of recvbuf" << endl ;
243 bool badrecvbuf = false ;
244 for ( i = 0 ; i < size ; i++ ) {
245 for ( int jj = 0 ; jj < datamsglength ; jj++ ) {
246 int index = i*datamsglength+jj ;
247 if ( jj < recvcounts[i] ) {
248 if ( recvbuf[index] != (index/datamsglength)*1000000 + myrank*1000 +
249 myrank*datamsglength+(index%datamsglength) ) {
251 debugStream << "test_AllToAllvTimeDEC" << myrank << " recvbuf[" << index << "] "
252 << recvbuf[index] << " # " << (index/datamsglength)*1000000 +
254 myrank*datamsglength+(index%datamsglength) << endl ;
256 else if ( badrecvbuf ) {
257 debugStream << "test_AllToAllvTimeDEC" << myrank << " recvbuf[" << index << "] "
258 << recvbuf[index] << " == " << (index/datamsglength)*1000000 +
260 myrank*datamsglength+(index%datamsglength) << endl ;
263 else if ( recvbuf[index] != -1 ) {
265 debugStream << "test_AllToAllvTimeDEC" << myrank << " recvbuf[" << index << "] "
266 << recvbuf[index] << " # -1" << endl ;
271 ostringstream strstream ;
272 strstream << "==============================================================" << endl
273 << "test_AllToAllvTimeDEC" << myrank << " badrecvbuf"
274 << endl << "============================================================="
276 debugStream << strstream.str() << endl ;
277 CPPUNIT_FAIL( strstream.str() ) ;
282 debugStream << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
283 mpi_access->barrier() ;
284 debugStream << "test_AllToAllvTimeDEC" << myrank << " Barrier done" << endl ;
286 debugStream << "test_AllToAllvTimeDEC" << myrank << " CheckFinalSent" << endl ;
287 sts = MyMPIAccessDEC->checkFinalSent() ;
288 if ( sts != MPI_SUCCESS ) {
289 ostringstream strstream ;
290 strstream << "================================================================" << endl
291 << "test_AllToAllvTimeDEC" << myrank << " final CheckSent ERROR"
292 << endl << "================================================================"
294 debugStream << strstream.str() << endl ;
295 CPPUNIT_FAIL( strstream.str() ) ;
298 debugStream << "test_AllToAllvTimeDEC" << myrank << " CheckFinalRecv" << endl ;
299 sts = MyMPIAccessDEC->checkFinalRecv() ;
300 if ( sts != MPI_SUCCESS ) {
301 ostringstream strstream ;
302 strstream << "================================================================" << endl
303 << "test_AllToAllvTimeDEC" << myrank << " CheckFinalRecv ERROR"
304 << endl << "================================================================"
306 debugStream << strstream.str() << endl ;
307 CPPUNIT_FAIL( strstream.str() ) ;
310 int nRecvReq = mpi_access->recvRequestIdsSize() ;
312 ostringstream strstream ;
313 strstream << "===============================================================" << endl
314 << "test_AllToAllvTimeDEC" << myrank << " RecvRequestIds " << nRecvReq
315 << " RecvRequests # 0 Error"
316 << endl << "==============================================================="
318 debugStream << strstream.str() << endl ;
319 CPPUNIT_FAIL( strstream.str() ) ;
322 debugStream << "test_AllToAllvTimeDEC" << myrank << " RecvRequestIds " << nRecvReq
323 << " RecvRequests = 0 OK" << endl ;
326 time_t endtime = time(NULL) ;
327 debugStream << "test_AllToAllvTimeDEC" << myrank << " begintime " << begintime << " endtime " << endtime
328 << " elapse " << endtime-begintime << " " << maxtime/deltatime[myrank]
329 << " calls to AllToAll" << endl ;
331 debugStream << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
332 mpi_access->barrier() ;
333 debugStream << "test_AllToAllvTimeDEC" << myrank << " Barrier done" << endl ;
337 delete MyMPIAccessDEC ;
338 // delete aLinearInterpDEC ;
340 delete [] sendcounts ;
342 delete [] recvcounts ;
344 delete [] sendtimecounts ;
345 delete [] stimedispls ;
346 delete [] recvtimecounts ;
347 delete [] rtimedispls ;
351 endtime = time(NULL) ;
353 debugStream << "test_AllToAllvTimeDEC" << myrank << " OK begintime " << begintime << " endtime " << endtime
354 << " elapse " << endtime-begintime << " " << maxtime/deltatime[myrank]
355 << " calls to AllToAll" << endl ;