Salome HOME
adding a new test for makeMesh method.
[modules/med.git] / src / ParaMEDMEM / Test / test_MPI_Access_Send_Recv_Length.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
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_Send_Recv_Length() {
43
44   cout << "test_MPI_Access_Send_Recv_Length" << 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 < 2 ) {
54     ostringstream strstream ;
55     strstream << "test_MPI_Access_Send_Recv_Length must be runned with 2 procs" << endl ;
56     cout << strstream.str() << endl ;
57     CPPUNIT_FAIL( strstream.str() ) ;
58   }
59
60   cout << "test_MPI_Access_Send_Recv_Length" << myrank << endl ;
61
62   ParaMEDMEM::CommInterface interface ;
63
64   ParaMEDMEM::MPIProcessorGroup* group = new ParaMEDMEM::MPIProcessorGroup(interface) ;
65
66   ParaMEDMEM::MPIAccess mpi_access( group ) ;
67
68   if ( myrank >= 2 ) {
69     mpi_access.barrier() ;
70     delete group ;
71     return ;
72   }
73
74   int target = 1 - myrank ;
75   int RequestId[10] ;
76   int sendbuf[9000] ;
77   int recvbuf[9000] ;
78   bool recvbufok ;
79   int sts ;
80   int i , j ;
81   for ( i = 0 ; i < 9000 ; i++ ) {
82      sendbuf[i] = i ;
83   }
84   for ( i = 0 ; i < 10 ; i++ ) {
85      if ( myrank == 0 ) {
86        sts = mpi_access.send( sendbuf, 1000*i, MPI_INT, target, RequestId[i] ) ;
87        cout << "test" << myrank << " Send RequestId " << RequestId[i]
88             << " tag " << mpi_access.sendMPITag(target) << endl ;
89      }
90      else {
91        sts = MPI_SUCCESS ;
92        RequestId[i] = -1 ;
93        int outcount = 0 ;
94        if ( i != 0 ) {
95          sts = mpi_access.recv( recvbuf,1000*i+1,MPI_INT,target, RequestId[i],
96                                 &outcount ) ;
97        }
98        //int source, tag, error, outcount ;
99        //mpi_access.Status( RequestId[i], source, tag, error, outcount, true) ;
100        cout << "test" << myrank << " Recv RequestId " << RequestId[i]
101             << " tag " << mpi_access.recvMPITag(target)
102             << " outcount " << outcount << endl ;
103        recvbufok = true ;
104        for ( j = 0 ; j < outcount ; j++ ) {
105           if ( recvbuf[j] != j ) {
106             cout << "test" << myrank << " recvbuf[ " << j << " ] = " << recvbuf[j]
107                  << endl ;
108             recvbufok = false ;
109             break ;
110           }
111        }
112        if ( (outcount != 1000*i) | !recvbufok ) {
113          ostringstream strstream ;
114          strstream << "==========================================================="
115                    << endl << "test" << myrank << " outcount " << outcount
116                    << " recvbuf " << recvbuf << " KO"
117                    << "==========================================================="
118                    << endl ;
119          cout << strstream.str() << endl ;
120          CPPUNIT_FAIL( strstream.str() ) ;
121        }
122      }
123      char msgerr[MPI_MAX_ERROR_STRING] ;
124      int lenerr ;
125      mpi_access.errorString(sts, msgerr, &lenerr) ;
126      cout << "test" << myrank << " lenerr " << lenerr << " "
127           << msgerr << endl ;
128
129      if ( sts != MPI_SUCCESS ) {
130        ostringstream strstream ;
131        strstream << "==========================================================="
132                  << "test" << myrank << " KO"
133                  << "==========================================================="
134                  << endl ;
135        cout << strstream.str() << endl ;
136        CPPUNIT_FAIL( strstream.str() ) ;
137      }
138      mpi_access.check() ;
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   if ( myrank == 0 ) {
152     int sendrequests[10] ;
153     int sendreqsize = mpi_access.sendRequestIds( target , 10 , 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   }
163   else {
164     int recvrequests[10] ;
165     int recvreqsize = mpi_access.sendRequestIds( target , 10 , recvrequests ) ;
166     if ( recvreqsize != 0 ) {
167       ostringstream strstream ;
168       strstream << "=========================================================" << endl
169                 << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
170                 << "=========================================================" << endl ;
171       cout << strstream.str() << endl ;
172       CPPUNIT_FAIL( strstream.str() ) ;
173     }
174   }
175
176   mpi_access.barrier() ;
177
178   delete group ;
179
180 //  MPI_Finalize();
181
182   cout << "test" << myrank << " OK" << endl ;
183
184   return ;
185 }
186
187
188
189