Skip to content
Snippets Groups Projects
helloBPWriter_nompi.cpp 2.87 KiB
Newer Older
wfg's avatar
wfg committed
/*
 * helloADIOSNoXML_OOP.cpp
 *
 *  Created on: Jan 9, 2017
 *      Author: wfg
 */

#include <vector>
#include <iostream>

#include "ADIOS_CPP.h"


int main( int argc, char* argv [] )
wfg's avatar
wfg committed
    const bool adiosDebug = true;
    adios::ADIOS adios( adiosDebug );

    //Application variable
    std::vector<double> myDoubles = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    const std::size_t Nx = myDoubles.size();

    const std::size_t rows = 3;
    const std::size_t columns = 3;
    std::vector<float> myMatrix = { 1, 2, 3,
                                    4, 5, 6,
                                    7, 8, 9 };

    std::vector<float> myMatrix2 = { -1, -2, -3,
                                     -4, -5, -6,
                                     -7, -8, -9 };
wfg's avatar
wfg committed
    try
    {
        //Define variable and local size
        adios::Variable<double>& ioMyDoubles = adios.DefineVariable<double>( "myDoubles", adios::Dims{Nx} );
        adios::Variable<float>& ioMyMatrix = adios.DefineVariable<float>( "myMatrix", adios::Dims{rows,columns} );
        adios::Variable<float>& ioMyMatrix2 = adios.DefineVariable<float>( "myMatrix2", adios::Dims{rows,columns} );
wfg's avatar
wfg committed
        adios::Variable<float>& ioMyMatrix3 = adios.DefineVariable<float>( "myMatrix3", adios::Dims{rows,columns} );
wfg's avatar
wfg committed

        //Define method for engine creation, it is basically straight-forward parameters
        adios::Method& bpWriterSettings = adios.DeclareMethod( "SinglePOSIXFile" ); //default method type is Writer
wfg's avatar
wfg committed
        bpWriterSettings.SetParameters( "profile_units=mus" );
        bpWriterSettings.AddTransport( "File", "have_metadata_file=yes", "profile_units=mus" );
wfg's avatar
wfg committed

        //Create engine smart pointer due to polymorphism,
        //Open returns a smart pointer to Engine containing the Derived class Writer
        auto bpFileWriter = adios.Open( "myDoubles_nompi.bp", "w", bpWriterSettings, adios::IOMode::COLLECTIVE );
wfg's avatar
wfg committed

wfg's avatar
wfg committed
        if( bpFileWriter == nullptr )
wfg's avatar
wfg committed
            throw std::ios_base::failure( "ERROR: couldn't create bpWriter at Open\n" );

wfg's avatar
wfg committed
        bpFileWriter->Write<double>( ioMyDoubles, myDoubles.data() ); // Base class Engine own the Write<T> that will call overloaded Write from Derived
        bpFileWriter->Write<float>( ioMyMatrix, myMatrix.data() ); //2d Example
        bpFileWriter->Write<float>( ioMyMatrix2, myMatrix2.data() ); //2d Example
        bpFileWriter->Write<float>( ioMyMatrix3, myMatrix2.data() ); //2d Example
        bpFileWriter->Close( );
wfg's avatar
wfg committed
    }
    catch( std::invalid_argument& e )
    {
        std::cout << "Invalid argument exception, STOPPING PROGRAM\n";
        std::cout << e.what() << "\n";
    }
    catch( std::ios_base::failure& e )
    {
        std::cout << "System exception, STOPPING PROGRAM\n";
        std::cout << e.what() << "\n";
    }
    catch( std::exception& e )
    {
        std::cout << "Exception, STOPPING PROGRAM\n";
        std::cout << e.what() << "\n";
    }

    return 0;
}