Salome HOME
typo-fix by Kunda
[tools/medcoupling.git] / src / ParaMEDMEMTest / test_AllToAllvDEC.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
29 //#include "CommInterface.hxx"
30 //#include "ProcessorGroup.hxx"
31 //#include "MPIProcessorGroup.hxx"
32 #include "MPIAccessDEC.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 MEDCoupling;
42
43 void MPIAccessDECTest::test_AllToAllvDECSynchronousPointToPoint() {
44   test_AllToAllvDEC( false ) ;
45 }
46 void MPIAccessDECTest::test_AllToAllvDECAsynchronousPointToPoint() {
47   test_AllToAllvDEC( true ) ;
48 }
49
50 static void chksts( int sts , int myrank , MEDCoupling::MPIAccess mpi_access ) {
51   char msgerr[MPI_MAX_ERROR_STRING] ;
52   int lenerr ;
53   if ( sts != MPI_SUCCESS ) {
54     mpi_access.errorString(sts, msgerr, &lenerr) ;
55     debugStream << "test_AllToAllvDEC" << myrank << " lenerr " << lenerr << " "
56          << msgerr << endl ;
57     ostringstream strstream ;
58     strstream << "==========================================================="
59               << "test_AllToAllvDEC" << myrank << " KO"
60               << "==========================================================="
61               << endl ;
62     debugStream << strstream.str() << endl ;
63     CPPUNIT_FAIL( strstream.str() ) ;
64   }
65   return ;
66 }
67
68 void MPIAccessDECTest::test_AllToAllvDEC( bool Asynchronous ) {
69
70   debugStream << "test_AllToAllvDEC" << endl ;
71
72   //  MPI_Init(&argc, &argv) ; 
73
74   int size ;
75   int myrank ;
76   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
77   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
78
79   if ( size < 2 || size > 11 ) {
80     ostringstream strstream ;
81     strstream << "usage :" << endl
82               << "mpirun -np <nbprocs> test_AllToAllvDEC" << endl
83               << " (nbprocs >=2)" << endl
84               << "test must be run with more than 1 proc and less than 12 procs"
85               << endl ;
86     cerr << strstream.str() << endl ;
87     CPPUNIT_FAIL( strstream.str() ) ;
88   }
89
90   //  int Asynchronous = atoi(argv[1]);
91
92   debugStream << "test_AllToAllvDEC" << myrank << endl ;
93
94   MEDCoupling::CommInterface interface ;
95   std::set<int> sourceprocs;
96   std::set<int> targetprocs;
97   int i ;
98   for ( i = 0 ; i < size/2 ; i++ ) {
99     sourceprocs.insert(i);
100   }
101   for ( i = size/2 ; i < size ; i++ ) {
102     targetprocs.insert(i);
103   }
104
105   MEDCoupling::MPIProcessorGroup* sourcegroup = new MEDCoupling::MPIProcessorGroup(interface,sourceprocs) ;
106   MEDCoupling::MPIProcessorGroup* targetgroup = new MEDCoupling::MPIProcessorGroup(interface,targetprocs) ;
107
108   MPIAccessDEC * MyMPIAccessDEC = new MPIAccessDEC( *sourcegroup , *targetgroup ,
109                                                     Asynchronous ) ;
110   
111   MPIAccess * mpi_access = MyMPIAccessDEC->getMPIAccess() ;
112
113 #define maxreq 100
114 #define datamsglength 10
115
116   //  int sts ;
117   int *sendcounts = new int[size] ;
118   int *sdispls = new int[size] ;
119   int *recvcounts = new int[size] ;
120   int *rdispls = new int[size] ;
121   for ( i = 0 ; i < size ; i++ ) {
122     sendcounts[i] = datamsglength-i;
123     sdispls[i] = i*datamsglength ;
124     recvcounts[i] = datamsglength-myrank;
125     rdispls[i] = i*datamsglength ;
126   }
127   int * recvbuf = new int[datamsglength*size] ;
128
129   int ireq ;
130   for ( ireq = 0 ; ireq < maxreq ; ireq++ ) {
131     int * sendbuf = new int[datamsglength*size] ;
132     //    int * sendbuf = (int *) malloc( sizeof(int)*datamsglength*size) ;
133     int j ;
134     for ( j = 0 ; j < datamsglength*size ; j++ ) {
135       sendbuf[j] = myrank*1000000 + ireq*1000 + j ;
136       recvbuf[j] = -1 ;
137     }
138
139     MyMPIAccessDEC->allToAllv( sendbuf, sendcounts , sdispls , MPI_INT ,
140                                recvbuf, recvcounts , rdispls , MPI_INT ) ;
141
142     //    debugStream << "test_AllToAllvDEC" << myrank << " recvbuf before CheckSent" ;
143     //    for ( i = 0 ; i < datamsglength*size ; i++ ) {
144     //       debugStream << " " << recvbuf[i] ;
145     //    }
146     //    debugStream << endl ;
147
148     //    debugStream << "test_AllToAllvDEC" << myrank << " sendbuf " << sendbuf << endl ;
149     //    MyMPIAccessDEC->CheckSent() ;
150
151     int nRecvReq = mpi_access->recvRequestIdsSize() ;
152     //    debugStream << "test_AllToAllvDEC" << myrank << " WaitAllRecv " << nRecvReq << " Requests" << endl ;
153     int *ArrayOfRecvRequests = new int[nRecvReq] ;
154     int nReq = mpi_access->recvRequestIds( nRecvReq, ArrayOfRecvRequests ) ;
155     mpi_access->waitAll( nReq , ArrayOfRecvRequests ) ;
156     mpi_access->deleteRequests( nReq , ArrayOfRecvRequests ) ;
157     delete [] ArrayOfRecvRequests ;
158
159     //    debugStream << "test_AllToAllvDEC" << myrank << " recvbuf" ;
160     //    for ( i = 0 ; i < datamsglength*size ; i++ ) {
161     //       debugStream << " " << recvbuf[i] ;
162     //    }
163     //    debugStream << endl ;
164   }
165
166   //  debugStream << "test_AllToAllvDEC" << myrank << " final CheckSent" << endl ;
167   //  MyMPIAccessDEC->CheckSent() ;
168
169   int nSendReq = mpi_access->sendRequestIdsSize() ;
170   debugStream << "test_AllToAllvDEC" << myrank << " final SendRequestIds " << nSendReq << " SendRequests"
171        << endl ;
172   if ( nSendReq ) {
173     int *ArrayOfSendRequests = new int[nSendReq] ;
174     int nReq = mpi_access->sendRequestIds( nSendReq, ArrayOfSendRequests ) ;
175     mpi_access->waitAll( nReq , ArrayOfSendRequests ) ;
176     delete [] ArrayOfSendRequests ;
177   }
178
179   int nRecvReq = mpi_access->recvRequestIdsSize() ;
180   if ( nRecvReq ) {
181     ostringstream strstream ;
182     strstream << "test_AllToAllvDEC" << myrank << " final RecvRequestIds " << nRecvReq
183               << " RecvRequests # 0 Error" << endl ;
184     debugStream << strstream.str() << endl ;
185     CPPUNIT_FAIL( strstream.str() ) ;
186   }
187   else {
188     debugStream << "test_AllToAllvDEC" << myrank << " final RecvRequestIds " << nRecvReq
189          << " RecvRequests = 0 OK" << endl ;
190   }
191
192   mpi_access->barrier() ;
193
194   delete sourcegroup ;
195   delete targetgroup ;
196   delete MyMPIAccessDEC ;
197   delete [] sendcounts ;
198   delete [] sdispls ;
199   delete [] recvcounts ;
200   delete [] rdispls ;
201   delete [] recvbuf ;
202
203   //  MPI_Finalize();
204
205   debugStream << "test_AllToAllvDEC" << myrank << " OK" << endl ;
206
207   return ;
208 }
209
210
211
212