Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / ParaMEDMEM / Test / test_MPI_Access_Cancel.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 <time.h>
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_Cancel() {
44
45   cout << "test_MPI_Access_Cancel" << 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_Cancel must be runned with 2 procs" << endl ;
57     cout << strstream.str() << endl ;
58     CPPUNIT_FAIL( strstream.str() ) ;
59   }
60
61   cout << "test_MPI_Access_Cancel" << 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 intsendbuf[5] ;
77   double doublesendbuf[10] ;
78   int RequestId[10] ;
79   int sts ;
80   int i , j ;
81   for ( j = 0 ; j < 3 ; j++ ) {
82      for ( i = 0 ; i < 10 ; i++ ) {
83         cout << "test" << myrank << " ============================ i " << i
84              << "============================" << endl ;
85         if ( myrank == 0 ) {
86           if ( i < 5 ) {
87             intsendbuf[i] = i ;
88             sts = mpi_access.ISend(&intsendbuf[i],1,MPI_INT,target, RequestId[i]) ;
89             cout << "test" << myrank << " Send MPI_INT RequestId " << RequestId[i]
90                  << endl ;
91           }
92           else {
93             doublesendbuf[i] = i ;
94             sts = mpi_access.ISend(&doublesendbuf[i],1,MPI_DOUBLE,target,
95                                    RequestId[i]) ;
96             cout << "test" << myrank << " Send MPI_DOUBLE RequestId " << RequestId[i]
97                  << endl ;
98           }
99         }
100         else {
101           int flag = false ;
102           while ( !flag ) {
103                int source, tag, outcount ;
104                MPI_Datatype datatype ;
105                sts = mpi_access.IProbe(target, source, tag, datatype, outcount,
106                                        flag ) ;
107                if ( flag ) {
108                  cout << "test" << myrank << " " << i << " IProbe target " << target
109                       << " source " << source << " tag " << tag
110                       << " outcount " << outcount << " flag " << flag << endl ;
111                }
112                else {
113                  cout << "test" << myrank << " flag " << flag << endl ;
114                  sleep( 1 ) ;
115                }
116                if ( flag ) {
117                  int recvbuf ;
118                  sts = mpi_access.IRecv(&recvbuf,outcount,MPI_INT,source,
119                                         RequestId[i] ) ;
120                  if ( datatype == MPI_INT ) {
121                    int source, tag, error, outcount ;
122                    mpi_access.wait( RequestId[i] ) ;
123                    mpi_access.status( RequestId[i], source, tag, error, outcount,
124                                       true ) ;
125                    if ( (outcount != 1) | (recvbuf != i) ) {
126                      ostringstream strstream ;
127                      strstream << "======================================================"
128                                << endl << "test" << myrank << " outcount " << outcount
129                                << " recvbuf " << recvbuf << " KO" << endl
130                                << "======================================================"
131                                << endl ;
132                      cout << strstream.str() << endl ;
133                      CPPUNIT_FAIL( strstream.str() ) ;
134                    }
135                    cout << "========================================================"
136                         << endl << "test" << myrank << " outcount " << outcount
137                         << " recvbuf " << recvbuf << " OK" << endl
138                         << "========================================================"
139                         << endl ;
140                  }
141                }
142           }
143         }
144         char msgerr[MPI_MAX_ERROR_STRING] ;
145         int lenerr ;
146         mpi_access.errorString(sts, msgerr, &lenerr) ;
147         cout << "test" << myrank << " lenerr " << lenerr << " "
148              << msgerr << endl ;
149         if ( sts != MPI_SUCCESS ) {
150           ostringstream strstream ;
151           strstream << "==========================================================="
152                     << endl << "test" << myrank << " KO"
153                     << "==========================================================="
154                     << endl ;
155           cout << strstream.str() << endl ;
156           CPPUNIT_FAIL( strstream.str() ) ;
157         }
158         mpi_access.check() ;
159      }
160
161      if ( myrank != 0 ) {
162        int iprobe ;
163        for ( iprobe = 5 ; iprobe < 10 ; iprobe++ ) {
164           cout << "test" << myrank << " ============================ iprobe "
165                << iprobe << "============================" << endl ;
166           int source, tag, outcount ;
167           MPI_Datatype datatype ;
168           int probeflag = false ;
169           while ( !probeflag ) {
170                sts = mpi_access.IProbe( target, source, tag, datatype, outcount,
171                                         probeflag ) ;
172                char msgerr[MPI_MAX_ERROR_STRING] ;
173                int lenerr ;
174                mpi_access.errorString(sts, msgerr, &lenerr) ;
175                cout << "test" << myrank << " IProbe iprobe " << iprobe
176                     << " target " << target << " probeflag " << probeflag
177                     << " tag " << tag << " outcount " << outcount << " datatype "
178                     << datatype << " lenerr " << lenerr << " " << msgerr << endl ;
179                if ( sts != MPI_SUCCESS ) {
180                  ostringstream strstream ;
181                  strstream << "=========================================================="
182                            << endl << "test" << myrank << " IProbe KO iprobe " << iprobe
183                            << endl
184                            << "=========================================================="
185                            << endl ;
186                  cout << strstream.str() << endl ;
187                  CPPUNIT_FAIL( strstream.str() ) ;
188                }
189                if ( !probeflag ) {
190                  //cout << "========================================================"
191                  //     << endl << "test" << myrank << " IProbe KO(OK) iprobe " << iprobe
192                  //     << " probeflag " << probeflag << endl
193                  //     << "========================================================"
194                  //     << endl ;
195                }
196                else {
197                  cout << "test" << myrank << " " << iprobe << " IProbe target "
198                       << target << " source " << source << " tag " << tag
199                       << " outcount " << outcount << " probeflag " << probeflag
200                       << endl ;
201                  if ( datatype != MPI_DOUBLE ) {
202                    ostringstream strstream ;
203                    strstream << "========================================================"
204                              << endl << "test" << myrank << " MPI_DOUBLE KO" << endl
205                              << "========================================================"
206                              << endl ;
207                    cout << strstream.str() << endl ;
208                    CPPUNIT_FAIL( strstream.str() ) ;
209                  }
210                  else {
211                    int flag ;
212                    sts = mpi_access.cancel( source, tag, datatype, outcount, flag ) ;
213                    if ( sts != MPI_SUCCESS || !flag ) {
214                      mpi_access.errorString(sts, msgerr, &lenerr) ;
215                      cout << "======================================================"
216                           << endl << "test" << myrank << " lenerr " << lenerr << " "
217                           << msgerr << endl << "test" << myrank
218                           << " Cancel PendingIrecv KO flag " << flag << " iprobe "
219                           << iprobe << " Irecv completed" << endl
220                           << "======================================================"
221                           << endl ;
222                      //return 1 ;
223                    }
224                    else {
225                      cout << "======================================================"
226                           << endl << "test" << myrank
227                           << " Cancel PendingIrecv OK RequestId " << " flag "
228                           << flag << " iprobe " << iprobe << endl
229                           << "======================================================"
230                           << endl ;
231                    }
232                  }
233                  int Reqtarget, Reqtag, Reqerror, Reqoutcount ;
234                  mpi_access.status( RequestId[iprobe], Reqtarget, Reqtag, Reqerror,
235                                     Reqoutcount, true ) ;
236                  cout << "test" << myrank << " Status Reqtarget "<< Reqtarget
237                       << " Reqtag " << Reqtag << " Reqoutcount " << Reqoutcount
238                       << endl ;
239                  int Reqflag ;
240                  sts = mpi_access.cancel( RequestId[iprobe] , Reqflag ) ;
241                  cout << "test" << myrank << " " << iprobe
242                       << " Cancel Irecv done Reqtarget " << Reqtarget
243                       << " Reqtag " << Reqtag << " Reqoutcount " << Reqoutcount
244                       << " Reqflag " << Reqflag << endl ;
245                  if ( sts != MPI_SUCCESS || !Reqflag ) {
246                    mpi_access.errorString(sts, msgerr, &lenerr) ;
247                    ostringstream strstream ;
248                    strstream << "========================================================"
249                              << endl << "test" << myrank << " lenerr " << lenerr << " "
250                              << msgerr << endl << "test" << myrank
251                              << " Cancel Irecv KO Reqflag " << Reqflag << " iprobe "
252                              << iprobe << endl
253                              << "========================================================"
254                              << endl ;
255                    cout << strstream.str() << endl ;
256                    CPPUNIT_FAIL( strstream.str() ) ;
257                  }
258                  else {
259                    cout << "========================================================"
260                         << endl << "test" << myrank
261                         << " Cancel Irecv OK RequestId " << RequestId[iprobe]
262                         << " Reqflag " << Reqflag << " iprobe " << iprobe << endl
263                         << "========================================================"
264                         << endl ;
265                    probeflag = Reqflag ;
266                  }
267                }
268           }
269        }
270      }
271      mpi_access.waitAll(10,RequestId) ;
272      mpi_access.deleteRequests(10,RequestId) ;
273   }
274
275   int source, tag, outcount, flag ;
276   MPI_Datatype datatype ;
277   sts = mpi_access.IProbe(target, source, tag, datatype, outcount, flag ) ;
278   char msgerr[MPI_MAX_ERROR_STRING] ;
279   int lenerr ;
280   mpi_access.errorString(sts, msgerr, &lenerr) ;
281   cout << "test" << myrank << " lenerr " << lenerr << " "
282        << msgerr << endl ;
283   if ( sts != MPI_SUCCESS || flag ) {
284     ostringstream strstream ;
285     strstream << "==========================================================="
286               << endl << "test" << myrank << " IProbe KO flag " << flag
287               << " remaining unread/cancelled message :" << endl
288               << " source " << source << " tag " << tag << endl
289               << "==========================================================="
290               << endl ;
291     cout << strstream.str() << endl ;
292     CPPUNIT_FAIL( strstream.str() ) ;
293   }
294
295   mpi_access.testAll(10,RequestId,flag) ;
296   mpi_access.waitAll(10,RequestId) ;
297   mpi_access.deleteRequests(10,RequestId) ;
298   mpi_access.testAll(10,RequestId,flag) ;
299   if ( !flag ) {
300     ostringstream strstream ;
301     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
302     cout << strstream.str() << endl ;
303     CPPUNIT_FAIL( strstream.str() ) ;
304   }
305   mpi_access.check() ;
306
307   mpi_access.barrier() ;
308
309   delete group ;
310
311 //  MPI_Finalize();
312
313   cout << "test" << myrank << " OK" << endl ;
314
315   return ;
316 }
317
318
319
320