Salome HOME
fce8694343bb6b16c51ba52d4ab3fbecd26136f4
[tools/medcoupling.git] / src / ParaMEDMEMTest / test_AllToAllDEC.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 <string>
21 #include <vector>
22 #include <map>
23 #include <iostream>
24 #include <mpi.h>
25
26 #include "MPIAccessDECTest.hxx"
27 #include <cppunit/TestAssert.h>
28 #include "MPIAccessDEC.hxx"
29
30 // use this define to enable lines, execution of which leads to Segmentation Fault
31 #define ENABLE_FAULTS
32
33 // use this define to enable CPPUNIT asserts and fails, showing bugs
34 #define ENABLE_FORCED_FAILURES
35
36 using namespace std;
37 using namespace MEDCoupling;
38
39 void MPIAccessDECTest::test_AllToAllDECSynchronousPointToPoint() {
40   test_AllToAllDEC( false ) ;
41 }
42 void MPIAccessDECTest::test_AllToAllDECAsynchronousPointToPoint() {
43   test_AllToAllDEC( true ) ;
44 }
45
46 static void chksts( int sts , int myrank , MEDCoupling::MPIAccess mpi_access ) {
47   char msgerr[MPI_MAX_ERROR_STRING] ;
48   int lenerr ;
49   if ( sts != MPI_SUCCESS ) {
50     mpi_access.errorString(sts, msgerr, &lenerr) ;
51     debugStream << "test" << myrank << " lenerr " << lenerr << " "
52          << msgerr << endl ;
53     ostringstream strstream ;
54     strstream << "===========================================================" << endl
55               << "test_AllToAllDEC" << myrank << " KO" << endl
56               << "==========================================================="
57               << endl ;
58     debugStream << strstream.str() << endl ;
59     CPPUNIT_FAIL( strstream.str() ) ;
60   }
61   return ;
62 }
63
64 void MPIAccessDECTest::test_AllToAllDEC( bool Asynchronous ) {
65
66   debugStream << "test_AllToAllDEC" << endl ;
67
68   //  MPI_Init(&argc, &argv) ; 
69
70   int size ;
71   int myrank ;
72   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
73   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
74
75   if ( size < 2 || size > 11 ) {
76     ostringstream strstream ;
77     strstream << "usage :" << endl
78               << "mpirun -np <nbprocs> test_AllToAllDEC" << endl
79               << " (nbprocs >=2)" << endl
80               << "test must be runned with more than 1 proc and less than 12 procs"
81               << endl ;
82     cerr << strstream.str() << endl ;
83     CPPUNIT_FAIL( strstream.str() ) ;
84   }
85
86   debugStream << "test_AllToAllDEC" << myrank << endl ;
87
88   MEDCoupling::CommInterface interface ;
89   std::set<int> sourceprocs;
90   std::set<int> targetprocs;
91   int i ;
92   for ( i = 0 ; i < size/2 ; i++ ) {
93     sourceprocs.insert(i);
94   }
95   for ( i = size/2 ; i < size ; i++ ) {
96     targetprocs.insert(i);
97   }
98
99   MEDCoupling::MPIProcessorGroup* sourcegroup = new MEDCoupling::MPIProcessorGroup(interface,sourceprocs) ;
100   MEDCoupling::MPIProcessorGroup* targetgroup = new MEDCoupling::MPIProcessorGroup(interface,targetprocs) ;
101
102   MPIAccessDEC * MyMPIAccessDEC = new MPIAccessDEC( *sourcegroup , *targetgroup ,
103                                                     Asynchronous ) ;
104   
105   MPIAccess * mpi_access = MyMPIAccessDEC->getMPIAccess() ;
106
107 #define maxreq 100
108 #define datamsglength 10
109
110   //  int sts ;
111   int sendcount = datamsglength ;
112   int recvcount = datamsglength ;
113   int * recvbuf = new int[datamsglength*size] ;
114
115   int ireq ;
116   for ( ireq = 0 ; ireq < maxreq ; ireq++ ) {
117     int * sendbuf = new int[datamsglength*size] ;
118     int j ;
119     for ( j = 0 ; j < datamsglength*size ; j++ ) {
120       sendbuf[j] = myrank*1000000 + ireq*1000 + j ;
121       recvbuf[j] = -1 ;
122     }
123
124     MyMPIAccessDEC->allToAll( sendbuf, sendcount , MPI_INT ,
125                               recvbuf, recvcount , MPI_INT ) ;
126
127     int nRecvReq = mpi_access->recvRequestIdsSize() ;
128     int *ArrayOfRecvRequests = new int[nRecvReq] ;
129     int nReq = mpi_access->recvRequestIds( nRecvReq, ArrayOfRecvRequests ) ;
130     mpi_access->waitAll( nReq , ArrayOfRecvRequests ) ;
131     mpi_access->deleteRequests( nReq , ArrayOfRecvRequests ) ;
132     delete [] ArrayOfRecvRequests ;
133   }
134
135   int nSendReq = mpi_access->sendRequestIdsSize() ;
136   debugStream << "test_AllToAllDEC" << myrank << " final SendRequestIds " << nSendReq << " SendRequests"
137        << endl ;
138   if ( nSendReq ) {
139     int *ArrayOfSendRequests = new int[nSendReq] ;
140     int nReq = mpi_access->sendRequestIds( nSendReq, ArrayOfSendRequests ) ;
141     mpi_access->waitAll( nReq , ArrayOfSendRequests ) ;
142     delete [] ArrayOfSendRequests ;
143   }
144
145   int nRecvReq = mpi_access->recvRequestIdsSize() ;
146   if ( nRecvReq ) {
147     ostringstream strstream ;
148     strstream << "test_AllToAllDEC" << myrank << " final RecvRequestIds " << nRecvReq
149               << " RecvRequests # 0 Error" << endl ;
150     debugStream << strstream.str() << endl ;
151     CPPUNIT_FAIL( strstream.str() ) ;
152   }
153   else {
154     debugStream << "test_AllToAllDEC" << myrank << " final RecvRequestIds " << nRecvReq
155          << " RecvRequests = 0 OK" << endl ;
156   }
157
158   mpi_access->barrier() ;
159
160   delete sourcegroup ;
161   delete targetgroup ;
162   delete MyMPIAccessDEC ;
163   delete [] recvbuf ;
164
165   //  MPI_Finalize();
166
167   debugStream << "test_AllToAllDEC" << myrank << " OK" << endl ;
168
169   return ;
170 }