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