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