19 #ifndef GEOSX_DATAREPOSITORY_WRAPPER_HPP_ 20 #define GEOSX_DATAREPOSITORY_WRAPPER_HPP_ 27 #include "codingUtilities/SFINAE_Macros.hpp" 29 #include "BufferOps.hpp" 30 #include "BufferOpsDevice.hpp" 32 #include "codingUtilities/traits.hpp" 39 #include <type_traits> 41 #include <type_traits> 46 namespace dataRepository
55 template<
typename T >
76 Group *
const parent ):
82 if( traits::is_tensorT< T > || std::is_arithmetic< T >::value || traits::is_string< T > )
98 std::unique_ptr< T >
object ):
101 m_data( object.release() ),
104 if( traits::is_tensorT< T > || std::is_arithmetic< T >::value || traits::is_string< T > )
119 Group *
const parent,
126 if( traits::is_tensorT< T > || std::is_arithmetic< T >::value || traits::is_string< T > )
156 m_data = source.m_data;
167 m_data = std::move( source.m_data );
179 virtual std::unique_ptr< WrapperBase >
clone(
string const & name,
180 Group *
const parent )
override 182 std::unique_ptr< WrapperBase >
183 clonedWrapper = std::make_unique< Wrapper< T > >( name, parent, this->m_data );
184 clonedWrapper->copyWrapperAttributes( *
this );
186 return clonedWrapper;
194 m_ownsData = castedSource.m_ownsData;
195 m_default = castedSource.m_default;
205 m_ownsData = castedSource.m_ownsData;
206 m_default = castedSource.m_default;
210 virtual const std::type_info &
get_typeid() const noexcept
override 230 return dynamicCast< Wrapper< T > * >( base );
241 return dynamicCast< Wrapper< T >
const * >( base );
252 return dynamicCast< Wrapper< T > & >( base );
262 return dynamicCast< Wrapper< T >
const & >( base );
287 return bufferOps::can_memcpy< T >;
291 return bufferOps::is_packable< T >;
301 if( withMetadata ) packedSize += bufferOps::Pack< true >( buffer, this->
getName() );
306 packedSize += wrapperHelpers::PackDevice< true >( buffer,
reference() );
310 packedSize += wrapperHelpers::PackDataDevice< true >( buffer,
reference() );
315 packedSize += bufferOps::Pack< true >( buffer, *m_data );
328 if( withMetadata ) packedSize += bufferOps::Pack< true >( buffer, this->
getName() );
333 packedSize += wrapperHelpers::PackByIndexDevice< true >( buffer,
reference(), packList );
337 packedSize += wrapperHelpers::PackDataByIndexDevice< true >( buffer,
reference(), packList );
342 packedSize += wrapperHelpers::PackByIndex< true >( buffer, *m_data, packList );
355 if( withMetadata ) packedSize += bufferOps::Pack< false >( buffer, this->
getName() );
360 packedSize += wrapperHelpers::PackDevice< false >( buffer,
reference() );
364 packedSize += wrapperHelpers::PackDataDevice< false >( buffer,
reference() );
369 packedSize += bufferOps::Pack< false >( buffer, *m_data );
383 if( withMetadata ) packedSize += bufferOps::Pack< false >( buffer, this->
getName() );
388 packedSize += wrapperHelpers::PackByIndexDevice< false >( buffer,
reference(), packList );
392 packedSize += wrapperHelpers::PackDataByIndexDevice< false >( buffer,
reference(), packList );
397 packedSize += wrapperHelpers::PackByIndex< false >( buffer, *m_data, packList );
412 unpackedSize += bufferOps::Unpack( buffer, name );
419 unpackedSize += wrapperHelpers::UnpackDevice( buffer,
referenceAsView() );
423 unpackedSize += wrapperHelpers::UnpackDataDevice( buffer,
referenceAsView() );
428 unpackedSize += bufferOps::Unpack( buffer, *m_data );
444 unpackedSize += bufferOps::Unpack( buffer, name );
451 unpackedSize += wrapperHelpers::UnpackByIndexDevice( buffer,
referenceAsView(), unpackIndices );
455 unpackedSize += wrapperHelpers::UnpackDataByIndexDevice( buffer,
referenceAsView(), unpackIndices );
460 unpackedSize += wrapperHelpers::UnpackByIndex( buffer, *m_data, unpackIndices );
470 {
return wrapperHelpers::dataPtr( *m_data ); }
474 {
return wrapperHelpers::byteSizeOfElement< T >(); }
486 {
return wrapperHelpers::size( *m_data ); }
491 wrapperHelpers::move( *m_data, LvArray::MemorySpace::CPU,
true );
492 wrapperHelpers::resizeDimensions( *m_data, ndims, dims );
498 wrapperHelpers::move( *m_data, LvArray::MemorySpace::CPU,
true );
499 wrapperHelpers::reserve(
reference(), newCapacity );
506 return wrapperHelpers::capacity( *m_data );
512 wrapperHelpers::move( *m_data, LvArray::MemorySpace::CPU,
true );
513 wrapperHelpers::resizeDefault(
reference(), newSize, m_default );
519 template<
typename U,
int NDIM,
typename PERMUTATION >
523 [destIndex, &array]( U
const & sourceVal,
auto const ... indices )
525 array( destIndex, indices ... ) = sourceVal;
529 template<
typename U >
533 template<
typename U=T >
535 typename std::enable_if< traits::hasCopyAssignmentOp< U >,
void >::type
536 copyData( U & destinationData, U
const & sourceData )
538 destinationData = sourceData;
541 template<
typename U=T >
543 typename std::enable_if< !traits::hasCopyAssignmentOp< U >,
void >::type
565 copy_wrapper::copyData( *m_data, *castedSource.m_data );
571 {
return wrapperHelpers::move( *m_data, space, touch ); }
607 template<
typename _T=T,
typename=std::enable_if_t< traits::HasMemberFunction_toView< _T > > >
609 {
return m_data->toView(); }
614 template<
typename _T=T,
typename=std::enable_if_t< !traits::HasMemberFunction_toView< _T > > >
621 template<
typename _T=T,
typename=std::enable_if_t< traits::HasMemberFunction_toView< _T > > >
623 {
return m_data->toViewConst(); }
628 template<
typename _T=T,
typename=std::enable_if_t< !traits::HasMemberFunction_toView< _T > > >
644 return m_default.has_default_value;
651 template<
typename U=T >
663 template<
typename U=T >
667 return m_default.value;
675 template<
typename U=T >
676 typename std::enable_if< DefaultValue< U >::has_default_value,
Wrapper< T > * >::type
679 m_default.value = defaultVal;
688 template<
typename U=T >
692 m_default.value = defaultVal;
693 *m_data = m_default.value;
702 template<
typename U=T >
703 typename std::enable_if< traits::is_array< U > && DefaultValue< U >::has_default_value,
Wrapper< T > * >::type
706 m_default.value = defaultVal;
707 m_data->template setValues< serialPolicy >( m_default.value );
719 if( wrapper_type.find(
"array3d" ) != std::string::npos )
723 else if( wrapper_type.find(
"array2d" ) != std::string::npos )
727 else if( wrapper_type.find(
"array" ) != std::string::npos )
733 std::stringstream ss;
735 for(
integer ii=0; ii<value_dim; ++ii )
742 for(
integer ii=0; ii<value_dim; ++ii )
762 "Input variable " +
getName() +
" is required in " + targetNode.path()
764 +
"\nFor more details, please refer to documentation at: \n" 765 +
"http://geosx-geosx.readthedocs-hosted.com/en/latest/docs/sphinx/userGuide/Index.html \n" );
795 std::vector< std::string >
const & componentNames = {} )
const override 796 { wrapperHelpers::addBlueprintField(
reference(), fields, name, topology, componentNames ); }
799 void populateMCArray( conduit::Node & node, std::vector< std::string >
const & componentNames = {} )
const override 800 { wrapperHelpers::populateMCArray(
reference(), node, componentNames ); }
805 auto ptr = wrapperHelpers::averageOverSecondDim(
reference() );
806 using U =
typename decltype( ptr )::element_type;
808 GEOSX_ERROR_IF( ptr ==
nullptr,
"Failed to average over the second dimension of." );
810 return std::make_unique< Wrapper< U > >( name, &group, std::move( ptr ) );
823 move( LvArray::MemorySpace::CPU,
false );
827 wrapperHelpers::pushDataToConduitNode( *m_data,
m_conduitNode );
846 wrapperHelpers::pullDataFromConduitNode( *m_data,
m_conduitNode );
922 #if defined(USE_TOTALVIEW_OUTPUT) 923 virtual string totalviewTypeName()
const override 928 virtual int setTotalviewDisplay()
const override 931 WrapperBase::setTotalviewDisplay();
932 TV_ttf_add_row(
"m_ownsData",
"bool", &m_ownsData );
933 TV_ttf_add_row(
"m_data", LvArray::system::demangle< T >().c_str(), m_data );
string m_name
Name of the object that is being wrapped.
virtual void copy(localIndex const sourceIndex, localIndex const destIndex) override
Calls T::copy(sourceIndex, destIndex)
int sizedFromParent() const
Check whether this wrapper is resized when its parent is resized.
Wrapper & operator=(Wrapper const &source)
Copy Assignment Operator.
virtual void resize(int ndims, localIndex const *const dims) override
Calls T::resize( num_dims, dims )
GEOSX_DECLTYPE_AUTO_RETURN referenceAsView()
Provide access to wrapped object converted to a view, if possible.
Doe no write into restart.
#define GEOSX_DECLTYPE_AUTO_RETURN
Doxygen can't parse a decltype( auto ) return type, using this gets around that.
pugi::xml_node xmlNode
Alias for the type of an xml node.
Wrapper(std::string const &name, Group *const parent, T *object)
Constructor that does not take ownership of an existing object.
std::unique_ptr< WrapperBase > averageOverSecondDim(std::string const &name, Group &group) const override
Create a new Wrapper with values averaged over the second dimension.
T & referenceAsView()
Provide access to wrapped object converted to a view, if possible.
WrapperBase * setPlotLevel(PlotLevel const flag)
Set the PlotLevel of the wrapper.
Wrapper(std::string const &name, Group *const parent)
Constructor that creates a new instance of wrapped type.
virtual ~Wrapper() noexcept override
Default destructor.
virtual bool hasDefaultValue() const final override
Return true iff this wrapper has a valid default value.
void registerToWrite() const override
Register the wrapper's data for writing with Conduit.
void finishWriting() const override
Write the wrapped data into Conduit.
InputFlags getInputFlag() const
Get the InputFlag of the wrapper.
T TYPE
Alias for the wrapped type T.
virtual bool processInputFile(xmlWrapper::xmlNode const &targetNode) override
Initialize the wrapper from the input xml node.
std::enable_if< DefaultValue< U >::has_default_value, Wrapper< T > *>::type setDefaultValue(typename DefaultValue< U >::value_type const &defaultVal)
Setter for default value.
static std::enable_if_t< canParseVariable< T >, bool > ReadAttributeAsType(T &rval, string const &name, xmlNode const &targetNode, T_DEF const &defVal)
Extract attribute in an xml tree, and translate its value into a typed variable.
Wrapper & operator=(Wrapper &&source)
Move Assignment Operator.
static Wrapper< T > * cast(WrapperBase *const base)
Static function to cast a Wrapper base to a derived Wrapper<T>
Wrapper< T > * setDescription(string const &description)
Set the description string of the wrapper.
static string get()
Get the type's regex (default implementation).
std::enable_if< !traits::is_array< U > &&DefaultValue< U >::has_default_value, Wrapper< T > *>::type setApplyDefaultValue(typename DefaultValue< U >::value_type const &defaultVal)
Set and apply for default value.
Contains functions that interact with the system or runtime environment.
virtual void copyWrapperAttributes(WrapperBase const &source) override
Copy attributes from another wrapper.
GEOSX_DECLTYPE_AUTO_RETURN referenceAsView() const
Provide access to wrapped object converted to a view, if possible.
virtual localIndex Pack(buffer_unit_type *&buffer, bool withMetadata, bool onDevice) const override final
Pack the entire wrapped object into a buffer.
Contains portable access to std::numeric_limits and functions for converting between integral types...
string dumpInputOptions(bool const outputHeader) const
virtual void move(LvArray::MemorySpace const space, bool const touch) const override
Calls T::move(space, touch)
static Wrapper< T > const & cast(WrapperBase const &base)
This class serves to provide a "view" of a multidimensional array.
void addBlueprintField(conduit::Node &fields, std::string const &name, std::string const &topology, std::vector< std::string > const &componentNames={}) const override
Push the data in the wrapper into a Conduit blueprint field.
constexpr void copy(DST_VECTOR &&LVARRAY_RESTRICT_REF dstVector, SRC_VECTOR const &LVARRAY_RESTRICT_REF srcVector)
Copy srcVector into dstVector.
Wrapper< T > * setSizedFromParent(int val)
Set whether this wrapper is resized when its parent is resized.
WrapperBase * setSizedFromParent(int val)
Set whether this wrapper is resized when its parent is resized.
Write and read from restart.
static std::string typeNames(std::type_index const key)
Convert a std::type_index to a string.
virtual void copyWrapper(WrapperBase const &source) override
Copies the contents of a Wrapper into *this.
virtual localIndex PackByIndex(buffer_unit_type *&buffer, arrayView1d< localIndex const > const &packList, bool withMetadata, bool onDevice) const override final
For indexable types, pack selected indices of wrapped object into a buffer.
Wrapper< T > * setInputFlag(InputFlags const input)
Set the InputFlag of the wrapper.
virtual localIndex Unpack(buffer_unit_type const *&buffer, bool withMetadata, bool onDevice) override final
Unpack the entire wrapped object from a buffer.
virtual std::unique_ptr< WrapperBase > clone(string const &name, Group *const parent) override
Creates a clone of *this WrapperBase.
static Wrapper< T > & cast(WrapperBase &base)
Static function to cast a Wrapper base to a derived Wrapper<T>
virtual localIndex size() const override
Calls T::size()
void populateMCArray(conduit::Node &node, std::vector< std::string > const &componentNames={}) const override
Push the data in the wrapper into a Conduit Blueprint mcarray.
RestartFlags getRestartFlags() const
Get the RestartFlags of the wrapper.
internal::Helper< T > DefaultValue
A templated alias to hold default values.
virtual bool isPackable(bool onDevice) const override
Check whether wrapped type is can be packed into a buffer on host or device.
string const & getName() const
Get name of the wrapper.
Base class for all wrappers containing common operations.
Wrapper(std::string const &name, Group *const parent, std::unique_ptr< T > object)
Constructor that takes ownership of an existing object.
void setName()
DO_NOT_DOCUMENT.
WrapperBase * setRestartFlags(RestartFlags flags)
Set the RestartFlags of the wrapper.
virtual localIndex PackSize(bool withMetadata, bool onDevice) const override final
Get the buffer size needed to pack the entire wrapped object.
virtual localIndex elementByteSize() const override
signed char buffer_unit_type
Type stored in communication buffers.
MemorySpace
An enum containing the available memory spaces.
GEOSX_DECLTYPE_AUTO_RETURN reference() const
const Accessor for m_data
virtual localIndex capacity() const override
std::int32_t integer
Signed integer type.
WrapperBase * setInputFlag(InputFlags const input)
Set the InputFlag of the wrapper.
virtual void resize(localIndex const newSize) override
Calls T::resize(newsize) if it exists.
WrapperBase * setRegisteringObjects(string const &objectName)
Add a new name to the list of groups that register this wrapper.
std::enable_if< traits::is_array< U > &&DefaultValue< U >::has_default_value, Wrapper< T > *>::type setApplyDefaultValue(typename DefaultValue< U >::value_type const &defaultVal)
Set and apply for default value.
Contains a bunch of macro definitions.
Wrapper< T > * setRegisteringObjects(string const &objectName)
Add a new name to the list of groups that register this wrapper.
virtual localIndex PackByIndexSize(arrayView1d< localIndex const > const &packList, bool withMetadata, bool onDevice) const override final
Get the buffer size needed to pack the selected indices wrapped object.
bool loadFromConduit() override
Read the wrapped data from Conduit.
T & reference()
Accessor for m_data.
virtual localIndex UnpackByIndex(buffer_unit_type const *&buffer, arrayView1d< localIndex const > const &unpackIndices, bool withMetadata, bool onDevice) override final
For indexable types, unpack selected indices of wrapped object from a buffer.
std::ptrdiff_t localIndex
Local index type (for indexing objects within an MPI partition).
void forValuesInSliceWithIndices(T &value, LAMBDA &&f, INDICES const ... indices)
Apply the function f to the value value also passing f any indices used to reach value.
virtual const std::type_info & get_typeid() const noexcept override
Get the typeid of T.
virtual void copyData(WrapperBase const &source) override
Copy the the data contained in another wrapper into this wrapper.
Wrapper< T > * setRestartFlags(RestartFlags flags)
Set the RestartFlags of the wrapper.
static Wrapper< T > const * cast(WrapperBase const *const base)
Static function to cast a Wrapper base to a derived Wrapper<T>
WrapperBase * setDescription(string const &description)
Set the description string of the wrapper.
virtual void reserve(localIndex const newCapacity) override
Calls T::reserve( newCapacity ) if it exists, otherwise a no-op.
virtual void copyWrapperAttributes(WrapperBase const &source)
Copy attributes from another wrapper.
std::string string
String type.
#define GEOSX_ERROR_IF(EXP, msg)
Conditionally raise a hard error and terminate the program.
virtual HistoryMetadata getHistoryMetadata(localIndex const packCount=-1) const override final
Get a description of the wrapped data for time history collection/output.
std::enable_if< can_history_io< T >, HistoryMetadata >::type getHistoryMetadata(string const &name, ArrayView< T const, 1, 0 > const &arr, localIndex sizeOverride=-1)
Produce a HistoryMetadata object for a supported one-dimensional array type.
std::string demangle(char const *const name)
DefaultValue< T > const & getDefaultValueStruct() const
Accessor for m_default.
T const & referenceAsView() const
Provide access to wrapped object converted to a view, if possible.
This class provides a fixed dimensional resizeable array interface in addition to an interface simila...
std::enable_if< DefaultValue< U >::has_default_value, typename DefaultValue< U >::value_type const &>::type getDefaultValue() const
Accessor for default value.
Wrapper< T > * setPlotLevel(PlotLevel const flag)
Set the PlotLevel of the wrapper.
A minimal class to specify information about time history information being collected and output...
void const * voidPointer() const override
virtual string typeRegex() const override
Calls TypeRegex< T >::get().
virtual std::string getDefaultValueString() const override
Return a string representing the default value.
conduit::Node & m_conduitNode
A reference to the corresponding conduit::Node.