Salome HOME
Merge from V6_main 01/04/2013
[modules/med.git] / src / ParaMEDMEMTest / test_MPI_Access_Send_Recv.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 <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_Send_Recv() {
44
45   cout << "test_MPI_Access_Send_Recv" << endl ;
46
47   int size ;
48   int myrank ;
49   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
50   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
51
52   if ( size < 2 ) {
53     cout << "test_MPI_Access_Send_Recv must be runned with 2 procs" << endl ;
54     CPPUNIT_FAIL("test_MPI_Access_Send_Recv must be runned with 2 procs") ;
55   }
56
57   cout << "test_MPI_Access_Send_Recv" << myrank << endl ;
58
59   ParaMEDMEM::CommInterface interface ;
60
61   ParaMEDMEM::MPIProcessorGroup* group = new ParaMEDMEM::MPIProcessorGroup(interface) ;
62
63   ParaMEDMEM::MPIAccess mpi_access( group ) ;
64
65   if ( myrank >= 2 ) {
66     mpi_access.barrier() ;
67     delete group ;
68     return ;
69   }
70
71   int target = 1 - myrank ;
72   int RequestId[10] ;
73   int sts ;
74   int i ;
75   for ( i = 0 ; i < 10 ; i++ ) {
76      if ( myrank == 0 ) {
77        sts = mpi_access.send(&i,1,MPI_INT,target, RequestId[i]) ;
78        cout << "test" << myrank << " Send RequestId " << RequestId[i]
79             << " tag " << mpi_access.sendMPITag(target) << endl ;
80      }
81      else {
82        int recvbuf ;
83        int outcount ;
84        sts = mpi_access.recv(&recvbuf,1,MPI_INT,target, RequestId[i],&outcount) ;
85        //int source, tag, error, outcount ;
86        //mpi_access.Status( RequestId[i], source, tag, error, outcount, true) ;
87        cout << "test" << myrank << " Recv RequestId " << RequestId[i]
88             << " tag " << mpi_access.recvMPITag(target)
89             << " outcount " << outcount << endl ;
90        if ( (outcount != 1) | (recvbuf != i) ) {
91          ostringstream strstream ;
92          strstream << "==========================================================="
93                    << "test" << myrank << " outcount " << outcount
94                    << " recvbuf " << recvbuf << " KO"
95                    << "==========================================================="
96                    << endl ;
97          cout << strstream.str() << endl ;
98          CPPUNIT_FAIL( strstream.str() ) ;
99        }
100      }
101      char msgerr[MPI_MAX_ERROR_STRING] ;
102      int lenerr ;
103      mpi_access.errorString(sts, msgerr, &lenerr) ;
104      cout << "test" << myrank << " lenerr " << lenerr << " "
105           << msgerr << endl ;
106
107      if ( sts != MPI_SUCCESS ) {
108        ostringstream strstream ;
109        strstream << "==========================================================="
110                  << "test" << myrank << " KO"
111                  << "==========================================================="
112                  << endl ;
113        CPPUNIT_FAIL( strstream.str() ) ;
114      }
115      mpi_access.check() ;
116   }
117   int flag ;
118   mpi_access.testAll(10,RequestId,flag) ;
119   if ( !flag ) {
120     ostringstream strstream ;
121     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
122     cout << strstream.str() << endl ;
123     CPPUNIT_FAIL( strstream.str() ) ;
124   }
125   mpi_access.waitAll(10,RequestId) ;
126   mpi_access.check() ;
127
128   if ( myrank == 0 ) {
129     int sendrequests[10] ;
130     int sendreqsize = mpi_access.sendRequestIds( target , 10 , sendrequests ) ;
131     if ( sendreqsize != 0 ) {
132       ostringstream strstream ;
133       strstream << "=========================================================" << endl
134                 << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
135                 << "=========================================================" << endl ;
136       cout << strstream.str() << endl ;
137       CPPUNIT_FAIL( strstream.str() ) ;
138     }
139   }
140   else {
141     int recvrequests[10] ;
142     int recvreqsize = mpi_access.sendRequestIds( target , 10 , recvrequests ) ;
143     if ( recvreqsize != 0 ) {
144       ostringstream strstream ;
145       strstream << "=========================================================" << endl
146                 << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
147                 << "=========================================================" << endl ;
148       cout << strstream.str() << endl ;
149       CPPUNIT_FAIL( strstream.str() ) ;
150     }
151   }
152
153   mpi_access.barrier() ;
154
155   delete group ;
156
157 //  MPI_Finalize();
158
159   cout << "test" << myrank << " OK" << endl ;
160
161   return ;
162 }
163
164
165
166