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