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