Salome HOME
Update copyrights
[tools/medcoupling.git] / src / ParaMEDMEMTest / test_AllToAllTimeDEC.cxx
1 // Copyright (C) 2007-2019  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
26 #include "MPIAccessDECTest.hxx"
27 #include <cppunit/TestAssert.h>
28
29 //#include "CommInterface.hxx"
30 //#include "ProcessorGroup.hxx"
31 //#include "MPIProcessorGroup.hxx"
32 #include "MPIAccessDEC.hxx"
33 #include "LinearTimeInterpolator.hxx"
34
35 // use this define to enable lines, execution of which leads to Segmentation Fault
36 #define ENABLE_FAULTS
37
38 // use this define to enable CPPUNIT asserts and fails, showing bugs
39 #define ENABLE_FORCED_FAILURES
40
41 using namespace std;
42 using namespace MEDCoupling;
43
44 void MPIAccessDECTest::test_AllToAllTimeDECSynchronousPointToPoint() {
45   test_AllToAllTimeDEC( false ) ;
46 }
47 void MPIAccessDECTest::test_AllToAllTimeDECAsynchronousPointToPoint() {
48   test_AllToAllTimeDEC( true ) ;
49 }
50
51 static void chksts( int sts , int myrank , MEDCoupling::MPIAccess * mpi_access ) {
52   char msgerr[MPI_MAX_ERROR_STRING] ;
53   int lenerr ;
54   if ( sts != MPI_SUCCESS ) {
55     mpi_access->errorString(sts, msgerr, &lenerr) ;
56     debugStream << "test_AllToAllTimeDEC" << myrank << " lenerr " << lenerr << " "
57          << msgerr << endl ;
58     ostringstream strstream ;
59     strstream << "==========================================================="
60               << "test_AllToAllTimeDEC" << myrank << " KO"
61               << "==========================================================="
62               << endl ;
63     debugStream << strstream.str() << endl ;
64     CPPUNIT_FAIL( strstream.str() ) ;
65   }
66   return ;
67 }
68
69 void MPIAccessDECTest::test_AllToAllTimeDEC( bool Asynchronous ) {
70
71   debugStream << "test_AllToAllTimeDEC" << endl ;
72
73   //  MPI_Init(&argc, &argv) ; 
74
75   int size ;
76   int myrank ;
77   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
78   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
79
80   if ( size < 2 || size > 11 ) {
81     ostringstream strstream ;
82     strstream << "usage :" << endl
83               << "mpirun -np <nbprocs> test_AllToAllTimeDEC" << endl
84               << " (nbprocs >=2)" << endl
85               << "test must be run with more than 1 proc and less than 12 procs"
86               << endl ;
87     cerr << strstream.str() << endl ;
88     CPPUNIT_FAIL( strstream.str() ) ;
89   }
90
91   //  int Asynchronous = atoi(argv[1]);
92
93   debugStream << "test_AllToAllTimeDEC" << myrank << " Asynchronous " << Asynchronous << endl ;
94
95   MEDCoupling::CommInterface interface ;
96   std::set<int> sourceprocs;
97   std::set<int> targetprocs;
98   int i ;
99   for ( i = 0 ; i < size/2 ; i++ ) {
100     sourceprocs.insert(i);
101   }
102   for ( i = size/2 ; i < size ; i++ ) {
103     targetprocs.insert(i);
104   }
105
106   MEDCoupling::MPIProcessorGroup* sourcegroup = new MEDCoupling::MPIProcessorGroup(interface,sourceprocs) ;
107   MEDCoupling::MPIProcessorGroup* targetgroup = new MEDCoupling::MPIProcessorGroup(interface,targetprocs) ;
108
109   //  LinearTimeInterpolator * aLinearInterpDEC = new LinearTimeInterpolator( 0.5 ) ;
110   MPIAccessDEC * MyMPIAccessDEC = new MPIAccessDEC( *sourcegroup , *targetgroup ,
111                                                     Asynchronous ) ;
112   //                                                    Asynchronous , LinearInterp , 0.5 ) ;
113   MyMPIAccessDEC->setTimeInterpolator( LinearTimeInterp ) ;
114   MPIAccess * mpi_access = MyMPIAccessDEC->getMPIAccess() ;
115
116   debugStream << "test_AllToAllTimeDEC" << myrank << " Barrier :" << endl ;
117   mpi_access->barrier() ;
118   debugStream << "test_AllToAllTimeDEC" << myrank << " Barrier done" << endl ;
119   
120 #define maxproc 11
121 #define maxreq 10000
122 #define datamsglength 10
123
124   int sts ;
125   int sendcount = datamsglength ;
126   int recvcount = datamsglength ;
127
128   double time = 0 ;
129   //  double deltatime[maxproc] = {1.,2.1,3.2,4.3,5.4,6.5,7.6,8.7,9.8,10.9,11.} ;
130   double deltatime[maxproc] = {1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.} ;
131   double maxtime = maxreq ;
132   double nextdeltatime = deltatime[myrank] ;
133   //  MyMPIAccessDEC->InitTime( time , deltatime[myrank] , maxtime ) ;
134   //  for ( time = 0 ; time <= maxtime ; time+=deltatime[myrank] ) {
135   for ( time = 0 ; time <= maxtime && nextdeltatime != 0 ; time+=nextdeltatime ) {
136     if ( time != 0 ) {
137       nextdeltatime = deltatime[myrank] ;
138       if ( time+nextdeltatime > maxtime ) {
139         nextdeltatime = 0 ;
140       }
141       //       MyMPIAccessDEC->NextTime( nextdeltatime ) ;
142     }
143     MyMPIAccessDEC->setTime( time , nextdeltatime ) ;
144     debugStream << "test_AllToAllTimeDEC" << myrank << "=====TIME " << time << "=====DELTATIME "
145          << nextdeltatime << "=====MAXTIME " << maxtime << " ======" << endl ; 
146     int * sendbuf = new int[datamsglength*size] ;
147     //     int * sendbuf = (int *) malloc(sizeof(int)*datamsglength*size) ;
148     int * recvbuf = new int[datamsglength*size] ;
149     int j ;
150     for ( j = 0 ; j < datamsglength*size ; j++ ) {
151       sendbuf[j] = myrank*1000000 + (j/datamsglength)*1000 + j ;
152       recvbuf[j] = -1 ;
153     }
154
155     int sts = MyMPIAccessDEC->allToAllTime( sendbuf, sendcount , MPI_INT ,
156                                             recvbuf, recvcount , MPI_INT ) ;
157     chksts( sts , myrank , mpi_access ) ;
158
159     //     debugStream << "test_AllToAllTimeDEC" << myrank << " recvbuf before CheckSent" ;
160     //     for ( i = 0 ; i < datamsglength*size ; i++ ) {
161     //        debugStream << " " << recvbuf[i] ;
162     //     }
163     //     debugStream << endl ;
164
165     //     debugStream << "test_AllToAllTimeDEC" << myrank << " sendbuf " << sendbuf << endl ;
166     //     MyMPIAccessDEC->CheckSent() ;
167
168     int nRecvReq = mpi_access->recvRequestIdsSize() ;
169     if ( nRecvReq != 0 ) {
170       ostringstream strstream ;
171       strstream << "=============================================================" << endl
172                 << "test_AllToAllTimeDEC" << myrank << " WaitAllRecv " << nRecvReq << " Requests # 0 ERROR"
173                 << endl << "============================================================="
174                 << endl ;
175       int *ArrayOfRecvRequests = new int[nRecvReq] ;
176       int nReq = mpi_access->recvRequestIds( nRecvReq, ArrayOfRecvRequests ) ;
177       mpi_access->waitAll( nReq , ArrayOfRecvRequests ) ;
178       delete [] ArrayOfRecvRequests ;
179       debugStream << strstream.str() << endl ;
180       CPPUNIT_FAIL( strstream.str() ) ;
181     }
182
183     //     debugStream << "test_AllToAllTimeDEC" << myrank << " recvbuf" << endl ;
184     bool badrecvbuf = false ;
185     for ( i = 0 ; i < datamsglength*size ; i++ ) {
186       if ( recvbuf[i] != (i/datamsglength)*1000000 + myrank*1000 +
187            myrank*datamsglength+(i%datamsglength) ) {
188         badrecvbuf = true ;
189         debugStream << "test_AllToAllTimeDEC" << myrank << " recvbuf[" << i << "] "
190              << recvbuf[i] << " # " << (i/datamsglength)*1000000 + myrank*1000 +
191           myrank*datamsglength+(i%datamsglength) << endl ;
192       }
193       else if ( badrecvbuf ) {
194         debugStream << "test_AllToAllTimeDEC" << myrank << " recvbuf[" << i << "] "
195              << recvbuf[i] << " == " << (i/datamsglength)*1000000 + myrank*1000 +
196           myrank*datamsglength+(i%datamsglength) << endl ;
197       }
198     }
199     if ( badrecvbuf ) {
200       ostringstream strstream ;
201       strstream << "==============================================================" << endl
202                 << "test_AllToAllTimeDEC" << myrank << " badrecvbuf"
203                 << endl << "============================================================="
204                 << endl ;
205       debugStream << strstream.str() << endl ;
206       CPPUNIT_FAIL( strstream.str() ) ;
207     }
208     delete [] recvbuf ;
209   }
210
211   debugStream << "test_AllToAllTimeDEC" << myrank << " final CheckSent" << endl ;
212   sts = MyMPIAccessDEC->checkSent() ;
213   if ( sts != MPI_SUCCESS ) {
214     ostringstream strstream ;
215     strstream << "================================================================" << endl
216               << "test_AllToAllTimeDEC" << myrank << " final CheckSent ERROR"
217               << endl << "================================================================"
218               << endl ;
219     debugStream << strstream.str() << endl ;
220     CPPUNIT_FAIL( strstream.str() ) ;
221   }
222
223   int nSendReq = mpi_access->sendRequestIdsSize() ;
224   debugStream << "test_AllToAllTimeDEC" << myrank << " final SendRequestIds " << nSendReq << " SendRequests"
225        << endl ;
226   if ( nSendReq ) {
227     int *ArrayOfSendRequests = new int[nSendReq] ;
228     int nReq = mpi_access->sendRequestIds( nSendReq, ArrayOfSendRequests ) ;
229     mpi_access->waitAll( nReq , ArrayOfSendRequests ) ;
230     delete [] ArrayOfSendRequests ;
231   }
232
233   int nRecvReq = mpi_access->recvRequestIdsSize() ;
234   if ( nRecvReq ) {
235     ostringstream strstream ;
236     strstream << "===============================================================" << endl
237               << "test_AllToAllTimeDEC" << myrank << " RecvRequestIds " << nRecvReq
238               << " RecvRequests # 0 Error"
239               << endl << "==============================================================="
240               << endl ;
241     debugStream << strstream.str() << endl ;
242     CPPUNIT_FAIL( strstream.str() ) ;
243   }
244   else {
245     debugStream << "test_AllToAllTimeDEC" << myrank << " RecvRequestIds " << nRecvReq
246          << " RecvRequests = 0 OK" << endl ;
247   }
248
249   debugStream << "test_AllToAllTimeDEC" << myrank << " Barrier :" << endl ;
250   mpi_access->barrier() ;
251   debugStream << "test_AllToAllTimeDEC" << myrank << " Barrier done" << endl ;
252
253   delete sourcegroup ;
254   delete targetgroup ;
255   //  delete aLinearInterpDEC ;
256   delete MyMPIAccessDEC ;
257
258   //  MPI_Finalize();
259
260   debugStream << "test_AllToAllTimeDEC" << myrank << " OK" << endl ;
261
262   return ;
263 }
264
265
266
267