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