Salome HOME
99aaf640b626acaf81cb71aecf3181a953158266
[tools/medcoupling.git] / src / ParaMEDMEMTest / MPIAccess / test_MPI_Access_ISend_IRecv.cxx
1 // Copyright (C) 2007-2020  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_ISend_IRecv() {
44
45   debugStream << "test_MPI_Access_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 < 2 ) {
55       cerr << "test_MPI_Access_ISend_IRecv must be run with 2 procs" << endl ;
56     //CPPUNIT_FAIL("test_MPI_Access_ISend_IRecv must be run with 2 procs") ;
57     return;
58   }
59
60   debugStream << "test_MPI_Access_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 maxreq 100
69
70   if ( myrank >= 2 ) {
71     mpi_access.barrier() ;
72     delete group ;
73     return ;
74   }
75
76   int target = 1 - myrank ;
77   int SendRequestId[maxreq] ;
78   int RecvRequestId[maxreq] ;
79   int sts ;
80   int sendbuf[maxreq] ;
81   int recvbuf[maxreq] ;
82   int i ;
83   for ( i = 0 ; i < maxreq ; i++ ) {
84     if ( myrank == 0 ) {
85       sendbuf[i] = i ;
86       sts = mpi_access.ISend(&sendbuf[i],1,MPI_INT,target, SendRequestId[i]) ;
87       debugStream << "test" << myrank << " ISend RequestId " << SendRequestId[i]
88            << " tag " << mpi_access.sendMPITag(target) << endl ;
89     }
90     else {
91       sts = mpi_access.IRecv(&recvbuf[i],1,MPI_INT,target, RecvRequestId[i]) ;
92       debugStream << "test" << myrank << " IRecv RequestId " << RecvRequestId[i]
93            << " tag " << mpi_access.recvMPITag(target) << endl ;
94     }
95     int j ;
96     for (j = 0 ; j <= i ; j++) {
97       int flag ;
98       if ( myrank == 0 ) {
99         mpi_access.test( SendRequestId[j], flag ) ;
100       }
101       else {
102         mpi_access.test( RecvRequestId[j], flag ) ;
103       }
104       if ( flag ) {
105         int source, tag, error, outcount ;
106         if ( myrank == 0 ) {
107           mpi_access.status( SendRequestId[j], target, tag, error, outcount,
108                              true ) ;
109           debugStream << "test" << myrank << " Test(Send RequestId " << SendRequestId[j]
110                << ") : target " << target << " tag " << tag << " error " << error
111                << " flag " << flag << endl ;
112         }
113         else {
114           mpi_access.status( RecvRequestId[j], source, tag, error, outcount,
115                              true ) ;
116           debugStream << "test" << myrank << " Test(Recv RequestId "
117                << RecvRequestId[j] << ") : source " << source << " tag " << tag
118                << " error " << error << " outcount " << outcount
119                << " flag " << flag << endl ;
120           if ( (outcount != 1) | (recvbuf[j] != j) ) {
121             ostringstream strstream ;
122             strstream << "==========================================================="
123                       << endl << "test" << myrank << " outcount "
124                       << outcount << " recvbuf " << recvbuf[j] << " KO" << endl
125                       << "==========================================================="
126                       << endl ;
127             debugStream << strstream.str() << endl ;
128             CPPUNIT_FAIL( strstream.str() ) ;
129           }
130           //else {
131           //  debugStream << "==========================================================="
132           //       << endl << "test" << myrank << " outcount " << outcount
133           //       << " RequestId " << RecvRequestId[j] << " recvbuf "
134           //       << recvbuf[j] << " OK" << endl
135           //       << "==========================================================="
136           //       << endl ;
137           //}
138         }
139       }
140     }
141     char msgerr[MPI_MAX_ERROR_STRING] ;
142     int lenerr ;
143     mpi_access.errorString(sts, msgerr, &lenerr) ;
144     debugStream << "test" << myrank << " lenerr " << lenerr << " "
145          << msgerr << endl ;
146
147     if ( sts != MPI_SUCCESS ) {
148       ostringstream strstream ;
149       strstream << "==========================================================="
150                 << "test" << myrank << " KO"
151                 << "==========================================================="
152                 << endl ;
153       debugStream << strstream.str() << endl ;
154       CPPUNIT_FAIL( strstream.str() ) ;
155     }
156   }
157
158   if(MPI_ACCESS_VERBOSE) mpi_access.check() ;
159   if ( myrank == 0 ) {
160     mpi_access.waitAll(maxreq, SendRequestId) ;
161     mpi_access.deleteRequests(maxreq, SendRequestId) ;
162   }
163   else {
164     mpi_access.waitAll(maxreq, RecvRequestId) ;
165     mpi_access.deleteRequests(maxreq, RecvRequestId) ;
166   }
167   if(MPI_ACCESS_VERBOSE) mpi_access.check() ;
168
169   if ( myrank == 0 ) {
170     int sendrequests[maxreq] ;
171     int sendreqsize = mpi_access.sendRequestIds( target , maxreq , sendrequests ) ;
172     if ( sendreqsize != 0 ) {
173       ostringstream strstream ;
174       strstream << "=========================================================" << endl
175                 << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
176                 << "=========================================================" << endl ;
177       debugStream << strstream.str() << endl ;
178       for ( i = 0 ; i < sendreqsize ; i++ ) {
179         debugStream << "test" << myrank << " sendrequests[ " << i << " ] = "
180              << sendrequests[i] << endl ;
181       }
182       CPPUNIT_FAIL( strstream.str() ) ;
183     }
184     else {
185       debugStream << "=========================================================" << endl
186            << "test" << myrank << " sendreqsize " << sendreqsize << " OK" << endl
187            << "=========================================================" << endl ;
188     }
189   }
190   else {
191     int recvrequests[maxreq] ;
192     int recvreqsize = mpi_access.sendRequestIds( target , maxreq , recvrequests ) ;
193     if ( recvreqsize != 0 ) {
194       ostringstream strstream ;
195       strstream << "=========================================================" << endl
196                 << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
197                 << "=========================================================" << endl ;
198       debugStream << strstream.str() << endl ;
199       CPPUNIT_FAIL( strstream.str() ) ;
200     }
201     else {
202       debugStream << "=========================================================" << endl
203            << "test" << myrank << " recvreqsize " << recvreqsize << " OK" << endl
204            << "=========================================================" << endl ;
205     }
206   }
207
208   mpi_access.barrier() ;
209
210   delete group ;
211
212   //  MPI_Finalize();
213
214   debugStream << "test" << myrank << " OK" << endl ;
215
216   return ;
217 }
218
219
220
221