Salome HOME
Update copyrights
[tools/medcoupling.git] / src / ParaMEDMEMTest / test_MPI_Access_Probe.cxx
1 // Copyright (C) 2007-2019  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_Probe() {
44
45   debugStream << "test_MPI_Access_Probe" << 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 << "test_MPI_Access_Probe must be run with 2 procs" << endl ;
56     //CPPUNIT_FAIL("test_MPI_Access_Probe must be run with 2 procs") ;
57     return;
58   }
59
60   debugStream << "test_MPI_Access_Probe" << 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 RequestId[10] ;
76   int sts ;
77   int i ;
78   for ( i = 0 ; i < 10 ; i++ ) {
79      if ( myrank == 0 ) {
80        sts = mpi_access.send(&i,1,MPI_INT,target, RequestId[i]) ;
81        debugStream << "test" << myrank << " Send RequestId " << RequestId[i]
82             << endl ;
83      }
84      else {
85        int source, tag, outcount ;
86        MPI_Datatype datatype ;
87        sts = mpi_access.probe(target, source, tag, datatype, outcount ) ;
88        debugStream << "test" << myrank << " Probe target " << target << " source " << source
89             << " tag " << tag << " outcount " << outcount << endl ;
90        int recvbuf ;
91        sts = mpi_access.recv(&recvbuf,outcount,datatype,source, RequestId[i],
92                              &outcount) ;
93        if ( (outcount != 1) | (recvbuf != i) ) {
94          ostringstream strstream ;
95          strstream << "==========================================================="
96                    << "test" << myrank << " outcount " << outcount
97                    << " recvbuf " << recvbuf << " KO"
98                    << "==========================================================="
99                    << endl ;
100          debugStream << strstream.str() << endl ;
101          CPPUNIT_FAIL( strstream.str() ) ;
102        }
103      }
104      char msgerr[MPI_MAX_ERROR_STRING] ;
105      int lenerr ;
106      mpi_access.errorString(sts, msgerr, &lenerr) ;
107      debugStream << "test" << myrank << " lenerr " << lenerr << " "
108           << msgerr << endl ;
109
110      if ( sts != MPI_SUCCESS ) {
111        ostringstream strstream ;
112        strstream << "==========================================================="
113                  << "test" << myrank << " KO"
114                  << "==========================================================="
115                  << endl ;
116        debugStream << strstream.str() << endl ;
117        CPPUNIT_FAIL( strstream.str() ) ;
118      }
119      if(MPI_ACCESS_VERBOSE) mpi_access.check() ;
120   }
121   int flag ;
122   mpi_access.testAll(10,RequestId,flag) ;
123   if ( !flag ) {
124     ostringstream strstream ;
125     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
126     debugStream << strstream.str() << endl ;
127     CPPUNIT_FAIL( strstream.str() ) ;
128   }
129   mpi_access.waitAll(10,RequestId) ;
130   if(MPI_ACCESS_VERBOSE) mpi_access.check() ;
131
132   mpi_access.barrier() ;
133
134   delete group ;
135
136 //  MPI_Finalize();
137
138   debugStream << "test" << myrank << " OK" << endl ;
139
140   return ;
141 }
142
143
144
145