Skip to content
Snippets Groups Projects
helloBPWriter_nompi.cpp 2.54 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

        //Define method for engine creation, it is basically straight-forward parameters
        adios::Method& bpWriterSettings = adios.DeclareMethod( "SinglePOSIXFile" ); //default method type is Writer
        bpWriterSettings.AddTransport( "File", "have_metadata_file=yes" );
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 bpWriter = adios.Open( "myDoubles_nompi.bp", "w", bpWriterSettings );

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

        bpWriter->Write<double>( ioMyDoubles, myDoubles.data() ); // Base class Engine own the Write<T> that will call overloaded Write from Derived
        bpWriter->Write<float>( ioMyMatrix, myMatrix.data() ); //2d Example
        bpWriter->Write<float>( ioMyMatrix2, myMatrix2.data() ); //2d Example
wfg's avatar
wfg committed
        bpWriter->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;
}