]> SALOME platform Git repositories - tools/medcoupling.git/blob - src/ParaMEDMEM/Test/test_MPI_Access_SendRecv.cxx
Salome HOME
Merge from BR_V5_DEV 16Feb09
[tools/medcoupling.git] / src / ParaMEDMEM / Test / test_MPI_Access_SendRecv.cxx
1 //  Copyright (C) 2007-2008  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 #include <string>
20 #include <vector>
21 #include <map>
22 #include <iostream>
23 #include <mpi.h>
24
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 ParaMEDMEM;
41
42 void MPIAccessTest::test_MPI_Access_SendRecv() {
43
44   cout << "MPIAccessTest::test_MPI_Access_SendRecv" << 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 < 2 ) {
54     cout << "MPIAccessTest::test_MPI_Access_SendRecv must be runned with 2 procs" << endl ;
55     CPPUNIT_FAIL("test_MPI_Access_SendRecv must be runned with 2 procs") ;
56   }
57
58   cout << "MPIAccessTest::test_MPI_Access_SendRecv" << myrank << endl ;
59
60   ParaMEDMEM::CommInterface interface ;
61
62   ParaMEDMEM::MPIProcessorGroup* group = new ParaMEDMEM::MPIProcessorGroup(interface) ;
63
64   ParaMEDMEM::MPIAccess mpi_access( group ) ;
65
66   if ( myrank >= 2 ) {
67     mpi_access.barrier() ;
68     delete group ;
69     return ;
70   }
71
72   int target = 1 - myrank ;
73   int sendRequestId[10] ;
74   int recvRequestId[10] ;
75   int sts ;
76   int i ;
77   for ( i = 0 ; i < 10 ; i++ ) {
78      int recvbuf ;
79      int outcount ;
80      if ( i & 1 ) {
81        outcount = -1 ;
82        sts = mpi_access.sendRecv(&i,1,MPI_INT,target, sendRequestId[i],
83                                  &recvbuf,1,MPI_INT,target, recvRequestId[i],
84                                  &outcount) ;
85      }
86      else {
87        sts = mpi_access.sendRecv(&i,1,MPI_INT,target, sendRequestId[i],
88                                  &recvbuf,1,MPI_INT,target, recvRequestId[i]) ;
89 //       outcount = mpi_access.MPIOutCount( recvRequestId[i] ) ;
90        outcount = 1 ;
91      }
92      cout << "test" << myrank << " Send sendRequestId " << sendRequestId[i]
93           << " tag " << mpi_access.sendMPITag(target)
94           << " recvRequestId " << recvRequestId[i]
95           << " tag " << mpi_access.recvMPITag(target)
96           << " outcount " << outcount << " MPIOutCount "
97           << mpi_access.MPIOutCount( recvRequestId[i] ) << endl ;
98      if ( (outcount != 1) | (recvbuf != i) ) {
99        ostringstream strstream ;
100        strstream << "==========================================================="
101                  << "test" << myrank << " outcount " << outcount
102                  << " recvbuf " << recvbuf << " KO"
103                  << "==========================================================="
104                  << endl ;
105        cout << strstream.str() << endl ;
106        CPPUNIT_FAIL( strstream.str() ) ;
107      }
108      char msgerr[MPI_MAX_ERROR_STRING] ;
109      int lenerr ;
110      mpi_access.errorString(sts, msgerr, &lenerr) ;
111      cout << "test" << myrank << " lenerr " << lenerr << " "
112           << msgerr << endl ;
113
114      if ( sts != MPI_SUCCESS ) {
115        ostringstream strstream ;
116        strstream << "==========================================================="
117                  << "test" << myrank << " KO"
118                  << "==========================================================="
119                  << endl ;
120        cout << strstream.str() << endl ;
121        CPPUNIT_FAIL( strstream.str() ) ;
122      }
123      mpi_access.check() ;
124   }
125
126   int flag ;
127   mpi_access.testAll(10,sendRequestId,flag) ;
128   if ( !flag ) {
129     ostringstream strstream ;
130     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
131     cout << strstream.str() << endl ;
132     CPPUNIT_FAIL( strstream.str() ) ;
133   }
134   mpi_access.waitAll(10,sendRequestId) ;
135   mpi_access.testAll(10,recvRequestId,flag) ;
136   if ( !flag ) {
137     ostringstream strstream ;
138     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
139     cout << strstream.str() << endl ;
140     CPPUNIT_FAIL( strstream.str() ) ;
141   }
142   mpi_access.waitAll(10,recvRequestId) ;
143   mpi_access.check() ;
144
145   int sendrequests[10] ;
146   int sendreqsize = mpi_access.sendRequestIds( target , 10 , sendrequests ) ;
147   if ( sendreqsize != 0 ) {
148     ostringstream strstream ;
149     strstream << "=========================================================" << endl
150               << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
151               << "=========================================================" << endl ;
152     cout << strstream.str() << endl ;
153     CPPUNIT_FAIL( strstream.str() ) ;
154   }
155   int recvrequests[10] ;
156   int recvreqsize = mpi_access.sendRequestIds( target , 10 , recvrequests ) ;
157   if ( recvreqsize != 0 ) {
158     ostringstream strstream ;
159     strstream << "=========================================================" << endl
160               << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
161               << "=========================================================" << endl ;
162     cout << strstream.str() << endl ;
163     CPPUNIT_FAIL( strstream.str() ) ;
164   }
165
166   mpi_access.barrier() ;
167
168   delete group ;
169
170 //  MPI_Finalize();
171
172   cout << "test" << myrank << " OK" << endl ;
173
174   return ;
175 }
176
177
178
179