Salome HOME
Copyrights update 2015.
[modules/med.git] / src / ParaMEDMEMTest / test_MPI_Access_Cyclic_Send_Recv.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 #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_Cyclic_Send_Recv() {
43
44   cout << "test_MPI_Access_Cyclic_Send_Recv" << 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 < 3 ) {
54     cout << "test_MPI_Access_Send_Recv must be runned with 3 procs" << endl ;
55     CPPUNIT_FAIL("test_MPI_Access_Send_Recv must be runned with 3 procs") ;
56   }
57
58   cout << "test_MPI_Access_Cyclic_Send_Recv" << 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 >= 3 ) {
67     mpi_access.barrier() ;
68     delete group ;
69     return ;
70   }
71
72   int alltarget[3] = {1 , 2 , 0 } ;
73   int allsource[3] = {2 , 0 , 1 } ;
74   int RequestId[10] ;
75   int sts ;
76   int i = 0 ;
77   if ( myrank == 0 ) {
78     sts = mpi_access.send(&i,1,MPI_INT,alltarget[myrank], RequestId[i]) ;
79     cout << "test" << myrank << " Send RequestId " << RequestId[i]
80          << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
81   }
82   for ( i = 0 ; i < 10 ; i++ ) {
83      int recvbuf ;
84      int outcount ;
85      if ( i & 1 ) {
86        outcount = 0 ;
87        sts = mpi_access.recv(&recvbuf,1,MPI_INT,allsource[myrank], RequestId[i],
88                              &outcount) ;
89      }
90      else {
91        sts = mpi_access.recv(&recvbuf,1,MPI_INT,allsource[myrank], RequestId[i]) ;
92        outcount = 1 ;
93      }
94      //int source, tag, error, outcount ;
95      //mpi_access.Status( RequestId[i], source, tag, error, outcount, true) ;
96      cout << "test" << myrank << " Recv RequestId " << RequestId[i]
97           << " tag " << mpi_access.recvMPITag(allsource[myrank])
98           << " outcount " << outcount << endl ;
99      if ( (outcount != 1) | (recvbuf != i) ) {
100        ostringstream strstream ;
101        strstream << "==========================================================="
102                  << "test" << myrank << " outcount "
103                  << outcount << " recvbuf " << recvbuf << " KO"
104                  << "==========================================================="
105                  << endl ;
106        cout << strstream.str() << endl ;
107        CPPUNIT_FAIL( strstream.str() ) ;
108      }
109      if ( myrank == 0 ) {
110        if ( i != 9 ) {
111          int ii = i + 1 ;
112          sts = mpi_access.send(&ii,1,MPI_INT,alltarget[myrank], RequestId[i]) ;
113          cout << "test" << myrank << " Send RequestId " << RequestId[i]
114               << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
115        }
116      }
117      else {
118        sts = mpi_access.send(&i,1,MPI_INT,alltarget[myrank], RequestId[i]) ;
119        cout << "test" << myrank << " Send RequestId " << RequestId[i]
120             << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
121      }
122      char msgerr[MPI_MAX_ERROR_STRING] ;
123      int lenerr ;
124      mpi_access.errorString(sts, msgerr, &lenerr) ;
125      cout << "test" << myrank << " lenerr " << lenerr
126           << " " << msgerr << endl ;
127
128      if ( sts != MPI_SUCCESS ) {
129        ostringstream strstream ;
130        strstream << "==========================================================="
131                  << "test" << myrank << " KO"
132                  << "==========================================================="
133                  << endl ;
134        cout << strstream.str() << endl ;
135        CPPUNIT_FAIL( strstream.str() ) ;
136      }
137      mpi_access.check() ;
138   }
139
140   int flag ;
141   mpi_access.testAll(10,RequestId,flag) ;
142   if ( !flag ) {
143     ostringstream strstream ;
144     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
145     cout << strstream.str() << endl ;
146     CPPUNIT_FAIL( strstream.str() ) ;
147   }
148   mpi_access.waitAll(10,RequestId) ;
149   mpi_access.check() ;
150
151   int sendrequests[10] ;
152   int sendreqsize = mpi_access.sendRequestIds( alltarget[myrank] , 10 ,
153                                                sendrequests ) ;
154   if ( sendreqsize != 0 ) {
155     ostringstream strstream ;
156     strstream << "=========================================================" << endl
157               << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
158               << "=========================================================" << endl ;
159     cout << strstream.str() << endl ;
160     CPPUNIT_FAIL( strstream.str() ) ;
161   }
162   int recvrequests[10] ;
163   int recvreqsize = mpi_access.sendRequestIds( allsource[myrank] , 10 ,
164                                                recvrequests ) ;
165   if ( recvreqsize != 0 ) {
166     ostringstream strstream ;
167     strstream << "=========================================================" << endl
168               << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
169               << "=========================================================" << endl ;
170     cout << strstream.str() << endl ;
171     CPPUNIT_FAIL( strstream.str() ) ;
172   }
173
174   mpi_access.barrier() ;
175
176   delete group ;
177
178 //  MPI_Finalize();
179
180   cout << "test" << myrank << " OK" << endl ;
181
182   return ;
183 }
184
185
186
187