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