Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / ParaMEDMEM / Test / ParaMEDMEMTest_MPIProcessorGroup.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 "ParaMEDMEMTest.hxx"
20 #include <cppunit/TestAssert.h>
21 #include "CommInterface.hxx"
22 #include "ProcessorGroup.hxx"
23 #include "MPIProcessorGroup.hxx"
24 #include "InterpolationUtils.hxx"
25
26 #include <string>
27
28 // use this define to enable lines, execution of which leads to Segmentation Fault
29 #define ENABLE_FAULTS
30
31 // use this define to enable CPPUNIT asserts and fails, showing bugs
32 #define ENABLE_FORCED_FAILURES
33
34
35 using namespace std;
36 using namespace ParaMEDMEM;
37  
38 /*
39  * Check methods defined in MPPIProcessorGroup.hxx
40  *
41  (+) MPIProcessorGroup(const CommInterface& interface);
42  (+) MPIProcessorGroup(const CommInterface& interface, set<int> proc_ids);
43  (u) MPIProcessorGroup (const ProcessorGroup& proc_group, set<int> proc_ids);
44  (+) MPIProcessorGroup(const CommInterface& interface,int pstart, int pend);
45  (+) virtual ~MPIProcessorGroup();
46  (+) virtual ProcessorGroup* fuse (const ProcessorGroup&) const;
47  (u) void intersect (ProcessorGroup&){};
48  (+) int myRank() const {int rank; MPI_Comm_rank(_comm,&rank); return rank;}
49  (+) bool containsMyRank() const { int rank; MPI_Group_rank(_group, &rank); return (rank!=MPI_UNDEFINED);}
50  (+) int translateRank(const ProcessorGroup* group, int rank) const;
51  (+) const MPI_Comm* getComm() const {return &_comm;}
52  (+) ProcessorGroup* createComplementProcGroup() const;
53  (o) ProcessorGroup* createProcGroup() const;
54    
55 */
56  
57 void ParaMEDMEMTest::testMPIProcessorGroup_constructor()
58 {
59   CommInterface comm_interface;
60   MPIProcessorGroup* group= new MPIProcessorGroup(comm_interface);
61   int size;
62   MPI_Comm_size(MPI_COMM_WORLD, &size);
63   CPPUNIT_ASSERT_EQUAL(size,group->size());
64   int size2;
65   const MPI_Comm* communicator=group->getComm();
66   MPI_Comm_size(*communicator, &size2);
67   CPPUNIT_ASSERT_EQUAL(size,size2);
68   delete group;
69   
70   set <int> procs;
71   
72   procs.insert(0);
73   procs.insert(1);
74   if (size==1)
75     CPPUNIT_ASSERT_THROW(group=new MPIProcessorGroup(comm_interface,procs),INTERP_KERNEL::Exception);
76   else
77     {
78       CPPUNIT_ASSERT_NO_THROW(  group=new MPIProcessorGroup(comm_interface,procs));
79       CPPUNIT_ASSERT_EQUAL (group->size(),2);
80       delete group;
81     }
82   
83   
84   //throws because plast<pfirst
85   CPPUNIT_ASSERT_THROW(group=new MPIProcessorGroup(comm_interface,1,0),INTERP_KERNEL::Exception);
86   //throws because plast is beyond size-1
87   CPPUNIT_ASSERT_THROW(group=new MPIProcessorGroup(comm_interface,0,size),INTERP_KERNEL::Exception);
88   if (size>1)
89     {
90       group=new MPIProcessorGroup(comm_interface,0,size-2);
91       CPPUNIT_ASSERT_EQUAL(group->size(),size-1);
92       delete group;
93     }
94   
95 }
96  
97 void ParaMEDMEMTest::testMPIProcessorGroup_boolean()
98 {
99   int size;
100   MPI_Comm_size(MPI_COMM_WORLD, &size);
101   
102   CommInterface comm_interface;
103   MPIProcessorGroup group(comm_interface,0,0);
104   MPIProcessorGroup group2(comm_interface,size-1,size-1);
105   ProcessorGroup* group_fuse=group.fuse(group2);
106   int group_fuse_size=(size==1)?1:2;
107   CPPUNIT_ASSERT_EQUAL(group_fuse_size,group_fuse->size());
108  
109   ProcessorGroup* group_complement=((MPIProcessorGroup*)group_fuse)->createComplementProcGroup();
110   CPPUNIT_ASSERT_EQUAL(group_complement->size(),size-group_fuse_size);
111   
112   delete group_fuse;
113   delete group_complement;
114
115   //intersect not implemented yet
116   //   if (size>1)
117   //   {
118   //     MPIProcessorGroup group3(comm_interface,0,size-2);
119   //     MPIProcessorGroup group4(comm_interface,1,size-1);
120   //     group3.intersect(group4);
121   //     CPPUNIT_ASSERT_EQUAL(group3.size(),size-2);
122   //   }
123 }
124
125 void ParaMEDMEMTest::testMPIProcessorGroup_rank()
126 {
127   int size;
128   MPI_Comm_size(MPI_COMM_WORLD, &size);
129   int rank;
130   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
131   
132   CommInterface comm_interface;
133   MPIProcessorGroup group(comm_interface,0,0);
134   MPIProcessorGroup group2(comm_interface,size-1,size-1);
135   ProcessorGroup* group_fuse=group2.fuse(group);
136   
137   if (group.containsMyRank())
138     CPPUNIT_ASSERT_EQUAL (group.myRank(), rank);
139
140   if (group2.containsMyRank())
141     {
142       int trank=group_fuse->translateRank(&group2,0);
143       if (size==1)
144         CPPUNIT_ASSERT_EQUAL(trank,0);
145       else  
146         CPPUNIT_ASSERT_EQUAL(trank,1);
147     }
148   delete group_fuse;
149 }