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