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