Salome HOME
adding a new test for makeMesh method.
[modules/med.git] / src / ParaMEDMEM / Test / test_MPI_Access_Time.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_Time() {
43
44   cout << "test_MPI_Access_Time" << 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_Time must be runned with 2 procs" << endl ;
56     cout << strstream.str() << endl ;
57     CPPUNIT_FAIL( strstream.str() ) ;
58   }
59
60   cout << "test_MPI_Access_Time" << 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 #define maxreq 10
69
70   if ( myrank >= 2 ) {
71     cout << "test_MPI_Access_Time_0 rank" << myrank << " --> mpi_access->Barrier" << endl ;
72     mpi_access.barrier() ;
73     cout << "test_MPI_Access_Time_0 rank" << myrank << " <-- mpi_access->Barrier" << endl ;
74     delete group ;
75     cout << "test_MPI_Access_Time" << myrank << " OK" << endl ;
76     return ;
77   }
78
79   int target = 1 - myrank ;
80   int SendTimeRequestId[maxreq] ;
81   int RecvTimeRequestId[maxreq] ;
82   int SendRequestId[maxreq] ;
83   int RecvRequestId[maxreq] ;
84   int sts ;
85   int sendbuf[maxreq] ;
86   int recvbuf[maxreq] ;
87   int i = 0 ;
88   ParaMEDMEM::TimeMessage aSendTimeMsg[maxreq] ;
89   ParaMEDMEM::TimeMessage aRecvTimeMsg[maxreq] ;
90   double t ;
91   double dt = 1. ;
92   double maxt = 10. ;
93   for ( t = 0 ; t < maxt ; t = t+dt ) {
94     if ( myrank == 0 ) {
95       aSendTimeMsg[i].time = t ;
96       aSendTimeMsg[i].deltatime = dt ;
97       //aSendTimeMsg[i].maxtime = maxt ;
98       //sts = mpi_access.ISend( &aSendTimeMsg , mpi_access.timeExtent() ,
99       sts = mpi_access.ISend( &aSendTimeMsg[i] , 1 ,
100                               mpi_access.timeType() , target ,
101                               SendTimeRequestId[i]) ;
102       cout << "test" << myrank << " ISend RequestId " << SendTimeRequestId[i]
103            << " tag " << mpi_access.sendMPITag(target) << endl ;
104       sendbuf[i] = i ;
105       sts = mpi_access.ISend(&sendbuf[i],1,MPI_INT,target, SendRequestId[i]) ;
106       cout << "test" << myrank << " ISend RequestId " << SendRequestId[i]
107            << " tag " << mpi_access.sendMPITag(target) << endl ;
108     }
109     else {
110       //sts = mpi_access.IRecv( &aRecvTimeMsg , mpi_access.timeExtent() ,
111       sts = mpi_access.IRecv( &aRecvTimeMsg[i] , 1 ,
112                               mpi_access.timeType() , target ,
113                               RecvTimeRequestId[i]) ;
114       cout << "test" << myrank << " IRecv RequestId " << RecvTimeRequestId[i]
115            << " tag " << mpi_access.recvMPITag(target) << endl ;
116       sts = mpi_access.IRecv(&recvbuf[i],1,MPI_INT,target, RecvRequestId[i]) ;
117       cout << "test" << myrank << " IRecv RequestId " << RecvRequestId[i]
118            << " tag " << mpi_access.recvMPITag(target) << endl ;
119     }
120     int j ;
121     for (j = 0 ; j <= i ; j++) {
122       int flag ;
123       if ( myrank == 0 ) {
124         mpi_access.test( SendTimeRequestId[j], flag ) ;
125       }
126       else {
127         mpi_access.test( RecvTimeRequestId[j], flag ) ;
128       }
129       if ( flag ) {
130         int target,source, tag, error, outcount ;
131         if ( myrank == 0 ) {
132           mpi_access.status( SendTimeRequestId[j], target, tag, error, outcount,
133                              true ) ;
134           cout << "test" << myrank << " Test(Send TimeRequestId " << SendTimeRequestId[j]
135                << ") : target " << target << " tag " << tag << " error " << error
136                << " flag " << flag << aSendTimeMsg[j] << endl ;
137         }
138         else {
139           mpi_access.status( RecvTimeRequestId[j], source, tag, error, outcount,
140                              true ) ;
141           cout << "test" << myrank << " Test(Recv TimeRequestId "
142                << RecvTimeRequestId[j] << ") : source " << source << " tag " << tag
143                << " error " << error << " outcount " << outcount
144                << " flag " << flag << aRecvTimeMsg[j] << endl ;
145           if ( (outcount != 1) | (aRecvTimeMsg[j].time != j) ) {
146             ostringstream strstream ;
147             strstream << "==========================================================="
148                       << endl << "test" << myrank << " outcount " << outcount << " KO"
149                       << " RecvTimeRequestId " << RecvTimeRequestId[j] << endl
150                       << "==========================================================="
151                       << endl ;
152             cout << strstream.str() << endl ;
153             CPPUNIT_FAIL( strstream.str() ) ;
154           }
155           else {
156             cout << "==========================================================="
157                  << endl << "test" << myrank << " outcount " << outcount
158                  << " RecvTimeRequestId " << RecvTimeRequestId[j] << " OK" << endl
159                  << "==========================================================="
160                  << endl ;
161           }
162         }
163       }
164       if ( myrank == 0 ) {
165         mpi_access.test( SendRequestId[j], flag ) ;
166       }
167       else {
168         mpi_access.test( RecvRequestId[j], flag ) ;
169       }
170       if ( flag ) {
171         int target,source, tag, error, outcount ;
172         if ( myrank == 0 ) {
173           mpi_access.status( SendRequestId[j], target, tag, error, outcount,
174                              true ) ;
175           cout << "test" << myrank << " Test(Send RequestId " << SendRequestId[j]
176                << ") : target " << target << " tag " << tag << " error " << error
177                << " flag " << flag << endl ;
178         }
179         else {
180           mpi_access.status( RecvRequestId[j], source, tag, error, outcount,
181                              true ) ;
182           cout << "test" << myrank << " Test(Recv RequestId "
183                << RecvRequestId[j] << ") : source " << source << " tag " << tag
184                << " error " << error << " outcount " << outcount
185                << " flag " << flag << endl ;
186           if ( (outcount != 1) | (recvbuf[j] != j) ) {
187             ostringstream strstream ;
188             strstream << "==========================================================="
189                       << endl << "test" << myrank << " outcount "
190                       << outcount << " recvbuf " << recvbuf[j] << " KO" << endl
191                       << "==========================================================="
192                       << endl ;
193             cout << strstream.str() << endl ;
194             CPPUNIT_FAIL( strstream.str() ) ;
195           }
196           else {
197             cout << "==========================================================="
198                  << endl << "test" << myrank << " outcount " << outcount
199                  << " RequestId " << RecvRequestId[j] << " OK" << endl
200                  << "==========================================================="
201                  << endl ;
202           }
203         }
204       }
205     }
206     char msgerr[MPI_MAX_ERROR_STRING] ;
207     int lenerr ;
208     mpi_access.errorString(sts, msgerr, &lenerr) ;
209     cout << "test" << myrank << " lenerr " << lenerr << " "
210          << msgerr << endl ;
211
212     if ( sts != MPI_SUCCESS ) {
213       ostringstream strstream ;
214       strstream << "==========================================================="
215                 << "test" << myrank << " KO"
216                 << "==========================================================="
217                 << endl ;
218       cout << strstream.str() << endl ;
219       CPPUNIT_FAIL( strstream.str() ) ;
220     }
221     i = i + 1 ;
222   }
223
224   mpi_access.check() ;
225   if ( myrank == 0 ) {
226     mpi_access.waitAll(maxreq, SendTimeRequestId) ;
227     mpi_access.deleteRequests(maxreq, SendTimeRequestId) ;
228     mpi_access.waitAll(maxreq, SendRequestId) ;
229     mpi_access.deleteRequests(maxreq, SendRequestId) ;
230   }
231   else {
232     mpi_access.waitAll(maxreq, RecvTimeRequestId) ;
233     mpi_access.deleteRequests(maxreq, RecvTimeRequestId) ;
234     mpi_access.waitAll(maxreq, RecvRequestId) ;
235     mpi_access.deleteRequests(maxreq, RecvRequestId) ;
236   }
237   mpi_access.check() ;
238
239   if ( myrank == 0 ) {
240     int sendrequests[2*maxreq] ;
241     int sendreqsize = mpi_access.sendRequestIds( target , 2*maxreq , sendrequests ) ;
242     if ( sendreqsize != 0 ) {
243       ostringstream strstream ;
244       strstream << "=========================================================" << endl
245                 << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
246                 << "=========================================================" << endl ;
247       cout << strstream.str() << endl ;
248       CPPUNIT_FAIL( strstream.str() ) ;
249     }
250     else {
251       cout << "=========================================================" << endl
252            << "test" << myrank << " sendreqsize " << sendreqsize << " OK" << endl
253            << "=========================================================" << endl ;
254     }
255   }
256   else {
257     int recvrequests[2*maxreq] ;
258     int recvreqsize = mpi_access.sendRequestIds( target , 2*maxreq , recvrequests ) ;
259     if ( recvreqsize != 0 ) {
260       ostringstream strstream ;
261       strstream << "=========================================================" << endl
262                 << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
263                 << "=========================================================" << endl ;
264       cout << strstream.str() << endl ;
265       CPPUNIT_FAIL( strstream.str() ) ;
266     }
267     else {
268       cout << "=========================================================" << endl
269            << "test" << myrank << " recvreqsize " << recvreqsize << " OK" << endl
270            << "=========================================================" << endl ;
271     }
272   }
273
274   cout << "test_MPI_Access_Time_0 rank" << myrank << " --> mpi_access->Barrier" << endl ;
275   mpi_access.barrier() ;
276   cout << "test_MPI_Access_Time_0 rank" << myrank << " <-- mpi_access->Barrier" << endl ;
277
278   delete group ;
279
280   //  MPI_Finalize();
281
282   cout << "test_MPI_Access_Time" << myrank << " OK" << endl ;
283
284   return ;
285 }
286
287
288
289