20 #ifndef GEOS_DATAREPOSITORY_WRAPPER_HPP_
21 #define GEOS_DATAREPOSITORY_WRAPPER_HPP_
26 #include "LvArray/src/limits.hpp"
28 #include "codingUtilities/SFINAE_Macros.hpp"
29 #include "LvArray/src/Macros.hpp"
30 #include "BufferOps.hpp"
31 #include "BufferOpsDevice.hpp"
33 #include "codingUtilities/traits.hpp"
36 #include "LvArray/src/system.hpp"
40 #include <type_traits>
42 #include <type_traits>
47 namespace dataRepository
56 template<
typename T >
78 WrapperBase( name, parent, rtTypes::getTypeName( typeid( T ) ) ),
84 if( traits::is_tensorT< T > || std::is_arithmetic< T >::value || traits::is_string< T > )
100 std::unique_ptr< T >
object ):
101 WrapperBase( name, parent, rtTypes::getTypeName( typeid( T ) ) ),
104 m_data( object.release() ),
107 if( traits::is_tensorT< T > || std::is_arithmetic< T >::value || traits::is_string< T > )
124 WrapperBase( name, parent, rtTypes::getTypeName( typeid( T ) ) ),
130 if( traits::is_tensorT< T > || std::is_arithmetic< T >::value || traits::is_string< T > )
160 m_data = source.m_data;
171 m_data = std::move( source.m_data );
183 virtual std::unique_ptr< WrapperBase >
clone(
string const & name,
184 Group & parent )
override
186 std::unique_ptr< Wrapper< T > > clonedWrapper = std::make_unique< Wrapper< T > >( name, parent, m_data );
187 clonedWrapper->copyWrapperAttributes( *
this );
188 clonedWrapper->m_isClone =
true;
189 return clonedWrapper;
203 Wrapper< T > const & castedSource = dynamicCast< Wrapper< T >
const & >( source );
204 m_ownsData = castedSource.m_ownsData;
205 m_default = castedSource.m_default;
206 m_dimLabels = castedSource.m_dimLabels;
210 virtual const std::type_info &
getTypeId() const noexcept
override
224 "Invalid downcast to Wrapper< " << LvArray::system::demangleType< T >() <<
" >" );
237 "Invalid downcast to Wrapper< " << LvArray::system::demangleType< T >() <<
" >" );
245 return wrapperHelpers::numArrayDims(
reference() );
250 return wrapperHelpers::numArrayComp(
reference() );
255 m_dimLabels.set( dim, labels );
261 return m_dimLabels.get( dim );
286 return bufferOps::can_memcpy< T >;
290 return bufferOps::is_packable< T >;
303 unpackedSize += bufferOps::Unpack( buffer, name );
310 unpackedSize += wrapperHelpers::UnpackDevice( buffer,
referenceAsView(), events );
314 unpackedSize += wrapperHelpers::UnpackDataDevice( buffer,
referenceAsView(), events );
319 unpackedSize += bufferOps::Unpack( buffer, *m_data );
331 parallelDeviceEvents & events,
332 MPI_Op op )
override final
339 unpackedSize += bufferOps::Unpack( buffer, name );
346 unpackedSize += wrapperHelpers::UnpackByIndexDevice( buffer,
referenceAsView(), unpackIndices, events, op );
350 unpackedSize += wrapperHelpers::UnpackDataByIndexDevice( buffer,
referenceAsView(), unpackIndices, events, op );
355 unpackedSize += wrapperHelpers::UnpackByIndex( buffer, *m_data, unpackIndices );
365 {
return wrapperHelpers::dataPtr( *m_data ); }
369 {
return wrapperHelpers::byteSizeOfElement< T >(); }
373 return m_isClone ? 0 : wrapperHelpers::byteSize< T >( *m_data );
387 {
return wrapperHelpers::size( *m_data ); }
392 wrapperHelpers::move( *m_data, hostMemorySpace,
true );
393 wrapperHelpers::resizeDimensions( *m_data, ndims, dims );
399 wrapperHelpers::move( *m_data, hostMemorySpace,
true );
400 wrapperHelpers::reserve(
reference(), newCapacity );
407 return wrapperHelpers::capacity( *m_data );
413 wrapperHelpers::move( *m_data, hostMemorySpace,
true );
414 wrapperHelpers::resizeDefault(
reference(), newSize, m_default );
420 template<
typename U,
int NDIM,
typename PERMUTATION >
423 LvArray::forValuesInSliceWithIndices( array[ sourceIndex ],
424 [destIndex, &array]( U
const & sourceVal,
auto const ... indicesToErase )
426 array( destIndex, indicesToErase ... ) = sourceVal;
430 template<
typename U >
434 template<
typename U=T >
435 static std::enable_if_t< traits::hasCopyAssignmentOp< U > >
436 copyData( U & destinationData, U
const & sourceData )
438 destinationData = sourceData;
441 template<
typename U=T >
442 static std::enable_if_t< !traits::hasCopyAssignmentOp< U > >
451 copy_wrapper::copy(
reference(), sourceIndex, destIndex );
458 Wrapper< T > const & castedSource = dynamicCast< Wrapper< T >
const & >( source );
459 copy_wrapper::copyData( *m_data, *castedSource.m_data );
466 template<
typename TYPE >
467 static void erase(
TYPE &, std::set< localIndex >
const & )
470 template<
typename TYPE >
473 int oldSize = array.size( 0 );
474 int numToErase = indicesToErase.size();
475 int newSize = oldSize - numToErase;
476 std::set< localIndex >::iterator it = indicesToErase.begin();
483 if( offset < numToErase )
488 array[i-offset] = array[i];
490 array.resize( newSize );
493 template<
typename TYPE >
494 static void erase( array2d< TYPE > & array, std::set< localIndex >
const & indicesToErase )
496 int oldSize = array.size( 0 );
497 int numToErase = indicesToErase.size();
498 int newSize = oldSize - numToErase;
499 int dim1 = array.size( 1 );
500 std::set< localIndex >::iterator it = indicesToErase.begin();
507 if( offset < numToErase )
512 for(
int j=0; j<dim1; j++ )
514 array[i-offset][j] = array[i][j];
517 array.resize( newSize );
520 template<
typename TYPE >
521 static void erase( array3d< TYPE > & array, std::set< localIndex >
const & indicesToErase )
523 int oldSize = array.size( 0 );
524 int numToErase = indicesToErase.size();
525 int newSize = oldSize - numToErase;
526 int dim1 = array.size( 1 );
527 int dim2 = array.size( 2 );
528 std::set< localIndex >::iterator it = indicesToErase.begin();
535 if( offset < numToErase )
540 for(
int j=0; j<dim1; j++ )
542 for(
int k=0; k<dim2; k++ )
544 array[i-offset][j][k] = array[i][j][k];
548 array.resize( newSize );
555 void erase( std::set< localIndex >
const & indicesToErase )
override
557 GEOS_ERROR_IF( indicesToErase.size() == 0,
"Wrapper::erase() can only be called on a populated set of indices!" );
563 virtual void move( LvArray::MemorySpace
const space,
bool const touch )
const override
564 {
return wrapperHelpers::move( *m_data, space, touch ); }
600 template<
typename _T=T,
typename=std::enable_if_t< traits::HasMemberFunction_toView< _T > > >
602 {
return m_data->toView(); }
607 template<
typename _T=T,
typename=std::enable_if_t< !traits::HasMemberFunction_toView< _T > > >
614 template<
typename _T=T,
typename=std::enable_if_t< traits::HasMemberFunction_toView< _T > > >
616 {
return m_data->toViewConst(); }
621 template<
typename _T=T,
typename=std::enable_if_t< !traits::HasMemberFunction_toView< _T > > >
637 return m_default.has_default_value;
644 template<
typename U=T >
656 template<
typename U=T >
657 std::enable_if_t< DefaultValue< U >::has_default_value,
typename DefaultValue< U >::value_type
const & >
660 return m_default.value;
668 template<
typename U=T >
669 std::enable_if_t< DefaultValue< U >::has_default_value,
Wrapper< T > & >
672 m_default.value = defaultVal;
681 template<
typename U=T >
685 m_default.value = defaultVal;
686 *m_data = m_default.value;
695 template<
typename U=T >
699 m_default.value = defaultVal;
700 m_data->template setValues< serialPolicy >( m_default.value );
709 std::ostringstream ss;
730 GEOS_FMT(
"XML Node {} ({}) with name={} is missing required attribute '{}'."
731 "Available options are:\n {}\n For more details, please refer to documentation at:\n"
732 "http://geosx-geosx.readthedocs-hosted.com/en/latest/docs/sphinx/userGuide/Index.html",
733 targetNode.name(), nodePos.
toString(), targetNode.attribute(
"name" ).value(),
746 catch( std::exception
const & ex )
774 string const & topology,
775 std::vector< string >
const & componentNames = {} )
const override
776 { wrapperHelpers::addBlueprintField(
reference(), fields, name, topology, componentNames ); }
779 void populateMCArray( conduit::Node & node, std::vector< string >
const & componentNames = {} )
const override
780 { wrapperHelpers::populateMCArray(
reference(), node, componentNames ); }
785 auto ptr = wrapperHelpers::averageOverSecondDim(
reference() );
786 using U =
typename decltype( ptr )::element_type;
788 GEOS_ERROR_IF( ptr ==
nullptr,
"Failed to average over the second dimension of." );
790 auto ret = std::make_unique< Wrapper< U > >( name, group, std::move( ptr ) );
809 move( hostMemorySpace,
false );
813 wrapperHelpers::pushDataToConduitNode( *m_data,
m_conduitNode );
832 wrapperHelpers::pullDataFromConduitNode( *m_data,
m_conduitNode );
926 #if defined(USE_TOTALVIEW_OUTPUT)
927 virtual string totalviewTypeName()
const override
929 return LvArray::system::demangle(
typeid(
Wrapper< T > ).name() );
932 virtual int setTotalviewDisplay()
const override
935 WrapperBase::setTotalviewDisplay();
936 TV_ttf_add_row(
"m_ownsData",
"bool", &m_ownsData );
937 TV_ttf_add_row(
"m_data", LvArray::system::demangle< T >().c_str(), m_data );
938 TV_ttf_add_row(
"m_default", LvArray::system::demangle< DefaultValue< T > >().c_str(), &m_default );
944 #if defined(GEOS_USE_PYGEOSX)
945 virtual PyObject * createPythonObject( )
override
946 {
return wrapperHelpers::createPythonObject(
reference() ); }
964 template<
bool DO_PACKING >
968 parallelDeviceEvents & events )
const
973 { packedSize += bufferOps::Pack< DO_PACKING >( buffer,
getName() ); }
978 packedSize += wrapperHelpers::PackDevice< DO_PACKING >( buffer,
reference(), events );
982 packedSize += wrapperHelpers::PackDataDevice< DO_PACKING >( buffer,
reference(), events );
987 packedSize += bufferOps::Pack< DO_PACKING >( buffer, *m_data );
1006 template<
bool DO_PACKING >
1008 arrayView1d< localIndex const >
const & packList,
1011 parallelDeviceEvents & events )
const
1016 { packedSize += bufferOps::Pack< DO_PACKING >( buffer,
getName() ); }
1021 packedSize += wrapperHelpers::PackByIndexDevice< DO_PACKING >( buffer,
reference(), packList, events );
1025 packedSize += wrapperHelpers::PackDataByIndexDevice< DO_PACKING >( buffer,
reference(), packList, events );
1030 packedSize += wrapperHelpers::PackByIndex< DO_PACKING >( buffer, *m_data, packList );
1042 parallelDeviceEvents & events )
const override final
1044 return this->packImpl< true >( buffer, withMetadata, onDevice, events );
1051 arrayView1d< localIndex const >
const & packList,
1054 parallelDeviceEvents & events )
const override final
1056 return this->packByIndexImpl< true >( buffer, packList, withMetadata, onDevice, events );
1062 localIndex packSizePrivate(
bool withMetadata,
1064 parallelDeviceEvents & events )
const override final
1067 return this->packImpl< false >( dummy, withMetadata, onDevice, events );
1073 localIndex packByIndexSizePrivate( arrayView1d< localIndex const >
const & packList,
1076 parallelDeviceEvents & events )
const override final
1079 return this->packByIndexImpl< false >( dummy, packList, withMetadata, onDevice, events );
1092 DefaultValue< T > m_default;
1095 wrapperHelpers::ArrayDimLabels< T > m_dimLabels;
#define GEOS_DECLTYPE_AUTO_RETURN
Doxygen can't parse a decltype( auto ) return type, using this gets around that.
#define GEOS_ERROR_IF(EXP, msg)
Conditionally raise a hard error and terminate the program.
#define GEOS_THROW_IF(EXP, msg, TYPE)
Conditionally throw an exception.
A minimal class to specify information about time history information being collected and output.
Lightweight non-owning wrapper over a contiguous range of elements.
Base class for all wrappers containing common operations.
InputFlags getInputFlag() const
Get the InputFlag of the wrapper.
string const & getName() const
Get name of the wrapper.
string const & getRTTypeName() const
int sizedFromParent() const
Check whether this wrapper is resized when its parent is resized.
RestartFlags getRestartFlags() const
Get the RestartFlags of the wrapper.
WrapperBase & setInputFlag(InputFlags const input)
Set the InputFlag of the wrapper.
conduit::Node & m_conduitNode
A reference to the corresponding conduit::Node.
string dumpInputOptions(bool const outputHeader) const
WrapperBase & setDescription(string const &description)
Set the description string of the wrapper.
WrapperBase & setSizedFromParent(int val)
Set whether this wrapper is resized when its parent is resized.
virtual void copyWrapperAttributes(WrapperBase const &source)
Copy attributes from another wrapper.
WrapperBase & setRegisteringObjects(string const &objectName)
Add a new name to the list of groups that register this wrapper.
WrapperBase & setRestartFlags(RestartFlags flags)
Set the RestartFlags of the wrapper.
string m_rtTypeName
A string regex to validate the input values string to parse for the wrapped object.
WrapperBase & appendDescription(string const &description)
Add up more text to the existing description string of the wrapper.
void createDataContext(xmlWrapper::xmlNode const &targetNode, xmlWrapper::xmlNodePos const &nodePos)
Sets the m_dataContext to a DataFileContext by retrieving the attribute file line.
bool m_successfulReadFromInput
Flag to indicate if wrapped object was successfully read from input.
virtual std::type_info const & getTypeId() const =0
Get the typeid of T.
WrapperBase & setRTTypeName(string_view rtTypeName)
override the rtType to use when parsing an input value to the wrapped object. It can be useful to cha...
string m_name
Name of the object that is being wrapped.
WrapperBase & setPlotLevel(PlotLevel const flag)
Set the PlotLevel of the wrapper.
void addBlueprintField(conduit::Node &fields, string const &name, string const &topology, std::vector< string > const &componentNames={}) const override
Push the data in the wrapper into a Conduit blueprint field.
virtual Wrapper & setDimLabels(integer const dim, Span< string const > const labels) override
Set dimension labels for an array.
Wrapper< T > & setRTTypeName(string_view rtTypeName)
override the rtType to use when parsing an input value to the wrapped object. It can be useful to cha...
Wrapper< T > & setRestartFlags(RestartFlags flags)
Set the RestartFlags of the wrapper.
virtual const std::type_info & getTypeId() const noexcept override
Get the typeid of T.
Wrapper & operator=(Wrapper &&source)
Move Assignment Operator.
virtual void copyWrapperAttributes(WrapperBase const &source) override
Copy attributes from another wrapper.
Wrapper< T > & setInputFlag(InputFlags const input)
Set the InputFlag of the wrapper.
T TYPE
Alias for the wrapped type T.
void registerToWrite() const override
Register the wrapper's data for writing with Conduit.
std::enable_if_t< !traits::is_array< U > &&DefaultValue< U >::has_default_value, Wrapper< T > & > setApplyDefaultValue(typename DefaultValue< U >::value_type const &defaultVal)
Set and apply for default value.
virtual void resize(localIndex const newSize) override
Calls T::resize(newsize) if it exists.
virtual void reserve(localIndex const newCapacity) override
Calls T::reserve( newCapacity ) if it exists, otherwise a no-op.
virtual bool isPackable(bool onDevice) const override
Check whether wrapped type is can be packed into a buffer on host or device.
Wrapper< T > & setRegisteringObjects(string const &objectName)
Add a new name to the list of groups that register this wrapper.
static Wrapper< T > const & cast(WrapperBase const &wrapper)
Downcast base to a const typed wrapper.
GEOS_DECLTYPE_AUTO_RETURN referenceAsView() const
Provide access to wrapped object converted to a view, if possible.
void setName()
DO_NOT_DOCUMENT.
T & referenceAsView()
Provide access to wrapped object converted to a view, if possible.
virtual ~Wrapper() noexcept override
Default destructor.
Wrapper(string const &name, Group &parent, std::unique_ptr< T > object)
Constructor that takes ownership of an existing object.
std::enable_if_t< traits::is_array< U > &&DefaultValue< U >::has_default_value, Wrapper< T > & > setApplyDefaultValue(typename DefaultValue< U >::value_type const &defaultVal)
Set and apply for default value.
virtual std::unique_ptr< WrapperBase > clone(string const &name, Group &parent) override
Creates a clone of *this WrapperBase.
Wrapper< T > & setPlotLevel(PlotLevel const flag)
Set the PlotLevel of the wrapper.
virtual void copy(localIndex const sourceIndex, localIndex const destIndex) override
Calls T::copy(sourceIndex, destIndex)
virtual localIndex capacity() const override
GEOS_DECLTYPE_AUTO_RETURN referenceAsView()
Provide access to wrapped object converted to a view, if possible.
GEOS_DECLTYPE_AUTO_RETURN reference() const
const Accessor for m_data
void populateMCArray(conduit::Node &node, std::vector< string > const &componentNames={}) const override
Push the data in the wrapper into a Conduit Blueprint mcarray.
virtual size_t bytesAllocated() const override final
virtual localIndex unpack(buffer_unit_type const *&buffer, bool withMetadata, bool onDevice, parallelDeviceEvents &events) override final
Unpack the entire wrapped object from a buffer.
virtual HistoryMetadata getHistoryMetadata(localIndex const packCount=-1) const override final
Get a description of the wrapped data for time history collection/output.
Wrapper(string const &name, Group &parent, T *object)
Constructor that does not take ownership of an existing object.
Wrapper< T > & setDescription(string const &description)
Set the description string of the wrapper.
virtual localIndex numArrayComp() const override
Return the number of components in a multidimensional array.
Wrapper(string const &name, Group &parent)
Constructor that creates a new instance of wrapped type.
virtual bool processInputFile(xmlWrapper::xmlNode const &targetNode, xmlWrapper::xmlNodePos const &nodePos) override
Initialize the wrapper from the input xml node.
virtual void resize(int ndims, localIndex const *const dims) override
Calls T::resize( num_dims, dims )
Wrapper< T > & appendDescription(string const &description)
Add up more text to the existing description string of the wrapper.
void finishWriting() const override
Write the wrapped data into Conduit.
virtual localIndex size() const override
Calls T::size()
virtual localIndex unpackByIndex(buffer_unit_type const *&buffer, arrayView1d< localIndex const > const &unpackIndices, bool withMetadata, bool onDevice, parallelDeviceEvents &events, MPI_Op op) override final
For indexable types, unpack selected indices of wrapped object from a buffer.
T const & referenceAsView() const
Provide access to wrapped object converted to a view, if possible.
virtual string getDefaultValueString() const override
Return a string representing the default value.
bool loadFromConduit() override
Read the wrapped data from Conduit.
DefaultValue< T > const & getDefaultValueStruct() const
Accessor for m_default.
virtual int numArrayDims() const override
Return the number of dimensions of the array.
void const * voidPointer() const override
std::enable_if_t< DefaultValue< U >::has_default_value, Wrapper< T > & > setDefaultValue(typename DefaultValue< U >::value_type const &defaultVal)
Setter for default value.
Wrapper< T > & setSizedFromParent(int val)
Set whether this wrapper is resized when its parent is resized.
virtual bool hasDefaultValue() const final override
Return true iff this wrapper has a valid default value.
std::enable_if_t< DefaultValue< U >::has_default_value, typename DefaultValue< U >::value_type const & > getDefaultValue() const
Accessor for default value.
Wrapper & operator=(Wrapper const &source)
Copy Assignment Operator.
virtual void copyWrapper(WrapperBase const &source) override
Copies the contents of a Wrapper into *this.
void erase(std::set< localIndex > const &indicesToErase) override
Calls T::erase(indicesToErase)
static Wrapper & cast(WrapperBase &wrapper)
Downcast base to a typed wrapper.
virtual Regex const & getTypeRegex() const override
virtual void copyData(WrapperBase const &source) override
Copy the the data contained in another wrapper into this wrapper.
T & reference()
Accessor for m_data.
virtual void move(LvArray::MemorySpace const space, bool const touch) const override
Calls T::move(space, touch)
std::unique_ptr< WrapperBase > averageOverSecondDim(string const &name, Group &group) const override
Create a new Wrapper with values averaged over the second dimension.
virtual localIndex elementByteSize() const override
virtual Span< string const > getDimLabels(integer const dim) const override
Get dimension labels of an array.
@ OPTIONAL
Optional in input.
@ REQUIRED
Required in input.
internal::Helper< T > DefaultValue
A templated alias to hold default values.
@ WRITE_AND_READ
Write and read from restart.
@ NO_WRITE
Do not write into restart.
std::enable_if_t< !internal::canParseVariable< T >, bool > readAttributeAsType(T &, string const &name, Regex const &, xmlNode const &, U const &)
Extract attribute in an xml tree, and translate its value into a typed variable. This SFINAE implemen...
void processInputException(std::exception const &ex, string const &targetAttributeName, xmlWrapper::xmlNode const &targetNode, xmlWrapper::xmlNodePos const &nodePos)
Helper method to process an exception that has been thrown during xml parsing.
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
LvArray::Array< T, NDIM, PERMUTATION, localIndex, LvArray::ChaiBuffer > Array
Multidimensional array type. See LvArray:Array for details.
std::string string
String type.
GEOS_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
std::int32_t integer
Signed integer type.
void erase(OrderedVariableToManyElementRelation &relation, localIndex const firstIndex, localIndex const er, localIndex const esr, localIndex const ei)
Remove an element relation from an object in the relation.
signed char buffer_unit_type
Type stored in communication buffers.
Array< T, 1 > array1d
Alias for 1D array.
std::enable_if< can_history_io< T >, HistoryMetadata >::type getHistoryMetadata(string const &name, ArrayView< T const, 1, 0 > const &arr, localIndex const numComps, localIndex sizeOverride=-1)
Produce a HistoryMetadata object for a supported one-dimensional array type.
std::string_view string_view
String type.