Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
RuntimeContext Struct Reference

The RunttimeContext is an example specific class to store the run specific input data, MOAB datastructures used during the run and provides other utility functions to profile operations etc. More...

+ Collaboration diagram for RuntimeContext:

Public Member Functions

 RuntimeContext (MPI_Comm comm=MPI_COMM_WORLD)
 Constructor: allocate MOAB interface and communicator, and initialize other data members with some default values. More...
 
 ~RuntimeContext ()
 Destructor: deallocate MOAB interface and communicator. More...
 
void ParseCLOptions (int argc, char *argv[])
 Parse the runtime command line options. More...
 
void timer_push (const std::string &operation)
 Measure and start the timer to profile a task. More...
 
void timer_pop (const int nruns=1)
 Stop the timer and store the elapsed duration. More...
 
double last_elapsed () const
 Return the last elapsed time. More...
 
moab::ErrorCode load_file (bool load_ghosts=false)
 Load a MOAB supported file (h5m or nc format) from disk representing an MPAS mesh. More...
 
moab::ErrorCode create_sv_tags (moab::Tag &tagScalar, moab::Tag &tagVector, moab::Range &entities)
 Create scalar and vector tags in the MOAB mesh instance. More...
 
double evaluate_function (double lon, double lat, int type=1, double multiplier=1.0) const
 Evaluate some closed-form Spherical Harmonic functions with an optional multiplier term. More...
 

Public Attributes

int dimension { 2 }
 
std::string input_filename
 dimension of the problem More...
 
std::string output_filename
 input file name (nc format) More...
 
int ghost_layers { 3 }
 output file name (h5m format) More...
 
std::string scalar_tagname
 number of ghost layers More...
 
std::string vector_tagname
 scalar tag name More...
 
int vector_length { 3 }
 vector tag name More...
 
int num_max_exchange { 10 }
 length of the vector tag components More...
 
bool debug_output { false }
 total number of exchange iterations More...
 
int proc_id { 1 }
 write debug output information? More...
 
int num_procs { 1 }
 process identifier More...
 
double last_counter { 0.0 }
 total number of processes More...
 
moab::Core moab_interface
 last time counter between push/pop timer More...
 
moab::ParallelCommparallel_communicator
 
moab::EntityHandle fileset { 0 }
 
moab::EntityHandle partnset { 0 }
 

Private Member Functions

std::vector< double > compute_centroids (const moab::Range &entities) const
 Compute the centroids of elements in 2D lat/lon space. More...
 

Private Attributes

moab::CpuTimer mTimer
 
double mTimerOps { 0.0 }
 
std::string mOpName
 

Detailed Description

The RunttimeContext is an example specific class to store the run specific input data, MOAB datastructures used during the run and provides other utility functions to profile operations etc.

Definition at line 77 of file ExchangeHalos.cpp.

Constructor & Destructor Documentation

◆ RuntimeContext()

RuntimeContext::RuntimeContext ( MPI_Comm  comm = MPI_COMM_WORLD)
explicit

Constructor: allocate MOAB interface and communicator, and initialize other data members with some default values.

Implementation details ///.

Definition at line 331 of file ExchangeHalos.cpp.

332  : input_filename( std::string( MESH_DIR ) + std::string( "/io/mpasx1.642.t.2.nc" ) ),
333  output_filename( "exchangeHalos_output.h5m" ), scalar_tagname( "scalar_variable" ),
334  vector_tagname( "vector_variable" )
335 {
336  // Create sets for the mesh and partition. Then pass these to the load_file functions to populate the mesh.
337  runchk_cont( moab_interface.create_meshset( moab::MESHSET_SET, fileset ), "Creating root set failed" );
338  runchk_cont( moab_interface.create_meshset( moab::MESHSET_SET, partnset ), "Creating partition set failed" );
339 
340  // Create the parallel communicator object with the partition handle associated with MOAB
342 
345 }

References moab::Core::create_meshset(), fileset, moab::ParallelComm::get_pcomm(), MESHSET_SET, moab_interface, num_procs, parallel_communicator, partnset, proc_id, moab::ParallelComm::rank(), runchk_cont, and moab::ParallelComm::size().

◆ ~RuntimeContext()

RuntimeContext::~RuntimeContext ( )

Destructor: deallocate MOAB interface and communicator.

Definition at line 347 of file ExchangeHalos.cpp.

348 {
349  delete parallel_communicator;
350 }

References parallel_communicator.

Member Function Documentation

◆ compute_centroids()

std::vector< double > RuntimeContext::compute_centroids ( const moab::Range entities) const
private

Compute the centroids of elements in 2D lat/lon space.

Parameters
entitiesEntities to compute centroids
Returns
Vector of centroids (as lat/lon)

Definition at line 506 of file ExchangeHalos.cpp.

507 {
508  double node[3];
509  std::vector< double > eCentroids( entities.size() * 2 ); // [lon, lat]
510  size_t offset = 0;
511  for( auto entity : entities )
512  {
513  // Get the element coordinates (centroid) on the real mesh
514  runchk_cont( moab_interface.get_coords( &entity, 1, node ), "Getting entity coordinates failed" );
515 
516  // scale by magnitude so that element is on unit sphere
517  double magnitude = std::sqrt( node[0] * node[0] + node[1] * node[1] + node[2] * node[2] );
518  node[0] /= magnitude;
519  node[1] /= magnitude;
520  node[2] /= magnitude;
521 
522  // compute the spherical transformation onto unit sphere
523  eCentroids[offset] = atan2( node[1], node[0] );
524  if( eCentroids[offset] < 0.0 ) eCentroids[offset] += 2.0 * M_PI;
525  eCentroids[offset + 1] = asin( node[2] );
526 
527  offset += 2; // increment the offset
528  }
529  // return centroid list for elements
530  return eCentroids;
531 }

References entities, moab::Core::get_coords(), moab_interface, and runchk_cont.

Referenced by create_sv_tags().

◆ create_sv_tags()

moab::ErrorCode RuntimeContext::create_sv_tags ( moab::Tag tagScalar,
moab::Tag tagVector,
moab::Range entities 
)

Create scalar and vector tags in the MOAB mesh instance.

Parameters
tagScalarTag reference to the scalar field
tagVectorTag reference to the vector field
entitiesEntities on which both the scalar and vector fields are defined
Returns
Error code if any (else MB_SUCCESS)

Definition at line 408 of file ExchangeHalos.cpp.

409 {
410  // Get element (centroid) coordinates so that we can evaluate some arbitrary data
411  std::vector< double > entCoords = compute_centroids( entities ); // [entities * [lon, lat]]
412 
413  if( proc_id == 0 ) std::cout << "> Getting scalar tag handle " << scalar_tagname << "..." << std::endl;
414  double defSTagValue = -1.0;
415  bool createdTScalar = false;
416  // Get or create the scalar tag: default name = "scalar_variable"
417  // Type: double, Components: 1, Layout: Dense (all entities potentially), Default: -1.0
419  moab::MB_TAG_CREAT | moab::MB_TAG_DENSE, &defSTagValue, &createdTScalar ),
420  "Retrieving scalar tag handle failed" );
421 
422  // we expect to create a new tag -- fail if Tag already exists since we do not want to overwrite data
423  assert( createdTScalar );
424  // set the data for scalar tag with an analytical Spherical Harmonic function
425  {
426  std::vector< double > tagValues( entities.size(), -1.0 );
427  std::generate( tagValues.begin(), tagValues.end(), [=, &entCoords]() {
428  static int index = 0;
429  const int offset = index++ * 2;
430  return evaluate_function( entCoords[offset], entCoords[offset + 1] );
431  } );
432  // Set local scalar tag data for exchange
433  runchk( moab_interface.tag_set_data( tagScalar, entities, tagValues.data() ),
434  "Setting scalar tag data failed" );
435  }
436 
437  if( proc_id == 0 ) std::cout << "> Getting vector tag handle " << vector_tagname << "..." << std::endl;
438  std::vector< double > defVTagValue( vector_length, -1.0 );
439  bool createdTVector = false;
440  // Get or create the scalar tag: default name = "vector_variable"
441  // Type: double, Components: vector_length, Layout: Dense (all entities potentially), Default: [-1.0,..]
443  moab::MB_TAG_CREAT | moab::MB_TAG_DENSE, defVTagValue.data(),
444  &createdTVector ),
445  "Retrieving vector tag handle failed" );
446 
447  // we expect to create a new tag -- fail if Tag already exists since we do not want to overwrite data
448  assert( createdTVector );
449  // set the data for vector tag with an analytical Spherical Harmonic function
450  // with an optional scaling for each component; just to make it look different :-)
451  {
452  const int veclength = vector_length;
453  std::vector< double > tagValues( entities.size() * veclength, -1.0 );
454  std::generate( tagValues.begin(), tagValues.end(), [=, &entCoords]() {
455  static int index = 0;
456  const int offset = ( index++ / veclength ) * 2;
457  return this->evaluate_function( entCoords[offset], entCoords[offset + 1], 2, ( index % veclength + 1.0 ) );
458  } );
459  // Set local tag data for exchange
460  runchk( moab_interface.tag_set_data( tagVector, entities, tagValues.data() ),
461  "Setting vector tag data failed" );
462  }
463 
464  return moab::MB_SUCCESS;
465 }

References compute_centroids(), entities, MB_SUCCESS, MB_TAG_CREAT, MB_TAG_DENSE, MB_TYPE_DOUBLE, moab_interface, proc_id, runchk, scalar_tagname, moab::Core::tag_get_handle(), moab::Core::tag_set_data(), vector_length, and vector_tagname.

◆ evaluate_function()

double RuntimeContext::evaluate_function ( double  lon,
double  lat,
int  type = 1,
double  multiplier = 1.0 
) const
inline

Evaluate some closed-form Spherical Harmonic functions with an optional multiplier term.

Parameters
lonLongitude in lat-lon space
latLatitude in lat-lon space
typeFunction type
multiplierOptional multiplier to scale value (default=1.0)
Returns
value of the evaluated function

Definition at line 142 of file ExchangeHalos.cpp.

143  {
144  switch( type )
145  {
146  case 1:
147  return ( 2.0 + std::pow( sin( 2.0 * lat ), 16.0 ) * cos( 16.0 * lon ) ) * multiplier;
148  default:
149  return ( 2.0 + cos( lon ) * cos( lon ) * cos( 2.0 * lat ) ) * multiplier;
150  }
151  }

◆ last_elapsed()

double RuntimeContext::last_elapsed ( ) const
inline

Return the last elapsed time.

Returns
last_counter from timer_pop was called

Definition at line 403 of file ExchangeHalos.cpp.

404 {
405  return last_counter;
406 }

References last_counter.

◆ load_file()

moab::ErrorCode RuntimeContext::load_file ( bool  load_ghosts = false)

Load a MOAB supported file (h5m or nc format) from disk representing an MPAS mesh.

Parameters
load_ghostsOptional boolean to specify whether to load ghosts when reading the file (only relevant for h5m)
Returns
Error code if any (else MB_SUCCESS)

Parallel Read options: PARALLEL = type {READ_PART} : Read on all tasks PARTITION_METHOD = RCBZOLTAN : Use Zoltan partitioner to compute an online partition and redistribute on the fly PARTITION = PARALLEL_PARTITION : Partition as you read based on part information stored in h5m file PARALLEL_RESOLVE_SHARED_ENTS : Communicate to all processors to get the shared adjacencies consistently in parallel PARALLEL_GHOSTS : a.b.c : a = 2 - highest dimension of entities (2D in this case) : b = 1 - dimension of entities to calculate adjacencies (vertex=0, edges=1) : c = 3 - number of ghost layers needed (3 in this case)

Definition at line 467 of file ExchangeHalos.cpp.

468 {
469  /// Parallel Read options:
470  /// PARALLEL = type {READ_PART} : Read on all tasks
471  /// PARTITION_METHOD = RCBZOLTAN : Use Zoltan partitioner to compute an online partition and redistribute on the
472  /// fly PARTITION = PARALLEL_PARTITION : Partition as you read based on part information stored in h5m file
473  /// PARALLEL_RESOLVE_SHARED_ENTS : Communicate to all processors to get the shared adjacencies
474  /// consistently in parallel
475  /// PARALLEL_GHOSTS : a.b.c
476  /// : a = 2 - highest dimension of entities (2D in this case)
477  /// : b = 1 - dimension of entities to calculate adjacencies (vertex=0, edges=1)
478  /// : c = 3 - number of ghost layers needed (3 in this case)
479  std::string read_options = "DEBUG_IO=0;";
480  std::string::size_type idx = input_filename.rfind( '.' );
481  if( num_procs > 1 && idx != std::string::npos )
482  {
483  std::string extension = input_filename.substr( idx + 1 );
484  if( !extension.compare( "nc" ) )
485  // PARTITION_METHOD= [RCBZOLTAN, TRIVIAL]
486  read_options += "PARALLEL=READ_PART;PARTITION_METHOD=RCBZOLTAN;"
487  "PARALLEL_RESOLVE_SHARED_ENTS;NO_EDGES;NO_MIXED_ELEMENTS;VARIABLE=;";
488  else if( !extension.compare( "h5m" ) )
489  read_options +=
490  "PARALLEL=READ_PART;PARTITION=PARALLEL_PARTITION;"
491  "PARALLEL_RESOLVE_SHARED_ENTS;" +
492  ( load_ghosts ? "PARALLEL_THIN_GHOST_LAYER;PARALLEL_GHOSTS=2.1." + std::to_string( ghost_layers ) + ";"
493  : "" );
494  else
495  {
496  std::cout << "Error unsupported file type (only h5m and nc) for this example: " << input_filename
497  << std::endl;
499  }
500  }
501 
502  // Load the file from disk with given read options in parallel and associate all entities to fileset
503  return moab_interface.load_file( input_filename.c_str(), &fileset, read_options.c_str() );
504 }

References fileset, ghost_layers, input_filename, moab::Core::load_file(), MB_UNSUPPORTED_OPERATION, moab_interface, and num_procs.

◆ ParseCLOptions()

void RuntimeContext::ParseCLOptions ( int  argc,
char *  argv[] 
)

Parse the runtime command line options.

Parameters
argc- number of command line arguments
argv- command line arguments as string list

Definition at line 352 of file ExchangeHalos.cpp.

353 {
354  ProgOptions opts;
355  // Input mesh
356  opts.addOpt< std::string >( "input", "Input mesh filename to load in parallel. Default=data/default_mesh_holes.h5m",
357  &input_filename );
358  // Output mesh
359  opts.addOpt< void >( "debug", "Should we write output file? Default=false", &debug_output );
360  opts.addOpt< std::string >( "output",
361  "Output mesh filename for verification (use --debug). Default=exchangeHalos_output.h5m",
362  &output_filename );
363  // Dimension of the input mesh
364  // Vector tag length
365  opts.addOpt< int >( "vtaglength", "Size of vector components per each entity. Default=3", &vector_length );
366  // Number of halo (ghost) regions
367  opts.addOpt< int >( "nghosts", "Number of ghost layers (halos) to exchange. Default=3", &ghost_layers );
368  // Number of times to perform the halo exchange for timing
369  opts.addOpt< int >( "nexchanges", "Number of ghost-halo exchange iterations to perform. Default=10",
370  &num_max_exchange );
371 
372  opts.parseCommandLine( argc, argv );
373 }

References ProgOptions::addOpt(), debug_output, ghost_layers, input_filename, num_max_exchange, output_filename, ProgOptions::parseCommandLine(), and vector_length.

◆ timer_pop()

void RuntimeContext::timer_pop ( const int  nruns = 1)

Stop the timer and store the elapsed duration.

Parameters
nrunsOptional argument used to average the measured time

Definition at line 381 of file ExchangeHalos.cpp.

382 {
383  double locElapsed = mTimer.time_since_birth() - mTimerOps;
384  double avgElapsed = 0;
385  double maxElapsed = 0;
386  MPI_Reduce( &locElapsed, &maxElapsed, 1, MPI_DOUBLE, MPI_MAX, 0, parallel_communicator->comm() );
387  MPI_Reduce( &locElapsed, &avgElapsed, 1, MPI_DOUBLE, MPI_SUM, 0, parallel_communicator->comm() );
388  if( proc_id == 0 )
389  {
390  avgElapsed /= num_procs;
391  if( nruns > 1 )
392  std::cout << "[LOG] Time taken to " << mOpName.c_str() << ", averaged over " << nruns
393  << " runs : max = " << maxElapsed / nruns << ", avg = " << avgElapsed / nruns << "\n";
394  else
395  std::cout << "[LOG] Time taken to " << mOpName.c_str() << " : max = " << maxElapsed
396  << ", avg = " << avgElapsed << "\n";
397 
398  last_counter = maxElapsed / nruns;
399  }
400  mOpName.clear();
401 }

References moab::ParallelComm::comm(), last_counter, mOpName, mTimer, mTimerOps, num_procs, parallel_communicator, proc_id, and moab::CpuTimer::time_since_birth().

◆ timer_push()

void RuntimeContext::timer_push ( const std::string &  operation)
inline

Measure and start the timer to profile a task.

Parameters
operationString name of the task being measured

Definition at line 375 of file ExchangeHalos.cpp.

376 {
378  mOpName = operation;
379 }

References mOpName, mTimer, mTimerOps, and moab::CpuTimer::time_since_birth().

Member Data Documentation

◆ debug_output

bool RuntimeContext::debug_output { false }

total number of exchange iterations

Definition at line 88 of file ExchangeHalos.cpp.

Referenced by ParseCLOptions().

◆ dimension

int RuntimeContext::dimension { 2 }

Definition at line 80 of file ExchangeHalos.cpp.

◆ fileset

moab::EntityHandle RuntimeContext::fileset { 0 }

Definition at line 96 of file ExchangeHalos.cpp.

Referenced by load_file(), and RuntimeContext().

◆ ghost_layers

int RuntimeContext::ghost_layers { 3 }

output file name (h5m format)

Definition at line 83 of file ExchangeHalos.cpp.

Referenced by load_file(), and ParseCLOptions().

◆ input_filename

std::string RuntimeContext::input_filename

dimension of the problem

Definition at line 81 of file ExchangeHalos.cpp.

Referenced by load_file(), and ParseCLOptions().

◆ last_counter

double RuntimeContext::last_counter { 0.0 }

total number of processes

Definition at line 91 of file ExchangeHalos.cpp.

Referenced by last_elapsed(), and timer_pop().

◆ moab_interface

moab::Core RuntimeContext::moab_interface

last time counter between push/pop timer

Definition at line 94 of file ExchangeHalos.cpp.

Referenced by compute_centroids(), create_sv_tags(), load_file(), and RuntimeContext().

◆ mOpName

std::string RuntimeContext::mOpName
private

Definition at line 161 of file ExchangeHalos.cpp.

Referenced by timer_pop(), and timer_push().

◆ mTimer

moab::CpuTimer RuntimeContext::mTimer
private

Definition at line 159 of file ExchangeHalos.cpp.

Referenced by timer_pop(), and timer_push().

◆ mTimerOps

double RuntimeContext::mTimerOps { 0.0 }
private

Definition at line 160 of file ExchangeHalos.cpp.

Referenced by timer_pop(), and timer_push().

◆ num_max_exchange

int RuntimeContext::num_max_exchange { 10 }

length of the vector tag components

Definition at line 87 of file ExchangeHalos.cpp.

Referenced by ParseCLOptions().

◆ num_procs

int RuntimeContext::num_procs { 1 }

process identifier

Definition at line 90 of file ExchangeHalos.cpp.

Referenced by load_file(), RuntimeContext(), and timer_pop().

◆ output_filename

std::string RuntimeContext::output_filename

input file name (nc format)

Definition at line 82 of file ExchangeHalos.cpp.

Referenced by ParseCLOptions().

◆ parallel_communicator

moab::ParallelComm* RuntimeContext::parallel_communicator

Definition at line 95 of file ExchangeHalos.cpp.

Referenced by RuntimeContext(), timer_pop(), and ~RuntimeContext().

◆ partnset

moab::EntityHandle RuntimeContext::partnset { 0 }

Definition at line 96 of file ExchangeHalos.cpp.

Referenced by RuntimeContext().

◆ proc_id

int RuntimeContext::proc_id { 1 }

write debug output information?

Definition at line 89 of file ExchangeHalos.cpp.

Referenced by create_sv_tags(), RuntimeContext(), and timer_pop().

◆ scalar_tagname

std::string RuntimeContext::scalar_tagname

number of ghost layers

Definition at line 84 of file ExchangeHalos.cpp.

Referenced by create_sv_tags().

◆ vector_length

int RuntimeContext::vector_length { 3 }

vector tag name

Definition at line 86 of file ExchangeHalos.cpp.

Referenced by create_sv_tags(), and ParseCLOptions().

◆ vector_tagname

std::string RuntimeContext::vector_tagname

scalar tag name

Definition at line 85 of file ExchangeHalos.cpp.

Referenced by create_sv_tags().


The documentation for this struct was generated from the following file: