Salome HOME
Merge from V6_main 01/04/2013
[modules/med.git] / src / ParaMEDMEMTest / test_MPI_Access_Cyclic_ISend_IRecv.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_Cyclic_ISend_IRecv() {
44
45   cout << "test_MPI_Access_Cyclic_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 < 3 ) {
55     cout << "test_MPI_Access_Cyclic_ISend_IRecv must be runned with 3 procs" << endl ;
56     CPPUNIT_FAIL("test_MPI_Access_Cyclic_ISend_IRecv must be runned with 3 procs") ;
57   }
58
59   cout << "test_MPI_Access_Cyclic_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 maxsend 100
68
69   if ( myrank >= 3 ) {
70     mpi_access.barrier() ;
71     delete group ;
72     return ;
73   }
74
75   int alltarget[3] = {1 , 2 , 0 } ;
76   int allsource[3] = {2 , 0 , 1 } ;
77   int SendRequestId[maxsend] ;
78   int RecvRequestId[maxsend] ;
79   int sendbuf[maxsend] ;
80   int recvbuf[maxsend] ;
81   int sts ;
82   int i = 0 ;
83   if ( myrank == 0 ) {
84     sendbuf[i] = i ;
85     sts = mpi_access.ISend(&sendbuf[i],1,MPI_INT,alltarget[myrank],
86                            SendRequestId[i]) ;
87     cout << "test" << myrank << " Send RequestId " << SendRequestId[i]
88          << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
89   }
90   for ( i = 0 ; i < maxsend ; i++ ) {
91      recvbuf[i] = -1 ;
92      sts = mpi_access.IRecv(&recvbuf[i],1,MPI_INT,allsource[myrank],
93                             RecvRequestId[i]) ;
94      cout << "test" << myrank << " Recv RequestId " << RecvRequestId[i]
95           << " tag " << mpi_access.recvMPITag(allsource[myrank]) << endl ;
96      char msgerr[MPI_MAX_ERROR_STRING] ;
97      int lenerr ;
98      mpi_access.errorString(sts, msgerr, &lenerr) ;
99      cout << "test" << myrank << " lenerr " << lenerr
100           << " " << msgerr << endl ;
101
102      if ( sts != MPI_SUCCESS ) {
103        ostringstream strstream ;
104        strstream << "==========================================================="
105                  << "test" << myrank << " KO"
106                  << "==========================================================="
107                  << endl ;
108        cout << strstream.str() << endl ;
109        CPPUNIT_FAIL( strstream.str() ) ;
110      }
111      int j ;
112      for (j = 0 ; j <= i ; j++) {
113         int flag ;
114         if ( j < i ) {
115           cout << "test" << myrank << " " << j << " -> Test-Send("<< SendRequestId[j]
116                << ")" << endl ;
117           mpi_access.test( SendRequestId[j], flag ) ;
118           if ( flag ) {
119             int target, tag, error, outcount ;
120             mpi_access.status( SendRequestId[j], target, tag, error, outcount,
121                                true ) ;
122             cout << "test" << myrank << " Send RequestId " << SendRequestId[j]
123                  << " target " << target << " tag " << tag << " error " << error
124                  << endl ;
125             mpi_access.deleteRequest( SendRequestId[j] ) ;
126           }
127         }
128         cout << "test" << myrank << " " << j << " -> Test-Recv("<< SendRequestId[j]
129              << ")" << endl ;
130         mpi_access.test( RecvRequestId[j], flag ) ;
131         if ( flag ) {
132           int source, tag, error, outcount ;
133           mpi_access.status( RecvRequestId[j], source, tag, error, outcount,
134                              true ) ;
135           cout << "test" << myrank << " Recv RequestId" << j << " "
136                << RecvRequestId[j] << " source " << source << " tag " << tag
137                << " error " << error << " outcount " << outcount << endl ;
138           if ( (outcount != 1) | (recvbuf[j] != j) ) {
139             ostringstream strstream ;
140             strstream << "====================================================="
141                       << endl << "test" << myrank << " outcount "
142                       << outcount << " recvbuf[ " << j << " ] " << recvbuf[j] << " KO"
143                       << endl << "====================================================="
144                       << endl ;
145             cout << strstream.str() << endl ;
146             CPPUNIT_FAIL( strstream.str() ) ;
147           }
148         }
149      }
150      if ( myrank == 0 ) {
151        if ( i != maxsend-1 ) {
152          sendbuf[i+1] = i + 1 ;
153          sts = mpi_access.ISend(&sendbuf[i+1],1,MPI_INT,alltarget[myrank],
154                                 SendRequestId[i+1]) ;
155          cout << "test" << myrank << " Send RequestId " << SendRequestId[i+1]
156               << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
157        }
158      }
159      else {
160        sendbuf[i] = i ;
161        sts = mpi_access.ISend(&sendbuf[i],1,MPI_INT,alltarget[myrank],
162                               SendRequestId[i]) ;
163        cout << "test" << myrank << " Send RequestId " << SendRequestId[i]
164             << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
165      }
166      mpi_access.errorString(sts, msgerr, &lenerr) ;
167      cout << "test" << myrank << " lenerr " << lenerr
168           << " " << msgerr << endl ;
169
170      if ( sts != MPI_SUCCESS ) {
171        ostringstream strstream ;
172        strstream << "==========================================================="
173                  << "test" << myrank << " KO"
174                  << "==========================================================="
175                  << endl ;
176        cout << strstream.str() << endl ;
177        CPPUNIT_FAIL( strstream.str() ) ;
178      }
179      mpi_access.check() ;
180   }
181
182   int flag ;
183   mpi_access.testAll(maxsend,SendRequestId,flag) ;
184   mpi_access.testAll(maxsend,RecvRequestId,flag) ;
185   mpi_access.waitAll(maxsend,SendRequestId) ;
186   mpi_access.deleteRequests(maxsend,SendRequestId) ;
187   mpi_access.waitAll(maxsend,RecvRequestId) ;
188   mpi_access.deleteRequests(maxsend,RecvRequestId) ;
189   mpi_access.check() ;
190   mpi_access.testAll(maxsend,SendRequestId,flag) ;
191   if ( !flag ) {
192     ostringstream strstream ;
193     strstream << "=========================================================" << endl
194               << "test" << myrank << " TestAllSendflag " << flag << " KO" << endl
195               << "=========================================================" << endl ;
196     cout << strstream.str() << endl ;
197     CPPUNIT_FAIL( strstream.str() ) ;
198   }
199   else {
200     cout << "=========================================================" << endl
201          << "test" << myrank << " TestAllSendflag " << flag << " OK" << endl
202          << "=========================================================" << endl ;
203   }
204   mpi_access.testAll(maxsend,RecvRequestId,flag) ;
205   if ( !flag ) {
206     ostringstream strstream ;
207     strstream << "=========================================================" << endl
208               << "test" << myrank << " TestAllRecvflag " << flag << " KO" << endl
209               << "=========================================================" << endl ;
210     cout << strstream.str() << endl ;
211     CPPUNIT_FAIL( strstream.str() ) ;
212   }
213   else {
214     cout << "=========================================================" << endl
215          << "test" << myrank << " TestAllRecvflag " << flag << " OK" << endl
216          << "=========================================================" << endl ;
217   }
218
219   int sendrequests[maxsend] ;
220   int sendreqsize = mpi_access.sendRequestIds( alltarget[myrank] , maxsend ,
221                                                sendrequests ) ;
222   if ( sendreqsize != 0 ) {
223     ostringstream strstream ;
224     strstream << "=========================================================" << endl
225               << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
226               << "=========================================================" << endl ;
227     cout << strstream.str() << endl ;
228     int source, tag, error, outcount ;
229     mpi_access.status(sendrequests[0], source, tag, error, outcount, true) ;
230     cout << "test" << myrank << " RequestId " << sendrequests[0]
231          << " source " << source << " tag " << tag << " error " << error
232          << " outcount " << outcount << endl ;
233     CPPUNIT_FAIL( strstream.str() ) ;
234   }
235   else {
236     cout << "=========================================================" << endl
237          << "test" << myrank << " sendreqsize " << sendreqsize << " OK" << endl
238          << "=========================================================" << endl ;
239   }
240   int recvrequests[maxsend] ;
241   int recvreqsize = mpi_access.sendRequestIds( allsource[myrank] , maxsend ,
242                                                recvrequests ) ;
243   if ( recvreqsize != 0 ) {
244     ostringstream strstream ;
245     strstream << "=========================================================" << endl
246               << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
247               << "=========================================================" << endl ;
248     cout << strstream.str() << endl ;
249     CPPUNIT_FAIL( strstream.str() ) ;
250   }
251   else {
252     cout << "=========================================================" << endl
253          << "test" << myrank << " recvreqsize " << recvreqsize << " OK" << endl
254          << "=========================================================" << endl ;
255   }
256
257   mpi_access.barrier() ;
258
259   delete group ;
260
261 //  MPI_Finalize();
262
263   cout << "test" << myrank << " OK" << endl ;
264
265   return ;
266 }
267
268
269
270