1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
26 #include "MPIAccessDECTest.hxx"
27 #include <cppunit/TestAssert.h>
29 //#include "CommInterface.hxx"
30 //#include "ProcessorGroup.hxx"
31 //#include "MPIProcessorGroup.hxx"
32 #include "MPIAccessDEC.hxx"
33 #include "LinearTimeInterpolator.hxx"
35 // use this define to enable lines, execution of which leads to Segmentation Fault
38 // use this define to enable CPPUNIT asserts and fails, showing bugs
39 #define ENABLE_FORCED_FAILURES
42 using namespace ParaMEDMEM;
44 void MPIAccessDECTest::test_AllToAllvTimeDECSynchronousNative() {
45 test_AllToAllvTimeDEC( false , true ) ;
47 void MPIAccessDECTest::test_AllToAllvTimeDECSynchronousPointToPoint() {
48 test_AllToAllvTimeDEC( false , false ) ;
50 void MPIAccessDECTest::test_AllToAllvTimeDECAsynchronousPointToPoint() {
51 test_AllToAllvTimeDEC( true , false ) ;
54 static void chksts( int sts , int myrank , ParaMEDMEM::MPIAccess * mpi_access ) {
55 char msgerr[MPI_MAX_ERROR_STRING] ;
57 if ( sts != MPI_SUCCESS ) {
58 mpi_access->errorString(sts, msgerr, &lenerr) ;
59 cout << "test_AllToAllvTimeDEC" << myrank << " lenerr " << lenerr << " "
61 ostringstream strstream ;
62 strstream << "==========================================================="
63 << "test_AllToAllvTimeDEC" << myrank << " KO"
64 << "==========================================================="
66 cout << strstream.str() << endl ;
67 CPPUNIT_FAIL( strstream.str() ) ;
72 void MPIAccessDECTest::test_AllToAllvTimeDEC( bool Asynchronous , bool UseMPINative ) {
74 cout << "test_AllToAllvTimeDEC" << endl ;
76 // MPI_Init(&argc, &argv) ;
80 MPI_Comm_size(MPI_COMM_WORLD,&size) ;
81 MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
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"
90 cout << strstream.str() << endl ;
91 CPPUNIT_FAIL( strstream.str() ) ;
94 // int Asynchronous = atoi(argv[1]) ;
95 int UseMPI_Alltoallv = UseMPINative ;
97 // UseMPI_Alltoallv = atoi(argv[2]) ;
100 cout << "test_AllToAllvTimeDEC" << myrank << " Asynchronous " << Asynchronous
101 << " UseMPI_Alltoallv " << UseMPI_Alltoallv << endl ;
103 ParaMEDMEM::CommInterface interface ;
104 std::set<int> sourceprocs;
105 std::set<int> targetprocs;
107 for ( i = 0 ; i < size/2 ; i++ ) {
108 sourceprocs.insert(i);
110 for ( i = size/2 ; i < size ; i++ ) {
111 targetprocs.insert(i);
114 ParaMEDMEM::MPIProcessorGroup* sourcegroup = new ParaMEDMEM::MPIProcessorGroup(interface,sourceprocs) ;
115 ParaMEDMEM::MPIProcessorGroup* targetgroup = new ParaMEDMEM::MPIProcessorGroup(interface,targetprocs) ;
117 // TimeInterpolator * aLinearInterpDEC = new LinearTimeInterpolator( 0.5 ) ;
118 MPIAccessDEC * MyMPIAccessDEC = new MPIAccessDEC( *sourcegroup , *targetgroup ,
120 // Asynchronous , LinearInterp , 0.5 ) ;
121 MyMPIAccessDEC->setTimeInterpolator( LinearTimeInterp , 0.5 ) ;
122 MPIAccess * mpi_access = MyMPIAccessDEC->getMPIAccess() ;
124 cout << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
125 mpi_access->barrier() ;
126 cout << "test_AllToAllvTimeDEC" << myrank << " Barrier done" << endl ;
130 #define datamsglength 10
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 ;
148 recvtimecounts[i] = 1 ;
150 //rtimedispls[i] = i*mpi_access->TimeExtent() ;
154 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 nextdeltatime = deltatime[myrank] ;
157 if ( UseMPI_Alltoallv ) {
158 maxtime = maxreq*nextdeltatime - 0.1 ;
162 // MyMPIAccessDEC->InitTime( time , nextdeltatime , maxtime ) ;
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] ;
169 nextdeltatime = deltatime[myrank] ;
170 if ( time+nextdeltatime > maxtime ) {
173 // MyMPIAccessDEC->NextTime( nextdeltatime ) ;
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] ;
182 for ( j = 0 ; j < datamsglength*size ; j++ ) {
183 sendbuf[j] = myrank*1000000 + (j/datamsglength)*1000 + j ;
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 ;
204 delete aSendTimeMessage ;
205 delete [] aRecvTimeMessage ;
206 interface.allToAllV(sendbuf, sendcounts , sdispls , MPI_INT ,
207 recvbuf, recvcounts , rdispls , MPI_INT , *comm ) ;
212 int sts = MyMPIAccessDEC->allToAllvTime( sendbuf, sendcounts , sdispls , MPI_INT ,
213 recvbuf, recvcounts , rdispls , MPI_INT ) ;
214 chksts( sts , myrank , mpi_access ) ;
217 // cout << "test_AllToAllvTimeDEC" << myrank << " recvbuf before CheckSent" ;
218 // for ( i = 0 ; i < datamsglength*size ; i++ ) {
219 // cout << " " << recvbuf[i] ;
223 // cout << "test_AllToAllvTimeDEC" << myrank << " sendbuf " << sendbuf << endl ;
224 // MyMPIAccessDEC->CheckSent() ;
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 << "============================================================="
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() ) ;
241 // cout << "test_AllToAllvTimeDEC" << myrank << " check of recvbuf" << endl ;
242 bool badrecvbuf = false ;
243 for ( i = 0 ; i < size ; i++ ) {
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) ) {
251 cout << "test_AllToAllvTimeDEC" << myrank << " recvbuf[" << index << "] "
252 << recvbuf[index] << " # " << (index/datamsglength)*1000000 +
254 myrank*datamsglength+(index%datamsglength) << endl ;
256 else if ( badrecvbuf ) {
257 cout << "test_AllToAllvTimeDEC" << myrank << " recvbuf[" << index << "] "
258 << recvbuf[index] << " == " << (index/datamsglength)*1000000 +
260 myrank*datamsglength+(index%datamsglength) << endl ;
263 else if ( recvbuf[index] != -1 ) {
265 cout << "test_AllToAllvTimeDEC" << myrank << " recvbuf[" << index << "] "
266 << recvbuf[index] << " # -1" << endl ;
271 ostringstream strstream ;
272 strstream << "==============================================================" << endl
273 << "test_AllToAllvTimeDEC" << myrank << " badrecvbuf"
274 << endl << "============================================================="
276 cout << strstream.str() << endl ;
277 CPPUNIT_FAIL( strstream.str() ) ;
282 cout << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
283 mpi_access->barrier() ;
284 cout << "test_AllToAllvTimeDEC" << myrank << " Barrier done" << endl ;
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 << "================================================================"
294 cout << strstream.str() << endl ;
295 CPPUNIT_FAIL( strstream.str() ) ;
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 << "================================================================"
306 cout << strstream.str() << endl ;
307 CPPUNIT_FAIL( strstream.str() ) ;
310 int nRecvReq = mpi_access->recvRequestIdsSize() ;
312 ostringstream strstream ;
313 strstream << "===============================================================" << endl
314 << "test_AllToAllvTimeDEC" << myrank << " RecvRequestIds " << nRecvReq
315 << " RecvRequests # 0 Error"
316 << endl << "==============================================================="
318 cout << strstream.str() << endl ;
319 CPPUNIT_FAIL( strstream.str() ) ;
322 cout << "test_AllToAllvTimeDEC" << myrank << " RecvRequestIds " << nRecvReq
323 << " RecvRequests = 0 OK" << endl ;
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 ;
331 cout << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
332 mpi_access->barrier() ;
333 cout << "test_AllToAllvTimeDEC" << myrank << " Barrier done" << endl ;
337 delete MyMPIAccessDEC ;
338 // delete aLinearInterpDEC ;
340 delete [] sendcounts ;
342 delete [] recvcounts ;
344 delete [] sendtimecounts ;
345 delete [] stimedispls ;
346 delete [] recvtimecounts ;
347 delete [] rtimedispls ;
351 endtime = std::time(NULL) ;
353 cout << "test_AllToAllvTimeDEC" << myrank << " OK begintime " << begintime << " endtime " << endtime
354 << " elapse " << endtime-begintime << " " << maxtime/deltatime[myrank]
355 << " calls to AllToAll" << endl ;