Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
parread.cpp
Go to the documentation of this file.
1 #include "moab_mpi.h"
2 #include "moab/Core.hpp"
3 #include <sstream>
4 #include <iostream>
5 #include <cstdlib>
6 #include <unistd.h>
7 
8 const char usage[] = "[-b|-d|-f] [-P <rank>] [-p <name>] [-R] [-g <level>] [-O <option>] <filename>";
9 
10 const char DEFAULT_PARTITION_TAG[] = "PARALLEL_PARTITION";
11 
12 void error( const char* argv0 )
13 {
14  std::cerr << "Usage: " << argv0 << " " << usage << std::endl;
15  exit( 1 );
16 }
17 
18 void help( const char* argv0 )
19 {
20  std::cout << argv0 << " " << usage << std::endl
21  << "-P <rank> Specified processor will wait for debugger to attach." << std::endl
22  << "-p <name> Tag identifying partition sets (default: \"" << DEFAULT_PARTITION_TAG << "\")" << std::endl
23  << "-a Assign partitions to processes by matching part number to rank" << std::endl
24  << "-R Do not resolve shared entities" << std::endl
25  << "-b Use broadcast & delete read method" << std::endl
26  << "-d Use read & delete method" << std::endl
27  << "-f Use true parallel read (default)" << std::endl
28  << "-g Set debug output level" << std::endl;
29  exit( 0 );
30 }
31 
32 int main( int argc, char* argv[] )
33 {
34  MPI_Init( &argc, &argv );
35 
36  const char BCAST_MODE[] = "BCAST_DELETE";
37  const char DELETE_MODE[] = "READ_DELETE";
38  const char PART_MODE[] = "READ_PART";
39  const char* read_mode = PART_MODE;
40  const char* partition_tag_name = DEFAULT_PARTITION_TAG;
41  bool assign_by_id = false;
42  bool resolve_shared = true;
43  int debug_level = 0;
44  int pause_rank = -1;
45  const char* filename = 0;
46  std::ostringstream options;
47  options << ";";
48 
49  int rank;
50  MPI_Comm_rank( MPI_COMM_WORLD, &rank );
51 
52  int expect_tag = 0;
53  int expect_level = 0;
54  int expect_opt = 0;
55  int expect_rank = 0;
56  bool no_more_flags = false;
57  for( int i = 1; i < argc; ++i )
58  {
59  int arg_pos = i;
60  if( expect_tag == i )
61  {
62  partition_tag_name = argv[i];
63  expect_tag = 0;
64  }
65  else if( expect_level == i )
66  {
67  char* endptr;
68  debug_level = (int)strtol( argv[i], &endptr, 0 );
69  if( *endptr || endptr == argv[i] || debug_level < 0 )
70  {
71  std::cerr << "Expected positive integer value following '-g' flag" << std::endl;
72  error( argv[0] );
73  }
74  expect_level = 0;
75  }
76  else if( expect_opt == i )
77  {
78  options << ";" << argv[i];
79  expect_opt = 0;
80  }
81  else if( expect_rank == i )
82  {
83  char* endptr;
84  pause_rank = (int)strtol( argv[i], &endptr, 0 );
85  if( *endptr || endptr == argv[i] || pause_rank < 0 )
86  {
87  std::cerr << "Expected positive integer value following '-P' flag" << std::endl;
88  error( argv[0] );
89  }
90  expect_rank = 0;
91  }
92  else if( argv[i][0] == '-' && !no_more_flags )
93  {
94  for( int j = 1; argv[i][j]; ++j )
95  {
96  switch( argv[i][j] )
97  {
98  case '-':
99  no_more_flags = true;
100  break;
101  case 'P':
102  expect_rank = ++arg_pos;
103  break;
104  case 'p':
105  expect_tag = ++arg_pos;
106  break;
107  case 'a':
108  assign_by_id = true;
109  break;
110  case 'R':
111  resolve_shared = false;
112  break;
113  case 'b':
114  read_mode = BCAST_MODE;
115  break;
116  case 'd':
117  read_mode = DELETE_MODE;
118  break;
119  case 'f':
120  read_mode = PART_MODE;
121  break;
122  case 'g':
123  expect_level = ++arg_pos;
124  break;
125  case 'O':
126  expect_opt = ++arg_pos;
127  break;
128  case 'h':
129  help( argv[0] );
130  break;
131  default:
132  std::cerr << "Unknown flag: -" << argv[i][j] << std::endl;
133  error( argv[0] );
134  }
135  }
136  }
137  else if( filename )
138  {
139  std::cerr << "Unexpected argument: \"" << argv[i] << "\"" << std::endl;
140  error( argv[0] );
141  }
142  else
143  {
144  filename = argv[i];
145  }
146  }
147 
148  if( expect_tag )
149  {
150  std::cerr << "Expected value following -p flag" << std::endl;
151  error( argv[0] );
152  }
153  if( expect_level )
154  {
155  std::cerr << "Expected value following -g flag" << std::endl;
156  error( argv[0] );
157  }
158  if( expect_opt )
159  {
160  std::cerr << "Expected value following -O flag" << std::endl;
161  error( argv[0] );
162  }
163  if( expect_rank )
164  {
165  std::cerr << "Expected rank following -P flag" << std::endl;
166  error( argv[0] );
167  }
168  if( !filename )
169  {
170  std::cerr << "No file name specified" << std::endl;
171  error( argv[0] );
172  }
173 
174  options << ";PARTITION=" << partition_tag_name << ";PARALLEL=" << read_mode;
175  if( resolve_shared ) options << ";PARALLEL_RESOLVE_SHARED_ENTS";
176  if( assign_by_id ) options << ";PARTITION_BY_RANK";
177  if( debug_level ) options << ";DEBUG_IO=" << debug_level;
178 
179  moab::Core core;
180  moab::Interface& mb = core;
181 
182  if( pause_rank >= 0 )
183  {
184  if( pause_rank == rank )
185  {
186  std::cout << "Process " << rank << " with PID " << getpid() << " waiting for debugger" << std::endl
187  << "Set local variable 'do_wait' to zero to continue" << std::endl;
188 
189  volatile int do_wait = 1;
190  while( do_wait )
191  {
192  sleep( 1 );
193  }
194  }
195  MPI_Barrier( MPI_COMM_WORLD );
196  }
197 
198  std::string opts = options.str();
199  if( rank == 0 ) std::cout << "Reading \"" << filename << "\" with options=\"" << opts << "\"." << std::endl;
200 
201  double init_time = MPI_Wtime();
202  moab::ErrorCode rval = mb.load_file( filename, 0, opts.c_str() );
203  double fini_time = MPI_Wtime();
204 
205  long send_data[2] = { (long)( 100 * ( fini_time - init_time ) ), rval };
206  long recv_data[2];
207  MPI_Allreduce( send_data, recv_data, 2, MPI_LONG, MPI_MAX, MPI_COMM_WORLD );
208  double time = recv_data[0] / 100.0;
209 
210  if( moab::MB_SUCCESS != rval )
211  {
212  std::string estr = mb.get_error_string( rval );
213  std::string msg;
214  mb.get_last_error( msg );
215  std::cout << "Read failed for proccess " << rank << " with error code " << rval << " (" << estr << ")"
216  << std::endl;
217  if( !msg.empty() ) std::cerr << '"' << msg << '"' << std::endl;
218  }
219 
220  if( rank == 0 )
221  {
222  if( recv_data[1] == moab::MB_SUCCESS ) std::cout << "Success!" << std::endl;
223  std::cout << "Read returned in " << time << " seconds" << std::endl;
224  }
225 
226  MPI_Finalize();
227  return ( moab::MB_SUCCESS != rval );
228 }