diff --git a/examples/groupless/multistep/Makefile b/examples/groupless/multistep/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..1498091ae7097fdb382fd07a35bcd46d996700ef --- /dev/null +++ b/examples/groupless/multistep/Makefile @@ -0,0 +1,26 @@ +# Makefile for testing purposes, will build writer_mpi (make or make mpi) or writer_nompi (make nompi) +# Created on: Feb 13, 2017 +# Author: pnorbert + +#COMPILERS +CC=g++ +MPICC=mpic++ + +#ADIOS LOCATION +ADIOS_DIR=../../.. +ADIOS_INCLUDE=-I$(ADIOS_DIR)/include +ADIOS_LIB=$(ADIOS_DIR)/lib/libadios.a +ADIOS_NOMPI_LIB=$(ADIOS_DIR)/lib/libadios_nompi.a + +#FLAGS +CFLAGS=-Wall -Wpedantic -Woverloaded-virtual -std=c++11 -O0 -g +LDFLAGS= + +all: writer_multistep reader_allsteps reader_stepping + +writer_multistep reader_allsteps reader_stepping: $(ADIOS_LIB) $(ADIOS_HFiles) + $(MPICC) $(CFLAGS) $(ADIOS_INCLUDE) -DHAVE_MPI $@.cpp -o $@ $(ADIOS_LIB) $(LDFLAGS) -lpthread \ + +clean: + rm -f writer_multistep reader_allsteps reader_stepping + diff --git a/examples/groupless/multistep/reader_allsteps.cpp b/examples/groupless/multistep/reader_allsteps.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50438c0ae23aa4bce9e77dcc584c1f3a254a0971 --- /dev/null +++ b/examples/groupless/multistep/reader_allsteps.cpp @@ -0,0 +1,175 @@ +/* + * reader.cpp + * + * Created on: Feb 13, 2017 + * Author: pnorbert + */ + +#include <vector> +#include <iostream> + +#include <mpi.h> +#include "ADIOS_CPP.h" + + +int main( int argc, char* argv [] ) +{ + int rank, nproc; + MPI_Init( &argc, &argv ); + MPI_Comm_rank( MPI_COMM_WORLD, &rank ); + MPI_Comm_size( MPI_COMM_WORLD, &nproc); + const bool adiosDebug = true; + + adios::ADIOS adios( MPI_COMM_WORLD, adiosDebug ); + + //Application variable + std::vector<double> NiceArray; + std::vector<float> RaggedArray; + unsigned int Nx; + int Nparts; + int Nwriters; + int Nsteps; + + try + { + //Define method for engine creation + // 1. Get method def from config file or define new one + adios::Method& bpReaderSettings = adios.GetMethod( "input" ); + if( bpReaderSettings.undeclared() ) + { + // if not defined by user, we can change the default settings + bpReaderSettings.SetEngine( "BP" ); // BP is the default engine + // By default we see all steps available in a file, so the next line is not needed + bpReaderSettings.SetParameters ("Stepping", false); + } + + //Create engine smart pointer due to polymorphism, + // Default behavior + // auto bpReader = adios.Open( "myNumbers.bp", "r" ); + // this would just open with a default transport, which is "BP" + auto bpReader = adios.Open( "myNumbers.bp", "r", bpReaderSettings ); + + if( bpReader == nullptr ) + throw std::ios_base::failure( "ERROR: failed to open ADIOS bpReader\n" ); + + /* NX */ + bpReader->Read<unsigned int>( "NX", &Nx ); // read a Global scalar which has a single value in a step + + /* nproc */ + bpReader->Read<int>( "nproc", &Nwriters ); // also a global scalar + + + /* Nparts */ + // Nparts local scalar is presented as a 1D array of Nwriters elements. + // We need to read a specific value the same way as reading from any 1D array. + // Make a single-value selection to describe our rank's position in the + // 1D array of Nwriters values. + if( rank < Nwriters ) + { + std::unique_ptr<adios::Selection> selNparts = adios.SelectionBoundingBox( {1}, {rank} ); + bpReader->Read<int>( "Nparts", selNparts, &Nparts ); + } + + + /* Nice */ + // inquiry about a variable, whose name we know + std::shared_ptr<adios::Variable<void> > varNice = bpReader.InquiryVariable("Nice"); + + if( varNice == nullptr ) + throw std::ios_base::failure( "ERROR: failed to find variable 'myDoubles' in input file\n" ); + + // ? how do we know about the type? std::string varNice->m_Type + unsigned long long int gdim = varMyDoubles->m_GlobalDimensions[0]; // ?member var or member func? + unsigned long long int ldim = gdim / nproc; + unsigned long long int offs = rank * ldim; + if( rank == nproc-1 ) + { + ldim = gdim - (ldim * gdim); + } + + NiceArray.reserve(ldim); + + // Make a 1D selection to describe the local dimensions of the variable we READ and + // its offsets in the global spaces + std::unique_ptr<adios::Selection> bbsel = adios.SelectionBoundingBox( {ldim}, {offs} ); // local dims and offsets; both as list + bpReader->Read<double>( "Nice", bbsel, NiceArray.data() ); // Base class Engine own the Read<T> that will call overloaded Read from Derived + + + + /* Ragged */ + // inquiry about a variable, whose name we know + std::shared_ptr<adios::Variable<void> > varRagged = bpReader.InquiryVariable("Ragged"); + if( varRagged->m_GlobalDimensions[1] != adios::VARYING_DIMENSION) + { + throw std::ios_base::failure( "Unexpected condition: Ragged array's fast dimension " + "is supposed to be VARYING_DIMENSION\n" ); + } + // We have here varRagged->sum_nblocks, nsteps, nblocks[], global + if( rank < varRagged->nblocks[0] ) // same as rank < Nwriters in this example + { + // get per-writer size information + varRagged->InquiryBlocks(); + // now we have the dimensions per block + + unsigned long long int ldim = varRagged->blockinfo[rank].m_Dimensions[0]; + RaggedArray.resize( ldim ); + + std::unique_ptr<adios::Selection> wbsel = adios.SelectionWriteblock( rank ); + bpReader->Read<float>( "Ragged", wbsel, RaggedArray.data() ); + + // We can use bounding box selection as well + std::unique_ptr<adios::Selection> rbbsel = adios.SelectionBoundingBox( {1,ldim}, {rank,0} ); + bpReader->Read<float>( "Ragged", rbbsel, RaggedArray.data() ); + } + + /* Extra help to process Ragged */ + int maxRaggedDim = varRagged->GetMaxGlobalDimensions(1); // contains the largest + vector<int> raggedDims = varRagged->GetVaryingGlobalDimensions(1); // contains all individual sizes in that dimension + + + + // promise to not read more from this step + bpReader->Release(); + + // want to move on to the next available step + //bpReader->Advance(adios::NextImmediateStep); + //bpReader->Advance(adios::NextAvailableStep); + bpReader->Advance(); // default is adios::NextAvailableStep + + // Close file/stream + bpReader->Close( ); + + } + catch( std::invalid_argument& e ) + { + if( rank == 0 ) + { + std::cout << "Invalid argument exception, STOPPING PROGRAM\n"; + std::cout << e.what() << "\n"; + } + } + catch( std::ios_base::failure& e ) + { + if( rank == 0 ) + { + std::cout << "System exception, STOPPING PROGRAM\n"; + std::cout << e.what() << "\n"; + } + } + catch( std::exception& e ) + { + if( rank == 0 ) + { + std::cout << "Exception, STOPPING PROGRAM\n"; + std::cout << e.what() << "\n"; + } + } + + MPI_Finalize( ); + + return 0; + +} + + + diff --git a/examples/groupless/multistep/reader_stepping.cpp b/examples/groupless/multistep/reader_stepping.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c049ebef489c6389c3072652e434e09ebc3d4099 --- /dev/null +++ b/examples/groupless/multistep/reader_stepping.cpp @@ -0,0 +1,185 @@ +/* + * reader.cpp + * + * Created on: Feb 13, 2017 + * Author: pnorbert + */ + +#include <vector> +#include <iostream> + +#include <mpi.h> +#include "ADIOS_CPP.h" + + +int main( int argc, char* argv [] ) +{ + int rank, nproc; + MPI_Init( &argc, &argv ); + MPI_Comm_rank( MPI_COMM_WORLD, &rank ); + MPI_Comm_size( MPI_COMM_WORLD, &nproc); + const bool adiosDebug = true; + + adios::ADIOS adios( MPI_COMM_WORLD, adiosDebug ); + + //Application variable + std::vector<double> NiceArray; + std::vector<float> RaggedArray; + unsigned int Nx; + int Nparts; + int Nwriters; + int Nsteps; + + try + { + //Define method for engine creation + // 1. Get method def from config file or define new one + adios::Method& bpReaderSettings = adios.GetMethod( "input" ); + if( bpReaderSettings.undeclared() ) + { + // if not defined by user, we can change the default settings + bpReaderSettings.SetEngine( "BP" ); // BP is the default engine + bpReaderSettings.SetParameters ("Stepping", true); // see only one step at a time + } + + //Create engine smart pointer due to polymorphism, + // Default behavior + // auto bpReader = adios.Open( "myNumbers.bp", "r" ); + // this would just open with a default transport, which is "BP" + try + { + auto bpReader = adios.Open( "myNumbers.bp", "r", bpReaderSettings ); + + while (true) + { + /* NX */ + bpReader->Read<unsigned int>( "NX", &Nx ); // read a Global scalar which has a single value in a step + + /* nproc */ + bpReader->Read<int>( "nproc", &Nwriters ); // also a global scalar + + + /* Nparts */ + // Nparts local scalar is presented as a 1D array of Nwriters elements. + // We need to read a specific value the same way as reading from any 1D array. + // Make a single-value selection to describe our rank's position in the + // 1D array of Nwriters values. + if( rank < Nwriters ) + { + std::unique_ptr<adios::Selection> selNparts = adios.SelectionBoundingBox( {1}, {rank} ); + bpReader->Read<int>( "Nparts", selNparts, &Nparts ); + } + + + /* Nice */ + // inquiry about a variable, whose name we know + std::shared_ptr<adios::Variable<void> > varNice = bpReader.InquiryVariable("Nice"); + + if( varNice == nullptr ) + throw std::ios_base::failure( "ERROR: failed to find variable 'myDoubles' in input file\n" ); + + // ? how do we know about the type? std::string varNice->m_Type + unsigned long long int gdim = varMyDoubles->m_GlobalDimensions[0]; // ?member var or member func? + unsigned long long int ldim = gdim / nproc; + unsigned long long int offs = rank * ldim; + if( rank == nproc-1 ) + { + ldim = gdim - (ldim * gdim); + } + + NiceArray.reserve(ldim); + + // Make a 1D selection to describe the local dimensions of the variable we READ and + // its offsets in the global spaces + std::unique_ptr<adios::Selection> bbsel = adios.SelectionBoundingBox( {ldim}, {offs} ); // local dims and offsets; both as list + bpReader->Read<double>( "Nice", bbsel, NiceArray.data() ); // Base class Engine own the Read<T> that will call overloaded Read from Derived + + + + /* Ragged */ + // inquiry about a variable, whose name we know + std::shared_ptr<adios::Variable<void> > varRagged = bpReader.InquiryVariable("Ragged"); + if( varRagged->m_GlobalDimensions[1] != adios::VARYING_DIMENSION) + { + throw std::ios_base::failure( "Unexpected condition: Ragged array's fast dimension " + "is supposed to be VARYING_DIMENSION\n" ); + } + // We have here varRagged->sum_nblocks, nsteps, nblocks[], global + if( rank < varRagged->nblocks[0] ) // same as rank < Nwriters in this example + { + // get per-writer size information + varRagged->InquiryBlocks(); + // now we have the dimensions per block + + unsigned long long int ldim = varRagged->blockinfo[rank].m_Dimensions[0]; + RaggedArray.resize( ldim ); + + std::unique_ptr<adios::Selection> wbsel = adios.SelectionWriteblock( rank ); + bpReader->Read<float>( "Ragged", wbsel, RaggedArray.data() ); + + // We can use bounding box selection as well + std::unique_ptr<adios::Selection> rbbsel = adios.SelectionBoundingBox( {1,ldim}, {rank,0} ); + bpReader->Read<float>( "Ragged", rbbsel, RaggedArray.data() ); + } + + /* Extra help to process Ragged */ + int maxRaggedDim = varRagged->GetMaxGlobalDimensions(1); // contains the largest + vector<int> raggedDims = varRagged->GetVaryingGlobalDimensions(1); // contains all individual sizes in that dimension + + + + // promise to not read more from this step + bpReader->Release(); + + // want to move on to the next available step + //bpReader->Advance(adios::NextImmediateStep); + //bpReader->Advance(adios::NextAvailableStep); + bpReader->Advance(); // default is adios::NextAvailableStep + + } + + // Close file/stream + bpReader->Close( ); + + } + catch( adios::end_of_stream& e ) + { + } + catch( adios::file_not_found& e ) + { + + } + } + catch( std::invalid_argument& e ) + { + if( rank == 0 ) + { + std::cout << "Invalid argument exception, STOPPING PROGRAM\n"; + std::cout << e.what() << "\n"; + } + } + catch( std::ios_base::failure& e ) + { + if( rank == 0 ) + { + std::cout << "System exception, STOPPING PROGRAM\n"; + std::cout << e.what() << "\n"; + } + } + catch( std::exception& e ) + { + if( rank == 0 ) + { + std::cout << "Exception, STOPPING PROGRAM\n"; + std::cout << e.what() << "\n"; + } + } + + MPI_Finalize( ); + + return 0; + +} + + + diff --git a/examples/groupless/multistep/writer_multistep.cpp b/examples/groupless/multistep/writer_multistep.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89eba61d40152d2f3cd088373fcab5472792506e --- /dev/null +++ b/examples/groupless/multistep/writer_multistep.cpp @@ -0,0 +1,150 @@ +/* + * writer.cpp + * + * Created on: Feb 13, 2017 + * Author: pnorbert + */ + +#include <vector> +#include <iostream> + +#include <mpi.h> +#include "ADIOS_CPP.h" + +namespace adios { + typedef enum { + VARYING_DIMENSION = -1, + LOCAL_VALUE = 0, + GLOBAL_VALUE = 1 + }; +} + +int main( int argc, char* argv [] ) +{ + int rank, nproc; + MPI_Init( &argc, &argv ); + MPI_Comm_rank( MPI_COMM_WORLD, &rank); + MPI_Comm_size( MPI_COMM_WORLD, &nproc); + const bool adiosDebug = true; + const int NSTEPS = 5; + + adios::ADIOS adios( MPI_COMM_WORLD, adiosDebug ); + + //Application variable + const unsigned int Nx = 10; + int Nparts; // random size per process, 5..10 each + + std::vector<double> NiceArray( Nx ); + for( int i=0; i < Nx; i++ ) + { + NiceArray[i] = rank*Nx + (double)i; + } + + std::vector<float> RaggedArray; + + try + { + //Define group and variables with transforms, variables don't have functions, only group can access variables + adios::Variable<unsigned int>& varNX = adios.DefineVariable<unsigned int>( "NX" ); // global single-value across processes + adios::Variable<int>& varNproc = adios.DefineVariable<int>( "nproc", adios::GLOBAL_VALUE ); // same def for global value + adios::Variable<int>& varNparts = adios.DefineVariable<int>( "Nparts", adios::LOCAL_VALUE ); // a single-value different on every process + adios::Variable<double>& varNice = adios.DefineVariable<double>( "Nice", {nproc*Nx} ); // 1D global array + adios::Variable<float>& varRagged = adios.DefineVariable<float>( "Ragged", {nproc,adios::VARYING_DIMENSION} ); // ragged array + + //add transform to variable in group...not executed (just testing API) + adios::Transform bzip2 = adios::transform::BZIP2( ); + varNice->AddTransform( bzip2, 1 ); + + //Define method for engine creation + // 1. Get method def from config file or define new one + adios::Method& bpWriterSettings = adios.GetMethod( "output" ); + if( bpWriterSettings.undeclared() ) + { + // if not defined by user, we can change the default settings + bpWriterSettings.SetEngine( "BP" ); // BP is the default engine + bpWriterSettings.AddTransport( "File", "lucky=yes" ); // ISO-POSIX file is the default transport + // Passing parameters to the transport + bpWriterSettings.SetParameters("have_metadata_file","yes" ); // Passing parameters to the engine + bpWriterSettings.SetParameters( "Aggregation", (nproc+1)/2 ); // number of aggregators + } + + //Open returns a smart pointer to Engine containing the Derived class Writer + // "w" means we overwrite any existing file on disk, but AdvanceStep will append steps later. + auto bpWriter = adios.Open( "myNumbers.bp", "w", bpWriterSettings ); + + if( bpWriter == nullptr ) + throw std::ios_base::failure( "ERROR: failed to open ADIOS bpWriter\n" ); + + for ( int step; step < NSTEPS; step++ ) + { + int Nparts = rand()%6 + 5; // random size per process, 5..10 each + RaggedArray.reserve(Nparts); + for( int i=0; i < Nparts; i++ ) + { + RaggedArray[i] = rank*Nx + (float)i; + } + + if( rank == 0 ) + { + // Writing a global scalar from only one process + bpWriter->Write<unsigned int>( varNX, &Nx ); + } + // Writing a local scalar on every process. Will be shown at reading as a 1D array + bpWriter->Write<int>( varNparts, &Nparts ); + + // Writing a global scalar on every process is useless. Information will be thrown away + // and only rank 0's data will be in the output + bpWriter->Write<int>( varNproc, &nproc ); + + // Make a 1D selection to describe the local dimensions of the variable we write and + // its offsets in the global spaces + adios::Selection& sel = adios.SelectionBoundingBox( {Nx}, {rank*Nx} ); // local dims and offsets; both as list + NiceArray.SetSelection( sel ); + bpWriter->Write<double>( varNice, NiceArray.data() ); // Base class Engine own the Write<T> that will call overloaded Write from Derived + + adios::Selection& lsel = adios.SelectionBoundingBox( {1,Nparts}, {rank,0} ); + RaggedArray.SetSelection( sel ); + bpWriter->Write<float>( varRagged, RaggedArray.data() ); // Base class Engine own the Write<T> that will call overloaded Write from Derived + + // Indicate we are done for this step + // N-to-M Aggregation, disk I/O will be performed during this call, unless + // time aggregation postpones all of that to some later step + bpWriter->Advance( ); + } + + // Called once: indicate that we are done with this output for the run + bpWriter->Close( ); + } + catch( std::invalid_argument& e ) + { + if( rank == 0 ) + { + std::cout << "Invalid argument exception, STOPPING PROGRAM\n"; + std::cout << e.what() << "\n"; + } + } + catch( std::ios_base::failure& e ) + { + if( rank == 0 ) + { + std::cout << "System exception, STOPPING PROGRAM\n"; + std::cout << e.what() << "\n"; + } + } + catch( std::exception& e ) + { + if( rank == 0 ) + { + std::cout << "Exception, STOPPING PROGRAM\n"; + std::cout << e.what() << "\n"; + } + } + + MPI_Finalize( ); + + return 0; + +} + + +