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