Salome HOME
Copyright update 2021
[tools/medcoupling.git] / src / ParaMEDMEMTest / MPIAccess / test_MPI_Access_IProbe.cxx
1 // Copyright (C) 2007-2021  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 <time.h>
21 #include <string>
22 #include <vector>
23 #include <map>
24 #include <iostream>
25 #include <mpi.h>
26
27 #ifndef WIN32
28 #include <unistd.h>
29 #endif
30
31 #include "MPIAccessTest.hxx"
32 #include <cppunit/TestAssert.h>
33
34 //#include "CommInterface.hxx"
35 //#include "ProcessorGroup.hxx"
36 //#include "MPIProcessorGroup.hxx"
37 #include "MPIAccess.hxx"
38
39 // use this define to enable lines, execution of which leads to Segmentation Fault
40 #define ENABLE_FAULTS
41
42 // use this define to enable CPPUNIT asserts and fails, showing bugs
43 #define ENABLE_FORCED_FAILURES
44
45 using namespace std;
46 using namespace MEDCoupling;
47
48 void MPIAccessTest::test_MPI_Access_IProbe() {
49
50   debugStream << "test_MPI_Access_IProbe" << endl ;
51
52 //  MPI_Init(&argc, &argv) ; 
53
54   int size ;
55   int myrank ;
56   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
57   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
58
59   if ( size < 2 ) {
60     ostringstream strstream ;
61     strstream << "test_MPI_Access_IProbe must be run with 2 procs" << endl ;
62     cerr << strstream.str() << endl ;
63     //CPPUNIT_FAIL( strstream.str() ) ;
64     return;
65   }
66
67   debugStream << "test_MPI_Access_IProbe" << myrank << endl ;
68
69   MEDCoupling::CommInterface interface ;
70
71   MEDCoupling::MPIProcessorGroup* group = new MEDCoupling::MPIProcessorGroup(interface) ;
72
73   MEDCoupling::MPIAccess mpi_access( group ) ;
74
75   if ( myrank >= 2 ) {
76     mpi_access.barrier() ;
77     delete group ;
78     return ;
79   }
80
81   int target = 1 - myrank ;
82   int sendbuf[10] ;
83   int RequestId[10] ;
84   int sts = 0;
85   int i ;
86   for ( i = 0 ; i < 10 ; i++ ) {
87      if ( myrank == 0 ) {
88        sendbuf[i] = i ;
89        sts = mpi_access.ISend(&sendbuf[i],1,MPI_INT,target, RequestId[i]) ;
90        debugStream << "test" << myrank << " Send RequestId " << RequestId[i]
91             << endl ;
92      }
93      else {
94        int flag = false ;
95        while ( !flag ) {
96             int source, tag, outcount ;
97             MPI_Datatype datatype ;
98             sts = mpi_access.IProbe(target, source, tag, datatype, outcount, flag ) ;
99             if ( flag ) {
100               debugStream << "test" << myrank << " " << i << " IProbe target " << target
101                    << " source " << source << " tag " << tag
102                    << " outcount " << outcount << " flag " << flag << endl ;
103             }
104             else {
105               debugStream << "test" << myrank << " IProbe flag " << flag << endl ;
106               sleep( 1 ) ;
107             }
108             if ( flag ) {
109               int recvbuf ;
110               sts = mpi_access.recv(&recvbuf,outcount,datatype,source, RequestId[i],
111                                     &outcount) ;
112               if ( (outcount != 1) | (recvbuf != i) ) {
113                 ostringstream strstream ;
114                 strstream << "==========================================================="
115                           << endl << "test" << myrank << " outcount " << outcount
116                           << " recvbuf " << recvbuf << " KO" << endl
117                           << "==========================================================="
118                           << endl ;
119                 debugStream << strstream.str() << endl ;
120                 CPPUNIT_FAIL( strstream.str() ) ;
121               }
122               debugStream << "==========================================================="
123                    << endl << "test" << myrank << " outcount " << outcount
124                    << " recvbuf " << recvbuf << " OK" << endl
125                    << "==========================================================="
126                    << endl ;
127             }
128        }
129      }
130      char msgerr[MPI_MAX_ERROR_STRING] ;
131      int lenerr ;
132      mpi_access.errorString(sts, msgerr, &lenerr) ;
133      debugStream << "test" << myrank << " lenerr " << lenerr << " "
134           << msgerr << endl ;
135
136      if ( sts != MPI_SUCCESS ) {
137        ostringstream strstream ;
138        strstream << "==========================================================="
139                  << "test" << myrank << " KO"
140                  << "==========================================================="
141                  << endl ;
142        debugStream << strstream.str() << endl ;
143        CPPUNIT_FAIL( strstream.str() ) ;
144      }
145      if(MPI_ACCESS_VERBOSE) mpi_access.check() ;
146   }
147   int flag ;
148   mpi_access.testAll(10,RequestId,flag) ;
149   mpi_access.waitAll(10,RequestId) ;
150   mpi_access.deleteRequests(10,RequestId) ;
151   mpi_access.testAll(10,RequestId,flag) ;
152   if ( !flag ) {
153     ostringstream strstream ;
154     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
155     debugStream << strstream.str() << endl ;
156     CPPUNIT_FAIL( strstream.str() ) ;
157   }
158   if(MPI_ACCESS_VERBOSE) mpi_access.check() ;
159
160   mpi_access.barrier() ;
161
162   delete group ;
163
164 //  MPI_Finalize();
165
166   debugStream << "test" << myrank << " OK" << endl ;
167
168   return ;
169 }
170
171
172
173