Salome HOME
Copyright update 2020
[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 size = mpi_access.sendRequestIdsSize() ;
145     debugStream << "test" << myrank << " before WaitAll sendreqsize " << size << endl ;
146     mpi_access.waitAll(maxreq, SendRequestId) ;
147     size = mpi_access.sendRequestIdsSize() ;
148     debugStream << "test" << myrank << " after WaitAll sendreqsize " << size << endl ;
149     int * ArrayOfSendRequests = new int[ size ] ;
150     int nSendRequest = mpi_access.sendRequestIds( size , ArrayOfSendRequests ) ;
151     int i ;
152     for ( i = 0 ; i < nSendRequest ; i++ ) {
153        mpi_access.deleteRequest( ArrayOfSendRequests[i] ) ;
154     }
155     delete [] ArrayOfSendRequests ;
156   }
157   else {
158     int size = mpi_access.recvRequestIdsSize() ;
159     debugStream << "test" << myrank << " before WaitAll recvreqsize " << size << endl ;
160     mpi_access.waitAll(maxreq, RecvRequestId) ;
161     size = mpi_access.recvRequestIdsSize() ;
162     debugStream << "test" << myrank << " after WaitAll recvreqsize " << size << endl ;
163     int * ArrayOfRecvRequests = new int[ size ] ;
164     int nRecvRequest = mpi_access.recvRequestIds( size , ArrayOfRecvRequests ) ;
165     int i ;
166     for ( i = 0 ; i < nRecvRequest ; i++ ) {
167        mpi_access.deleteRequest( ArrayOfRecvRequests[i] ) ;
168     }
169     delete [] ArrayOfRecvRequests ;
170   }
171   if(MPI_ACCESS_VERBOSE) mpi_access.check() ;
172
173   if ( myrank == 0 ) {
174     int sendrequests[maxreq] ;
175     int sendreqsize = mpi_access.sendRequestIds( target , maxreq , sendrequests ) ;
176     int i ;
177     if ( sendreqsize != 0 ) {
178       ostringstream strstream ;
179       strstream << "=========================================================" << endl
180                 << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
181                 << "=========================================================" << endl ;
182       debugStream << strstream.str() << endl ;
183       for ( i = 0 ; i < sendreqsize ; i++ ) {
184          debugStream << "test" << myrank << " sendrequests[ " << i << " ] = "
185               << sendrequests[i] << endl ;
186       }
187       CPPUNIT_FAIL( strstream.str() ) ;
188     }
189     else {
190       debugStream << "=========================================================" << endl
191            << "test" << myrank << " sendreqsize " << sendreqsize << " OK" << endl
192            << "=========================================================" << endl ;
193     }
194   }
195   else {
196     int recvrequests[maxreq] ;
197     int recvreqsize = mpi_access.recvRequestIds( target , maxreq , recvrequests ) ;
198     if ( recvreqsize != 0 ) {
199       ostringstream strstream ;
200       strstream << "=========================================================" << endl
201                 << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
202                 << "=========================================================" << endl ;
203       debugStream << strstream.str() << endl ;
204       CPPUNIT_FAIL( strstream.str() ) ;
205     }
206     else {
207       debugStream << "=========================================================" << endl
208            << "test" << myrank << " recvreqsize " << recvreqsize << " OK" << endl
209            << "=========================================================" << endl ;
210     }
211   }
212
213   mpi_access.barrier() ;
214
215   delete group ;
216
217 //  MPI_Finalize();
218
219   debugStream << "test" << myrank << " OK" << endl ;
220
221   return ;
222 }
223
224
225
226