Salome HOME
c5e38b9f6e02cbf586819be45ffbf7adf4e38241
[tools/medcoupling.git] / src / ParaMEDMEMTest / MPIAccess / test_AllToAllvTimeDEC.cxx
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <string>
21 #include <vector>
22 #include <map>
23 #include <iostream>
24 #include <mpi.h>
25 #include <ctime>
26
27 #include "MPIAccessDECTest.hxx"
28 #include <cppunit/TestAssert.h>
29
30 #include "../ParaMEDMEM/MPIAccess/MPIAccessDEC.hxx"
31 //#include "CommInterface.hxx"
32 //#include "ProcessorGroup.hxx"
33 //#include "MPIProcessorGroup.hxx"
34 #include "LinearTimeInterpolator.hxx"
35
36 // use this define to enable lines, execution of which leads to Segmentation Fault
37 #define ENABLE_FAULTS
38
39 // use this define to enable CPPUNIT asserts and fails, showing bugs
40 #define ENABLE_FORCED_FAILURES
41
42 using namespace std;
43 using namespace MEDCoupling;
44
45 void MPIAccessDECTest::test_AllToAllvTimeDECSynchronousNative() {
46   test_AllToAllvTimeDEC( false , true ) ;
47 }
48 void MPIAccessDECTest::test_AllToAllvTimeDECSynchronousPointToPoint() {
49   test_AllToAllvTimeDEC( false , false ) ;
50 }
51 void MPIAccessDECTest::test_AllToAllvTimeDECAsynchronousPointToPoint() {
52   test_AllToAllvTimeDEC( true , false ) ;
53 }
54
55 static void chksts( int sts , int myrank , MEDCoupling::MPIAccess * mpi_access ) {
56   char msgerr[MPI_MAX_ERROR_STRING] ;
57   int lenerr ;
58   if ( sts != MPI_SUCCESS ) {
59     mpi_access->errorString(sts, msgerr, &lenerr) ;
60     debugStream << "test_AllToAllvTimeDEC" << myrank << " lenerr " << lenerr << " "
61          << msgerr << endl ;
62     ostringstream strstream ;
63     strstream << "==========================================================="
64               << "test_AllToAllvTimeDEC" << myrank << " KO"
65               << "==========================================================="
66               << endl ;
67     debugStream << strstream.str() << endl ;
68     CPPUNIT_FAIL( strstream.str() ) ;
69   }
70   return ;
71 }
72
73 void MPIAccessDECTest::test_AllToAllvTimeDEC( bool Asynchronous , bool UseMPINative ) {
74
75   debugStream << "test_AllToAllvTimeDEC" << endl ;
76
77   //  MPI_Init(&argc, &argv) ; 
78
79   int size ;
80   int myrank ;
81   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
82   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
83
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"
90               << endl ;
91     cerr << strstream.str() << endl ;
92     CPPUNIT_FAIL( strstream.str() ) ;
93   }
94
95   //  int Asynchronous = atoi(argv[1]) ;
96   int UseMPI_Alltoallv = UseMPINative ;
97   //  if ( argc == 3 ) {
98   //    UseMPI_Alltoallv = atoi(argv[2]) ;
99   //  }
100
101   debugStream << "test_AllToAllvTimeDEC" << myrank << " Asynchronous " << Asynchronous
102        << " UseMPI_Alltoallv " << UseMPI_Alltoallv << endl ;
103
104   MEDCoupling::CommInterface interface ;
105   std::set<int> sourceprocs;
106   std::set<int> targetprocs;
107   int i ;
108   for ( i = 0 ; i < size/2 ; i++ ) {
109     sourceprocs.insert(i);
110   }
111   for ( i = size/2 ; i < size ; i++ ) {
112     targetprocs.insert(i);
113   }
114
115   MEDCoupling::MPIProcessorGroup* sourcegroup = new MEDCoupling::MPIProcessorGroup(interface,sourceprocs) ;
116   MEDCoupling::MPIProcessorGroup* targetgroup = new MEDCoupling::MPIProcessorGroup(interface,targetprocs) ;
117
118   //  TimeInterpolator * aLinearInterpDEC = new LinearTimeInterpolator( 0.5 ) ;
119   MPIAccessDEC * MyMPIAccessDEC = new MPIAccessDEC( *sourcegroup , *targetgroup ,
120                                                     Asynchronous ) ;
121   //                                                    Asynchronous , LinearInterp , 0.5 ) ;
122   MyMPIAccessDEC->setTimeInterpolator( LinearTimeInterp , 0.5 ) ;
123   MPIAccess * mpi_access = MyMPIAccessDEC->getMPIAccess() ;
124
125   debugStream << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
126   mpi_access->barrier() ;
127   debugStream << "test_AllToAllvTimeDEC" << myrank << " Barrier done" << endl ;
128
129 #define maxproc 11
130 #define maxreq 10000
131 #define datamsglength 10
132
133   int sts ;
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 ;
148     stimedispls[i] = 0 ;
149     recvtimecounts[i] = 1 ;
150     rtimedispls[i] = i ;
151     //rtimedispls[i] = i*mpi_access->TimeExtent() ;
152   }
153
154   double timeLoc = 0 ;
155   double deltatime[maxproc] = {1.,2.1,3.2,4.3,5.4,6.5,7.6,8.7,9.8,10.9,11.} ;
156   double maxtime ;
157   double nextdeltatime = deltatime[myrank] ;
158   if ( UseMPI_Alltoallv ) {
159     maxtime = maxreq*nextdeltatime - 0.1 ;
160   }
161   else {
162     maxtime = maxreq ;
163     //    MyMPIAccessDEC->InitTime( time , nextdeltatime , maxtime ) ;
164   }
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 ) {
172         nextdeltatime = 0 ;
173       }
174       //       MyMPIAccessDEC->NextTime( nextdeltatime ) ;
175     }
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] ;
182     int j ;
183     for ( j = 0 ; j < datamsglength*size ; j++ ) {
184       sendbuf[j] = myrank*1000000 + (j/datamsglength)*1000 + j ;
185       recvbuf[j] = -1 ;
186     }
187
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 ;
204       //       }
205       delete aSendTimeMessage ;
206       delete [] aRecvTimeMessage ;
207       interface.allToAllV(sendbuf, sendcounts , sdispls , MPI_INT ,
208                           recvbuf, recvcounts , rdispls , MPI_INT , *comm ) ;
209       //       free(sendbuf) ;
210       delete [] sendbuf ;
211     }
212     else {
213       int sts2 = MyMPIAccessDEC->allToAllvTime( sendbuf, sendcounts , sdispls , MPI_INT ,
214                                                recvbuf, recvcounts , rdispls , MPI_INT ) ;
215       chksts( sts2 , myrank , mpi_access ) ;
216     }
217
218     //     debugStream << "test_AllToAllvTimeDEC" << myrank << " recvbuf before CheckSent" ;
219     //     for ( i = 0 ; i < datamsglength*size ; i++ ) {
220     //        debugStream << " " << recvbuf[i] ;
221     //     }
222     //     debugStream << endl ;
223
224     //     debugStream << "test_AllToAllvTimeDEC" << myrank << " sendbuf " << sendbuf << endl ;
225     //     MyMPIAccessDEC->CheckSent() ;
226
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 << "============================================================="
233                 << 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() ) ;
240     }
241
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) ) {
250             badrecvbuf = true ;
251             debugStream << "test_AllToAllvTimeDEC" << myrank << " recvbuf[" << index << "] "
252                  << recvbuf[index] << " # " << (index/datamsglength)*1000000 +
253               myrank*1000 +
254               myrank*datamsglength+(index%datamsglength) << endl ;
255           }
256           else if ( badrecvbuf ) {
257             debugStream << "test_AllToAllvTimeDEC" << myrank << " recvbuf[" << index << "] "
258                  << recvbuf[index] << " == " << (index/datamsglength)*1000000 +
259               myrank*1000 +
260               myrank*datamsglength+(index%datamsglength) << endl ;
261           }
262         }
263         else if ( recvbuf[index] != -1 ) {
264           badrecvbuf = true ;
265           debugStream << "test_AllToAllvTimeDEC" << myrank << " recvbuf[" << index << "] "
266                << recvbuf[index] << " # -1" << endl ;
267         }
268       }
269     }
270     if ( badrecvbuf ) {
271       ostringstream strstream ;
272       strstream << "==============================================================" << endl
273                 << "test_AllToAllvTimeDEC" << myrank << " badrecvbuf"
274                 << endl << "============================================================="
275                 << endl ;
276       debugStream << strstream.str() << endl ;
277       CPPUNIT_FAIL( strstream.str() ) ;
278     }
279     delete [] recvbuf ;
280   }
281
282   debugStream << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
283   mpi_access->barrier() ;
284   debugStream << "test_AllToAllvTimeDEC" << myrank << " Barrier done" << endl ;
285
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 << "================================================================"
293               << endl ;
294     debugStream << strstream.str() << endl ;
295     CPPUNIT_FAIL( strstream.str() ) ;
296   }
297
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 << "================================================================"
305               << endl ;
306     debugStream << strstream.str() << endl ;
307     CPPUNIT_FAIL( strstream.str() ) ;
308   }
309
310   int nRecvReq = mpi_access->recvRequestIdsSize() ;
311   if ( nRecvReq ) {
312     ostringstream strstream ;
313     strstream << "===============================================================" << endl
314               << "test_AllToAllvTimeDEC" << myrank << " RecvRequestIds " << nRecvReq
315               << " RecvRequests # 0 Error"
316               << endl << "==============================================================="
317               << endl ;
318     debugStream << strstream.str() << endl ;
319     CPPUNIT_FAIL( strstream.str() ) ;
320   }
321   else {
322     debugStream << "test_AllToAllvTimeDEC" << myrank << " RecvRequestIds " << nRecvReq
323          << " RecvRequests = 0 OK" << endl ;
324   }
325
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 ;
330
331   debugStream << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
332   mpi_access->barrier() ;
333   debugStream << "test_AllToAllvTimeDEC" << myrank << " Barrier done" << endl ;
334
335   delete sourcegroup ;
336   delete targetgroup ;
337   delete MyMPIAccessDEC ;
338   //  delete aLinearInterpDEC ;
339
340   delete [] sendcounts ;
341   delete [] sdispls ;
342   delete [] recvcounts ;
343   delete [] rdispls ;
344   delete [] sendtimecounts ;
345   delete [] stimedispls ;
346   delete [] recvtimecounts ;
347   delete [] rtimedispls ;
348
349   //  MPI_Finalize();
350
351   endtime = time(NULL) ;
352
353   debugStream << "test_AllToAllvTimeDEC" << myrank << " OK begintime " << begintime << " endtime " << endtime
354        << " elapse " << endtime-begintime << " " << maxtime/deltatime[myrank]
355        << " calls to AllToAll" << endl ;
356
357   return ;
358 }
359
360
361
362