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