Salome HOME
Get relevant changes from V7_dev branch (copyright update, adm files etc)
[tools/medcoupling.git] / src / ParaMEDMEMTest / test_MPI_Access_SendRecv.cxx
1 // Copyright (C) 2007-2016  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 MEDCoupling;
42
43 void MPIAccessTest::test_MPI_Access_SendRecv() {
44
45   debugStream << "MPIAccessTest::test_MPI_Access_SendRecv" << 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       cerr << "MPIAccessTest::test_MPI_Access_SendRecv must be runned with 2 procs" << endl ;
56     //CPPUNIT_FAIL("test_MPI_Access_SendRecv must be runned with 2 procs") ;
57     return;
58   }
59
60   debugStream << "MPIAccessTest::test_MPI_Access_SendRecv" << myrank << endl ;
61
62   MEDCoupling::CommInterface interface ;
63
64   MEDCoupling::MPIProcessorGroup* group = new MEDCoupling::MPIProcessorGroup(interface) ;
65
66   MEDCoupling::MPIAccess mpi_access( group ) ;
67
68   if ( myrank >= 2 ) {
69     mpi_access.barrier() ;
70     delete group ;
71     return ;
72   }
73
74   int target = 1 - myrank ;
75   int sendRequestId[10] ;
76   int recvRequestId[10] ;
77   int sts ;
78   int i ;
79   for ( i = 0 ; i < 10 ; i++ ) {
80      int recvbuf ;
81      int outcount ;
82      if ( i & 1 ) {
83        outcount = -1 ;
84        sts = mpi_access.sendRecv(&i,1,MPI_INT,target, sendRequestId[i],
85                                  &recvbuf,1,MPI_INT,target, recvRequestId[i],
86                                  &outcount) ;
87      }
88      else {
89        sts = mpi_access.sendRecv(&i,1,MPI_INT,target, sendRequestId[i],
90                                  &recvbuf,1,MPI_INT,target, recvRequestId[i]) ;
91 //       outcount = mpi_access.MPIOutCount( recvRequestId[i] ) ;
92        outcount = 1 ;
93      }
94      debugStream << "test" << myrank << " Send sendRequestId " << sendRequestId[i]
95           << " tag " << mpi_access.sendMPITag(target)
96           << " recvRequestId " << recvRequestId[i]
97           << " tag " << mpi_access.recvMPITag(target)
98           << " outcount " << outcount << " MPIOutCount "
99           << mpi_access.MPIOutCount( recvRequestId[i] ) << endl ;
100      if ( (outcount != 1) | (recvbuf != i) ) {
101        ostringstream strstream ;
102        strstream << "==========================================================="
103                  << "test" << myrank << " outcount " << outcount
104                  << " recvbuf " << recvbuf << " KO"
105                  << "==========================================================="
106                  << endl ;
107        debugStream << strstream.str() << endl ;
108        CPPUNIT_FAIL( strstream.str() ) ;
109      }
110      char msgerr[MPI_MAX_ERROR_STRING] ;
111      int lenerr ;
112      mpi_access.errorString(sts, msgerr, &lenerr) ;
113      debugStream << "test" << myrank << " lenerr " << lenerr << " "
114           << msgerr << endl ;
115
116      if ( sts != MPI_SUCCESS ) {
117        ostringstream strstream ;
118        strstream << "==========================================================="
119                  << "test" << myrank << " KO"
120                  << "==========================================================="
121                  << endl ;
122        debugStream << strstream.str() << endl ;
123        CPPUNIT_FAIL( strstream.str() ) ;
124      }
125      if(MPI_ACCESS_VERBOSE) mpi_access.check() ;
126   }
127
128   int flag ;
129   mpi_access.testAll(10,sendRequestId,flag) ;
130   if ( !flag ) {
131     ostringstream strstream ;
132     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
133     debugStream << strstream.str() << endl ;
134     CPPUNIT_FAIL( strstream.str() ) ;
135   }
136   mpi_access.waitAll(10,sendRequestId) ;
137   mpi_access.testAll(10,recvRequestId,flag) ;
138   if ( !flag ) {
139     ostringstream strstream ;
140     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
141     debugStream << strstream.str() << endl ;
142     CPPUNIT_FAIL( strstream.str() ) ;
143   }
144   mpi_access.waitAll(10,recvRequestId) ;
145   if(MPI_ACCESS_VERBOSE) mpi_access.check() ;
146
147   int sendrequests[10] ;
148   int sendreqsize = mpi_access.sendRequestIds( target , 10 , sendrequests ) ;
149   if ( sendreqsize != 0 ) {
150     ostringstream strstream ;
151     strstream << "=========================================================" << endl
152               << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
153               << "=========================================================" << endl ;
154     debugStream << strstream.str() << endl ;
155     CPPUNIT_FAIL( strstream.str() ) ;
156   }
157   int recvrequests[10] ;
158   int recvreqsize = mpi_access.sendRequestIds( target , 10 , recvrequests ) ;
159   if ( recvreqsize != 0 ) {
160     ostringstream strstream ;
161     strstream << "=========================================================" << endl
162               << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
163               << "=========================================================" << endl ;
164     debugStream << strstream.str() << endl ;
165     CPPUNIT_FAIL( strstream.str() ) ;
166   }
167
168   mpi_access.barrier() ;
169
170   delete group ;
171
172 //  MPI_Finalize();
173
174   debugStream << "test" << myrank << " OK" << endl ;
175
176   return ;
177 }
178
179
180
181