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