Salome HOME
adding a new test for makeMesh method.
[modules/med.git] / src / ParaMEDMEM / Test / test_MPI_Access_Cyclic_Send_Recv.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 #include "MPIAccessTest.hxx"
25 #include <cppunit/TestAssert.h>
26
27 //#include "CommInterface.hxx"
28 //#include "ProcessorGroup.hxx"
29 //#include "MPIProcessorGroup.hxx"
30 #include "MPIAccess.hxx"
31
32 // use this define to enable lines, execution of which leads to Segmentation Fault
33 #define ENABLE_FAULTS
34
35 // use this define to enable CPPUNIT asserts and fails, showing bugs
36 #define ENABLE_FORCED_FAILURES
37
38 using namespace std;
39 using namespace ParaMEDMEM;
40
41 void MPIAccessTest::test_MPI_Access_Cyclic_Send_Recv() {
42
43   cout << "test_MPI_Access_Cyclic_Send_Recv" << endl ;
44
45 //  MPI_Init(&argc, &argv) ; 
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 < 3 ) {
53     cout << "test_MPI_Access_Send_Recv must be runned with 3 procs" << endl ;
54     CPPUNIT_FAIL("test_MPI_Access_Send_Recv must be runned with 3 procs") ;
55   }
56
57   cout << "test_MPI_Access_Cyclic_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 >= 3 ) {
66     mpi_access.barrier() ;
67     delete group ;
68     return ;
69   }
70
71   int alltarget[3] = {1 , 2 , 0 } ;
72   int allsource[3] = {2 , 0 , 1 } ;
73   int RequestId[10] ;
74   int sts ;
75   int i = 0 ;
76   if ( myrank == 0 ) {
77     sts = mpi_access.send(&i,1,MPI_INT,alltarget[myrank], RequestId[i]) ;
78     cout << "test" << myrank << " Send RequestId " << RequestId[i]
79          << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
80   }
81   for ( i = 0 ; i < 10 ; i++ ) {
82      int recvbuf ;
83      int outcount ;
84      if ( i & 1 ) {
85        outcount = 0 ;
86        sts = mpi_access.recv(&recvbuf,1,MPI_INT,allsource[myrank], RequestId[i],
87                              &outcount) ;
88      }
89      else {
90        sts = mpi_access.recv(&recvbuf,1,MPI_INT,allsource[myrank], RequestId[i]) ;
91        outcount = 1 ;
92      }
93      //int source, tag, error, outcount ;
94      //mpi_access.Status( RequestId[i], source, tag, error, outcount, true) ;
95      cout << "test" << myrank << " Recv RequestId " << RequestId[i]
96           << " tag " << mpi_access.recvMPITag(allsource[myrank])
97           << " outcount " << outcount << endl ;
98      if ( (outcount != 1) | (recvbuf != i) ) {
99        ostringstream strstream ;
100        strstream << "==========================================================="
101                  << "test" << myrank << " outcount "
102                  << outcount << " recvbuf " << recvbuf << " KO"
103                  << "==========================================================="
104                  << endl ;
105        cout << strstream.str() << endl ;
106        CPPUNIT_FAIL( strstream.str() ) ;
107      }
108      if ( myrank == 0 ) {
109        if ( i != 9 ) {
110          int ii = i + 1 ;
111          sts = mpi_access.send(&ii,1,MPI_INT,alltarget[myrank], RequestId[i]) ;
112          cout << "test" << myrank << " Send RequestId " << RequestId[i]
113               << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
114        }
115      }
116      else {
117        sts = mpi_access.send(&i,1,MPI_INT,alltarget[myrank], RequestId[i]) ;
118        cout << "test" << myrank << " Send RequestId " << RequestId[i]
119             << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
120      }
121      char msgerr[MPI_MAX_ERROR_STRING] ;
122      int lenerr ;
123      mpi_access.errorString(sts, msgerr, &lenerr) ;
124      cout << "test" << myrank << " lenerr " << lenerr
125           << " " << msgerr << endl ;
126
127      if ( sts != MPI_SUCCESS ) {
128        ostringstream strstream ;
129        strstream << "==========================================================="
130                  << "test" << myrank << " KO"
131                  << "==========================================================="
132                  << endl ;
133        cout << strstream.str() << endl ;
134        CPPUNIT_FAIL( strstream.str() ) ;
135      }
136      mpi_access.check() ;
137   }
138
139   int flag ;
140   mpi_access.testAll(10,RequestId,flag) ;
141   if ( !flag ) {
142     ostringstream strstream ;
143     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
144     cout << strstream.str() << endl ;
145     CPPUNIT_FAIL( strstream.str() ) ;
146   }
147   mpi_access.waitAll(10,RequestId) ;
148   mpi_access.check() ;
149
150   int sendrequests[10] ;
151   int sendreqsize = mpi_access.sendRequestIds( alltarget[myrank] , 10 ,
152                                                sendrequests ) ;
153   if ( sendreqsize != 0 ) {
154     ostringstream strstream ;
155     strstream << "=========================================================" << endl
156               << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
157               << "=========================================================" << endl ;
158     cout << strstream.str() << endl ;
159     CPPUNIT_FAIL( strstream.str() ) ;
160   }
161   int recvrequests[10] ;
162   int recvreqsize = mpi_access.sendRequestIds( allsource[myrank] , 10 ,
163                                                recvrequests ) ;
164   if ( recvreqsize != 0 ) {
165     ostringstream strstream ;
166     strstream << "=========================================================" << endl
167               << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
168               << "=========================================================" << endl ;
169     cout << strstream.str() << endl ;
170     CPPUNIT_FAIL( strstream.str() ) ;
171   }
172
173   mpi_access.barrier() ;
174
175   delete group ;
176
177 //  MPI_Finalize();
178
179   cout << "test" << myrank << " OK" << endl ;
180
181   return ;
182 }
183
184
185
186