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