Salome HOME
typo-fix by Kunda
[tools/medcoupling.git] / src / ParaMEDMEMTest / test_MPI_Access_Cancel.cxx
1 // Copyright (C) 2007-2016  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 <time.h>
21 #include <string>
22 #include <vector>
23 #include <map>
24 #include <iostream>
25 #include <mpi.h>
26
27 #ifndef WIN32
28 #include <unistd.h>
29 #endif
30
31 #include "MPIAccessTest.hxx"
32 #include <cppunit/TestAssert.h>
33
34 //#include "CommInterface.hxx"
35 //#include "ProcessorGroup.hxx"
36 //#include "MPIProcessorGroup.hxx"
37 #include "MPIAccess.hxx"
38
39 // use this define to enable lines, execution of which leads to Segmentation Fault
40 #define ENABLE_FAULTS
41
42 // use this define to enable CPPUNIT asserts and fails, showing bugs
43 #define ENABLE_FORCED_FAILURES
44
45 using namespace std;
46 using namespace MEDCoupling;
47
48 void MPIAccessTest::test_MPI_Access_Cancel() {
49
50   debugStream << "test_MPI_Access_Cancel" << endl ;
51
52 //  MPI_Init(&argc, &argv) ; 
53
54   int size ;
55   int myrank ;
56   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
57   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
58
59   if ( size < 2 ) {
60     ostringstream strstream ;
61     strstream << "test_MPI_Access_Cancel must be run with 2 procs" << endl ;
62     cerr << strstream.str() << endl ;
63     //CPPUNIT_FAIL( strstream.str() ) ;
64     return;
65   }
66
67   debugStream << "test_MPI_Access_Cancel" << myrank << endl ;
68
69   MEDCoupling::CommInterface interface ;
70
71   MEDCoupling::MPIProcessorGroup* group = new MEDCoupling::MPIProcessorGroup(interface) ;
72
73   MEDCoupling::MPIAccess mpi_access( group ) ;
74
75   if ( myrank >= 2 ) {
76     mpi_access.barrier() ;
77     delete group ;
78     return ;
79   }
80
81   int target = 1 - myrank ;
82   int intsendbuf[5] ;
83   double doublesendbuf[10] ;
84   int RequestId[10] ;
85   int sts ;
86   int i , j ;
87   for ( j = 0 ; j < 3 ; j++ ) {
88      for ( i = 0 ; i < 10 ; i++ ) {
89         debugStream << "test" << myrank << " ============================ i " << i
90              << "============================" << endl ;
91         if ( myrank == 0 ) {
92           if ( i < 5 ) {
93             intsendbuf[i] = i ;
94             sts = mpi_access.ISend(&intsendbuf[i],1,MPI_INT,target, RequestId[i]) ;
95             debugStream << "test" << myrank << " Send MPI_INT RequestId " << RequestId[i]
96                  << endl ;
97           }
98           else {
99             doublesendbuf[i] = i ;
100             sts = mpi_access.ISend(&doublesendbuf[i],1,MPI_DOUBLE,target,
101                                    RequestId[i]) ;
102             debugStream << "test" << myrank << " Send MPI_DOUBLE RequestId " << RequestId[i]
103                  << endl ;
104           }
105         }
106         else {
107           int flag = false ;
108           while ( !flag ) {
109                int source, tag, outcount ;
110                MPI_Datatype datatype ;
111                sts = mpi_access.IProbe(target, source, tag, datatype, outcount,
112                                        flag ) ;
113                if ( flag ) {
114                  debugStream << "test" << myrank << " " << i << " IProbe target " << target
115                       << " source " << source << " tag " << tag
116                       << " outcount " << outcount << " flag " << flag << endl ;
117                }
118                else {
119                  debugStream << "test" << myrank << " flag " << flag << endl ;
120                  sleep( 1 ) ;
121                }
122                if ( flag ) {
123                  int recvbuf ;
124                  sts = mpi_access.IRecv(&recvbuf,outcount,MPI_INT,source,
125                                         RequestId[i] ) ;
126                  if ( datatype == MPI_INT ) {
127                    int source, tag, error, outcount ;
128                    mpi_access.wait( RequestId[i] ) ;
129                    mpi_access.status( RequestId[i], source, tag, error, outcount,
130                                       true ) ;
131                    if ( (outcount != 1) | (recvbuf != i) ) {
132                      ostringstream strstream ;
133                      strstream << "======================================================"
134                                << endl << "test" << myrank << " outcount " << outcount
135                                << " recvbuf " << recvbuf << " KO" << endl
136                                << "======================================================"
137                                << endl ;
138                      debugStream << strstream.str() << endl ;
139                      CPPUNIT_FAIL( strstream.str() ) ;
140                    }
141                    debugStream << "========================================================"
142                         << endl << "test" << myrank << " outcount " << outcount
143                         << " recvbuf " << recvbuf << " OK" << endl
144                         << "========================================================"
145                         << endl ;
146                  }
147                }
148           }
149         }
150         char msgerr[MPI_MAX_ERROR_STRING] ;
151         int lenerr ;
152         mpi_access.errorString(sts, msgerr, &lenerr) ;
153         debugStream << "test" << myrank << " lenerr " << lenerr << " "
154              << msgerr << endl ;
155         if ( sts != MPI_SUCCESS ) {
156           ostringstream strstream ;
157           strstream << "==========================================================="
158                     << endl << "test" << myrank << " KO"
159                     << "==========================================================="
160                     << endl ;
161           debugStream << strstream.str() << endl ;
162           CPPUNIT_FAIL( strstream.str() ) ;
163         }
164         if(MPI_ACCESS_VERBOSE) mpi_access.check() ;
165      }
166
167      if ( myrank != 0 ) {
168        int iprobe ;
169        for ( iprobe = 5 ; iprobe < 10 ; iprobe++ ) {
170           debugStream << "test" << myrank << " ============================ iprobe "
171                << iprobe << "============================" << endl ;
172           int source, tag, outcount ;
173           MPI_Datatype datatype ;
174           int probeflag = false ;
175           while ( !probeflag ) {
176                sts = mpi_access.IProbe( target, source, tag, datatype, outcount,
177                                         probeflag ) ;
178                char msgerr[MPI_MAX_ERROR_STRING] ;
179                int lenerr ;
180                mpi_access.errorString(sts, msgerr, &lenerr) ;
181                debugStream << "test" << myrank << " IProbe iprobe " << iprobe
182                     << " target " << target << " probeflag " << probeflag
183                     << " tag " << tag << " outcount " << outcount << " datatype "
184                     << datatype << " lenerr " << lenerr << " " << msgerr << endl ;
185                if ( sts != MPI_SUCCESS ) {
186                  ostringstream strstream ;
187                  strstream << "=========================================================="
188                            << endl << "test" << myrank << " IProbe KO iprobe " << iprobe
189                            << endl
190                            << "=========================================================="
191                            << endl ;
192                  debugStream << strstream.str() << endl ;
193                  CPPUNIT_FAIL( strstream.str() ) ;
194                }
195                if ( !probeflag ) {
196                  //debugStream << "========================================================"
197                  //     << endl << "test" << myrank << " IProbe KO(OK) iprobe " << iprobe
198                  //     << " probeflag " << probeflag << endl
199                  //     << "========================================================"
200                  //     << endl ;
201                }
202                else {
203                  debugStream << "test" << myrank << " " << iprobe << " IProbe target "
204                       << target << " source " << source << " tag " << tag
205                       << " outcount " << outcount << " probeflag " << probeflag
206                       << endl ;
207                  if ( datatype != MPI_DOUBLE ) {
208                    ostringstream strstream ;
209                    strstream << "========================================================"
210                              << endl << "test" << myrank << " MPI_DOUBLE KO" << endl
211                              << "========================================================"
212                              << endl ;
213                    debugStream << strstream.str() << endl ;
214                    CPPUNIT_FAIL( strstream.str() ) ;
215                  }
216                  else {
217                    int flag ;
218                    sts = mpi_access.cancel( source, tag, datatype, outcount, flag ) ;
219                    if ( sts != MPI_SUCCESS || !flag ) {
220                      mpi_access.errorString(sts, msgerr, &lenerr) ;
221                      debugStream << "======================================================"
222                           << endl << "test" << myrank << " lenerr " << lenerr << " "
223                           << msgerr << endl << "test" << myrank
224                           << " Cancel PendingIrecv KO flag " << flag << " iprobe "
225                           << iprobe << " Irecv completed" << endl
226                           << "======================================================"
227                           << endl ;
228                      //return 1 ;
229                    }
230                    else {
231                      debugStream << "======================================================"
232                           << endl << "test" << myrank
233                           << " Cancel PendingIrecv OK RequestId " << " flag "
234                           << flag << " iprobe " << iprobe << endl
235                           << "======================================================"
236                           << endl ;
237                    }
238                  }
239                  int Reqtarget, Reqtag, Reqerror, Reqoutcount ;
240                  mpi_access.status( RequestId[iprobe], Reqtarget, Reqtag, Reqerror,
241                                     Reqoutcount, true ) ;
242                  debugStream << "test" << myrank << " Status Reqtarget "<< Reqtarget
243                       << " Reqtag " << Reqtag << " Reqoutcount " << Reqoutcount
244                       << endl ;
245                  int Reqflag ;
246                  sts = mpi_access.cancel( RequestId[iprobe] , Reqflag ) ;
247                  debugStream << "test" << myrank << " " << iprobe
248                       << " Cancel Irecv done Reqtarget " << Reqtarget
249                       << " Reqtag " << Reqtag << " Reqoutcount " << Reqoutcount
250                       << " Reqflag " << Reqflag << endl ;
251                  if ( sts != MPI_SUCCESS || !Reqflag ) {
252                    mpi_access.errorString(sts, msgerr, &lenerr) ;
253                    ostringstream strstream ;
254                    strstream << "========================================================"
255                              << endl << "test" << myrank << " lenerr " << lenerr << " "
256                              << msgerr << endl << "test" << myrank
257                              << " Cancel Irecv KO Reqflag " << Reqflag << " iprobe "
258                              << iprobe << endl
259                              << "========================================================"
260                              << endl ;
261                    debugStream << strstream.str() << endl ;
262                    CPPUNIT_FAIL( strstream.str() ) ;
263                  }
264                  else {
265                    debugStream << "========================================================"
266                         << endl << "test" << myrank
267                         << " Cancel Irecv OK RequestId " << RequestId[iprobe]
268                         << " Reqflag " << Reqflag << " iprobe " << iprobe << endl
269                         << "========================================================"
270                         << endl ;
271                    probeflag = Reqflag ;
272                  }
273                }
274           }
275        }
276      }
277      mpi_access.waitAll(10,RequestId) ;
278      mpi_access.deleteRequests(10,RequestId) ;
279   }
280
281   int source, tag, outcount, flag ;
282   MPI_Datatype datatype ;
283   sts = mpi_access.IProbe(target, source, tag, datatype, outcount, flag ) ;
284   char msgerr[MPI_MAX_ERROR_STRING] ;
285   int lenerr ;
286   mpi_access.errorString(sts, msgerr, &lenerr) ;
287   debugStream << "test" << myrank << " lenerr " << lenerr << " "
288        << msgerr << endl ;
289   if ( sts != MPI_SUCCESS || flag ) {
290     ostringstream strstream ;
291     strstream << "==========================================================="
292               << endl << "test" << myrank << " IProbe KO flag " << flag
293               << " remaining unread/cancelled message :" << endl
294               << " source " << source << " tag " << tag << endl
295               << "==========================================================="
296               << endl ;
297     debugStream << strstream.str() << endl ;
298     CPPUNIT_FAIL( strstream.str() ) ;
299   }
300
301   mpi_access.testAll(10,RequestId,flag) ;
302   mpi_access.waitAll(10,RequestId) ;
303   mpi_access.deleteRequests(10,RequestId) ;
304   mpi_access.testAll(10,RequestId,flag) ;
305   if ( !flag ) {
306     ostringstream strstream ;
307     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
308     debugStream << strstream.str() << endl ;
309     CPPUNIT_FAIL( strstream.str() ) ;
310   }
311   if(MPI_ACCESS_VERBOSE) mpi_access.check() ;
312
313   mpi_access.barrier() ;
314
315   delete group ;
316
317 //  MPI_Finalize();
318
319   debugStream << "test" << myrank << " OK" << endl ;
320
321   return ;
322 }
323
324
325
326