MOAB: Mesh Oriented datABase  (version 5.3.0)
ReadParallel.cpp
Go to the documentation of this file.
00001 #include "ReadParallel.hpp"
00002 #include "moab/Core.hpp"
00003 #include "moab/ProcConfig.hpp"
00004 #include "moab/FileOptions.hpp"
00005 #include "moab/Error.hpp"
00006 #include "moab/ReaderWriterSet.hpp"
00007 #include "moab/ReadUtilIface.hpp"
00008 #include "moab/ParallelComm.hpp"
00009 #include "MBParallelConventions.h"
00010 
00011 #include <iostream>
00012 #include <iomanip>
00013 #include <iterator>
00014 #include <sstream>
00015 #include <algorithm>
00016 #include <cassert>
00017 
00018 namespace moab
00019 {
00020 
00021 const bool debug = false;
00022 
00023 const char* ReadParallel::ParallelActionsNames[] = { "PARALLEL READ",
00024                                                      "PARALLEL READ PART",
00025                                                      "PARALLEL BROADCAST",
00026                                                      "PARALLEL DELETE NONLOCAL",
00027                                                      "PARALLEL CHECK_GIDS_SERIAL",
00028                                                      "PARALLEL GET_FILESET_ENTS",
00029                                                      "PARALLEL RESOLVE_SHARED_ENTS",
00030                                                      "PARALLEL EXCHANGE_GHOSTS",
00031                                                      "PARALLEL RESOLVE_SHARED_SETS",
00032                                                      "PARALLEL_AUGMENT_SETS_WITH_GHOSTS",
00033                                                      "PARALLEL PRINT_PARALLEL",
00034                                                      "PARALLEL_CREATE_TRIVIAL_PARTITION",
00035                                                      "PARALLEL_CORRECT_THIN_GHOST_LAYERS" };
00036 
00037 const char* ReadParallel::parallelOptsNames[] = { "NONE", "BCAST", "BCAST_DELETE", "READ_DELETE", "READ_PART", "", 0 };
00038 
00039 ReadParallel::ReadParallel( Interface* impl, ParallelComm* pc )
00040     : mbImpl( impl ), myPcomm( pc ), myDebug( "ReadPara", std::cerr )
00041 {
00042     if( !myPcomm )
00043     {
00044         myPcomm = ParallelComm::get_pcomm( mbImpl, 0 );
00045         if( NULL == myPcomm ) myPcomm = new ParallelComm( mbImpl, MPI_COMM_WORLD );
00046     }
00047     myDebug.set_rank( myPcomm->proc_config().proc_rank() );
00048     if( debug )  // For backwards compatibility, enable all debug output if constant is true
00049         myDebug.set_verbosity( 10 );
00050 
00051     impl->query_interface( mError );
00052 }
00053 
00054 ErrorCode ReadParallel::load_file( const char** file_names, const int num_files, const EntityHandle* file_set,
00055                                    const FileOptions& opts, const ReaderIface::SubsetList* subset_list,
00056                                    const Tag* file_id_tag )
00057 {
00058     int tmpval;
00059     if( MB_SUCCESS == opts.get_int_option( "DEBUG_PIO", 1, tmpval ) )
00060     {
00061         myDebug.set_verbosity( tmpval );
00062         myPcomm->set_debug_verbosity( tmpval );
00063     }
00064     myDebug.tprint( 1, "Setting up...\n" );
00065 
00066     // Get parallel settings
00067     int parallel_mode;
00068     ErrorCode result = opts.match_option( "PARALLEL", parallelOptsNames, parallel_mode );
00069     if( MB_FAILURE == result ) { MB_SET_ERR( MB_FAILURE, "Unexpected value for 'PARALLEL' option" ); }
00070     else if( MB_ENTITY_NOT_FOUND == result )
00071     {
00072         parallel_mode = 0;
00073     }
00074 
00075     // Get partition setting
00076     bool distrib;
00077     std::string partition_tag_name;
00078     result = opts.get_option( "PARTITION", partition_tag_name );
00079     if( MB_ENTITY_NOT_FOUND == result )
00080     {
00081         distrib            = false;
00082         partition_tag_name = "";
00083     }
00084     else
00085     {
00086         distrib = true;
00087         if( partition_tag_name.empty() ) partition_tag_name = PARALLEL_PARTITION_TAG_NAME;
00088 
00089         // Also get deprecated PARTITION_DISTRIBUTE option
00090         // so that higher-level code doesn't return an error
00091         // due to an unrecognized option
00092         opts.get_null_option( "PARTITION_DISTRIBUTE" );
00093     }
00094 
00095     // Get partition tag value(s), if any, and whether they're to be
00096     // distributed or assigned
00097     std::vector< int > partition_tag_vals;
00098     opts.get_ints_option( "PARTITION_VAL", partition_tag_vals );
00099 
00100     // see if partition tag name is "TRIVIAL", if the tag exists
00101     bool create_trivial_partition = false;
00102     if( partition_tag_name == std::string( "TRIVIAL" ) )
00103     {
00104         Tag ttag;  // see if the trivial tag exists
00105         result = mbImpl->tag_get_handle( partition_tag_name.c_str(), ttag );
00106         if( MB_TAG_NOT_FOUND == result ) create_trivial_partition = true;
00107     }
00108     // See if we need to report times
00109     bool cputime = false;
00110     result       = opts.get_null_option( "CPUTIME" );
00111     if( MB_SUCCESS == result ) cputime = true;
00112 
00113     // See if we need to report times
00114     bool print_parallel = false;
00115     result              = opts.get_null_option( "PRINT_PARALLEL" );
00116     if( MB_SUCCESS == result ) print_parallel = true;
00117 
00118     // Get ghosting options
00119     std::string ghost_str;
00120     int bridge_dim, ghost_dim = -1, num_layers, addl_ents = 0;
00121     result = opts.get_str_option( "PARALLEL_GHOSTS", ghost_str );
00122     if( MB_TYPE_OUT_OF_RANGE == result )
00123     {
00124         ghost_dim  = 3;
00125         bridge_dim = 0;
00126         num_layers = 1;
00127     }
00128     else if( MB_SUCCESS == result )
00129     {
00130         int num_fields = sscanf( ghost_str.c_str(), "%d.%d.%d.%d", &ghost_dim, &bridge_dim, &num_layers, &addl_ents );
00131         if( 3 > num_fields ) { MB_SET_ERR( MB_FAILURE, "Didn't read 3 fields from PARALLEL_GHOSTS string" ); }
00132     }
00133 
00134     // Get resolve_shared_ents option
00135     std::string shared_str;
00136     int resolve_dim = -2, shared_dim = -1;
00137     result = opts.get_str_option( "PARALLEL_RESOLVE_SHARED_ENTS", shared_str );
00138     if( MB_TYPE_OUT_OF_RANGE == result )
00139     {
00140         resolve_dim = -1;
00141         shared_dim  = -1;
00142     }
00143     else if( MB_SUCCESS == result )
00144     {
00145         int num_fields = sscanf( shared_str.c_str(), "%d.%d", &resolve_dim, &shared_dim );
00146         if( 2 != num_fields )
00147         {
00148             MB_SET_ERR( MB_FAILURE, "Didn't read 2 fields from PARALLEL_RESOLVE_SHARED_ENTS string" );
00149         }
00150     }
00151 
00152     // Get skip augmenting with ghosts option
00153     bool skip_augment = false;
00154     result            = opts.get_null_option( "SKIP_AUGMENT_WITH_GHOSTS" );
00155     if( MB_SUCCESS == result ) skip_augment = true;
00156 
00157     bool correct_thin_ghosts = false;
00158     result                   = opts.get_null_option( "PARALLEL_THIN_GHOST_LAYER" );
00159     if( MB_SUCCESS == result ) correct_thin_ghosts = true;
00160 
00161     // Get MPI IO processor rank
00162     int reader_rank;
00163     result = opts.get_int_option( "MPI_IO_RANK", reader_rank );
00164     if( MB_ENTITY_NOT_FOUND == result )
00165         reader_rank = 0;
00166     else if( MB_SUCCESS != result )
00167     {
00168         MB_SET_ERR( MB_FAILURE, "Unexpected value for 'MPI_IO_RANK' option" );
00169     }
00170 
00171     // Now that we've parsed all the parallel options, make an instruction queue
00172     std::vector< int > pa_vec;
00173     bool is_reader = ( reader_rank == (int)myPcomm->proc_config().proc_rank() );
00174 
00175     bool partition_by_rank = false;
00176     if( MB_SUCCESS == opts.get_null_option( "PARTITION_BY_RANK" ) )
00177     {
00178         partition_by_rank = true;
00179         if( !partition_tag_vals.empty() )
00180         {
00181             MB_SET_ERR( MB_FAILURE, "Cannot specify both PARTITION_VALS and PARTITION_BY_RANK" );
00182         }
00183     }
00184 
00185     double factor_seq;
00186     if( MB_SUCCESS == opts.get_real_option( "PARALLEL_SEQUENCE_FACTOR", factor_seq ) )
00187     {
00188         if( factor_seq < 1. ) MB_SET_ERR( MB_FAILURE, "cannot have sequence factor less than 1." );
00189         mbImpl->set_sequence_multiplier( factor_seq );
00190     }
00191     switch( parallel_mode )
00192     {
00193         case POPT_BCAST:
00194             myDebug.print( 1, "Read mode is BCAST\n" );
00195             if( is_reader )
00196             {
00197                 pa_vec.push_back( PA_READ );
00198                 pa_vec.push_back( PA_CHECK_GIDS_SERIAL );
00199                 pa_vec.push_back( PA_GET_FILESET_ENTS );
00200             }
00201             pa_vec.push_back( PA_BROADCAST );
00202             if( !is_reader ) pa_vec.push_back( PA_GET_FILESET_ENTS );
00203             break;
00204 
00205         case POPT_BCAST_DELETE:
00206             myDebug.print( 1, "Read mode is BCAST_DELETE\n" );
00207             if( is_reader )
00208             {
00209                 pa_vec.push_back( PA_READ );
00210                 pa_vec.push_back( PA_CHECK_GIDS_SERIAL );
00211                 pa_vec.push_back( PA_GET_FILESET_ENTS );
00212                 if( create_trivial_partition ) pa_vec.push_back( PA_CREATE_TRIVIAL_PARTITION );
00213             }
00214             pa_vec.push_back( PA_BROADCAST );
00215             if( !is_reader ) pa_vec.push_back( PA_GET_FILESET_ENTS );
00216             pa_vec.push_back( PA_DELETE_NONLOCAL );
00217             break;
00218 
00219         case POPT_DEFAULT:
00220         case POPT_READ_DELETE:
00221             myDebug.print( 1, "Read mode is READ_DELETE\n" );
00222             pa_vec.push_back( PA_READ );
00223             pa_vec.push_back( PA_CHECK_GIDS_SERIAL );
00224             pa_vec.push_back( PA_GET_FILESET_ENTS );
00225             pa_vec.push_back( PA_DELETE_NONLOCAL );
00226             break;
00227 
00228         case POPT_READ_PART:
00229             myDebug.print( 1, "Read mode is READ_PART\n" );
00230             pa_vec.push_back( PA_READ_PART );
00231             break;
00232 
00233         default:
00234             MB_SET_ERR( MB_FAILURE, "Unexpected parallel read mode" );
00235     }
00236 
00237     if( -2 != resolve_dim ) pa_vec.push_back( PA_RESOLVE_SHARED_ENTS );
00238 
00239     if( -1 != ghost_dim ) pa_vec.push_back( PA_EXCHANGE_GHOSTS );
00240 
00241     if( -2 != resolve_dim )
00242     {
00243         pa_vec.push_back( PA_RESOLVE_SHARED_SETS );
00244         if( -1 != ghost_dim && !skip_augment ) pa_vec.push_back( PA_AUGMENT_SETS_WITH_GHOSTS );
00245         if( -1 != ghost_dim && correct_thin_ghosts ) pa_vec.push_back( PA_CORRECT_THIN_GHOSTS );
00246     }
00247 
00248     if( print_parallel ) pa_vec.push_back( PA_PRINT_PARALLEL );
00249 
00250     result = load_file( file_names, num_files, file_set, parallel_mode, partition_tag_name, partition_tag_vals, distrib,
00251                         partition_by_rank, pa_vec, opts, subset_list, file_id_tag, reader_rank, cputime, resolve_dim,
00252                         shared_dim, ghost_dim, bridge_dim, num_layers, addl_ents );MB_CHK_ERR( result );
00253 
00254     if( parallel_mode == POPT_BCAST_DELETE && !is_reader ) opts.mark_all_seen();
00255 
00256     return MB_SUCCESS;
00257 }
00258 
00259 ErrorCode ReadParallel::load_file( const char** file_names, const int num_files, const EntityHandle* file_set_ptr,
00260                                    int /*parallel_mode*/, std::string& partition_tag_name,
00261                                    std::vector< int >& partition_tag_vals, bool distrib, bool partition_by_rank,
00262                                    std::vector< int >& pa_vec, const FileOptions& opts,
00263                                    const ReaderIface::SubsetList* subset_list, const Tag* file_id_tag,
00264                                    const int reader_rank, const bool cputime, const int resolve_dim,
00265                                    const int shared_dim, const int ghost_dim, const int bridge_dim,
00266                                    const int num_layers, const int addl_ents )
00267 {
00268     ErrorCode result = MB_SUCCESS;
00269     if( myPcomm == NULL ) myPcomm = new ParallelComm( mbImpl, MPI_COMM_WORLD );
00270 
00271     Range entities;
00272     Tag file_set_tag = 0;
00273     int other_sets   = 0;
00274     ReaderWriterSet::iterator iter;
00275     Range other_file_sets, file_sets;
00276     Core* impl = dynamic_cast< Core* >( mbImpl );
00277 
00278     std::vector< double > act_times( pa_vec.size() + 1 );
00279     std::vector< int >::iterator vit;
00280     int i, j;
00281     act_times[0] = MPI_Wtime();
00282 
00283     // Make a new set for the parallel read
00284     EntityHandle file_set;
00285     if( !file_set_ptr || !( *file_set_ptr ) )
00286     {
00287         result = mbImpl->create_meshset( MESHSET_SET, file_set );MB_CHK_SET_ERR( result, "Trouble creating file set" );
00288     }
00289     else
00290         file_set = *file_set_ptr;
00291 
00292     bool i_read     = false;
00293     Tag id_tag      = 0;
00294     bool use_id_tag = false;
00295     Range ents;
00296 
00297     for( i = 1, vit = pa_vec.begin(); vit != pa_vec.end(); ++vit, i++ )
00298     {
00299         ErrorCode tmp_result = MB_SUCCESS;
00300         switch( *vit )
00301         {
00302                 //==================
00303             case PA_READ:
00304                 i_read = true;
00305 
00306                 for( j = 0; j < num_files; j++ )
00307                 {
00308                     myDebug.tprintf( 1, "Reading file: \"%s\"\n", file_names[j] );
00309 
00310                     EntityHandle new_file_set;
00311                     result = mbImpl->create_meshset( MESHSET_SET, new_file_set );MB_CHK_SET_ERR( result, "Trouble creating file set" );
00312                     tmp_result = impl->serial_load_file( file_names[j], &new_file_set, opts, subset_list, file_id_tag );
00313                     if( MB_SUCCESS != tmp_result ) break;
00314 
00315                     // Put the contents of each file set for the reader into the
00316                     // file set for the parallel read
00317                     assert( 0 != new_file_set );
00318                     Range all_ents;
00319                     tmp_result = mbImpl->get_entities_by_handle( new_file_set, all_ents );
00320                     if( MB_SUCCESS != tmp_result ) break;
00321                     all_ents.insert( new_file_set );
00322                     tmp_result = mbImpl->add_entities( file_set, all_ents );
00323                     if( MB_SUCCESS != tmp_result ) break;
00324                 }
00325                 if( MB_SUCCESS != tmp_result ) break;
00326 
00327                 // Mark the file set for this parallel reader
00328                 tmp_result = mbImpl->tag_get_handle( "__file_set", 1, MB_TYPE_INTEGER, file_set_tag,
00329                                                      MB_TAG_SPARSE | MB_TAG_CREAT );
00330                 if( MB_SUCCESS != tmp_result ) break;
00331 
00332                 tmp_result = mbImpl->tag_set_data( file_set_tag, &file_set, 1, &other_sets );
00333                 break;
00334 
00335                 //==================
00336             case PA_READ_PART: {
00337                 myDebug.tprintf( 1, "Reading file: \"%s\"\n", file_names[0] );
00338 
00339                 i_read = true;
00340                 if( num_files != 1 )
00341                 {
00342                     MB_SET_ERR( MB_NOT_IMPLEMENTED, "Multiple file read not supported for READ_PART" );
00343                 }
00344 
00345                 // If we're going to resolve shared entities, then we need
00346                 // to ask the file reader to populate a tag with unique ids
00347                 // (typically file ids/indices/whatever.)
00348                 if( std::find( pa_vec.begin(), pa_vec.end(), PA_RESOLVE_SHARED_ENTS ) != pa_vec.end() )
00349                 {
00350                     use_id_tag = true;
00351                     if( !file_id_tag )
00352                     {
00353                         // This tag is really used for resolving shared entities with crystal router
00354                         // In the end, this is an identifier that gets converted to long
00355                         // In hdf5 file reader, we also convert from hdf5 file id type to long
00356                         tmp_result = mbImpl->tag_get_handle( "", sizeof( long ), MB_TYPE_OPAQUE, id_tag,
00357                                                              MB_TAG_DENSE | MB_TAG_CREAT );
00358                         if( MB_SUCCESS != tmp_result ) break;
00359                         file_id_tag = &id_tag;
00360                     }
00361                 }
00362 
00363                 ReaderIface::IDTag parts = { partition_tag_name.c_str(), 0, 0 };
00364                 ReaderIface::SubsetList sl;
00365                 sl.num_parts = 0;
00366                 int rank     = myPcomm->rank();
00367                 if( partition_by_rank )
00368                 {
00369                     assert( partition_tag_vals.empty() );
00370                     parts.tag_values     = &rank;
00371                     parts.num_tag_values = 1;
00372                 }
00373                 else
00374                 {
00375                     sl.num_parts   = myPcomm->size();
00376                     sl.part_number = myPcomm->rank();
00377                     if( !partition_tag_vals.empty() )
00378                     {
00379                         parts.tag_values     = &partition_tag_vals[0];
00380                         parts.num_tag_values = partition_tag_vals.size();
00381                     }
00382                 }
00383                 std::vector< ReaderIface::IDTag > subset;
00384                 if( subset_list )
00385                 {
00386                     std::vector< ReaderIface::IDTag > tmplist( subset_list->tag_list,
00387                                                                subset_list->tag_list + subset_list->tag_list_length );
00388                     tmplist.push_back( parts );
00389                     subset.swap( tmplist );
00390                     sl.tag_list        = &subset[0];
00391                     sl.tag_list_length = subset.size();
00392                 }
00393                 else
00394                 {
00395                     sl.tag_list        = &parts;
00396                     sl.tag_list_length = 1;
00397                 }
00398                 tmp_result = impl->serial_load_file( *file_names, &file_set, opts, &sl, file_id_tag );
00399                 if( MB_SUCCESS != tmp_result ) break;
00400 
00401                 if( !partition_tag_name.empty() )
00402                 {
00403                     Tag part_tag;
00404                     tmp_result = impl->tag_get_handle( partition_tag_name.c_str(), 1, MB_TYPE_INTEGER, part_tag );
00405                     if( MB_SUCCESS != tmp_result ) break;
00406 
00407                     tmp_result = impl->get_entities_by_type_and_tag( file_set, MBENTITYSET, &part_tag, 0, 1,
00408                                                                      myPcomm->partition_sets() );
00409                 }
00410             }
00411             break;
00412 
00413                 //==================
00414             case PA_GET_FILESET_ENTS:
00415                 myDebug.tprint( 1, "Getting fileset entities.\n" );
00416 
00417                 // Get entities in the file set, and add actual file set to it;
00418                 // mark the file set to make sure any receiving procs know which it is
00419                 tmp_result = mbImpl->get_entities_by_handle( file_set, entities );
00420                 if( MB_SUCCESS != tmp_result )
00421                 {
00422                     entities.clear();
00423                     break;
00424                 }
00425 
00426                 // Add actual file set to entities too
00427                 entities.insert( file_set );
00428                 break;
00429                 //==================
00430             case PA_CREATE_TRIVIAL_PARTITION: {
00431                 myDebug.tprint( 1, "create trivial partition, for higher dim entities.\n" );
00432                 // get high dim entities (2 or 3)
00433                 Range hi_dim_ents = entities.subset_by_dimension( 3 );
00434                 if( hi_dim_ents.empty() ) hi_dim_ents = entities.subset_by_dimension( 2 );
00435                 if( hi_dim_ents.empty() ) hi_dim_ents = entities.subset_by_dimension( 1 );
00436                 if( hi_dim_ents.empty() ) MB_SET_ERR( MB_FAILURE, "there are no elements of dim 1-3" );
00437 
00438                 size_t num_hi_ents     = hi_dim_ents.size();
00439                 unsigned int num_parts = myPcomm->size();
00440 
00441                 // create first the trivial partition tag
00442                 int dum_id = -1;
00443                 Tag ttag;  // trivial tag
00444                 tmp_result = mbImpl->tag_get_handle( partition_tag_name.c_str(), 1, MB_TYPE_INTEGER, ttag,
00445                                                      MB_TAG_CREAT | MB_TAG_SPARSE, &dum_id );MB_CHK_SET_ERR( tmp_result, "Can't create trivial partition tag" );
00446 
00447                 // Compute the number of high dim entities on each part
00448                 size_t nPartEnts = num_hi_ents / num_parts;
00449 
00450                 // Number of extra entities after equal split over parts
00451                 int iextra          = num_hi_ents % num_parts;
00452                 Range::iterator itr = hi_dim_ents.begin();
00453                 for( int k = 0; k < (int)num_parts; k++ )
00454                 {
00455                     // create a mesh set, insert a subrange of entities
00456                     EntityHandle part_set;
00457                     tmp_result = mbImpl->create_meshset( MESHSET_SET, part_set );MB_CHK_SET_ERR( tmp_result, "Can't create part set" );
00458                     entities.insert( part_set );
00459 
00460                     tmp_result = mbImpl->tag_set_data( ttag, &part_set, 1, &k );MB_CHK_SET_ERR( tmp_result, "Can't set trivial partition tag" );
00461                     Range subrange;
00462                     size_t num_ents_in_part = nPartEnts;
00463                     if( i < iextra ) num_ents_in_part++;
00464                     for( size_t i1 = 0; i1 < num_ents_in_part; i1++, itr++ )
00465                         subrange.insert( *itr );
00466                     tmp_result = mbImpl->add_entities( part_set, subrange );MB_CHK_SET_ERR( tmp_result, "Can't add entities to trivial part " << k );
00467                     myDebug.tprintf( 1, "create trivial part %d with %lu entities \n", k, num_ents_in_part );
00468                     tmp_result = mbImpl->add_entities( file_set, &part_set, 1 );MB_CHK_SET_ERR( tmp_result, "Can't add trivial part to file set " << k );
00469                 }
00470             }
00471 
00472             break;
00473                 //==================
00474             case PA_BROADCAST:
00475                 // Do the actual broadcast; if single-processor, ignore error
00476                 myDebug.tprint( 1, "Broadcasting mesh.\n" );
00477 
00478                 if( myPcomm->proc_config().proc_size() > 1 )
00479                 {
00480                     tmp_result = myPcomm->broadcast_entities( reader_rank, entities );
00481                     if( MB_SUCCESS != tmp_result ) break;
00482                 }
00483 
00484                 if( debug )
00485                 {
00486                     std::cerr << "Bcast done; entities:" << std::endl;
00487                     mbImpl->list_entities( 0, 0 );
00488                 }
00489 
00490                 // Add the received entities to this fileset if I wasn't the reader
00491                 if( !i_read && MB_SUCCESS == tmp_result ) tmp_result = mbImpl->add_entities( file_set, entities );
00492 
00493                 break;
00494 
00495                 //==================
00496             case PA_DELETE_NONLOCAL:
00497                 myDebug.tprint( 1, "Deleting nonlocal entities.\n" );
00498 
00499                 tmp_result = delete_nonlocal_entities( partition_tag_name, partition_tag_vals, distrib, file_set );
00500                 if( debug )
00501                 {
00502                     std::cerr << "Delete nonlocal done; entities:" << std::endl;
00503                     mbImpl->list_entities( 0, 0 );
00504                 }
00505 
00506                 if( MB_SUCCESS == tmp_result ) tmp_result = create_partition_sets( partition_tag_name, file_set );
00507 
00508                 break;
00509 
00510                 //==================
00511             case PA_CHECK_GIDS_SERIAL:
00512                 myDebug.tprint( 1, "Checking global IDs.\n" );
00513 
00514                 tmp_result = myPcomm->check_global_ids( file_set, 0, 1, true, false );
00515                 break;
00516 
00517                 //==================
00518             case PA_RESOLVE_SHARED_ENTS:
00519                 myDebug.tprint( 1, "Resolving shared entities.\n" );
00520 
00521                 if( 1 == myPcomm->size() )
00522                     tmp_result = MB_SUCCESS;
00523                 else
00524                     tmp_result =
00525                         myPcomm->resolve_shared_ents( file_set, resolve_dim, shared_dim, use_id_tag ? file_id_tag : 0 );
00526                 if( MB_SUCCESS != tmp_result ) break;
00527 
00528 #ifndef NDEBUG
00529                 // check number of owned vertices through pcomm's public interface
00530                 tmp_result = mbImpl->get_entities_by_type( 0, MBVERTEX, ents );
00531                 if( MB_SUCCESS == tmp_result )
00532                     tmp_result = myPcomm->filter_pstatus( ents, PSTATUS_NOT_OWNED, PSTATUS_NOT );
00533                 if( MB_SUCCESS == tmp_result )
00534                     myDebug.tprintf( 1, "Proc %u reports %lu owned vertices.\n", myPcomm->proc_config().proc_rank(),
00535                                      ents.size() );
00536 #endif
00537                 break;
00538 
00539                 //==================
00540             case PA_EXCHANGE_GHOSTS:
00541                 myDebug.tprint( 1, "Exchanging ghost entities.\n" );
00542 
00543                 tmp_result = myPcomm->exchange_ghost_cells( ghost_dim, bridge_dim, num_layers, addl_ents, true, true,
00544                                                             &file_set );
00545                 break;
00546 
00547                 //==================
00548             case PA_RESOLVE_SHARED_SETS:
00549                 myDebug.tprint( 1, "Resolving shared sets.\n" );
00550 
00551                 if( 1 == myPcomm->size() )
00552                     tmp_result = MB_SUCCESS;
00553                 else
00554                     tmp_result = myPcomm->resolve_shared_sets( file_set, use_id_tag ? file_id_tag : 0 );
00555                 break;
00556 
00557                 //==================
00558             case PA_AUGMENT_SETS_WITH_GHOSTS:
00559                 myDebug.tprint( 1, "Augmenting sets with ghost entities.\n" );
00560 
00561                 if( 1 == myPcomm->size() )
00562                     tmp_result = MB_SUCCESS;
00563                 else
00564                     tmp_result = myPcomm->augment_default_sets_with_ghosts( file_set );
00565                 break;
00566                 //==================
00567             case PA_CORRECT_THIN_GHOSTS:
00568                 myDebug.tprint( 1, "correcting thin ghost layers.\n" );
00569                 if( 2 >= myPcomm->size() )  // it is a problem only for multi-shared entities
00570                     tmp_result = MB_SUCCESS;
00571                 else
00572                     tmp_result = myPcomm->correct_thin_ghost_layers();
00573                 break;
00574             case PA_PRINT_PARALLEL:
00575                 myDebug.tprint( 1, "Printing parallel information.\n" );
00576 
00577                 tmp_result = myPcomm->list_entities( 0, -1 );
00578                 break;
00579 
00580                 //==================
00581             default:
00582                 MB_SET_ERR( MB_FAILURE, "Unexpected parallel action" );
00583         }  // switch (*vit)
00584 
00585         if( MB_SUCCESS != tmp_result ) { MB_SET_ERR( MB_FAILURE, "Failed in step " << ParallelActionsNames[*vit] ); }
00586 
00587         if( cputime ) act_times[i] = MPI_Wtime();
00588     }  // for (i = 1, vit = pa_vec.begin(); vit != pa_vec.end(); ++vit, i++)
00589 
00590     if( use_id_tag )
00591     {
00592         result = mbImpl->tag_delete( id_tag );MB_CHK_SET_ERR( result, "Trouble deleting id tag" );
00593     }
00594 
00595     if( cputime )
00596     {
00597         for( i = pa_vec.size(); i > 0; i-- )
00598             act_times[i] -= act_times[i - 1];
00599 
00600         // Replace initial time with overall time
00601         act_times[0] = MPI_Wtime() - act_times[0];
00602         // Get the maximum over all procs
00603         if( 0 != myPcomm->proc_config().proc_rank() )
00604         {
00605             MPI_Reduce( &act_times[0], 0, pa_vec.size() + 1, MPI_DOUBLE, MPI_MAX, 0,
00606                         myPcomm->proc_config().proc_comm() );
00607         }
00608         else
00609         {
00610 #if( MPI_VERSION >= 2 )
00611             MPI_Reduce( MPI_IN_PLACE, &act_times[0], pa_vec.size() + 1, MPI_DOUBLE, MPI_MAX, 0,
00612                         myPcomm->proc_config().proc_comm() );
00613 #else
00614             // Note, extra comm-size allocation is required
00615             std::vector< double > act_times_tmp( pa_vec.size() + 1 );
00616             MPI_Reduce( &act_times[0], &act_times_tmp[0], pa_vec.size() + 1, MPI_DOUBLE, MPI_MAX, 0,
00617                         myPcomm->proc_config().proc_comm() );
00618             act_times = act_times_tmp;  // extra copy here too
00619 #endif
00620             std::cout << "Parallel Read times: " << std::endl;
00621             for( i = 1, vit = pa_vec.begin(); vit != pa_vec.end(); ++vit, i++ )
00622                 std::cout << "  " << act_times[i] << " " << ParallelActionsNames[*vit] << std::endl;
00623             std::cout << "  " << act_times[0] << " PARALLEL TOTAL" << std::endl;
00624         }
00625     }
00626 
00627     return MB_SUCCESS;
00628 }
00629 
00630 ErrorCode ReadParallel::delete_nonlocal_entities( std::string& ptag_name, std::vector< int >& ptag_vals,
00631                                                   bool distribute, EntityHandle file_set )
00632 {
00633     Range partition_sets;
00634 
00635     Tag ptag;
00636     ErrorCode result = mbImpl->tag_get_handle( ptag_name.c_str(), 1, MB_TYPE_INTEGER, ptag );MB_CHK_SET_ERR( result, "Failed getting tag handle in delete_nonlocal_entities" );
00637 
00638     result = mbImpl->get_entities_by_type_and_tag( file_set, MBENTITYSET, &ptag, NULL, 1, myPcomm->partition_sets() );MB_CHK_SET_ERR( result, "Failed to get sets with partition-type tag" );
00639 
00640     int proc_sz = myPcomm->proc_config().proc_size();
00641     int proc_rk = myPcomm->proc_config().proc_rank();
00642 
00643     if( !ptag_vals.empty() )
00644     {
00645         // Values input, get sets with those values
00646         Range tmp_sets;
00647         std::vector< int > tag_vals( myPcomm->partition_sets().size() );
00648         result = mbImpl->tag_get_data( ptag, myPcomm->partition_sets(), &tag_vals[0] );MB_CHK_SET_ERR( result, "Failed to get tag data for partition vals tag" );
00649         for( std::vector< int >::iterator pit = tag_vals.begin(); pit != tag_vals.end(); ++pit )
00650         {
00651             std::vector< int >::iterator pit2 = std::find( ptag_vals.begin(), ptag_vals.end(), *pit );
00652             if( pit2 != ptag_vals.end() ) tmp_sets.insert( myPcomm->partition_sets()[pit - tag_vals.begin()] );
00653         }
00654 
00655         myPcomm->partition_sets().swap( tmp_sets );
00656     }
00657 
00658     if( distribute )
00659     {
00660         // For now, require that number of partition sets be greater
00661         // than number of procs
00662         if( myPcomm->partition_sets().size() < (unsigned int)proc_sz )
00663         {
00664             MB_SET_ERR( MB_FAILURE, "Too few parts; P = " << proc_rk << ", tag = " << ptag
00665                                                           << ", # sets = " << myPcomm->partition_sets().size() );
00666         }
00667 
00668         Range tmp_sets;
00669         // Distribute the partition sets
00670         unsigned int num_sets     = myPcomm->partition_sets().size() / proc_sz;
00671         unsigned int num_leftover = myPcomm->partition_sets().size() % proc_sz;
00672         int begin_set             = 0;
00673         if( proc_rk < (int)num_leftover )
00674         {
00675             num_sets++;
00676             begin_set = num_sets * proc_rk;
00677         }
00678         else
00679             begin_set = proc_rk * num_sets + num_leftover;
00680 
00681         for( unsigned int i = 0; i < num_sets; i++ )
00682             tmp_sets.insert( myPcomm->partition_sets()[begin_set + i] );
00683 
00684         myPcomm->partition_sets().swap( tmp_sets );
00685     }
00686 
00687     myDebug.print( 1, "My partition sets: ", myPcomm->partition_sets() );
00688 
00689     result = delete_nonlocal_entities( file_set );MB_CHK_ERR( result );
00690 
00691     return MB_SUCCESS;
00692 }
00693 
00694 ErrorCode ReadParallel::create_partition_sets( std::string& ptag_name, EntityHandle file_set )
00695 {
00696     int proc_rk      = myPcomm->proc_config().proc_rank();
00697     ErrorCode result = MB_SUCCESS;
00698 
00699     Tag ptag;
00700 
00701     // Tag the partition sets with a standard tag name
00702     if( ptag_name.empty() ) ptag_name = PARALLEL_PARTITION_TAG_NAME;
00703     bool tag_created = false;
00704     result = mbImpl->tag_get_handle( ptag_name.c_str(), 1, MB_TYPE_INTEGER, ptag, MB_TAG_SPARSE | MB_TAG_CREAT, 0,
00705                                      &tag_created );MB_CHK_SET_ERR( result, "Trouble getting PARALLEL_PARTITION tag" );
00706 
00707     if( !tag_created )
00708     {
00709         // This tag already exists; better check to see that tagged sets
00710         // agree with this partition
00711         Range tagged_sets;
00712         int* proc_rk_ptr = &proc_rk;
00713         result = mbImpl->get_entities_by_type_and_tag( file_set, MBENTITYSET, &ptag, (const void* const*)&proc_rk_ptr,
00714                                                        1, tagged_sets );MB_CHK_SET_ERR( result, "Trouble getting tagged sets" );
00715         if( !tagged_sets.empty() && tagged_sets != myPcomm->partition_sets() )
00716         {
00717             result = mbImpl->tag_delete_data( ptag, tagged_sets );MB_CHK_SET_ERR( result, "Trouble deleting data of PARALLEL_PARTITION tag" );
00718         }
00719         else if( tagged_sets == myPcomm->partition_sets() )
00720             return MB_SUCCESS;
00721     }
00722 
00723     // If we get here, we need to assign the tag
00724     std::vector< int > values( myPcomm->partition_sets().size() );
00725     for( unsigned int i = 0; i < myPcomm->partition_sets().size(); i++ )
00726         values[i] = proc_rk;
00727     result = mbImpl->tag_set_data( ptag, myPcomm->partition_sets(), &values[0] );MB_CHK_SET_ERR( result, "Trouble setting data to PARALLEL_PARTITION tag" );
00728 
00729     return MB_SUCCESS;
00730 }
00731 
00732 ErrorCode ReadParallel::delete_nonlocal_entities( EntityHandle file_set )
00733 {
00734     // Get partition entities and ents related to/used by those
00735     // get ents in the partition
00736     ReadUtilIface* read_iface;
00737     mbImpl->query_interface( read_iface );
00738     Range partition_ents, all_sets;
00739 
00740     myDebug.tprint( 2, "Gathering related entities.\n" );
00741 
00742     ErrorCode result = read_iface->gather_related_ents( myPcomm->partition_sets(), partition_ents, &file_set );MB_CHK_SET_ERR( result, "Failure gathering related entities" );
00743 
00744     // Get pre-existing entities
00745     Range file_ents;
00746     result = mbImpl->get_entities_by_handle( file_set, file_ents );MB_CHK_SET_ERR( result, "Couldn't get pre-existing entities" );
00747 
00748     if( 0 == myPcomm->proc_config().proc_rank() ) { myDebug.print( 2, "File entities: ", file_ents ); }
00749 
00750     // Get deletable entities by subtracting partition ents from file ents
00751     Range deletable_ents = subtract( file_ents, partition_ents );
00752 
00753     // Cache deletable vs. keepable sets
00754     Range deletable_sets = deletable_ents.subset_by_type( MBENTITYSET );
00755     Range keepable_sets  = subtract( file_ents.subset_by_type( MBENTITYSET ), deletable_sets );
00756 
00757     myDebug.tprint( 2, "Removing deletable entities from keepable sets.\n" );
00758 
00759     // Remove deletable ents from all keepable sets
00760     for( Range::iterator rit = keepable_sets.begin(); rit != keepable_sets.end(); ++rit )
00761     {
00762         result = mbImpl->remove_entities( *rit, deletable_ents );MB_CHK_SET_ERR( result, "Failure removing deletable entities" );
00763     }
00764     result = mbImpl->remove_entities( file_set, deletable_ents );MB_CHK_SET_ERR( result, "Failure removing deletable entities" );
00765 
00766     myDebug.tprint( 2, "Deleting deletable entities.\n" );
00767 
00768     if( 0 == myPcomm->proc_config().proc_rank() ) { myDebug.print( 2, "Deletable sets: ", deletable_sets ); }
00769 
00770     // Delete sets, then ents
00771     if( !deletable_sets.empty() )
00772     {
00773         result = mbImpl->delete_entities( deletable_sets );MB_CHK_SET_ERR( result, "Failure deleting sets in delete_nonlocal_entities" );
00774     }
00775 
00776     deletable_ents -= deletable_sets;
00777 
00778     if( 0 == myPcomm->proc_config().proc_rank() ) { myDebug.print( 2, "Deletable entities: ", deletable_ents ); }
00779 
00780     if( !deletable_ents.empty() )
00781     {
00782         result = mbImpl->delete_entities( deletable_ents );MB_CHK_SET_ERR( result, "Failure deleting entities in delete_nonlocal_entities" );
00783     }
00784 
00785     return MB_SUCCESS;
00786 }
00787 
00788 }  // namespace moab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines