1 // Copyright (C) 2007-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "MEDPARTITIONERTest.hxx"
22 #include "MEDPARTITIONER_MeshCollection.hxx"
23 #include "MEDPARTITIONER_ParallelTopology.hxx"
24 #include "MEDPARTITIONER_ParaDomainSelector.hxx"
25 #include "MEDPARTITIONER_Utils.hxx"
27 #include "CellModel.hxx"
28 #include "MEDFileMesh.hxx"
29 #include "MEDLoader.hxx"
30 #include "MEDLoaderBase.hxx"
31 #include "MEDCouplingUMesh.hxx"
32 #include "MEDCouplingMappedExtrudedMesh.hxx"
33 #include "MEDCouplingFieldDouble.hxx"
34 #include "MEDCouplingMemArray.hxx"
35 #include "MEDCouplingMultiFields.hxx"
37 #include <cppunit/TestAssert.h>
46 #include <unistd.h> // get_current_dir_name()
51 using namespace MEDCoupling;
52 using namespace MEDPARTITIONER;
55 std::string MEDPARTITIONERTest::getPartitionerParaExe() const
58 if ( getenv("MEDCOUPLING_ROOT_DIR") )
60 execName=getenv("MEDCOUPLING_ROOT_DIR");
61 execName+="/bin/medpartitioner_para";
62 std::ifstream my_file(execName.c_str());
66 execName = get_current_dir_name();
67 execName += "/../../MEDPartitioner/medpartitioner_para";
68 if (! std::ifstream(execName.c_str()))
69 CPPUNIT_FAIL("Can't find medpartitioner_para, please set MEDCOUPLING_ROOT_DIR");
73 void MEDPARTITIONERTest::verifyMedpartitionerOnSmallSizeForMesh()
76 string fileName,cmd,execName,sourceName,targetName,input;
77 execName=getPartitionerParaExe();
78 fileName=_file_name_with_faces;
80 MEDCoupling::MEDFileUMesh* initialMesh=MEDCoupling::MEDFileUMesh::New(fileName.c_str(),_mesh_name.c_str());
81 MEDCoupling::MEDCouplingUMesh* cellMesh=initialMesh->getLevel0Mesh(false);
82 MEDCoupling::MEDCouplingUMesh* faceMesh=initialMesh->getLevelM1Mesh(false);
84 cmd="mpirun -np 5 "+execName+" --ndomains=5 --split-method=metis"; //on same proc
87 targetName.replace(targetName.find(".med"),4,"_partitionedTo5_");
88 cmd+=" --input-file="+sourceName+" --output-file="+targetName+" --verbose="+IntToStr(_verbose);
89 if (_verbose) cout<<endl<<cmd<<endl;
90 res=system(cmd.c_str());
91 CPPUNIT_ASSERT_EQUAL(0, res);
92 input=targetName+".xml";
94 MEDPARTITIONER::ParaDomainSelector parallelizer(false);
95 MEDPARTITIONER::MeshCollection collection(input,parallelizer);
96 CPPUNIT_ASSERT_EQUAL(3, collection.getMeshDimension());
97 std::vector<MEDCoupling::MEDCouplingUMesh*>cellMeshes=collection.getMesh();
98 CPPUNIT_ASSERT_EQUAL(5, (int) cellMeshes.size());
100 for (std::size_t i = 0; i < cellMeshes.size(); i++)
101 nbcells+=cellMeshes[i]->getNumberOfCells();
102 CPPUNIT_ASSERT_EQUAL(cellMesh->getNumberOfCells(), nbcells);
104 std::vector<MEDCoupling::MEDCouplingUMesh*>faceMeshes=collection.getFaceMesh();
105 CPPUNIT_ASSERT_EQUAL(5, (int) faceMeshes.size());
107 for (std::size_t i=0; i < faceMeshes.size(); i++)
108 nbfaces+=faceMeshes[i]->getNumberOfCells();
109 CPPUNIT_ASSERT_EQUAL(faceMesh->getNumberOfCells(), nbfaces);
111 //merge split meshes and test equality
112 cmd="mpirun -np 1 "+execName+" --ndomains=1 --split-method=metis"; //on same proc
113 sourceName=targetName+".xml";
115 targetName.replace(targetName.find(".med"),4,"_remergedFrom5_");
116 cmd+=" --input-file="+sourceName+" --output-file="+targetName+" --verbose="+IntToStr(_verbose);
117 if (_verbose) cout<<endl<<cmd<<endl;
118 res=system(cmd.c_str());
119 CPPUNIT_ASSERT_EQUAL(0, res);
121 string refusedName=targetName+"1.med";
122 MEDCoupling::MEDFileUMesh* refusedMesh=MEDCoupling::MEDFileUMesh::New(refusedName.c_str(),_mesh_name.c_str());
123 MEDCoupling::MEDCouplingUMesh* refusedCellMesh=refusedMesh->getLevel0Mesh(false);
124 MEDCoupling::MEDCouplingUMesh* refusedFaceMesh=refusedMesh->getLevelM1Mesh(false);
126 CPPUNIT_ASSERT_EQUAL(cellMesh->getNumberOfCells(), refusedCellMesh->getNumberOfCells());
127 CPPUNIT_ASSERT_EQUAL(faceMesh->getNumberOfCells(), refusedFaceMesh->getNumberOfCells());
130 MEDCoupling::MEDCouplingMesh* mergeCell=cellMesh->mergeMyselfWith(refusedCellMesh);
131 CPPUNIT_ASSERT_EQUAL(cellMesh->getNumberOfCells(), mergeCell->getNumberOfCells());
133 MEDCoupling::MEDCouplingMesh* mergeFace=faceMesh->mergeMyselfWith(refusedFaceMesh);
134 CPPUNIT_ASSERT_EQUAL(faceMesh->getNumberOfCells(), mergeFace->getNumberOfCells());
136 CPPUNIT_ASSERT(faceMesh->isEqual(refusedFaceMesh,1e-12));
139 std::vector<const MEDCouplingUMesh *> meshes;
140 std::vector<DataArrayInt *> corr;
141 meshes.push_back(cellMesh);
142 refusedCellMesh->tryToShareSameCoordsPermute(*cellMesh, 1e-9);
143 meshes.push_back(refusedCellMesh);
144 MEDCouplingUMesh* fusedCell=MEDCouplingUMesh::FuseUMeshesOnSameCoords(meshes,0,corr);
145 CPPUNIT_ASSERT_EQUAL(cellMesh->getNumberOfCells(), fusedCell->getNumberOfCells());
148 for (std::size_t i = 0; i < corr.size(); i++)
151 meshes.push_back(faceMesh);
152 refusedFaceMesh->tryToShareSameCoordsPermute(*faceMesh, 1e-9);
153 meshes.push_back(refusedFaceMesh);
154 MEDCouplingUMesh* fusedFace=MEDCouplingUMesh::FuseUMeshesOnSameCoords(meshes,0,corr);
155 CPPUNIT_ASSERT_EQUAL(faceMesh->getNumberOfCells(), fusedFace->getNumberOfCells());
157 for (std::size_t i = 0; i < corr.size(); i++)
159 fusedFace->decrRef();
160 refusedFaceMesh->decrRef();
162 fusedCell->decrRef();
163 refusedCellMesh->decrRef();
165 //done in ~collection
166 //for (int i = 0; i < faceMeshes.size(); i++) faceMeshes[i]->decrRef();
167 //for (int i = 0; i < cellMeshes.size(); i++) cellMeshes[i]->decrRef();
170 void MEDPARTITIONERTest::verifyMedpartitionerOnSmallSizeForFieldOnCells()
173 string fileName,cmd,execName,sourceName,targetName,input;
174 execName=getPartitionerParaExe();
176 fileName.replace(fileName.find(".med"),4,"_WithVecFieldOnCells.med");
178 MEDCoupling::MEDFileUMesh* initialMesh=MEDCoupling::MEDFileUMesh::New(fileName.c_str(),_mesh_name.c_str());
179 MEDCoupling::MEDCouplingUMesh* cellMesh=initialMesh->getLevel0Mesh(false);
181 cmd="mpirun -np 5 "+execName+" --ndomains=5 --split-method=metis"; //on same proc
184 targetName.replace(targetName.find(".med"),4,"_partitionedTo5_");
185 cmd+=" --input-file="+sourceName+" --output-file="+targetName+" --verbose="+IntToStr(_verbose);
186 if (_verbose) cout<<endl<<cmd<<endl;
187 res=system(cmd.c_str());
188 CPPUNIT_ASSERT_EQUAL(0, res);
189 input=targetName+".xml";
191 //merge split meshes and test equality
192 cmd="mpirun -np 1 "+execName+" --ndomains=1 --split-method=metis"; //on same proc
193 sourceName=targetName+".xml";
195 targetName.replace(targetName.find(".med"),4,"_remergedFrom5_");
196 cmd+=" --input-file="+sourceName+" --output-file="+targetName+" --verbose="+IntToStr(_verbose);
197 if (_verbose) cout<<endl<<cmd<<endl;
198 res=system(cmd.c_str());
199 CPPUNIT_ASSERT_EQUAL(0, res);
201 string refusedName=targetName+"1.med";
202 MEDCoupling::MEDFileUMesh* refusedMesh=MEDCoupling::MEDFileUMesh::New(refusedName.c_str(),_mesh_name.c_str());
203 MEDCoupling::MEDCouplingUMesh* refusedCellMesh=refusedMesh->getLevel0Mesh(false);
205 CPPUNIT_ASSERT_EQUAL(cellMesh->getNumberOfCells(), refusedCellMesh->getNumberOfCells());
207 std::vector<const MEDCouplingUMesh *> meshes;
208 std::vector<DataArrayInt *> corr;
209 meshes.push_back(cellMesh);
210 refusedCellMesh->tryToShareSameCoordsPermute(*cellMesh, 1e-9);
211 meshes.push_back(refusedCellMesh);
212 MEDCouplingUMesh* fusedCell=MEDCouplingUMesh::FuseUMeshesOnSameCoords(meshes,0,corr);
213 CPPUNIT_ASSERT_EQUAL(cellMesh->getNumberOfCells(), fusedCell->getNumberOfCells());
215 MCAuto<MEDCouplingFieldDouble> field1=ReadFieldCell(fileName.c_str(),initialMesh->getName().c_str(),0,"VectorFieldOnCells",0,1);
216 MCAuto<MEDCouplingFieldDouble> field2=ReadFieldCell(refusedName.c_str(),refusedCellMesh->getName().c_str(),0,"VectorFieldOnCells",0,1);
218 int nbcells=corr[1]->getNumberOfTuples();
219 CPPUNIT_ASSERT_EQUAL(cellMesh->getNumberOfCells(), nbcells);
220 //use corr to test equality of field
221 DataArrayDouble* f1=field1->getArray();
222 DataArrayDouble* f2=field2->getArray();
225 cout<<"\nf1 : "<<f1->reprZip();
226 cout<<"\nf2 : "<<f2->reprZip(); //field2->advancedRepradvancedRepr();
227 for (std::size_t i = 0; i < corr.size(); i++)
228 cout << "\ncorr " << i << " : " << corr[i]->reprZip();
232 int nbcomp=field1->getNumberOfComponents();
233 double* p1=f1->getPointer();
234 double* p2=f2->getPointer();
235 int* pc=corr[1]->getPointer();
236 for (int i = 0; i < nbcells; i++)
240 for (int j = 0; j < nbcomp; j++)
242 if (p1[i1+j]==p2[i2+j]) nbequal++;
243 //cout<<" "<<p1[i1+j]<<"="<<p2[i2+j];
246 CPPUNIT_ASSERT_EQUAL(nbcells*nbcomp, nbequal);
248 for (std::size_t i = 0; i < corr.size(); i++)
250 fusedCell->decrRef();
251 refusedCellMesh->decrRef();
255 void MEDPARTITIONERTest::verifyMedpartitionerOnSmallSizeForFieldOnGaussNe()
258 string fileName,cmd,execName,sourceName,targetName,input;
259 execName=getPartitionerParaExe();
261 fileName.replace(fileName.find(".med"),4,"_WithVecFieldOnGaussNe.med");
263 MEDCoupling::MEDFileUMesh* initialMesh=MEDCoupling::MEDFileUMesh::New(fileName.c_str(),_mesh_name.c_str());
264 MEDCoupling::MEDCouplingUMesh* cellMesh=initialMesh->getLevel0Mesh(false);
266 cmd="mpirun -np 5 "+execName+" --ndomains=5 --split-method=metis"; //on same proc
269 targetName.replace(targetName.find(".med"),4,"_partitionedTo5_");
270 cmd+=" --input-file="+sourceName+" --output-file="+targetName+" --verbose="+IntToStr(_verbose);
271 if (_verbose) cout<<endl<<cmd<<endl;
272 res=system(cmd.c_str());
273 CPPUNIT_ASSERT_EQUAL(0, res);
274 input=targetName+".xml";
276 //merge split meshes and test equality
277 cmd="mpirun -np 1 "+execName+" --ndomains=1 --split-method=metis"; //on same proc
278 sourceName=targetName+".xml";
280 targetName.replace(targetName.find(".med"),4,"_remergedFrom5_");
281 cmd+=" --input-file="+sourceName+" --output-file="+targetName+" --verbose="+IntToStr(_verbose);
282 if (_verbose) cout<<endl<<cmd<<endl;
283 res=system(cmd.c_str());
284 CPPUNIT_ASSERT_EQUAL(0, res);
286 string refusedName=targetName+"1.med";
287 MEDCoupling::MEDFileUMesh* refusedMesh=MEDCoupling::MEDFileUMesh::New(refusedName.c_str(),_mesh_name.c_str());
288 MEDCoupling::MEDCouplingUMesh* refusedCellMesh=refusedMesh->getLevel0Mesh(false);
290 CPPUNIT_ASSERT_EQUAL(cellMesh->getNumberOfCells(), refusedCellMesh->getNumberOfCells());
292 std::vector<const MEDCouplingUMesh *> meshes;
293 std::vector<DataArrayInt *> corr;
294 meshes.push_back(cellMesh);
295 refusedCellMesh->tryToShareSameCoordsPermute(*cellMesh, 1e-9);
296 meshes.push_back(refusedCellMesh);
297 MEDCouplingUMesh* fusedCell=MEDCouplingUMesh::FuseUMeshesOnSameCoords(meshes,0,corr);
298 CPPUNIT_ASSERT_EQUAL(cellMesh->getNumberOfCells(), fusedCell->getNumberOfCells());
300 MCAuto<MEDCouplingFieldDouble> field1=ReadField(ON_GAUSS_NE,fileName.c_str(),initialMesh->getName().c_str(),0,"MyFieldOnGaussNE",5,6);
301 MCAuto<MEDCouplingFieldDouble> field2=ReadField(ON_GAUSS_NE,refusedName.c_str(),refusedCellMesh->getName().c_str(),0,"MyFieldOnGaussNE",5,6);
303 int nbcells=corr[1]->getNumberOfTuples();
304 CPPUNIT_ASSERT_EQUAL(cellMesh->getNumberOfCells(), nbcells);
305 //use corr to test equality of field
306 DataArrayDouble* f1=field1->getArray();
307 DataArrayDouble* f2=field2->getArray();
310 cout << "\nf1 : " << f1->reprZip(); //123.4 for 12th cell,3rd component, 4th gausspoint
311 cout << "\nf2 : " << f2->reprZip(); //field2->advancedRepradvancedRepr();
312 for (std::size_t i = 0; i < corr.size(); i++)
313 cout << "\ncorr " << i << " : " << corr[i]->reprZip();
318 int nbcomp=field1->getNumberOfComponents();
319 double* p1=f1->getPointer();
320 double* p2=f2->getPointer();
321 int* pc=corr[1]->getPointer();
322 for (int i = 0; i < nbcells; i++)
324 int i1=pc[i]*nbcomp*nbptgauss;
325 int i2=i*nbcomp*nbptgauss;
326 for (int j = 0; j < nbcomp*nbptgauss; j++)
328 if (p1[i1+j]==p2[i2+j]) nbequal++;
329 //cout<<" "<<p1[i1+j]<<"="<<p2[i2+j];
332 CPPUNIT_ASSERT_EQUAL(nbcells*nbcomp*nbptgauss, nbequal);
334 for (std::size_t i = 0; i < corr.size(); i++)
336 fusedCell->decrRef();
337 refusedCellMesh->decrRef();
341 void MEDPARTITIONERTest::launchMedpartitionerOnTestMeshes()
345 export INFI=/home/vb144235/resources/blade.med
346 //no need export MESH=Fuse_1
347 export INFI=tmp_testMeshxxx.med
348 //no need export MESH=testMesh
349 mpirun -np 2 medpartitioner_para --input-file=$INFI --output-file=ttmp1_ --ndomains=4
350 mpirun -np 5 medpartitioner_para --input-file=ttmp1_.xml --output-file=ttmp2_ --ndomains=5
351 mpirun -np 2 valgrind medpartitioner_para --input-file=tmp_testMesh_20x30x50.med --output-file=ttmp1petit_ --ndomains=4 --dump-cpu-memory --verbose=111
354 string cmd,execName,sourceName,targetName;
356 res=system("which mpirun 2>/dev/null 1>/dev/null"); //no trace
357 CPPUNIT_ASSERT_EQUAL(0, res);
359 execName=getPartitionerParaExe();
361 cmd="which "+execName+" 2>/dev/null 1>/dev/null"; //no trace
362 res=system(cmd.c_str());
363 CPPUNIT_ASSERT_EQUAL(0, res);
365 cmd="mpirun -np 2 "+execName+" --ndomains=2 --split-method=metis"; //on same proc
366 sourceName=_file_name;
367 targetName=_file_name;
368 targetName.replace(targetName.find(".med"),4,"_partitionedTo2_");
369 cmd+=" --input-file="+sourceName+" --output-file="+targetName+" --verbose="+IntToStr(_verbose);
370 if (_verbose) cout<<endl<<cmd<<endl;
371 res=system(cmd.c_str());
372 CPPUNIT_ASSERT_EQUAL(0, res);
374 cmd="mpirun -np 3 "+execName+" --ndomains=5 --split-method=metis"; //on less proc
375 sourceName=_file_name;
376 targetName=_file_name;
377 targetName.replace(targetName.find(".med"),4,"_partitionedTo5_");
378 cmd+=" --input-file="+sourceName+" --output-file="+targetName+" --verbose="+IntToStr(_verbose);
379 if (_verbose) cout<<endl<<cmd<<endl;
380 res=system(cmd.c_str());
381 CPPUNIT_ASSERT_EQUAL(0, res);
383 cmd="mpirun -np 1 "+execName+" --ndomains=1 --split-method=metis"; //on 1 proc
384 sourceName=targetName+".xml";
385 targetName=_file_name;
386 targetName.replace(targetName.find(".med"),4,"_remergedFrom5_");
387 cmd+=" --input-file="+sourceName+" --output-file="+targetName+" --verbose="+IntToStr(_verbose);
388 if (_verbose) cout<<endl<<cmd<<endl;
389 res=system(cmd.c_str());
390 CPPUNIT_ASSERT_EQUAL(0, res);
392 cmd="mpirun -np 8 "+execName+" --ndomains=1 --split-method=metis"; //on more proc
393 //sourceName=targetName+".xml";
394 targetName=_file_name;
395 targetName.replace(targetName.find(".med"),4,"_remergedFrom5_");
396 cmd+=" --input-file="+sourceName+" --output-file="+targetName+" --verbose="+IntToStr(_verbose);
397 if (_verbose) cout<<endl<<cmd<<endl;
398 res=system(cmd.c_str());
399 CPPUNIT_ASSERT_EQUAL(0, res);
402 void MEDPARTITIONERTest::launchMedpartitionerOnHugeTestMeshes()
405 string cmd,execName,sourceName,targetName;
406 execName=getPartitionerParaExe();
408 string snbTarget=IntToStr(_nb_target_huge);
409 cmd="mpirun -np "+snbTarget+" "+execName+" --ndomains="+snbTarget+" --split-method=metis"; //on same proc
410 sourceName=_file_name_huge_xml;
411 targetName=_file_name_huge_xml;
412 string tmp="_partitionedTo"+snbTarget+"_";
413 targetName.replace(targetName.find(".xml"),4,tmp);
414 cmd+=" --input-file="+sourceName+" --output-file="+targetName+" --verbose="+IntToStr(_verbose);
415 if (_verbose) cout<<endl<<cmd<<endl;
416 res=system(cmd.c_str());
417 CPPUNIT_ASSERT_EQUAL(0, res);
420 void MEDPARTITIONERTest::testMpirunSmallSize()
424 launchMedpartitionerOnTestMeshes();
425 verifyMedpartitionerOnSmallSizeForMesh();
426 verifyMedpartitionerOnSmallSizeForFieldOnCells();
427 verifyMedpartitionerOnSmallSizeForFieldOnGaussNe();
430 void MEDPARTITIONERTest::testMpirunMedianSize()
434 launchMedpartitionerOnTestMeshes();
437 void MEDPARTITIONERTest::testMpirunHugeSize()
439 //setBigSize(); //may be a lot for now
441 //create a set of nbx*nby*nbz files mesh of ni*ny*nz cells
443 createHugeTestMesh(_ni, _nj, _nk, 2, 2, 2, 32); //it is now to know how far we are going to test
444 launchMedpartitionerOnHugeTestMeshes();