MOAB: Mesh Oriented datABase  (version 5.3.1)
parread.cpp
Go to the documentation of this file.
00001 #include "moab_mpi.h"
00002 #include "moab/Core.hpp"
00003 #include <sstream>
00004 #include <iostream>
00005 #include <cstdlib>
00006 #include <unistd.h>
00007 
00008 const char usage[] = "[-b|-d|-f] [-P <rank>] [-p <name>] [-R] [-g <level>] [-O <option>] <filename>";
00009 
00010 const char DEFAULT_PARTITION_TAG[] = "PARALLEL_PARTITION";
00011 
00012 void error( const char* argv0 )
00013 {
00014     std::cerr << "Usage: " << argv0 << " " << usage << std::endl;
00015     exit( 1 );
00016 }
00017 
00018 void help( const char* argv0 )
00019 {
00020     std::cout << argv0 << " " << usage << std::endl
00021               << "-P <rank>  Specified processor will wait for debugger to attach." << std::endl
00022               << "-p <name>  Tag identifying partition sets (default: \"" << DEFAULT_PARTITION_TAG << "\")" << std::endl
00023               << "-a         Assign partitions to processes by matching part number to rank" << std::endl
00024               << "-R         Do not resolve shared entities" << std::endl
00025               << "-b         Use broadcast & delete read method" << std::endl
00026               << "-d         Use read & delete method" << std::endl
00027               << "-f         Use true parallel read (default)" << std::endl
00028               << "-g         Set debug output level" << std::endl;
00029     exit( 0 );
00030 }
00031 
00032 int main( int argc, char* argv[] )
00033 {
00034     MPI_Init( &argc, &argv );
00035 
00036     const char BCAST_MODE[]        = "BCAST_DELETE";
00037     const char DELETE_MODE[]       = "READ_DELETE";
00038     const char PART_MODE[]         = "READ_PART";
00039     const char* read_mode          = PART_MODE;
00040     const char* partition_tag_name = DEFAULT_PARTITION_TAG;
00041     bool assign_by_id              = false;
00042     bool resolve_shared            = true;
00043     int debug_level                = 0;
00044     int pause_rank                 = -1;
00045     const char* filename           = 0;
00046     std::ostringstream options;
00047     options << ";";
00048 
00049     int rank;
00050     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
00051 
00052     int expect_tag     = 0;
00053     int expect_level   = 0;
00054     int expect_opt     = 0;
00055     int expect_rank    = 0;
00056     bool no_more_flags = false;
00057     for( int i = 1; i < argc; ++i )
00058     {
00059         int arg_pos = i;
00060         if( expect_tag == i )
00061         {
00062             partition_tag_name = argv[i];
00063             expect_tag         = 0;
00064         }
00065         else if( expect_level == i )
00066         {
00067             char* endptr;
00068             debug_level = (int)strtol( argv[i], &endptr, 0 );
00069             if( *endptr || endptr == argv[i] || debug_level < 0 )
00070             {
00071                 std::cerr << "Expected positive integer value following '-g' flag" << std::endl;
00072                 error( argv[0] );
00073             }
00074             expect_level = 0;
00075         }
00076         else if( expect_opt == i )
00077         {
00078             options << ";" << argv[i];
00079             expect_opt = 0;
00080         }
00081         else if( expect_rank == i )
00082         {
00083             char* endptr;
00084             pause_rank = (int)strtol( argv[i], &endptr, 0 );
00085             if( *endptr || endptr == argv[i] || pause_rank < 0 )
00086             {
00087                 std::cerr << "Expected positive integer value following '-P' flag" << std::endl;
00088                 error( argv[0] );
00089             }
00090             expect_rank = 0;
00091         }
00092         else if( argv[i][0] == '-' && !no_more_flags )
00093         {
00094             for( int j = 1; argv[i][j]; ++j )
00095             {
00096                 switch( argv[i][j] )
00097                 {
00098                     case '-':
00099                         no_more_flags = true;
00100                         break;
00101                     case 'P':
00102                         expect_rank = ++arg_pos;
00103                         break;
00104                     case 'p':
00105                         expect_tag = ++arg_pos;
00106                         break;
00107                     case 'a':
00108                         assign_by_id = true;
00109                         break;
00110                     case 'R':
00111                         resolve_shared = false;
00112                         break;
00113                     case 'b':
00114                         read_mode = BCAST_MODE;
00115                         break;
00116                     case 'd':
00117                         read_mode = DELETE_MODE;
00118                         break;
00119                     case 'f':
00120                         read_mode = PART_MODE;
00121                         break;
00122                     case 'g':
00123                         expect_level = ++arg_pos;
00124                         break;
00125                     case 'O':
00126                         expect_opt = ++arg_pos;
00127                         break;
00128                     case 'h':
00129                         help( argv[0] );
00130                         break;
00131                     default:
00132                         std::cerr << "Unknown flag: -" << argv[i][j] << std::endl;
00133                         error( argv[0] );
00134                 }
00135             }
00136         }
00137         else if( filename )
00138         {
00139             std::cerr << "Unexpected argument: \"" << argv[i] << "\"" << std::endl;
00140             error( argv[0] );
00141         }
00142         else
00143         {
00144             filename = argv[i];
00145         }
00146     }
00147 
00148     if( expect_tag )
00149     {
00150         std::cerr << "Expected value following -p flag" << std::endl;
00151         error( argv[0] );
00152     }
00153     if( expect_level )
00154     {
00155         std::cerr << "Expected value following -g flag" << std::endl;
00156         error( argv[0] );
00157     }
00158     if( expect_opt )
00159     {
00160         std::cerr << "Expected value following -O flag" << std::endl;
00161         error( argv[0] );
00162     }
00163     if( expect_rank )
00164     {
00165         std::cerr << "Expected rank following -P flag" << std::endl;
00166         error( argv[0] );
00167     }
00168     if( !filename )
00169     {
00170         std::cerr << "No file name specified" << std::endl;
00171         error( argv[0] );
00172     }
00173 
00174     options << ";PARTITION=" << partition_tag_name << ";PARALLEL=" << read_mode;
00175     if( resolve_shared ) options << ";PARALLEL_RESOLVE_SHARED_ENTS";
00176     if( assign_by_id ) options << ";PARTITION_BY_RANK";
00177     if( debug_level ) options << ";DEBUG_IO=" << debug_level;
00178 
00179     moab::Core core;
00180     moab::Interface& mb = core;
00181 
00182     if( pause_rank >= 0 )
00183     {
00184         if( pause_rank == rank )
00185         {
00186             std::cout << "Process " << rank << " with PID " << getpid() << " waiting for debugger" << std::endl
00187                       << "Set local variable 'do_wait' to zero to continue" << std::endl;
00188 
00189             volatile int do_wait = 1;
00190             while( do_wait )
00191             {
00192                 sleep( 1 );
00193             }
00194         }
00195         MPI_Barrier( MPI_COMM_WORLD );
00196     }
00197 
00198     std::string opts = options.str();
00199     if( rank == 0 ) std::cout << "Reading \"" << filename << "\" with options=\"" << opts << "\"." << std::endl;
00200 
00201     double init_time     = MPI_Wtime();
00202     moab::ErrorCode rval = mb.load_file( filename, 0, opts.c_str() );
00203     double fini_time     = MPI_Wtime();
00204 
00205     long send_data[2] = { (long)( 100 * ( fini_time - init_time ) ), rval };
00206     long recv_data[2];
00207     MPI_Allreduce( send_data, recv_data, 2, MPI_LONG, MPI_MAX, MPI_COMM_WORLD );
00208     double time = recv_data[0] / 100.0;
00209 
00210     if( moab::MB_SUCCESS != rval )
00211     {
00212         std::string estr = mb.get_error_string( rval );
00213         std::string msg;
00214         mb.get_last_error( msg );
00215         std::cout << "Read failed for proccess " << rank << " with error code " << rval << " (" << estr << ")"
00216                   << std::endl;
00217         if( !msg.empty() ) std::cerr << '"' << msg << '"' << std::endl;
00218     }
00219 
00220     if( rank == 0 )
00221     {
00222         if( recv_data[1] == moab::MB_SUCCESS ) std::cout << "Success!" << std::endl;
00223         std::cout << "Read returned in " << time << " seconds" << std::endl;
00224     }
00225 
00226     MPI_Finalize();
00227     return ( moab::MB_SUCCESS != rval );
00228 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines