Salome HOME
Copyrights update 2015.
[modules/med.git] / src / ParaMEDMEMTest / test_MPI_Access_Probe.cxx
1 // Copyright (C) 2007-2015  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 ParaMEDMEM;
42
43 void MPIAccessTest::test_MPI_Access_Probe() {
44
45   cout << "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     cout << "test_MPI_Access_Probe must be runned with 2 procs" << endl ;
56     CPPUNIT_FAIL("test_MPI_Access_Probe must be runned with 2 procs") ;
57   }
58
59   cout << "test_MPI_Access_Probe" << myrank << endl ;
60
61   ParaMEDMEM::CommInterface interface ;
62
63   ParaMEDMEM::MPIProcessorGroup* group = new ParaMEDMEM::MPIProcessorGroup(interface) ;
64
65   ParaMEDMEM::MPIAccess mpi_access( group ) ;
66
67   if ( myrank >= 2 ) {
68     mpi_access.barrier() ;
69     delete group ;
70     return ;
71   }
72
73   int target = 1 - myrank ;
74   int RequestId[10] ;
75   int sts ;
76   int i ;
77   for ( i = 0 ; i < 10 ; i++ ) {
78      if ( myrank == 0 ) {
79        sts = mpi_access.send(&i,1,MPI_INT,target, RequestId[i]) ;
80        cout << "test" << myrank << " Send RequestId " << RequestId[i]
81             << endl ;
82      }
83      else {
84        int source, tag, outcount ;
85        MPI_Datatype datatype ;
86        sts = mpi_access.probe(target, source, tag, datatype, outcount ) ;
87        cout << "test" << myrank << " Probe target " << target << " source " << source
88             << " tag " << tag << " outcount " << outcount << endl ;
89        int recvbuf ;
90        sts = mpi_access.recv(&recvbuf,outcount,datatype,source, RequestId[i],
91                              &outcount) ;
92        if ( (outcount != 1) | (recvbuf != i) ) {
93          ostringstream strstream ;
94          strstream << "==========================================================="
95                    << "test" << myrank << " outcount " << outcount
96                    << " recvbuf " << recvbuf << " KO"
97                    << "==========================================================="
98                    << endl ;
99          cout << strstream.str() << endl ;
100          CPPUNIT_FAIL( strstream.str() ) ;
101        }
102      }
103      char msgerr[MPI_MAX_ERROR_STRING] ;
104      int lenerr ;
105      mpi_access.errorString(sts, msgerr, &lenerr) ;
106      cout << "test" << myrank << " lenerr " << lenerr << " "
107           << msgerr << endl ;
108
109      if ( sts != MPI_SUCCESS ) {
110        ostringstream strstream ;
111        strstream << "==========================================================="
112                  << "test" << myrank << " KO"
113                  << "==========================================================="
114                  << endl ;
115        cout << strstream.str() << endl ;
116        CPPUNIT_FAIL( strstream.str() ) ;
117      }
118      mpi_access.check() ;
119   }
120   int flag ;
121   mpi_access.testAll(10,RequestId,flag) ;
122   if ( !flag ) {
123     ostringstream strstream ;
124     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
125     cout << strstream.str() << endl ;
126     CPPUNIT_FAIL( strstream.str() ) ;
127   }
128   mpi_access.waitAll(10,RequestId) ;
129   mpi_access.check() ;
130
131   mpi_access.barrier() ;
132
133   delete group ;
134
135 //  MPI_Finalize();
136
137   cout << "test" << myrank << " OK" << endl ;
138
139   return ;
140 }
141
142
143
144