Zoltan2
taskMappingTest3.cpp
Go to the documentation of this file.
3 #include "Tpetra_MultiVector_decl.hpp"
4 
5 #include <GeometricGenerator.hpp>
6 #include <string>
7 #include "Teuchos_XMLParameterListHelpers.hpp"
8 //#include "Teuchos_MPIComm.hpp"
9 #define partDIM 3
10 #define procDIM 3
11 #define nProcs 200;
12 #define nParts 200;
13 
14 
16  const string& s,
17  const string& delimiters = " \f\n\r\t\v" )
18 {
19  return s.substr( 0, s.find_last_not_of( delimiters ) + 1 );
20 }
21 
23  const string& s,
24  const string& delimiters = " \f\n\r\t\v" )
25 {
26  return s.substr( s.find_first_not_of( delimiters ) );
27 }
28 
29 string trim_copy(
30  const string& s,
31  const string& delimiters = " \f\n\r\t\v" )
32 {
33  return trim_left_copy( trim_right_copy( s, delimiters ), delimiters );
34 }
35 
36 const char param_comment = '#';
37 void readGeoGenParams(string paramFileName, Teuchos::ParameterList &geoparams, const RCP<const Teuchos::Comm<int> > & comm){
38  std::string input = "";
39  char inp[25000];
40  for(int i = 0; i < 25000; ++i){
41  inp[i] = 0;
42  }
43 
44  bool fail = false;
45  if(comm->getRank() == 0){
46 
47  std::fstream inParam(paramFileName.c_str());
48  if (inParam.fail())
49  {
50  fail = true;
51  }
52  if(!fail)
53  {
54  std::string tmp = "";
55  getline (inParam,tmp);
56  while (!inParam.eof()){
57  if(tmp != ""){
58  tmp = trim_copy(tmp);
59  if(tmp != ""){
60  input += tmp + "\n";
61  }
62  }
63  getline (inParam,tmp);
64  }
65  inParam.close();
66  for (size_t i = 0; i < input.size(); ++i){
67  inp[i] = input[i];
68  }
69  }
70  }
71 
72 
73 
74  int size = input.size();
75  if(fail){
76  size = -1;
77  }
78  comm->broadcast(0, sizeof(int), (char*) &size);
79  if(size == -1){
80  throw "File " + paramFileName + " cannot be opened.";
81  }
82  comm->broadcast(0, size, inp);
83  std::istringstream inParam(inp);
84  std::string str;
85  getline (inParam,str);
86  while (!inParam.eof()){
87  if(str[0] != param_comment){
88  size_t pos = str.find('=');
89  if(pos == std::string::npos){
90  throw "Invalid Line:" + str + " in parameter file";
91  }
92  string paramname = trim_copy(str.substr(0,pos));
93  string paramvalue = trim_copy(str.substr(pos + 1));
94  geoparams.set(paramname, paramvalue);
95  }
96  getline (inParam,str);
97  }
98 }
99 string convert_to_string(char *args){
100  string tmp = "";
101  for(int i = 0; args[i] != 0; i++)
102  tmp += args[i];
103  return tmp;
104 }
105 bool getArgumentValue(string &argumentid, double &argumentValue, string argumentline){
106  std::stringstream stream(std::stringstream::in | std::stringstream::out);
107  stream << argumentline;
108  getline(stream, argumentid, '=');
109  if (stream.eof()){
110  return false;
111  }
112  stream >> argumentValue;
113  return true;
114 }
115 
116 template <typename part_t>
118  int argc,
119  char **argv,
120  std::string &procF,
121  part_t &nx,
122  part_t &ny,
123  part_t &nz){
124 
125  bool isprocset = false;
126  int ispartset = 0;
127 
128  for(int i = 0; i < argc; ++i){
129  string tmp = convert_to_string(argv[i]);
130  string tmp2 = "";
131  string identifier = "";
132  double fval = -1;
133  if(!getArgumentValue(identifier, fval, tmp)) continue;
134 
135  if(identifier == "PROC"){
136  std::stringstream stream(std::stringstream::in | std::stringstream::out);
137  stream << tmp;
138  getline(stream, procF, '=');
139 
140  stream >> procF;
141  isprocset = true;
142  }
143  else if(identifier == "NX"){
144  std::stringstream stream(std::stringstream::in | std::stringstream::out);
145  stream << tmp;
146  getline(stream, tmp2, '=');
147 
148  stream >> nx;
149  ispartset++;
150  }
151  else if(identifier == "NY"){
152  std::stringstream stream(std::stringstream::in | std::stringstream::out);
153  stream << tmp;
154  getline(stream, tmp2, '=');
155 
156  stream >> ny;
157  ispartset++;
158  }
159  else if(identifier == "NZ"){
160  std::stringstream stream(std::stringstream::in | std::stringstream::out);
161  stream << tmp;
162  getline(stream, tmp2, '=');
163 
164  stream >> nz;
165  ispartset++;
166  }
167 
168  else {
169  throw "Invalid argument at " + tmp;
170  }
171 
172  }
173  if(!(ispartset == 3&& isprocset)){
174  throw "(PROC && PART) are mandatory arguments.";
175  }
176 
177 }
178 int main(int argc, char *argv[]){
179 
180  typedef Tpetra::MultiVector<zscalar_t, zlno_t, zgno_t, znode_t> tMVector_t;
181  typedef Zoltan2::XpetraMultiVectorAdapter<tMVector_t> inputAdapter_t;
182  typedef inputAdapter_t::part_t part_t;
183 
184  Teuchos::GlobalMPISession session(&argc, &argv);
185  //if (argc != 3){
186  // cout << "Usage: " << argv[0] << " PART=partGeoParams.txt PROC=procGeoParams.txt" << endl;
187  // exit(1);
188  //}
189  part_t numParts = 0;
190  zscalar_t **partCenters = NULL;
191  int coordDim = 0;
192 
193  part_t numProcs = 0;
194  zscalar_t **procCoordinates = NULL;
195  int procDim = 0;
196 
197 
198 
199  part_t jobX = 1, jobY = 1, jobZ = 1;
200  string procfile = "";
201 
202  const RCP<Comm<int> > commN;
203  RCP<Comm<int> >comm = Teuchos::rcp_const_cast<Comm<int> >
204  (Teuchos::DefaultComm<int>::getDefaultSerialComm(commN));
205 
206  part_t *task_communication_xadj_ = NULL;
207  part_t *task_communication_adj_ = NULL;
208  try {
209 
210  getArgVals<part_t>(
211  argc,
212  argv,
213  procfile ,
214  jobX, jobY, jobZ);
215 
216  coordDim = 3;
217  procDim = 3;
218  numParts = jobZ*jobY*jobX;
219  numProcs = numParts;
220  //cout << "part:" << numParts << " proc:" << procfile << endl;
221  {
222  partCenters = new zscalar_t * [coordDim];
223  for(int i = 0; i < coordDim; ++i){
224  partCenters[i] = new zscalar_t[numParts];
225  }
226 
227 
228  task_communication_xadj_ = new part_t [numParts+1];
229  task_communication_adj_ = new part_t [numParts * 6];
230 
231  int prevNCount = 0;
232  task_communication_xadj_[0] = 0;
233  for (part_t i = 0; i < numParts; ++i) {
234  int x = i % jobX;
235  int y = (i / (jobX)) % jobY;
236  int z = (i / (jobX)) / jobY;
237  partCenters[0][i] = x;
238  partCenters[1][i] = y;
239  partCenters[2][i] = z;
240 
241  if (x > 0){
242  task_communication_adj_[prevNCount++] = i - 1;
243  }
244  if (x < jobX - 1){
245  task_communication_adj_[prevNCount++] = i + 1;
246  }
247  if (y > 0){
248  task_communication_adj_[prevNCount++] = i - jobX;
249  }
250  if (y < jobY - 1){
251  task_communication_adj_[prevNCount++] = i + jobX;
252  }
253  if (z > 0){
254  task_communication_adj_[prevNCount++] = i - jobX * jobY;
255  }
256  if (z < jobZ - 1){
257  task_communication_adj_[prevNCount++] = i + jobX * jobY;
258  }
259  task_communication_xadj_[i+1] = prevNCount;
260  }
261  }
262 
263 
264 
265  {
266  std::fstream m(procfile.c_str());
267  procCoordinates = new zscalar_t * [procDim];
268  for(int i = 0; i < procDim; ++i){
269  procCoordinates[i] = new zscalar_t[numParts];
270  }
271  part_t i = 0;
272  while(i < numProcs){
273  m >> procCoordinates[0][i] >> procCoordinates[1][i] >> procCoordinates[2][i];
274  //cout << "i:" <<i << endl;
275  ++i;
276 
277  }
278  m.close();
279  }
280 
281 
282  /*
283  Zoltan2::CoordinateCommunicationModel<zscalar_t,zscalar_t,int> *cm =
284  new Zoltan2::CoordinateCommunicationModel<zscalar_t,zscalar_t,int>(
285  procDim, procCoordinates,
286  coordDim, partCenters,
287  numProcs, numParts);
288 
289  Zoltan2::Environment *env = new Zoltan2::Environment();
290  Zoltan2::CoordinateTaskMapper <inputAdapter_t, int> *ctm=
291  new Zoltan2::CoordinateTaskMapper<inputAdapter_t,int>(env, cm);
292 
293  */
294  RCP<const Teuchos::Comm<int> > tcomm = Teuchos::DefaultComm<int>::getComm();
295  part_t *proc_to_task_xadj_ = new part_t[numProcs+1];
296  part_t *proc_to_task_adj_ = new part_t[numParts];
297 /*
298  cout << "procDim:" << procDim <<
299  " numProcs:" << numProcs <<
300  " coordDim:" << coordDim <<
301  " numParts" << numParts << endl;
302 
303  for(part_t j = 0; j < numProcs; ++j){
304  cout << "proc - coord:" << j << " " << procCoordinates[0][j]<< " " << procCoordinates[1][j]<< " " << procCoordinates[2][j] << endl;
305  }
306 
307  for(part_t j = 0; j < numParts; ++j){
308  cout << "part - coord:" << j << " " << partCenters[0][j]<< " " << partCenters[1][j]<< " " << partCenters[2][j] << endl;
309  }
310 */
311  /*
312  int partArray[3];
313  partArray[0] = 8;
314  partArray[1] = 4;
315  partArray[2] = 16;
316  */
317  part_t *partArray = NULL;
318  int partArraysize = -1;
319  //part_t hopper[3];
320  //hopper[0] = 17;
321  //hopper[1] = 8;
322  //hopper[2] = 24;
323  part_t *machineDimensions = NULL;
324  //machineDimensions = hopper;
325  Zoltan2::coordinateTaskMapperInterface<part_t, zscalar_t, zscalar_t>(
326  tcomm,
327  procDim,
328  numProcs,
329  procCoordinates,
330 
331  coordDim,
332  numParts,
333  partCenters,
334 
335  task_communication_xadj_,
336  task_communication_adj_,
337  NULL,
338 
339  proc_to_task_xadj_, /*output*/
340  proc_to_task_adj_, /*output*/
341 
342  partArraysize,
343  partArray,
344  machineDimensions
345  );
346 
347  if (tcomm->getRank() == 0){
348  cout << "PASS" << endl;
349  }
350  /*
351  delete ctm;
352  delete cm;
353  delete env;
354  */
355  delete [] proc_to_task_xadj_;
356  delete [] proc_to_task_adj_;
357  delete [] task_communication_xadj_;
358  delete [] task_communication_adj_;
359 
360  for (int i = 0; i < coordDim; i++) delete [] partCenters[i];
361  delete [] partCenters;
362  for (int i = 0; i < procDim; i++) delete [] procCoordinates[i];
363  delete [] procCoordinates;
364  }
365  catch(std::string &s){
366  cerr << s << endl;
367  }
368 
369  catch(char * s){
370  cerr << s << endl;
371  }
372  catch(char const * s){
373  cerr << s << endl;
374  }
375 }
376 
int main(int argc, char *argv[])
double zscalar_t
string convert_to_string(char *args)
common code used by tests
void readGeoGenParams(string paramFileName, Teuchos::ParameterList &geoparams, const RCP< const Teuchos::Comm< int > > &comm)
void getArgVals(int argc, char **argv, std::string &procF, part_t &nx, part_t &ny, part_t &nz)
const char param_comment
string trim_copy(const string &s, const string &delimiters=" \\\)
An adapter for Xpetra::MultiVector.
static const std::string fail
string trim_left_copy(const string &s, const string &delimiters=" \\\)
Tpetra::MultiVector< double, int, int > tMVector_t
bool getArgumentValue(string &argumentid, double &argumentValue, string argumentline)
string trim_right_copy(const string &s, const string &delimiters=" \\\)