Salome HOME
Compilation under Windows
[tools/medcoupling.git] / src / ParaMEDMEMTest / test_MPI_Access_ISend_IRecv_BottleNeck.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 <time.h>
21 #include <string>
22 #include <vector>
23 #include <map>
24 #include <iostream>
25 #include <mpi.h>
26
27 #include "MPIAccessTest.hxx"
28 #include <cppunit/TestAssert.h>
29
30 //#include "CommInterface.hxx"
31 //#include "ProcessorGroup.hxx"
32 //#include "MPIProcessorGroup.hxx"
33 #include "MPIAccess.hxx"
34
35 // use this define to enable lines, execution of which leads to Segmentation Fault
36 #define ENABLE_FAULTS
37
38 // use this define to enable CPPUNIT asserts and fails, showing bugs
39 #define ENABLE_FORCED_FAILURES
40
41 using namespace std;
42 using namespace MEDCoupling;
43
44 void MPIAccessTest::test_MPI_Access_ISend_IRecv_BottleNeck() {
45
46   debugStream << "test_MPI_Access_ISend_IRecv_BottleNeck" << endl ;
47
48 //  MPI_Init(&argc, &argv) ; 
49
50   int size ;
51   int myrank ;
52   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
53   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
54
55   if ( size < 2 ) {
56     ostringstream strstream ;
57     strstream << "test_MPI_Access_ISend_IRecv_BottleNeck must be run with 2 procs"
58               << endl ;
59     cerr << strstream.str() << endl ;
60     //CPPUNIT_FAIL( strstream.str() ) ;
61     return;
62   }
63
64   debugStream << "test_MPI_Access_ISend_IRecv_BottleNeck" << myrank << endl ;
65
66   MEDCoupling::CommInterface interface ;
67
68   MEDCoupling::MPIProcessorGroup* group = new MEDCoupling::MPIProcessorGroup(interface) ;
69
70   MEDCoupling::MPIAccess mpi_access( group ) ;
71
72 #define maxreq 10000
73
74   if ( myrank >= 2 ) {
75     mpi_access.barrier() ;
76     delete group ;
77     return ;
78   }
79
80   int target = 1 - myrank ;
81   int SendRequestId[maxreq] ;
82   int RecvRequestId[maxreq] ;
83   int sts ;
84   int sendbuf[maxreq] ;
85   int recvbuf[maxreq] ;
86   int i ;
87   for ( i = 0 ; i < maxreq ; i++ ) {
88      if ( myrank == 0 ) {
89        sendbuf[i] = i ;
90        sts = mpi_access.ISend(sendbuf,i,MPI_INT,target, SendRequestId[i]) ;
91        debugStream << "test" << myrank << " ISend RequestId " << SendRequestId[i]
92             << " tag " << mpi_access.sendMPITag(target) << endl ;
93      }
94      else {
95        //sleep( 1 ) ;
96        sts = mpi_access.IRecv(recvbuf,i,MPI_INT,target, RecvRequestId[i]) ;
97        debugStream << "test" << myrank << " IRecv RequestId " << RecvRequestId[i]
98             << " tag " << mpi_access.recvMPITag(target) << endl ;
99        int recvreqsize = mpi_access.recvRequestIdsSize() ;
100        int * recvrequests = new int[ recvreqsize ] ;
101        recvreqsize = mpi_access.recvRequestIds( target , recvreqsize , recvrequests ) ;
102        int j ;
103        for (j = 0 ; j < recvreqsize ; j++) {
104           int flag ;
105           mpi_access.test( recvrequests[j], flag ) ;
106           if ( flag ) {
107             int source, tag, error, outcount ;
108             mpi_access.status( recvrequests[j], source, tag, error, outcount,
109                                true ) ;
110             debugStream << "test" << myrank << " Test(Recv RequestId "
111                  << recvrequests[j] << ") : source " << source << " tag " << tag
112                  << " error " << error << " outcount " << outcount
113                  << " flag " << flag << " : DeleteRequest" << endl ;
114             mpi_access.deleteRequest( recvrequests[j] ) ;
115           }
116           else {
117 //            debugStream << "test" << myrank << " Test(Recv RequestId "
118 //                 << recvrequests[j] << ") flag " << flag << endl ;
119           }
120        }
121        delete [] recvrequests ;
122      }
123      if ( sts != MPI_SUCCESS ) {
124        char msgerr[MPI_MAX_ERROR_STRING] ;
125        int lenerr ;
126        mpi_access.errorString(sts, msgerr, &lenerr) ;
127        debugStream << "test" << myrank << " lenerr " << lenerr << " "
128             << msgerr << endl ;
129      }
130
131      if ( sts != MPI_SUCCESS ) {
132        ostringstream strstream ;
133        strstream << "==========================================================="
134                  << "test" << myrank << " KO"
135                  << "==========================================================="
136                  << endl ;
137        debugStream << strstream.str() << endl ;
138        CPPUNIT_FAIL( strstream.str() ) ;
139      }
140   }
141
142   if(MPI_ACCESS_VERBOSE) mpi_access.check() ;
143   if ( myrank == 0 ) {
144     int size2 = mpi_access.sendRequestIdsSize() ;
145     debugStream << "test" << myrank << " before WaitAll sendreqsize " << size2 << endl ;
146     mpi_access.waitAll(maxreq, SendRequestId) ;
147     size2 = mpi_access.sendRequestIdsSize() ;
148     debugStream << "test" << myrank << " after WaitAll sendreqsize " << size2 << endl ;
149     int * ArrayOfSendRequests = new int[ size2 ] ;
150     int nSendRequest = mpi_access.sendRequestIds( size2 , ArrayOfSendRequests ) ;
151     for ( i = 0 ; i < nSendRequest ; i++ ) {
152        mpi_access.deleteRequest( ArrayOfSendRequests[i] ) ;
153     }
154     delete [] ArrayOfSendRequests ;
155   }
156   else {
157     int size2 = mpi_access.recvRequestIdsSize() ;
158     debugStream << "test" << myrank << " before WaitAll recvreqsize " << size2 << endl ;
159     mpi_access.waitAll(maxreq, RecvRequestId) ;
160     size2 = mpi_access.recvRequestIdsSize() ;
161     debugStream << "test" << myrank << " after WaitAll recvreqsize " << size2 << endl ;
162     int * ArrayOfRecvRequests = new int[ size2 ] ;
163     int nRecvRequest = mpi_access.recvRequestIds( size2 , ArrayOfRecvRequests ) ;
164     for ( i = 0 ; i < nRecvRequest ; i++ ) {
165        mpi_access.deleteRequest( ArrayOfRecvRequests[i] ) ;
166     }
167     delete [] ArrayOfRecvRequests ;
168   }
169   if(MPI_ACCESS_VERBOSE) mpi_access.check() ;
170
171   if ( myrank == 0 ) {
172     int sendrequests[maxreq] ;
173     int sendreqsize = mpi_access.sendRequestIds( target , maxreq , sendrequests ) ;
174     if ( sendreqsize != 0 ) {
175       ostringstream strstream ;
176       strstream << "=========================================================" << endl
177                 << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
178                 << "=========================================================" << endl ;
179       debugStream << strstream.str() << endl ;
180       for ( i = 0 ; i < sendreqsize ; i++ ) {
181          debugStream << "test" << myrank << " sendrequests[ " << i << " ] = "
182               << sendrequests[i] << endl ;
183       }
184       CPPUNIT_FAIL( strstream.str() ) ;
185     }
186     else {
187       debugStream << "=========================================================" << endl
188            << "test" << myrank << " sendreqsize " << sendreqsize << " OK" << endl
189            << "=========================================================" << endl ;
190     }
191   }
192   else {
193     int recvrequests[maxreq] ;
194     int recvreqsize = mpi_access.recvRequestIds( target , maxreq , recvrequests ) ;
195     if ( recvreqsize != 0 ) {
196       ostringstream strstream ;
197       strstream << "=========================================================" << endl
198                 << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
199                 << "=========================================================" << endl ;
200       debugStream << strstream.str() << endl ;
201       CPPUNIT_FAIL( strstream.str() ) ;
202     }
203     else {
204       debugStream << "=========================================================" << endl
205            << "test" << myrank << " recvreqsize " << recvreqsize << " OK" << endl
206            << "=========================================================" << endl ;
207     }
208   }
209
210   mpi_access.barrier() ;
211
212   delete group ;
213
214 //  MPI_Finalize();
215
216   debugStream << "test" << myrank << " OK" << endl ;
217
218   return ;
219 }
220
221
222
223