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