]> SALOME platform Git repositories - tools/medcoupling.git/blob - src/ParaMEDMEM/Test/test_AllToAllvTimeDEC.cxx
Salome HOME
Merge from BR_V5_DEV 16Feb09
[tools/medcoupling.git] / src / ParaMEDMEM / Test / test_AllToAllvTimeDEC.cxx
1 //  Copyright (C) 2007-2008  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.
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 #include <string>
20 #include <vector>
21 #include <map>
22 #include <iostream>
23 #include <mpi.h>
24 #include <time.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 ParaMEDMEM;
43
44 void MPIAccessDECTest::test_AllToAllvTimeDECSynchronousNative() {
45   test_AllToAllvTimeDEC( false , true ) ;
46 }
47 void MPIAccessDECTest::test_AllToAllvTimeDECSynchronousPointToPoint() {
48   test_AllToAllvTimeDEC( false , false ) ;
49 }
50 void MPIAccessDECTest::test_AllToAllvTimeDECAsynchronousPointToPoint() {
51   test_AllToAllvTimeDEC( true , false ) ;
52 }
53
54 static void chksts( int sts , int myrank , ParaMEDMEM::MPIAccess * mpi_access ) {
55   char msgerr[MPI_MAX_ERROR_STRING] ;
56   int lenerr ;
57   if ( sts != MPI_SUCCESS ) {
58     mpi_access->errorString(sts, msgerr, &lenerr) ;
59     cout << "test_AllToAllvTimeDEC" << myrank << " lenerr " << lenerr << " "
60          << msgerr << endl ;
61     ostringstream strstream ;
62     strstream << "==========================================================="
63               << "test_AllToAllvTimeDEC" << myrank << " KO"
64               << "==========================================================="
65               << endl ;
66     cout << strstream.str() << endl ;
67     CPPUNIT_FAIL( strstream.str() ) ;
68   }
69   return ;
70 }
71
72 void MPIAccessDECTest::test_AllToAllvTimeDEC( bool Asynchronous , bool UseMPINative ) {
73
74   cout << "test_AllToAllvTimeDEC" << endl ;
75
76   //  MPI_Init(&argc, &argv) ; 
77
78   int size ;
79   int myrank ;
80   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
81   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
82
83   if ( size < 2 || size > 11 ) {
84     ostringstream strstream ;
85     strstream << "usage :" << endl
86               << "mpirun -np <nbprocs> test_AllToAllTimeDEC" << endl
87               << " (nbprocs >=2)" << endl
88               << "test must be runned with more than 1 proc and less than 12 procs"
89               << endl ;
90     cout << strstream.str() << endl ;
91     CPPUNIT_FAIL( strstream.str() ) ;
92   }
93
94   //  int Asynchronous = atoi(argv[1]) ;
95   int UseMPI_Alltoallv = UseMPINative ;
96   //  if ( argc == 3 ) {
97   //    UseMPI_Alltoallv = atoi(argv[2]) ;
98   //  }
99
100   cout << "test_AllToAllvTimeDEC" << myrank << " Asynchronous " << Asynchronous
101        << " UseMPI_Alltoallv " << UseMPI_Alltoallv << endl ;
102
103   ParaMEDMEM::CommInterface interface ;
104   std::set<int> sourceprocs;
105   std::set<int> targetprocs;
106   int i ;
107   for ( i = 0 ; i < size/2 ; i++ ) {
108     sourceprocs.insert(i);
109   }
110   for ( i = size/2 ; i < size ; i++ ) {
111     targetprocs.insert(i);
112   }
113
114   ParaMEDMEM::MPIProcessorGroup* sourcegroup = new ParaMEDMEM::MPIProcessorGroup(interface,sourceprocs) ;
115   ParaMEDMEM::MPIProcessorGroup* targetgroup = new ParaMEDMEM::MPIProcessorGroup(interface,targetprocs) ;
116
117   //  TimeInterpolator * aLinearInterpDEC = new LinearTimeInterpolator( 0.5 ) ;
118   MPIAccessDEC * MyMPIAccessDEC = new MPIAccessDEC( *sourcegroup , *targetgroup ,
119                                                     Asynchronous ) ;
120   //                                                    Asynchronous , LinearInterp , 0.5 ) ;
121   MyMPIAccessDEC->setTimeInterpolator( LinearTimeInterp , 0.5 ) ;
122   MPIAccess * mpi_access = MyMPIAccessDEC->getMPIAccess() ;
123
124   cout << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
125   mpi_access->barrier() ;
126   cout << "test_AllToAllvTimeDEC" << myrank << " Barrier done" << endl ;
127
128 #define maxproc 11
129 #define maxreq 10000
130 #define datamsglength 10
131
132   int sts ;
133   int *sendcounts = new int[size] ;
134   int *sdispls = new int[size] ;
135   int *recvcounts = new int[size] ;
136   int *rdispls = new int[size] ;
137   int *sendtimecounts = new int[size] ;
138   int *stimedispls = new int[size] ;
139   int *recvtimecounts = new int[size] ;
140   int *rtimedispls = new int[size] ;
141   for ( i = 0 ; i < size ; i++ ) {
142     sendcounts[i] = datamsglength-i ;
143     sdispls[i] = i*datamsglength ;
144     recvcounts[i] = datamsglength-myrank ;
145     rdispls[i] = i*datamsglength ;
146     sendtimecounts[i] = 1 ;
147     stimedispls[i] = 0 ;
148     recvtimecounts[i] = 1 ;
149     rtimedispls[i] = i ;
150     //rtimedispls[i] = i*mpi_access->TimeExtent() ;
151   }
152
153   double time = 0 ;
154   double deltatime[maxproc] = {1.,2.1,3.2,4.3,5.4,6.5,7.6,8.7,9.8,10.9,11.} ;
155   double maxtime ;
156   double nextdeltatime = deltatime[myrank] ;
157   if ( UseMPI_Alltoallv ) {
158     maxtime = maxreq*nextdeltatime - 0.1 ;
159   }
160   else {
161     maxtime = maxreq ;
162     //    MyMPIAccessDEC->InitTime( time , nextdeltatime , maxtime ) ;
163   }
164   time_t begintime = std::time(NULL) ;
165   //  for ( time = 0 ; time <= maxtime ; time+=deltatime[myrank] ) {
166   for ( time = 0 ; time <= maxtime && nextdeltatime != 0 ; time+=nextdeltatime ) {
167     nextdeltatime = deltatime[myrank] ;
168     if ( time != 0 ) {
169       nextdeltatime = deltatime[myrank] ;
170       if ( time+nextdeltatime > maxtime ) {
171         nextdeltatime = 0 ;
172       }
173       //       MyMPIAccessDEC->NextTime( nextdeltatime ) ;
174     }
175     MyMPIAccessDEC->setTime( time , nextdeltatime ) ;
176     cout << "test_AllToAllvTimeDEC" << myrank << "=====TIME " << time << "=====DELTATIME "
177          << nextdeltatime << "=====MAXTIME " << maxtime << " ======" << endl ; 
178     int * sendbuf = new int[datamsglength*size] ;
179     //     int * sendbuf = (int *) malloc(sizeof(int)*datamsglength*size) ;
180     int * recvbuf = new int[datamsglength*size] ;
181     int j ;
182     for ( j = 0 ; j < datamsglength*size ; j++ ) {
183       sendbuf[j] = myrank*1000000 + (j/datamsglength)*1000 + j ;
184       recvbuf[j] = -1 ;
185     }
186
187     if ( UseMPI_Alltoallv ) {
188       const MPI_Comm* comm = MyMPIAccessDEC->getComm();
189       TimeMessage * aSendTimeMessage = new TimeMessage ;
190       aSendTimeMessage->time = time ;
191       //       aSendTimeMessage->deltatime = deltatime[myrank] ;
192       aSendTimeMessage->deltatime = nextdeltatime ;
193       //       aSendTimeMessage->maxtime = maxtime ;
194       aSendTimeMessage->tag = (int ) (time/deltatime[myrank]) ;
195       TimeMessage * aRecvTimeMessage = new TimeMessage[size] ;
196       interface.allToAllV(aSendTimeMessage, sendtimecounts , stimedispls ,
197                           mpi_access->timeType() ,
198                           aRecvTimeMessage, recvtimecounts , rtimedispls ,
199                           mpi_access->timeType() , *comm ) ;
200       //       for ( j = 0 ; j < size ; j++ ) {
201       //          cout << "test_AllToAllvTimeDEC" << myrank << " TimeMessage received " << j << " "
202       //               << aRecvTimeMessage[j] << endl ;
203       //       }
204       delete aSendTimeMessage ;
205       delete [] aRecvTimeMessage ;
206       interface.allToAllV(sendbuf, sendcounts , sdispls , MPI_INT ,
207                           recvbuf, recvcounts , rdispls , MPI_INT , *comm ) ;
208       //       free(sendbuf) ;
209       delete [] sendbuf ;
210     }
211     else {
212       int sts = MyMPIAccessDEC->allToAllvTime( sendbuf, sendcounts , sdispls , MPI_INT ,
213                                                recvbuf, recvcounts , rdispls , MPI_INT ) ;
214       chksts( sts , myrank , mpi_access ) ;
215     }
216
217     //     cout << "test_AllToAllvTimeDEC" << myrank << " recvbuf before CheckSent" ;
218     //     for ( i = 0 ; i < datamsglength*size ; i++ ) {
219     //        cout << " " << recvbuf[i] ;
220     //     }
221     //     cout << endl ;
222
223     //     cout << "test_AllToAllvTimeDEC" << myrank << " sendbuf " << sendbuf << endl ;
224     //     MyMPIAccessDEC->CheckSent() ;
225
226     int nRecvReq = mpi_access->recvRequestIdsSize() ;
227     if ( nRecvReq != 0 ) {
228       ostringstream strstream ;
229       strstream << "=============================================================" << endl
230                 << "test_AllToAllvTimeDEC" << myrank << " WaitAllRecv " << nRecvReq << " Requests # 0 ERROR"
231                 << endl << "============================================================="
232                 << endl ;
233       int *ArrayOfRecvRequests = new int[nRecvReq] ;
234       int nReq = mpi_access->recvRequestIds( nRecvReq, ArrayOfRecvRequests ) ;
235       mpi_access->waitAll( nReq , ArrayOfRecvRequests ) ;
236       delete [] ArrayOfRecvRequests ;
237       cout << strstream.str() << endl ;
238       CPPUNIT_FAIL( strstream.str() ) ;
239     }
240
241     //     cout << "test_AllToAllvTimeDEC" << myrank << " check of recvbuf" << endl ;
242     bool badrecvbuf = false ;
243     for ( i = 0 ; i < size ; i++ ) {
244       int j ;
245       for ( j = 0 ; j < datamsglength ; j++ ) {
246         int index = i*datamsglength+j ;
247         if ( j < recvcounts[i] ) {
248           if ( recvbuf[index] != (index/datamsglength)*1000000 + myrank*1000 +
249                myrank*datamsglength+(index%datamsglength) ) {
250             badrecvbuf = true ;
251             cout << "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             cout << "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           cout << "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       cout << strstream.str() << endl ;
277       CPPUNIT_FAIL( strstream.str() ) ;
278     }
279     delete [] recvbuf ;
280   }
281
282   cout << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
283   mpi_access->barrier() ;
284   cout << "test_AllToAllvTimeDEC" << myrank << " Barrier done" << endl ;
285
286   cout << "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     cout << strstream.str() << endl ;
295     CPPUNIT_FAIL( strstream.str() ) ;
296   }
297
298   cout << "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     cout << 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     cout << strstream.str() << endl ;
319     CPPUNIT_FAIL( strstream.str() ) ;
320   }
321   else {
322     cout << "test_AllToAllvTimeDEC" << myrank << " RecvRequestIds " << nRecvReq
323          << " RecvRequests = 0 OK" << endl ;
324   }
325
326   time_t endtime = std::time(NULL) ;
327   cout << "test_AllToAllvTimeDEC" << myrank << " begintime " << begintime << " endtime " << endtime
328        << " elapse " << endtime-begintime << " " << maxtime/deltatime[myrank]
329        << " calls to AllToAll" << endl ;
330
331   cout << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
332   mpi_access->barrier() ;
333   cout << "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 = std::time(NULL) ;
352
353   cout << "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