Salome HOME
a041b8b3bc19cd4a4c45f6c2a69d9d6a64091b63
[tools/medcoupling.git] / src / ParaMEDMEMTest / test_MPI_Access_Send_Recv.cxx
1 // Copyright (C) 2007-2020  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_Send_Recv() {
44
45   debugStream << "test_MPI_Access_Send_Recv" << endl ;
46
47   int size ;
48   int myrank ;
49   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
50   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
51
52   if ( size < 2 ) {
53     cerr << "test_MPI_Access_Send_Recv must be run with 2 procs" << endl ;
54     //CPPUNIT_FAIL("test_MPI_Access_Send_Recv must be run with 2 procs") ;
55     return;
56   }
57
58   debugStream << "test_MPI_Access_Send_Recv" << myrank << endl ;
59
60   MEDCoupling::CommInterface interface ;
61
62   MEDCoupling::MPIProcessorGroup* group = new MEDCoupling::MPIProcessorGroup(interface) ;
63
64   MEDCoupling::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 RequestId[10] ;
74   int sts ;
75   int i ;
76   for ( i = 0 ; i < 10 ; i++ ) {
77      if ( myrank == 0 ) {
78        sts = mpi_access.send(&i,1,MPI_INT,target, RequestId[i]) ;
79        debugStream << "test" << myrank << " Send RequestId " << RequestId[i]
80             << " tag " << mpi_access.sendMPITag(target) << endl ;
81      }
82      else {
83        int recvbuf ;
84        int outcount ;
85        sts = mpi_access.recv(&recvbuf,1,MPI_INT,target, RequestId[i],&outcount) ;
86        //int source, tag, error, outcount ;
87        //mpi_access.Status( RequestId[i], source, tag, error, outcount, true) ;
88        debugStream << "test" << myrank << " Recv RequestId " << RequestId[i]
89             << " tag " << mpi_access.recvMPITag(target)
90             << " outcount " << outcount << endl ;
91        if ( (outcount != 1) | (recvbuf != i) ) {
92          ostringstream strstream ;
93          strstream << "==========================================================="
94                    << "test" << myrank << " outcount " << outcount
95                    << " recvbuf " << recvbuf << " KO"
96                    << "==========================================================="
97                    << endl ;
98          debugStream << strstream.str() << endl ;
99          CPPUNIT_FAIL( strstream.str() ) ;
100        }
101      }
102      char msgerr[MPI_MAX_ERROR_STRING] ;
103      int lenerr ;
104      mpi_access.errorString(sts, msgerr, &lenerr) ;
105      debugStream << "test" << myrank << " lenerr " << lenerr << " "
106           << msgerr << endl ;
107
108      if ( sts != MPI_SUCCESS ) {
109        ostringstream strstream ;
110        strstream << "==========================================================="
111                  << "test" << myrank << " KO"
112                  << "==========================================================="
113                  << endl ;
114        CPPUNIT_FAIL( strstream.str() ) ;
115      }
116      if(MPI_ACCESS_VERBOSE) mpi_access.check();
117   }
118   int flag ;
119   mpi_access.testAll(10,RequestId,flag) ;
120   if ( !flag ) {
121     ostringstream strstream ;
122     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
123     debugStream << strstream.str() << endl ;
124     CPPUNIT_FAIL( strstream.str() ) ;
125   }
126   mpi_access.waitAll(10,RequestId) ;
127   if(MPI_ACCESS_VERBOSE) mpi_access.check();
128
129   if ( myrank == 0 ) {
130     int sendrequests[10] ;
131     int sendreqsize = mpi_access.sendRequestIds( target , 10 , sendrequests ) ;
132     if ( sendreqsize != 0 ) {
133       ostringstream strstream ;
134       strstream << "=========================================================" << endl
135                 << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
136                 << "=========================================================" << endl ;
137       debugStream << strstream.str() << endl ;
138       CPPUNIT_FAIL( strstream.str() ) ;
139     }
140   }
141   else {
142     int recvrequests[10] ;
143     int recvreqsize = mpi_access.sendRequestIds( target , 10 , recvrequests ) ;
144     if ( recvreqsize != 0 ) {
145       ostringstream strstream ;
146       strstream << "=========================================================" << endl
147                 << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
148                 << "=========================================================" << endl ;
149       debugStream << strstream.str() << endl ;
150       CPPUNIT_FAIL( strstream.str() ) ;
151     }
152   }
153
154   mpi_access.barrier() ;
155
156   delete group ;
157
158 //  MPI_Finalize();
159
160   debugStream << "test" << myrank << " OK" << endl ;
161
162   return ;
163 }
164
165
166
167