moab
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 <stdlib.h>
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   std::cout << argv0 << " " << usage << std::endl
00020             << "-P <rank>  Specified processor will wait for debugger to attach." << std::endl
00021             << "-p <name>  Tag identifying partition sets (default: \"" << 
00022                   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     int arg_pos = i;
00059     if (expect_tag == i) {
00060       partition_tag_name = argv[i];
00061       expect_tag = 0;
00062     }
00063     else if (expect_level == i) {
00064       char* endptr;
00065       debug_level = (int)strtol( argv[i], &endptr, 0 );
00066       if (*endptr || endptr == argv[i] || debug_level < 0) {
00067         std::cerr << "Expected positive integer value following '-g' flag" << std::endl;
00068         error(argv[0]);
00069       }
00070       expect_level = 0;
00071     }
00072     else if (expect_opt == i) {
00073       options << ";" << argv[i];
00074       expect_opt = 0;
00075     }
00076     else if (expect_rank == i) {
00077       char* endptr;
00078       pause_rank = (int)strtol( argv[i], &endptr, 0 );
00079       if (*endptr || endptr == argv[i] || pause_rank < 0) {
00080         std::cerr << "Expected positive integer value following '-P' flag" << std::endl;
00081         error(argv[0]);
00082       }
00083       expect_rank = 0;
00084     }
00085     else if (argv[i][0] == '-' && !no_more_flags) {
00086       for (int j = 1; argv[i][j]; ++j) {
00087         switch (argv[i][j]) {
00088           case '-': no_more_flags = true; break;
00089           case 'P': expect_rank = ++arg_pos; break;
00090           case 'p': expect_tag = ++arg_pos; break;
00091           case 'a': assign_by_id = true; break;
00092           case 'R': resolve_shared = false; break;
00093           case 'b': read_mode = BCAST_MODE; break;
00094           case 'd': read_mode = DELETE_MODE; break;
00095           case 'f': read_mode = PART_MODE; break;
00096           case 'g': expect_level = ++arg_pos; break;
00097           case 'O': expect_opt = ++arg_pos; break;
00098           case 'h': help(argv[0]); break;
00099           default:
00100             std::cerr << "Unknown flag: -" << argv[i][j] << std::endl;
00101            error(argv[0]);
00102         }
00103       }
00104     }
00105     else if (filename) {
00106       std::cerr << "Unexpected argument: \"" << argv[i] << "\"" << std::endl;
00107       error(argv[0]);
00108     }
00109     else {
00110       filename = argv[i];
00111     }
00112   }
00113   
00114   if (expect_tag) {
00115     std::cerr << "Expected value following -p flag" << std::endl;
00116     error(argv[0]);
00117   }
00118   if (expect_level) {
00119     std::cerr << "Expected value following -g flag" << std::endl;
00120     error(argv[0]);
00121   }
00122   if (expect_opt) {
00123     std::cerr << "Expected value following -O flag" << std::endl;
00124     error(argv[0]);
00125   }
00126   if (expect_rank) {
00127     std::cerr << "Expected rank following -P flag" << std::endl;
00128     error(argv[0]);
00129   }
00130   if (!filename) {
00131     std::cerr << "No file name specified" << std::endl;
00132     error(argv[0]);
00133   }
00134   
00135   options << ";PARTITION=" << partition_tag_name
00136           << ";PARALLEL=" << read_mode;
00137   if (resolve_shared)
00138     options << ";PARALLEL_RESOLVE_SHARED_ENTS";
00139   if (assign_by_id)
00140     options << ";PARTITION_BY_RANK";
00141   if (debug_level)
00142     options << ";DEBUG_IO=" << debug_level;
00143   
00144   moab::Core core;
00145   moab::Interface& mb = core;
00146   
00147   if (pause_rank >= 0) {
00148     if (pause_rank == rank) {
00149       std::cout << "Process " << rank << " with PID " << getpid() << " waiting for debugger" << std::endl
00150                 << "Set local variable 'do_wait' to zero to continue" << std::endl;
00151     
00152       volatile int do_wait = 1;
00153       while (do_wait) {
00154         sleep(1);
00155       } 
00156     }
00157     MPI_Barrier( MPI_COMM_WORLD );
00158   }
00159   
00160   std::string opts = options.str();
00161   if (rank == 0) 
00162     std::cout << "Reading \"" << filename << "\" with options=\""
00163               << opts << "\"." << std::endl;
00164               
00165   double init_time = MPI_Wtime();
00166   moab::ErrorCode rval = mb.load_file( filename, 0, opts.c_str() );
00167   double fini_time = MPI_Wtime();
00168   
00169   long send_data[2] = { (long)(100*(fini_time-init_time)), rval };
00170   long recv_data[2];
00171   MPI_Allreduce( send_data, recv_data, 2, MPI_LONG, MPI_MAX, MPI_COMM_WORLD );
00172   double time = recv_data[0]/100.0;
00173   
00174   if (moab::MB_SUCCESS != rval) {
00175     std::string estr = mb.get_error_string(rval);
00176     std::string msg;
00177     mb.get_last_error( msg );
00178     std::cout << "Read failed for proccess " << rank << " with error code " 
00179               << rval << " (" << estr << ")" << std::endl;
00180     if (!msg.empty()) 
00181       std::cerr << '"' << msg << '"' << std::endl;
00182   }
00183   
00184   if (rank == 0) {
00185     if (recv_data[1] == moab::MB_SUCCESS)
00186       std::cout << "Success!" << std::endl;
00187     std::cout << "Read returned in " << time << " seconds" << std::endl;
00188   }
00189   
00190   MPI_Finalize();
00191   return (moab::MB_SUCCESS != rval);
00192 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines