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