From d9151bd515e729c05e71d470c31cca14058905b8 Mon Sep 17 00:00:00 2001 From: wfg <wfg@localhost> Date: Tue, 25 Oct 2016 17:48:26 -0400 Subject: [PATCH] Figuring out best way to pass different Variable types. Must use polymorphism of the CVariable class with Get and Set? Union should be avoided as it doesn't preserve actual memory size of variable type (always returns the largest). --- doc/CodingGuidelines | 11 +++++++---- examples/hello/fstream.xml | 2 +- examples/hello/helloADIOS_nompi.cpp | 2 +- include/core/CTransform.h | 8 ++++++-- include/core/CVariable.h | 3 +-- include/core/CVariableTemplate.h | 11 +++++++---- include/public/ADIOS.h | 2 ++ src/core/CGroup.cpp | 6 ++++++ src/core/CVariable.cpp | 3 +-- src/transport/CFStream.cpp | 13 +++++++++++++ 10 files changed, 45 insertions(+), 16 deletions(-) diff --git a/doc/CodingGuidelines b/doc/CodingGuidelines index a78b32033..bb6677a3d 100644 --- a/doc/CodingGuidelines +++ b/doc/CodingGuidelines @@ -48,7 +48,7 @@ Text style for readability (Coding format setup in Eclipse with Window > Prefere } -5) Brackets: use an extra space for brackets +5) Brackets: use an extra space for brackets. Only one line conditionals can skip having brackets. Do: if( number < 1 ) { @@ -61,10 +61,13 @@ Text style for readability (Coding format setup in Eclipse with Window > Prefere number = 4; ..... .....} + It's ok to omit brackets in one line conditionals: + if( itMap == map.end() ) throw std::invalid_argument( "ERROR: not found in map\n" ); -6) Prefer the keyword "using" over "typedef". Only rename complex types, do not rename standard types (int, double, std::vector) - Don't: typedef std::vector<std::vector<double>> Vector2D; - Do: using std::vector<std::vector<double>> = Vector2D; +6) Prefer the keyword "using" over "typedef". Only rename very complex custom types, + do not rename standard types (int, double, std::vector) + Don't: typedef std::vector<std::vector< std::map<std::string,double> >> MapIn2DVector; + Do: using std::vector<std::vector< std::map<std::string,double> >> = MapIn2DVector; Documentation/Comments diff --git a/examples/hello/fstream.xml b/examples/hello/fstream.xml index b72530ee2..bbb26e533 100644 --- a/examples/hello/fstream.xml +++ b/examples/hello/fstream.xml @@ -2,7 +2,7 @@ <adios-config host-language="C++"> <adios-group name="Vector"> - <var name="Numbers" type="integer"/> + <var name="Numbers" type="Vinteger"/> <attribute name="description" value="1 to 10"/> </adios-group> diff --git a/examples/hello/helloADIOS_nompi.cpp b/examples/hello/helloADIOS_nompi.cpp index 18a4f1c9b..c0876a894 100644 --- a/examples/hello/helloADIOS_nompi.cpp +++ b/examples/hello/helloADIOS_nompi.cpp @@ -16,7 +16,7 @@ int main( int argc, char* argv [] ) { try { - adios::ADIOS adios( "writer2Groups.xml" ); //testing with CPOSIXNoMPI + adios::ADIOS adios( "writer2Groups.xml", true ); //testing with CPOSIXNoMPI adios.MonitorGroups( std::cout ); std::cout << "Finished initializing ADIOS\n"; } diff --git a/include/core/CTransform.h b/include/core/CTransform.h index 6c8153f69..91bb5a7d7 100644 --- a/include/core/CTransform.h +++ b/include/core/CTransform.h @@ -36,9 +36,13 @@ public: * @param method zlib, bzip2, szip * @param variable */ - CTransform( const std::string method, const unsigned int compressionLevel, CVariable& variable ); + CTransform( const std::string method, const unsigned int compressionLevel, CVariable& variable ): + m_Method( method ), + m_CompressionLevel( compressionLevel ), + m_Variable( variable ) + { } - virtual ~CTransform( ); + virtual ~CTransform( ){ }; virtual void Compress( ) const = 0; ///< Compress m_Variable data m_Value diff --git a/include/core/CVariable.h b/include/core/CVariable.h index 2b289e2d3..f48e626b2 100644 --- a/include/core/CVariable.h +++ b/include/core/CVariable.h @@ -28,10 +28,9 @@ public: virtual ~CVariable( ); - //template<class T> const T& Get( ) const; + template<class T> const T* Get( ) const; template<class T> void Set( const void* values ); - //protected: turned off for testing bool m_IsGlobal = false; std::string m_Type = "NONE"; ///< mandatory, double, float, unsigned integer, integer, etc. diff --git a/include/core/CVariableTemplate.h b/include/core/CVariableTemplate.h index de7567e88..bae7aa7d9 100644 --- a/include/core/CVariableTemplate.h +++ b/include/core/CVariableTemplate.h @@ -38,19 +38,22 @@ public: const T* m_Value = nullptr; // pointer or no pointer? - //const T& Get() const { return m_Value; } - - void Set( const void* values ){ m_Value = static_cast<T const*> (values); } + const T* Get() const { return m_Value; } + void Set( const void* values ){ m_Value = static_cast<const T*> (values); } }; +template<class T> const T* CVariable::Get( ) const +{ + return dynamic_cast<CVariableTemplate<T>const & >(*this).Get( ); +} + template<class T> void CVariable::Set( const void* values ) { return dynamic_cast< CVariableTemplate<T>& >( *this ).Set( values ); } - } //end namespace #endif /* CVARIABLETEMPLATE_H_ */ diff --git a/include/public/ADIOS.h b/include/public/ADIOS.h index 2a2438d12..cc45c2cfe 100644 --- a/include/public/ADIOS.h +++ b/include/public/ADIOS.h @@ -121,6 +121,8 @@ private: std::map< std::string, std::unique_ptr<CTransform> > m_Transforms; + + /** * @brief Maximum buffer size in ADIOS write() operation. From buffer max - size - MB in XML file * Note, that if there are two ADIOS outputs going on at the same time, diff --git a/src/core/CGroup.cpp b/src/core/CGroup.cpp index d76023046..ce226c85c 100644 --- a/src/core/CGroup.cpp +++ b/src/core/CGroup.cpp @@ -61,6 +61,10 @@ void CGroup::SetVariable( const std::string name, const bool isGlobal, const std m_Variables[name] = std::make_shared< CVariableTemplate<float> >( isGlobal, type, dimensionsCSV, transform ); else if( type == "double") m_Variables[name] = std::make_shared< CVariableTemplate<double> >( isGlobal, type, dimensionsCSV, transform ); + else if( type == "Vinteger") + m_Variables[name] = std::make_shared< CVariableTemplate< std::vector<int> > >( isGlobal, type, dimensionsCSV, transform ); + else + throw std::invalid_argument( "ERROR: type " + type + " for variable " + name + "not supported\n" ); }; //Function body start here @@ -131,6 +135,8 @@ void CGroup::Write( const std::string variableName, const void* values ) if( type == "double" ) variable->Set<double>( values ); else if( type == "integer" ) variable->Set<int>( values ); + else if( type == "Vinteger" ) variable->Set<std::vector<int>>( values ); + // else if( type == "unsigned integer" ) variable->Set<unsigned int>( values ); // else if( type == "float" ) variable->Set<float>( values ); diff --git a/src/core/CVariable.cpp b/src/core/CVariable.cpp index f9805afa2..f63fae8e6 100644 --- a/src/core/CVariable.cpp +++ b/src/core/CVariable.cpp @@ -7,8 +7,7 @@ #include "core/CVariable.h" -#include "core/CVariableTemplate.h" //for dynamic_cast - +#include "core/CVariableTemplate.h" namespace adios { diff --git a/src/transport/CFStream.cpp b/src/transport/CFStream.cpp index 3d7a0e092..4d6a0e176 100644 --- a/src/transport/CFStream.cpp +++ b/src/transport/CFStream.cpp @@ -8,6 +8,8 @@ #include <iostream> #include "transport/CFStream.h" +#include "core/CVariableTemplate.h" +#include "core/CVariable.h" namespace adios @@ -31,6 +33,17 @@ void CFStream::Write( const CVariable& variable ) std::cout << "Just saying Hello from CFStream Write from process " << rank << "/" << size << "\n"; std::cout << "My variable type is " << variable.m_Type << "\n"; + + auto var = variable.Get< std::vector<int> >(); + +// std::cout << "Var is empty: " << std::boolalpha << var.empty() << "\n"; + + std::cout << "var " << var->at(0) << "\n"; + + //pointer to vector +// for( unsigned int i = 0; i < 10; ++i ) +// std::cout << "var[" << i << "] = " << var->at(i) << "\n"; + } -- GitLab