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