Salome HOME
Synchronize adm files
[modules/med.git] / src / MEDPartitioner / medpartitioner.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 /*
21   examples of launch
22   export verb=1
23   medpartitioner --input-file=blade.med --output-file=ttmp1_ --ndomains=2 --dump-cpu-memory --verbose=$verb
24   medpartitioner --input-file=medpartitioner_blade.xml --output-file=ttmp1_ --ndomains=2 --dump-cpu-memory --verbose=$verb
25   medpartitioner --input-file=ttmp1_.xml --output-file=tttmp1_ --ndomains=4 --dump-cpu-memory --verbose=$verb
26 */
27
28 /*
29 #include "MEDPARTITIONER_Graph.hxx"
30 #include "MEDPARTITIONER_Topology.hxx"
31 #include "MEDPARTITIONER_ParaDomainSelector.hxx"
32 #include "MEDPARTITIONER_MeshCollection.hxx"
33 #include "MEDPARTITIONER_Utils.hxx"
34 */
35
36 #include "MEDPARTITIONER_MeshCollection.hxx"
37 #include "MEDPARTITIONER_ParallelTopology.hxx"
38 #include "MEDPARTITIONER_ParaDomainSelector.hxx"
39 #include "MEDPARTITIONER_Utils.hxx"
40
41 #include <string>
42 #include <fstream>
43 #include <cstring>
44 #include <cstdlib>
45 #include <iostream>
46
47 using namespace std;
48 using namespace MEDPARTITIONER;
49
50 int main(int argc, char** argv)
51 {
52 #if !defined(MED_ENABLE_METIS) && !defined(MED_ENABLE_SCOTCH)
53   std::cout << "Sorry, no one split method is available. Please, compile with METIS or SCOTCH." << std::endl;
54   return 1;
55 #else
56
57   // Defining options
58   // by parsing the command line
59   
60   bool split_family=false;
61   bool empty_groups=false;
62   bool mesure_memory=false;
63   bool filter_face=true;
64
65   string input;
66   string output;
67   string meshname;
68   string library="metis";  //default
69   int ndomains;
70   int help=0;
71   
72   //sequential : no MPI
73   MyGlobals::_World_Size=1;
74   MyGlobals::_Rank=0;
75   MyGlobals::_Creates_Boundary_Faces=0;
76
77   // Primitive parsing of command-line options
78   string desc ("Available options of medpartitioner V1.0:\n"
79                "\t--help                   : produces this help message\n"
80                "\t--verbose                : echoes arguments\n"
81                "\t--input-file=<string>    : name of the input .med file or .xml master file\n"
82                "\t--output-file=<string>   : name of the resulting file (without extension)\n"
83                "\t--ndomains=<number>      : number of subdomains in the output file, default is 1\n"
84 #if defined(MED_ENABLE_METIS) && defined(MED_ENABLE_SCOTCH)
85   //user can choose!
86                "\t--split-method=<string>  : name of the splitting library (metis/scotch), default is metis\n"
87 #endif
88                "\t--creates-boundary-faces : creates boundary faces mesh in the output files\n"
89                "\t--dump-cpu-memory        : dumps passed CPU time and maximal increase of used memory\n"
90                );
91
92   if (argc<=1) help=1;
93   string value;
94   for (int i = 1; i < argc; i++)
95     {
96       if (strlen(argv[i]) < 3) 
97         {
98           cerr << "bad argument : "<< argv[i] << endl;
99           return 1;
100         }
101     
102       if (TestArg(argv[i],"--verbose",value)) 
103         {
104           MyGlobals::_Verbose=1;
105           if (value!="") MyGlobals::_Verbose = atoi(value.c_str());
106         }
107       else if (TestArg(argv[i],"--help",value)) help=1;
108 //      else if (TestArg(argv[i],"--test",value)) test=1;
109       else if (TestArg(argv[i],"--input-file",value)) input=value;
110       else if (TestArg(argv[i],"--output-file",value)) output=value;
111       else if (TestArg(argv[i],"--split-method",value)) library=value;
112       else if (TestArg(argv[i],"--ndomains",value)) ndomains=atoi(value.c_str());
113       else if (TestArg(argv[i],"--creates-boundary-faces",value)) MyGlobals::_Creates_Boundary_Faces=1;
114       else if (TestArg(argv[i],"--dump-cpu-memory",value)) mesure_memory=true;
115       else 
116         {
117           cerr << "unknown argument : "<< argv[i] << endl;
118           return 1;
119         }
120     }
121
122   MyGlobals::_Is0verbose=MyGlobals::_Verbose;
123   
124 //no choice
125 #if defined(MED_ENABLE_METIS) && !defined(MED_ENABLE_SCOTCH)
126   library = "metis";
127 #endif
128 #if !defined(MED_ENABLE_METIS) && defined(MED_ENABLE_SCOTCH)
129   library = "scotch";
130 #endif
131 //user choice
132 #if defined(MED_ENABLE_METIS) && defined(MED_ENABLE_SCOTCH)
133   if ((library!="metis") && (library!="scotch"))
134     {
135       cerr << "split-method only available : metis, scotch" << endl;
136       return 1;
137     }
138 #endif
139  
140   if (help==1)
141     {
142       cout<<desc<<"\n";
143       return 0;
144     }
145   
146   if (MyGlobals::_Is0verbose)
147     {
148       cout << "medpartitioner V1.0 :" << endl;
149       cout << "  input-file = " << input << endl;
150       cout << "  output-file = " << output << endl;
151       cout << "  split-method = " << library << endl;
152       cout << "  ndomains = " << ndomains << endl;
153       cout << "  creates_boundary_faces = " << MyGlobals::_Creates_Boundary_Faces << endl;
154       cout << "  dump-cpu-memory = " << mesure_memory<< endl;
155       cout << "  verbose = " << MyGlobals::_Verbose << endl;
156     }
157   //testing whether it is possible to write a file at the specified location
158   if (MyGlobals::_Rank==0)
159     {
160       string outputtest = output + ".testioms.";
161       ofstream testfile (outputtest.c_str());
162       if (testfile.fail())
163         { 
164           cerr << "output-file directory does not exist or is in read-only access" << endl;
165           return 1;
166         }
167       //deletes test file
168       remove(outputtest.c_str());
169     }
170   
171   // Beginning of the computation
172
173   // Loading the mesh collection
174   if (MyGlobals::_Is0verbose) cout << "Reading input files "<<endl;
175   
176   try
177     {
178       /*MEDPARTITIONER::ParaDomainSelector parallelizer(mesure_memory);
179       MEDPARTITIONER::MeshCollection collection(input,parallelizer);
180       MEDPARTITIONER::ParallelTopology* aPT = (MEDPARTITIONER::ParallelTopology*) collection.getTopology();
181       aPT->setGlobalNumerotationDefault(collection.getParaDomainSelector());
182       //int nbfiles=MyGlobals::_fileMedNames->size(); //nb domains
183       //to have unique valid fields names/pointers/descriptions for partitionning
184       collection.prepareFieldDescriptions();
185       //int nbfields=collection.getFieldDescriptions().size(); //on all domains
186       //cout<<ReprVectorOfString(collection.getFieldDescriptions());
187     
188       if (MyGlobals::_Is0verbose)
189         {
190           cout<<"fileNames :"<<endl
191               <<ReprVectorOfString(MyGlobals::_File_Names);
192           cout<<"fieldDescriptions :"<<endl
193               <<ReprFieldDescriptions(collection.getFieldDescriptions()," "); //cvwat07
194           cout<<"familyInfo :\n"
195               <<ReprMapOfStringInt(collection.getFamilyInfo())<<endl;
196           cout<<"groupInfo :\n"
197               <<ReprMapOfStringVectorOfString(collection.getGroupInfo())<<endl;
198         }*/
199       MEDPARTITIONER::ParaDomainSelector parallelizer(mesure_memory);
200       MEDPARTITIONER::MeshCollection collection(input,parallelizer);
201       MEDPARTITIONER::ParallelTopology* aPT = (MEDPARTITIONER::ParallelTopology*) collection.getTopology();
202       aPT->setGlobalNumerotationDefault(collection.getParaDomainSelector());
203       //to have unique valid fields names/pointers/descriptions for partitionning
204       collection.prepareFieldDescriptions();
205       
206       //MEDPARTITIONER::MeshCollection collection(input);
207
208       //Creating the graph and partitioning it
209       if (MyGlobals::_Is0verbose) cout << "Computing partition with " << library << endl;
210   
211       auto_ptr< MEDPARTITIONER::Topology > new_topo;
212       if (library == "metis")
213         new_topo.reset( collection.createPartition(ndomains,MEDPARTITIONER::Graph::METIS));
214       else
215         new_topo.reset( collection.createPartition(ndomains,MEDPARTITIONER::Graph::SCOTCH));
216       parallelizer.evaluateMemory();
217   
218       //Creating a new mesh collection from the partitioning
219       if (MyGlobals::_Is0verbose)  cout << "Creating new meshes"<< endl;
220       MEDPARTITIONER::MeshCollection new_collection(collection,new_topo.get(),split_family,empty_groups);
221   
222       if (filter_face) new_collection.filterFaceOnCell();
223     
224       //to get infos on all procs
225     
226       //see meshName
227       vector<string> finalInformations;
228       vector<string> r1,r2;
229       //r1=AllgathervVectorOfString(MyGlobals::_General_Informations);
230       r1=MyGlobals::_General_Informations;
231       //if (MyGlobals::_Is0verbose>1000) cout << "generalInformations : \n"<<ReprVectorOfString(r1);
232       r2=SelectTagsInVectorOfString(r1,"ioldDomain=");
233       r2=SelectTagsInVectorOfString(r2,"meshName=");
234       if (r2.size()==(collection.getMesh()).size())
235         {
236           for (std::size_t i=0; i<r2.size(); i++)
237             r2[i]=EraseTagSerialized(r2[i],"ioldDomain=");
238           r2=DeleteDuplicatesInVectorOfString(r2);
239           if (r2.size()==1)
240             {
241               string finalMesh="finalMeshName="+ExtractFromDescription(r2[0], "meshName=");
242               finalInformations.push_back(SerializeFromString(finalMesh));
243             }
244         }
245       if (finalInformations.size()==0)
246         {
247           if (MyGlobals::_Rank==0)
248             cerr<<"Problem on final meshName : set at 'Merge'"<<endl;
249           finalInformations.push_back(SerializeFromString("finalMeshName=Merge"));
250         }
251     
252       //see field info nbComponents & componentInfo (if fields present)
253       r2=SelectTagsInVectorOfString(r1,"fieldName=");
254       r2=SelectTagsInVectorOfString(r2,"nbComponents=");
255       //may be yes? or not?
256       for (std::size_t i=0; i<r2.size(); i++)
257         r2[i]=EraseTagSerialized(r2[i],"ioldFieldDouble=");
258       r2=DeleteDuplicatesInVectorOfString(r2);
259       for (std::size_t i=0; i<r2.size(); i++)
260         finalInformations.push_back(r2[i]);
261     
262       MyGlobals::_General_Informations=finalInformations;
263       if (MyGlobals::_Is0verbose) 
264         cout << "generalInformations : \n"<<ReprVectorOfString(finalInformations);
265     
266       //new_collection.setSubdomainBoundaryCreates(creates_boundary_faces);
267       if (MyGlobals::_Is0verbose) cout << "Writing "<<ndomains<<" output files "<<output<<"xx.med"<<" and "<<output<<".xml"<<endl;
268       new_collection.write(output);
269   
270       /*if ( mesure_memory )
271         if ( parallelizer.isOnDifferentHosts() || MyGlobals::_Rank==0 )
272           {
273             cout << "Elapsed time = " << parallelizer.getPassedTime()
274                  << ", max memory usage = " << parallelizer.evaluateMemory() << " KB"
275                  << endl;
276           }*/
277       
278       if (MyGlobals::_Is0verbose>0) cout<<"OK END"<< endl;
279       return 0;
280     }
281   catch(const char *mess)
282     {
283       cerr<<mess<<endl;
284       fflush(stderr);
285       return 1;
286     }
287   catch(INTERP_KERNEL::Exception& e)
288     {
289       cerr<<"INTERP_KERNEL_Exception : "<<e.what()<<endl;
290       fflush(stderr);
291       return 1;
292     }
293   catch(std::exception& e)
294     {
295       cerr<<"std_Exception : "<<e.what()<<endl;
296       fflush(stderr);
297       return 1;
298     }
299   catch(...)
300     {
301       cerr<<"an unknown type exception error was occured"<<endl;
302       fflush(stderr);
303       return 1;
304     }
305 #endif
306 }