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