Skip to content
Snippets Groups Projects
Commit bd09ed69 authored by Podhorszki, Norbert's avatar Podhorszki, Norbert
Browse files

added write/read example where we have no groups

parent 496bd313
No related branches found
No related tags found
1 merge request!8Integrate groupless
# 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 reader
writer reader: $(ADIOS_LIB) $(ADIOS_HFiles)
$(MPICC) $(CFLAGS) $(ADIOS_INCLUDE) -DHAVE_MPI $@.cpp -o $@ $(ADIOS_LIB) $(LDFLAGS) -lpthread \
clean:
rm writer reader
/*
* reader.cpp
*
* Created on: Feb 13, 2017
* Author: pnorbert
*/
#include <vector>
#include <iostream>
#include <mpi.h>
#include "ADIOS_OOP.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> myDoubles;
std::vector<float> myFloats;
const unsigned int Nx;
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
}
//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" );
bpReader->Read<unsigned int>( "Nx", &Nx );
// inquiry about a variable, whose name we know
std::shared_ptr<adios::Variable<void> > varMyDoubles = bpReader.InquiryVariable("myDoubles");
if( varMyDoubles == 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 varMyDoubles->m_Type
unsigned long long int gdim = varMyDoubles->m_GlobalDimensions[0];
unsigned long long int ldim = gdim / nproc;
unsigned long long int offs = rank * ldim;
if (rank == nproc-1)
{
ldim = gdim - (ldim * gdim);
}
myDoubles.resize(ldim);
// Make a 1D selection to describe the local dimensions of the variable we READ and
// its offsets in the global spaces
adios::Selection& sel = adios.SelectionBoundingBox( {ldim}, {offs} ); // local dims and offsets; both as list
bpReader->Read<double>( "myDoubles", myDoubles.data() ); // Base class Engine own the Read<T> that will call overloaded Read from Derived
// inquiry about a variable, whose name we know
std::shared_ptr<adios::Variable<void> > varMyFloats = bpReader.InquiryVariable("myFloats");
// We have here varMyFloats->sum_nblocks, nsteps, nblocks[], global
if (rank < varMyFloats->nblocks[0])
{
// get per-writer size information
varMyFloats->InquiryBlocks();
// now we have the dimensions per block
unsigned long long int ldim = varMyFloats->blockinfo[rank].m_Dimensions[0];
myFloats.resize( ldim );
adios::Selection& sel = adios.SelectionWriteblock( rank );
bpReader->Read<double>( "myDoubles", myDoubles.data() );
}
// promise to not read more from this step
bpReader->ReleaseStep();
// want to move on to the next available step
//bpReader->AdvanceStep(adios::NextImmediateStep);
//bpReader->AdvanceStep(adios::NextAvailableStep);
bpReader->AdvanceStep(); // 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;
}
/*
* writer.cpp
*
* Created on: Feb 13, 2017
* Author: pnorbert
*/
#include <vector>
#include <iostream>
#include <mpi.h>
#include "ADIOS_OOP.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> myDoubles = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::vector<float> myFloats = { 0, -1, -2, -3, -4, -5, -6, -7, -8, -9 };
const unsigned int Nx = static_cast<unsigned int>( myDoubles.size() );
try
{
//Define group and variables with transforms, variables don't have functions, only group can access variables
adios::Variable<int>& varNX = adios.DefineVariable<unsigned int>( "NX" ); // global scalar value
adios::Variable<int>& varRank = adios.DefineVariable<int>( "rank" ); // scalar value different on every process
adios::Variable<double>& varMyDoubles = adios.DefineVariable<double>( "D", {nproc*Nx} ); // 1D global array
adios::Variable<float>& varMyFloats = adios.DefineVariable<float>( "F" ); // local array
//add transform to variable in group...not executed (just testing API)
adios::Transform bzip2 = adios::transform::BZIP2( );
varMyDoubles.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.SetAggregation( (nproc+1)/2 ); // number of aggregators
bpWriterSettings.AddTransport( "BP", "have_metadata_file=yes" ); // BP is the default transport
}
//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" );
if (rank == 0)
{
bpWriter->Write<unsigned int>( varNX, &Nx );
}
bpWriter->Write<int>( varRank, &rank );
// 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
varMyDoubles.SetSelection( sel );
bpWriter->Write<double>( varMyDoubles, myDoubles.data() ); // Base class Engine own the Write<T> that will call overloaded Write from Derived
adios::Selection& lsel = adios.SelectionBoundingBox( {Nx} ); // 0 offsets assumed
bpWriter->Write<float>( varMyFloats, myFloats.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->AdvanceStep( );
// 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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment