Skip to content
Snippets Groups Projects
Writer.h 5.12 KiB
Newer Older
wfg's avatar
wfg committed
/*
 * SingleBP.h
 *
 *  Created on: Dec 16, 2016
 *      Author: wfg
 */

wfg's avatar
wfg committed
#ifndef WRITER_H_
#define WRITER_H_
wfg's avatar
wfg committed

#include "core/Engine.h"
wfg's avatar
wfg committed
#include "format/BP1Writer.h"
#include "capsule/Heap.h"
wfg's avatar
wfg committed


namespace adios
{


wfg's avatar
wfg committed
class Writer : public Engine
wfg's avatar
wfg committed
{

public:

    /**
wfg's avatar
wfg committed
     * Constructor for Writer writes in BP format into a single heap capsule, manages several transports
wfg's avatar
wfg committed
     * @param name unique name given to the engine
     * @param accessMode
     * @param mpiComm
     * @param method
     * @param debugMode
     */
wfg's avatar
wfg committed
    Writer( ADIOS& adios, const std::string name, const std::string accessMode, MPI_Comm mpiComm,
            const Method& method, const bool debugMode = false, const unsigned int cores = 1 );
wfg's avatar
wfg committed

wfg's avatar
wfg committed
    ~Writer( );
wfg's avatar
wfg committed

wfg's avatar
wfg committed
    void Write( Variable<char>& variable, const char* values );
    void Write( Variable<unsigned char>& variable, const unsigned char* values );
    void Write( Variable<short>& variable, const short* values );
    void Write( Variable<unsigned short>& variable, const unsigned short* values );
    void Write( Variable<int>& variable, const int* values );
    void Write( Variable<unsigned int>& variable, const unsigned int* values );
    void Write( Variable<long int>& variable, const long int* values );
    void Write( Variable<unsigned long int>& variable, const unsigned long int* values );
    void Write( Variable<long long int>& variable, const long long int* values );
    void Write( Variable<unsigned long long int>& variable, const unsigned long long int* values ) ;
    void Write( Variable<float>& variable, const float* values );
    void Write( Variable<double>& variable, const double* values );
    void Write( Variable<long double>& variable, const long double* values );
wfg's avatar
wfg committed

    void Write( const std::string variableName, const char* values );
    void Write( const std::string variableName, const unsigned char* values );
    void Write( const std::string variableName, const short* values );
    void Write( const std::string variableName, const unsigned short* values );
    void Write( const std::string variableName, const int* values );
    void Write( const std::string variableName, const unsigned int* values );
    void Write( const std::string variableName, const long int* values );
    void Write( const std::string variableName, const unsigned long int* values );
    void Write( const std::string variableName, const long long int* values );
    void Write( const std::string variableName, const unsigned long long int* values );
    void Write( const std::string variableName, const float* values );
    void Write( const std::string variableName, const double* values );
    void Write( const std::string variableName, const long double* values );
wfg's avatar
wfg committed

wfg's avatar
wfg committed
    void Close( const int transportIndex = -1 );
wfg's avatar
wfg committed
private:

    Heap m_Buffer; ///< heap capsule
    format::BP1Writer m_BP1Writer; ///< format object will provide the required BP functionality to be applied on m_Buffer and m_Transports
    format::BP1MetadataSet m_MetadataSet; ///< metadata set accompanying the heap buffer data in bp format. Needed by m_BP1Writer
    std::size_t m_MaxBufferSize;
    float m_GrowthFactor = 1.5;
    bool m_TransportFlush = false; ///< true: transport flush happened, buffer must be reset
wfg's avatar
wfg committed
    void Init( );
    void InitTransports( );

    /**
     * Common function
     * @param group
     * @param variableName
     * @param variable
     */
    template< class T >
wfg's avatar
wfg committed
    void WriteVariable( Variable<T>& variable, const T* values )
wfg's avatar
wfg committed
        //set variable
        variable.m_AppValues = values;
        m_WrittenVariables.insert( variable.m_Name );
        //precalculate new metadata and payload sizes
wfg's avatar
wfg committed
        const std::size_t indexSize = m_BP1Writer.GetVariableIndexSize( variable );
        const std::size_t payloadSize = variable.PayLoadSize();

        //Buffer reallocation, expensive part
wfg's avatar
wfg committed
        m_TransportFlush = CheckBuffersAllocation( variable.m_Name, indexSize, payloadSize );

        //WRITE INDEX to data buffer and metadata structure (in memory)//
wfg's avatar
wfg committed
        m_BP1Writer.WriteVariableIndex( variable, m_Buffer, m_MetadataSet );

        if( m_TransportFlush == true ) //in batches
        {
            //write pg index

            //flush to transports

            //reset positions to zero, update absolute position

        }
        else //Write data to buffer
        {
wfg's avatar
wfg committed
            //EXPENSIVE part might want to use threads if large.
            MemcpyThreads( m_Buffer.m_Data.data(), variable.m_AppValues, payloadSize, m_Cores );
            //update indices
            m_Buffer.m_DataPosition += payloadSize;
            m_Buffer.m_DataAbsolutePosition += payloadSize;
        }
    }

    /**
     * Check if heap buffers for data and metadata need reallocation or maximum sizes have been reached.
     * @param variableName name of the variable to be written
     * @param indexSize precalculated index size
     * @param payloadSize payload size from variable total size
     * @return true: transport must be flush and buffers reset, false: buffer is sufficient
     */
wfg's avatar
wfg committed
    bool CheckBuffersAllocation( const std::string variableName, const std::size_t indexSize, const std::size_t payloadSize );
} //end namespace adios
wfg's avatar
wfg committed
#endif /* WRITER_H_ */