Salome HOME
087c732687e38e96f35a966b1b8c0d3f6ce39b32
[tools/medcoupling.git] / src / ParaMEDMEMTest / test_MPI_Access_Send_Recv_Length.cxx
1 // Copyright (C) 2007-2016  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
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 MEDCoupling;
42
43 void MPIAccessTest::test_MPI_Access_Send_Recv_Length() {
44
45   debugStream << "test_MPI_Access_Send_Recv_Length" << endl ;
46
47 //  MPI_Init(&argc, &argv) ; 
48
49   int size ;
50   int myrank ;
51   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
52   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
53
54   if ( size < 2 ) {
55     ostringstream strstream ;
56     strstream << "test_MPI_Access_Send_Recv_Length must be runned with 2 procs" << endl ;
57     cerr << strstream.str() << endl ;
58     //CPPUNIT_FAIL( strstream.str() ) ;
59     return;
60   }
61
62   debugStream << "test_MPI_Access_Send_Recv_Length" << myrank << endl ;
63
64   MEDCoupling::CommInterface interface ;
65
66   MEDCoupling::MPIProcessorGroup* group = new MEDCoupling::MPIProcessorGroup(interface) ;
67
68   MEDCoupling::MPIAccess mpi_access( group ) ;
69
70   if ( myrank >= 2 ) {
71     mpi_access.barrier() ;
72     delete group ;
73     return ;
74   }
75
76   int target = 1 - myrank ;
77   int RequestId[10] ;
78   int sendbuf[9000] ;
79   int recvbuf[9000] ;
80   bool recvbufok ;
81   int sts ;
82   int i , j ;
83   for ( i = 0 ; i < 9000 ; i++ ) {
84      sendbuf[i] = i ;
85   }
86   for ( i = 0 ; i < 10 ; i++ ) {
87      if ( myrank == 0 ) {
88        sts = mpi_access.send( sendbuf, 1000*i, MPI_INT, target, RequestId[i] ) ;
89        debugStream << "test" << myrank << " Send RequestId " << RequestId[i]
90             << " tag " << mpi_access.sendMPITag(target) << endl ;
91      }
92      else {
93        sts = MPI_SUCCESS ;
94        RequestId[i] = -1 ;
95        int outcount = 0 ;
96        if ( i != 0 ) {
97          sts = mpi_access.recv( recvbuf,1000*i+1,MPI_INT,target, RequestId[i],
98                                 &outcount ) ;
99        }
100        //int source, tag, error, outcount ;
101        //mpi_access.Status( RequestId[i], source, tag, error, outcount, true) ;
102        debugStream << "test" << myrank << " Recv RequestId " << RequestId[i]
103             << " tag " << mpi_access.recvMPITag(target)
104             << " outcount " << outcount << endl ;
105        recvbufok = true ;
106        for ( j = 0 ; j < outcount ; j++ ) {
107           if ( recvbuf[j] != j ) {
108             debugStream << "test" << myrank << " recvbuf[ " << j << " ] = " << recvbuf[j]
109                  << endl ;
110             recvbufok = false ;
111             break ;
112           }
113        }
114        if ( (outcount != 1000*i) | !recvbufok ) {
115          ostringstream strstream ;
116          strstream << "==========================================================="
117                    << endl << "test" << myrank << " outcount " << outcount
118                    << " recvbuf " << recvbuf << " KO"
119                    << "==========================================================="
120                    << endl ;
121          debugStream << strstream.str() << endl ;
122          CPPUNIT_FAIL( strstream.str() ) ;
123        }
124      }
125      char msgerr[MPI_MAX_ERROR_STRING] ;
126      int lenerr ;
127      mpi_access.errorString(sts, msgerr, &lenerr) ;
128      debugStream << "test" << myrank << " lenerr " << lenerr << " "
129           << msgerr << endl ;
130
131      if ( sts != MPI_SUCCESS ) {
132        ostringstream strstream ;
133        strstream << "==========================================================="
134                  << "test" << myrank << " KO"
135                  << "==========================================================="
136                  << endl ;
137        debugStream << strstream.str() << endl ;
138        CPPUNIT_FAIL( strstream.str() ) ;
139      }
140      if(MPI_ACCESS_VERBOSE) mpi_access.check() ;
141   }
142   int flag ;
143   mpi_access.testAll(10,RequestId,flag) ;
144   if ( !flag ) {
145     ostringstream strstream ;
146     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
147     debugStream << strstream.str() << endl ;
148     CPPUNIT_FAIL( strstream.str() ) ;
149   }
150   mpi_access.waitAll(10,RequestId) ;
151   if(MPI_ACCESS_VERBOSE) mpi_access.check() ;
152
153   if ( myrank == 0 ) {
154     int sendrequests[10] ;
155     int sendreqsize = mpi_access.sendRequestIds( target , 10 , sendrequests ) ;
156     if ( sendreqsize != 0 ) {
157       ostringstream strstream ;
158       strstream << "=========================================================" << endl
159                 << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
160                 << "=========================================================" << endl ;
161       debugStream << strstream.str() << endl ;
162       CPPUNIT_FAIL( strstream.str() ) ;
163     }
164   }
165   else {
166     int recvrequests[10] ;
167     int recvreqsize = mpi_access.sendRequestIds( target , 10 , recvrequests ) ;
168     if ( recvreqsize != 0 ) {
169       ostringstream strstream ;
170       strstream << "=========================================================" << endl
171                 << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
172                 << "=========================================================" << endl ;
173       debugStream << strstream.str() << endl ;
174       CPPUNIT_FAIL( strstream.str() ) ;
175     }
176   }
177
178   mpi_access.barrier() ;
179
180   delete group ;
181
182 //  MPI_Finalize();
183
184   debugStream << "test" << myrank << " OK" << endl ;
185
186   return ;
187 }
188
189
190
191