Salome HOME
Merge from BR_V5_DEV 16Feb09
[tools/medcoupling.git] / src / ParaMEDMEM / Test / test_MPI_Access_Cyclic_ISend_IRecv.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 "MPIAccessTest.hxx"
26 #include <cppunit/TestAssert.h>
27
28 //#include "CommInterface.hxx"
29 //#include "ProcessorGroup.hxx"
30 //#include "MPIProcessorGroup.hxx"
31 #include "MPIAccess.hxx"
32
33 // use this define to enable lines, execution of which leads to Segmentation Fault
34 #define ENABLE_FAULTS
35
36 // use this define to enable CPPUNIT asserts and fails, showing bugs
37 #define ENABLE_FORCED_FAILURES
38
39 using namespace std;
40 using namespace ParaMEDMEM;
41
42 void MPIAccessTest::test_MPI_Access_Cyclic_ISend_IRecv() {
43
44   cout << "test_MPI_Access_Cyclic_ISend_IRecv" << endl ;
45
46 //  MPI_Init(&argc, &argv) ; 
47
48   int size ;
49   int myrank ;
50   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
51   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
52
53   if ( size < 3 ) {
54     cout << "test_MPI_Access_Cyclic_ISend_IRecv must be runned with 3 procs" << endl ;
55     CPPUNIT_FAIL("test_MPI_Access_Cyclic_ISend_IRecv must be runned with 3 procs") ;
56   }
57
58   cout << "test_MPI_Access_Cyclic_ISend_IRecv" << myrank << endl ;
59
60   ParaMEDMEM::CommInterface interface ;
61
62   ParaMEDMEM::MPIProcessorGroup* group = new ParaMEDMEM::MPIProcessorGroup(interface) ;
63
64   ParaMEDMEM::MPIAccess mpi_access( group ) ;
65
66 #define maxsend 100
67
68   if ( myrank >= 3 ) {
69     mpi_access.barrier() ;
70     delete group ;
71     return ;
72   }
73
74   int alltarget[3] = {1 , 2 , 0 } ;
75   int allsource[3] = {2 , 0 , 1 } ;
76   int SendRequestId[maxsend] ;
77   int RecvRequestId[maxsend] ;
78   int sendbuf[maxsend] ;
79   int recvbuf[maxsend] ;
80   int sts ;
81   int i = 0 ;
82   if ( myrank == 0 ) {
83     sendbuf[i] = i ;
84     sts = mpi_access.ISend(&sendbuf[i],1,MPI_INT,alltarget[myrank],
85                            SendRequestId[i]) ;
86     cout << "test" << myrank << " Send RequestId " << SendRequestId[i]
87          << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
88   }
89   for ( i = 0 ; i < maxsend ; i++ ) {
90      recvbuf[i] = -1 ;
91      sts = mpi_access.IRecv(&recvbuf[i],1,MPI_INT,allsource[myrank],
92                             RecvRequestId[i]) ;
93      cout << "test" << myrank << " Recv RequestId " << RecvRequestId[i]
94           << " tag " << mpi_access.recvMPITag(allsource[myrank]) << endl ;
95      char msgerr[MPI_MAX_ERROR_STRING] ;
96      int lenerr ;
97      mpi_access.errorString(sts, msgerr, &lenerr) ;
98      cout << "test" << myrank << " lenerr " << lenerr
99           << " " << msgerr << endl ;
100
101      if ( sts != MPI_SUCCESS ) {
102        ostringstream strstream ;
103        strstream << "==========================================================="
104                  << "test" << myrank << " KO"
105                  << "==========================================================="
106                  << endl ;
107        cout << strstream.str() << endl ;
108        CPPUNIT_FAIL( strstream.str() ) ;
109      }
110      int j ;
111      for (j = 0 ; j <= i ; j++) {
112         int flag ;
113         if ( j < i ) {
114           cout << "test" << myrank << " " << j << " -> Test-Send("<< SendRequestId[j]
115                << ")" << endl ;
116           mpi_access.test( SendRequestId[j], flag ) ;
117           if ( flag ) {
118             int target, tag, error, outcount ;
119             mpi_access.status( SendRequestId[j], target, tag, error, outcount,
120                                true ) ;
121             cout << "test" << myrank << " Send RequestId " << SendRequestId[j]
122                  << " target " << target << " tag " << tag << " error " << error
123                  << endl ;
124             mpi_access.deleteRequest( SendRequestId[j] ) ;
125           }
126         }
127         cout << "test" << myrank << " " << j << " -> Test-Recv("<< SendRequestId[j]
128              << ")" << endl ;
129         mpi_access.test( RecvRequestId[j], flag ) ;
130         if ( flag ) {
131           int source, tag, error, outcount ;
132           mpi_access.status( RecvRequestId[j], source, tag, error, outcount,
133                              true ) ;
134           cout << "test" << myrank << " Recv RequestId" << j << " "
135                << RecvRequestId[j] << " source " << source << " tag " << tag
136                << " error " << error << " outcount " << outcount << endl ;
137           if ( (outcount != 1) | (recvbuf[j] != j) ) {
138             ostringstream strstream ;
139             strstream << "====================================================="
140                       << endl << "test" << myrank << " outcount "
141                       << outcount << " recvbuf[ " << j << " ] " << recvbuf[j] << " KO"
142                       << endl << "====================================================="
143                       << endl ;
144             cout << strstream.str() << endl ;
145             CPPUNIT_FAIL( strstream.str() ) ;
146           }
147         }
148      }
149      if ( myrank == 0 ) {
150        if ( i != maxsend-1 ) {
151          sendbuf[i+1] = i + 1 ;
152          sts = mpi_access.ISend(&sendbuf[i+1],1,MPI_INT,alltarget[myrank],
153                                 SendRequestId[i+1]) ;
154          cout << "test" << myrank << " Send RequestId " << SendRequestId[i+1]
155               << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
156        }
157      }
158      else {
159        sendbuf[i] = i ;
160        sts = mpi_access.ISend(&sendbuf[i],1,MPI_INT,alltarget[myrank],
161                               SendRequestId[i]) ;
162        cout << "test" << myrank << " Send RequestId " << SendRequestId[i]
163             << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
164      }
165      mpi_access.errorString(sts, msgerr, &lenerr) ;
166      cout << "test" << myrank << " lenerr " << lenerr
167           << " " << msgerr << endl ;
168
169      if ( sts != MPI_SUCCESS ) {
170        ostringstream strstream ;
171        strstream << "==========================================================="
172                  << "test" << myrank << " KO"
173                  << "==========================================================="
174                  << endl ;
175        cout << strstream.str() << endl ;
176        CPPUNIT_FAIL( strstream.str() ) ;
177      }
178      mpi_access.check() ;
179   }
180
181   int flag ;
182   mpi_access.testAll(maxsend,SendRequestId,flag) ;
183   mpi_access.testAll(maxsend,RecvRequestId,flag) ;
184   mpi_access.waitAll(maxsend,SendRequestId) ;
185   mpi_access.deleteRequests(maxsend,SendRequestId) ;
186   mpi_access.waitAll(maxsend,RecvRequestId) ;
187   mpi_access.deleteRequests(maxsend,RecvRequestId) ;
188   mpi_access.check() ;
189   mpi_access.testAll(maxsend,SendRequestId,flag) ;
190   if ( !flag ) {
191     ostringstream strstream ;
192     strstream << "=========================================================" << endl
193               << "test" << myrank << " TestAllSendflag " << flag << " KO" << endl
194               << "=========================================================" << endl ;
195     cout << strstream.str() << endl ;
196     CPPUNIT_FAIL( strstream.str() ) ;
197   }
198   else {
199     cout << "=========================================================" << endl
200          << "test" << myrank << " TestAllSendflag " << flag << " OK" << endl
201          << "=========================================================" << endl ;
202   }
203   mpi_access.testAll(maxsend,RecvRequestId,flag) ;
204   if ( !flag ) {
205     ostringstream strstream ;
206     strstream << "=========================================================" << endl
207               << "test" << myrank << " TestAllRecvflag " << flag << " KO" << endl
208               << "=========================================================" << endl ;
209     cout << strstream.str() << endl ;
210     CPPUNIT_FAIL( strstream.str() ) ;
211   }
212   else {
213     cout << "=========================================================" << endl
214          << "test" << myrank << " TestAllRecvflag " << flag << " OK" << endl
215          << "=========================================================" << endl ;
216   }
217
218   int sendrequests[maxsend] ;
219   int sendreqsize = mpi_access.sendRequestIds( alltarget[myrank] , maxsend ,
220                                                sendrequests ) ;
221   if ( sendreqsize != 0 ) {
222     ostringstream strstream ;
223     strstream << "=========================================================" << endl
224               << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
225               << "=========================================================" << endl ;
226     cout << strstream.str() << endl ;
227     int source, tag, error, outcount ;
228     mpi_access.status(sendrequests[0], source, tag, error, outcount, true) ;
229     cout << "test" << myrank << " RequestId " << sendrequests[0]
230          << " source " << source << " tag " << tag << " error " << error
231          << " outcount " << outcount << endl ;
232     CPPUNIT_FAIL( strstream.str() ) ;
233   }
234   else {
235     cout << "=========================================================" << endl
236          << "test" << myrank << " sendreqsize " << sendreqsize << " OK" << endl
237          << "=========================================================" << endl ;
238   }
239   int recvrequests[maxsend] ;
240   int recvreqsize = mpi_access.sendRequestIds( allsource[myrank] , maxsend ,
241                                                recvrequests ) ;
242   if ( recvreqsize != 0 ) {
243     ostringstream strstream ;
244     strstream << "=========================================================" << endl
245               << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
246               << "=========================================================" << endl ;
247     cout << strstream.str() << endl ;
248     CPPUNIT_FAIL( strstream.str() ) ;
249   }
250   else {
251     cout << "=========================================================" << endl
252          << "test" << myrank << " recvreqsize " << recvreqsize << " OK" << endl
253          << "=========================================================" << endl ;
254   }
255
256   mpi_access.barrier() ;
257
258   delete group ;
259
260 //  MPI_Finalize();
261
262   cout << "test" << myrank << " OK" << endl ;
263
264   return ;
265 }
266
267
268
269