Salome HOME
adding a new test for makeMesh method.
[modules/med.git] / src / ParaMEDMEM / Test / test_AllToAllvTimeDoubleDEC.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 <math.h>
20 #include <string>
21 #include <vector>
22 #include <map>
23 #include <iostream>
24 #include <mpi.h>
25 #include <time.h>
26
27 #include "MPIAccessDECTest.hxx"
28 #include <cppunit/TestAssert.h>
29
30 //#include "CommInterface.hxx"
31 //#include "ProcessorGroup.hxx"
32 //#include "MPIProcessorGroup.hxx"
33 #include "MPIAccessDEC.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 ParaMEDMEM;
44
45 void MPIAccessDECTest::test_AllToAllvTimeDoubleDECSynchronousPointToPoint() {
46   test_AllToAllvTimeDoubleDEC( false ) ;
47 }
48 void MPIAccessDECTest::test_AllToAllvTimeDoubleDECAsynchronousPointToPoint() {
49   test_AllToAllvTimeDoubleDEC( true ) ;
50 }
51
52 static void chksts( int sts , int myrank , ParaMEDMEM::MPIAccess * mpi_access ) {
53   char msgerr[MPI_MAX_ERROR_STRING] ;
54   int lenerr ;
55   if ( sts != MPI_SUCCESS ) {
56     mpi_access->errorString(sts, msgerr, &lenerr) ;
57     cout << "test" << myrank << " lenerr " << lenerr << " "
58          << msgerr << endl ;
59     ostringstream strstream ;
60     strstream << "==========================================================="
61               << "test" << myrank << " KO"
62               << "==========================================================="
63               << endl ;
64     cout << strstream.str() << endl ;
65     CPPUNIT_FAIL( strstream.str() ) ;
66   }
67   return ;
68 }
69
70 void MPIAccessDECTest::test_AllToAllvTimeDoubleDEC( bool Asynchronous ) {
71
72   cout << "test_AllToAllvTimeDoubleDEC" << endl ;
73
74 //  MPI_Init(&argc, &argv) ; 
75
76   int size ;
77   int myrank ;
78   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
79   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
80
81   if ( size < 2 || size > 11 ) {
82     ostringstream strstream ;
83     strstream << "usage :" << endl
84               << "mpirun -np <nbprocs> test_AllToAllTimeDEC" << endl
85               << " (nbprocs >=2)" << endl
86               << "test must be runned with more than 1 proc and less than 12 procs"
87               << endl ;
88     cout << strstream.str() << endl ;
89     CPPUNIT_FAIL( strstream.str() ) ;
90   }
91
92 //  int Asynchronous = atoi(argv[1]) ;
93
94   cout << "test_AllToAllvTimeDoubleDEC" << myrank << " Asynchronous " << Asynchronous << endl ;
95
96   ParaMEDMEM::CommInterface interface ;
97   std::set<int> sourceprocs;
98   std::set<int> targetprocs;
99   int i ;
100   for ( i = 0 ; i < size/2 ; i++ ) {
101      sourceprocs.insert(i);
102   }
103   for ( i = size/2 ; i < size ; i++ ) {
104      targetprocs.insert(i);
105   }
106
107   ParaMEDMEM::MPIProcessorGroup* sourcegroup = new ParaMEDMEM::MPIProcessorGroup(interface,sourceprocs) ;
108   ParaMEDMEM::MPIProcessorGroup* targetgroup = new ParaMEDMEM::MPIProcessorGroup(interface,targetprocs) ;
109
110 //  TimeInterpolator * aLinearInterpDEC = new LinearTimeInterpolator( 0 ) ;
111   MPIAccessDEC * MyMPIAccessDEC = new MPIAccessDEC( *sourcegroup , *targetgroup ,
112                                                     Asynchronous ) ;
113 //                                                    Asynchronous , LinearInterp , 0.5 ) ;
114   MyMPIAccessDEC->setTimeInterpolator( LinearTimeInterp ) ;
115   MPIAccess * mpi_access = MyMPIAccessDEC->getMPIAccess() ;
116
117   cout << "test_AllToAllvTimeDoubleDEC" << myrank << " Barrier :" << endl ;
118   mpi_access->barrier() ;
119
120 #define maxproc 11
121 #define maxreq 100
122 #define datamsglength 10
123
124   int sts ;
125   int *sendcounts = new int[size] ;
126   int *sdispls = new int[size] ;
127   int *recvcounts = new int[size] ;
128   int *rdispls = new int[size] ;
129   int *sendtimecounts = new int[size] ;
130   int *stimedispls = new int[size] ;
131   int *recvtimecounts = new int[size] ;
132   int *rtimedispls = new int[size] ;
133   for ( i = 0 ; i < size ; i++ ) {
134      sendcounts[i] = datamsglength-i ;
135      sdispls[i] = i*datamsglength ;
136      recvcounts[i] = datamsglength-myrank ;
137      rdispls[i] = i*datamsglength ;
138      sendtimecounts[i] = 1 ;
139      stimedispls[i] = 0 ;
140      recvtimecounts[i] = 1 ;
141      rtimedispls[i] = i ;
142   }
143
144   double time[maxproc] ;
145   double deltatime[maxproc] = {1.,2.1,3.2,4.3,5.4,6.5,7.6,8.7,9.8,10.9,11.} ;
146   double maxtime[maxproc] ;
147   double nextdeltatime[maxproc] ;
148   for ( i = 0 ; i < size ; i++ ) {
149      time[i] = 0 ;
150      maxtime[i] = maxreq ;
151      nextdeltatime[i] = deltatime[i] ;
152   }
153   time_t begintime = std::time(NULL) ;
154   for ( time[myrank] = 0 ; time[myrank] <= maxtime[myrank] && nextdeltatime[myrank] != 0 ;
155         time[myrank]+=nextdeltatime[myrank] ) {
156 //local and target times
157      int target ;
158      for ( target = 0 ; target < size ; target++ ) {
159         nextdeltatime[target] = deltatime[target] ;
160         if ( time[target] != 0 ) {
161           if ( time[target]+nextdeltatime[target] > maxtime[target] ) {
162             nextdeltatime[target] = 0 ;
163           }
164         }
165         if ( target != myrank ) {
166           while ( time[myrank] >= time[target] ) {
167                time[target] += deltatime[target] ;
168           }
169         }
170      }
171      MyMPIAccessDEC->setTime( time[myrank] , nextdeltatime[myrank] ) ;
172      cout << "test" << myrank << "=====TIME " << time[myrank] << "=====DELTATIME "
173           << nextdeltatime[myrank] << "=====MAXTIME " << maxtime[myrank] << " ======"
174           << endl ; 
175      double * sendbuf = new double[datamsglength*size] ;
176 //     double * sendbuf = (double *) malloc(sizeof(double)*datamsglength*size) ;
177      double * recvbuf = new double[datamsglength*size] ;
178      int j ;
179      //cout << "test_AllToAllvTimeDoubleDEC" << myrank << " sendbuf" ;
180      for ( target = 0 ; target < size ; target++ ) {
181         for ( j = 0 ; j < datamsglength ; j++ ) {
182            //sendbuf[j] = myrank*10000 + (j/datamsglength)*100 + j ;
183            sendbuf[target*datamsglength+j] = myrank*1000000 + target*10000 +
184                                              (time[myrank]/deltatime[myrank])*100 + j ;
185            //cout << " " << (int ) sendbuf[target*datamsglength+j] ;
186            recvbuf[target*datamsglength+j] = -1 ;
187         }
188         //cout << endl ;
189      }
190
191      int sts = MyMPIAccessDEC->allToAllvTime( sendbuf, sendcounts , sdispls , MPI_DOUBLE ,
192                                             recvbuf, recvcounts , rdispls , MPI_DOUBLE ) ;
193      chksts( sts , myrank , mpi_access ) ;
194
195 //     cout << "test_AllToAllvTimeDoubleDEC" << myrank << " recvbuf before CheckSent" ;
196 //     for ( i = 0 ; i < datamsglength*size ; i++ ) {
197 //        cout << " " << recvbuf[i] ;
198 //     }
199 //     cout << endl ;
200
201      int nRecvReq = mpi_access->recvRequestIdsSize() ;
202      if ( nRecvReq != 0 ) {
203        ostringstream strstream ;
204        strstream << "=============================================================" << endl
205                  << "test_AllToAllvTimeDoubleDEC" << myrank << " WaitAllRecv "
206                  << nRecvReq << " Requests # 0 ERROR"
207                  << endl << "============================================================"
208                  << endl ;
209        int *ArrayOfRecvRequests = new int[nRecvReq] ;
210        int nReq = mpi_access->recvRequestIds( nRecvReq, ArrayOfRecvRequests ) ;
211        mpi_access->waitAll( nReq , ArrayOfRecvRequests ) ;
212        delete [] ArrayOfRecvRequests ;
213        cout << strstream.str() << endl ;
214        CPPUNIT_FAIL( strstream.str() ) ;
215      }
216
217 //     cout << "test_AllToAllvTimeDoubleDEC" << myrank << " check of recvbuf" << endl ;
218      bool badrecvbuf = false ;
219      for ( target = 0 ; target < size ; target++ ) {
220         int j ;
221         for ( j = 0 ; j < datamsglength ; j++ ) {
222            int index = target*datamsglength+j ;
223            if ( j < recvcounts[target] ) {
224              if ( fabs(recvbuf[index] - (target*1000000 + myrank*10000 +
225                   (time[target]/deltatime[target])*100 + j)) > 101) {
226                badrecvbuf = true ;
227                cout << "test_AllToAllvTimeDoubleDEC" << myrank << " target " << target << " time[target] "
228                     << time[target] << " recvbuf[" << index << "] " << (int ) recvbuf[index]
229                     << " # " << (int ) (target*1000000 +
230                        myrank*10000 + (time[target]/deltatime[target])*100 + j)
231                     << endl ;
232              }
233              else if ( badrecvbuf ) {
234                cout << "test_AllToAllvTimeDoubleDEC" << myrank << " recvbuf[" << index << "] "
235                     << recvbuf[index] << " ~= " << (int ) (target*1000000 +
236                        myrank*10000 + (time[target]/deltatime[target])*100 + j) << endl ;
237              }
238            }
239            else if ( recvbuf[index] != -1 ) {
240              badrecvbuf = true ;
241              cout << "test_AllToAllvTimeDoubleDEC" << myrank << " recvbuf[" << index << "] "
242                   << recvbuf[index] << " # -1" << endl ;
243            }
244         }
245      }
246      if ( badrecvbuf ) {
247        ostringstream strstream ;
248        strstream << "==================================================================" << endl
249                  << "test_AllToAllvTimeDoubleDEC" << myrank << " badrecvbuf"
250                  << endl << "=================================================================="
251                  << endl ;
252        cout << strstream.str() << endl ;
253        CPPUNIT_FAIL( strstream.str() ) ;
254      }
255      delete [] recvbuf ;
256   }
257
258   cout << "test_AllToAllvTimeDoubleDEC" << myrank << " Barrier :" << endl ;
259   mpi_access->barrier() ;
260
261   cout << "test_AllToAllvTimeDoubleDEC" << myrank << " CheckFinalSent" << endl ;
262   sts = MyMPIAccessDEC->checkFinalSent() ;
263   if ( sts != MPI_SUCCESS ) {
264     ostringstream strstream ;
265     strstream << "=================================================================" << endl
266               << "test_AllToAllvTimeDoubleDEC" << myrank << " CheckFinalSent ERROR"
267               << endl << "================================================================="
268               << endl ;
269     cout << strstream.str() << endl ;
270     CPPUNIT_FAIL( strstream.str() ) ;
271   }
272
273   cout << "test_AllToAllvTimeDoubleDEC" << myrank << " CheckFinalRecv" << endl ;
274   sts = MyMPIAccessDEC->checkFinalRecv() ;
275   if ( sts != MPI_SUCCESS ) {
276     ostringstream strstream ;
277     strstream << "=================================================================" << endl
278               << "test_AllToAllvTimeDoubleDEC" << myrank << " CheckFinalRecv ERROR"
279               << endl << "================================================================"
280               << endl ;
281     cout << strstream.str() << endl ;
282     CPPUNIT_FAIL( strstream.str() ) ;
283   }
284
285   int nRecvReq = mpi_access->recvRequestIdsSize() ;
286   if ( nRecvReq ) {
287     ostringstream strstream ;
288     strstream << "===============================================================" << endl
289               << "test_AllToAllvTimeDoubleDEC" << myrank << " RecvRequestIds " << nRecvReq
290               << " RecvRequests # 0 Error"
291               << endl << "==============================================================="
292               << endl ;
293     cout << strstream.str() << endl ;
294     CPPUNIT_FAIL( strstream.str() ) ;
295   }
296   else {
297     cout << "test_AllToAllvTimeDoubleDEC" << myrank << " RecvRequestIds " << nRecvReq
298          << " RecvRequests = 0 OK" << endl ;
299   }
300
301   time_t endtime = std::time(NULL) ;
302   cout << "test_AllToAllvTimeDoubleDEC" << myrank << " begintime " << begintime << " endtime " << endtime
303        << " elapse " << endtime-begintime << " " << maxtime[myrank]/deltatime[myrank]
304        << " calls to AllToAll" << endl ;
305
306   cout << "test" << myrank << " Barrier :" << endl ;
307   mpi_access->barrier() ;
308
309   delete sourcegroup ;
310   delete targetgroup ;
311   delete MyMPIAccessDEC ;
312 //  delete aLinearInterpDEC ;
313
314   delete [] sendcounts ;
315   delete [] sdispls ;
316   delete [] recvcounts ;
317   delete [] rdispls ;
318   delete [] sendtimecounts ;
319   delete [] stimedispls ;
320   delete [] recvtimecounts ;
321   delete [] rtimedispls ;
322
323 //  MPI_Finalize();
324
325   endtime = std::time(NULL) ;
326
327   cout << "test_AllToAllvTimeDoubleDEC" << myrank << " OK begintime " << begintime << " endtime " << endtime
328        << " elapse " << endtime-begintime << " " << maxtime[myrank]/deltatime[myrank]
329        << " calls to AllToAll" << endl ;
330
331   return ;
332 }
333
334
335
336