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