Salome HOME
Merge from V6_main 01/04/2013
[tools/medcoupling.git] / src / ParaMEDMEMTest / test_MPI_Access_IProbe.cxx
1 // Copyright (C) 2007-2013  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.
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 ParaMEDMEM;
47
48 void MPIAccessTest::test_MPI_Access_IProbe() {
49
50   cout << "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 runned with 2 procs" << endl ;
62     cout << strstream.str() << endl ;
63     CPPUNIT_FAIL( strstream.str() ) ;
64   }
65
66   cout << "test_MPI_Access_IProbe" << myrank << endl ;
67
68   ParaMEDMEM::CommInterface interface ;
69
70   ParaMEDMEM::MPIProcessorGroup* group = new ParaMEDMEM::MPIProcessorGroup(interface) ;
71
72   ParaMEDMEM::MPIAccess mpi_access( group ) ;
73
74   if ( myrank >= 2 ) {
75     mpi_access.barrier() ;
76     delete group ;
77     return ;
78   }
79
80   int target = 1 - myrank ;
81   int sendbuf[10] ;
82   int RequestId[10] ;
83   int sts ;
84   int i ;
85   for ( i = 0 ; i < 10 ; i++ ) {
86      if ( myrank == 0 ) {
87        sendbuf[i] = i ;
88        sts = mpi_access.ISend(&sendbuf[i],1,MPI_INT,target, RequestId[i]) ;
89        cout << "test" << myrank << " Send RequestId " << RequestId[i]
90             << endl ;
91      }
92      else {
93        int flag = false ;
94        while ( !flag ) {
95             int source, tag, outcount ;
96             MPI_Datatype datatype ;
97             sts = mpi_access.IProbe(target, source, tag, datatype, outcount, flag ) ;
98             if ( flag ) {
99               cout << "test" << myrank << " " << i << " IProbe target " << target
100                    << " source " << source << " tag " << tag
101                    << " outcount " << outcount << " flag " << flag << endl ;
102             }
103             else {
104               cout << "test" << myrank << " IProbe flag " << flag << endl ;
105               sleep( 1 ) ;
106             }
107             if ( flag ) {
108               int recvbuf ;
109               sts = mpi_access.recv(&recvbuf,outcount,datatype,source, RequestId[i],
110                                     &outcount) ;
111               if ( (outcount != 1) | (recvbuf != i) ) {
112                 ostringstream strstream ;
113                 strstream << "==========================================================="
114                           << endl << "test" << myrank << " outcount " << outcount
115                           << " recvbuf " << recvbuf << " KO" << endl
116                           << "==========================================================="
117                           << endl ;
118                 cout << strstream.str() << endl ;
119                 CPPUNIT_FAIL( strstream.str() ) ;
120               }
121               cout << "==========================================================="
122                    << endl << "test" << myrank << " outcount " << outcount
123                    << " recvbuf " << recvbuf << " OK" << endl
124                    << "==========================================================="
125                    << endl ;
126             }
127        }
128      }
129      char msgerr[MPI_MAX_ERROR_STRING] ;
130      int lenerr ;
131      mpi_access.errorString(sts, msgerr, &lenerr) ;
132      cout << "test" << myrank << " lenerr " << lenerr << " "
133           << msgerr << endl ;
134
135      if ( sts != MPI_SUCCESS ) {
136        ostringstream strstream ;
137        strstream << "==========================================================="
138                  << "test" << myrank << " KO"
139                  << "==========================================================="
140                  << endl ;
141        cout << strstream.str() << endl ;
142        CPPUNIT_FAIL( strstream.str() ) ;
143      }
144      mpi_access.check() ;
145   }
146   int flag ;
147   mpi_access.testAll(10,RequestId,flag) ;
148   mpi_access.waitAll(10,RequestId) ;
149   mpi_access.deleteRequests(10,RequestId) ;
150   mpi_access.testAll(10,RequestId,flag) ;
151   if ( !flag ) {
152     ostringstream strstream ;
153     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
154     cout << strstream.str() << endl ;
155     CPPUNIT_FAIL( strstream.str() ) ;
156   }
157   mpi_access.check() ;
158
159   mpi_access.barrier() ;
160
161   delete group ;
162
163 //  MPI_Finalize();
164
165   cout << "test" << myrank << " OK" << endl ;
166
167   return ;
168 }
169
170
171
172