19 #ifndef GEOS_DATAREPOSITORY_WRAPPER_HPP_
20 #define GEOS_DATAREPOSITORY_WRAPPER_HPP_
25 #include "LvArray/src/limits.hpp"
27 #include "codingUtilities/SFINAE_Macros.hpp"
28 #include "LvArray/src/Macros.hpp"
29 #include "BufferOps.hpp"
30 #include "BufferOpsDevice.hpp"
32 #include "codingUtilities/traits.hpp"
35 #include "LvArray/src/system.hpp"
39 #include <type_traits>
41 #include <type_traits>
46 namespace dataRepository
55 template<
typename T >
83 if( traits::is_tensorT< T > || std::is_arithmetic< T >::value || traits::is_string< T > )
99 std::unique_ptr< T >
object ):
103 m_data( object.release() ),
106 if( traits::is_tensorT< T > || std::is_arithmetic< T >::value || traits::is_string< T > )
129 if( traits::is_tensorT< T > || std::is_arithmetic< T >::value || traits::is_string< T > )
159 m_data = source.m_data;
170 m_data = std::move( source.m_data );
182 virtual std::unique_ptr< WrapperBase >
clone(
string const & name,
183 Group & parent )
override
185 std::unique_ptr< Wrapper< T > > clonedWrapper = std::make_unique< Wrapper< T > >( name, parent, m_data );
186 clonedWrapper->copyWrapperAttributes( *
this );
187 clonedWrapper->m_isClone =
true;
188 return clonedWrapper;
202 Wrapper< T > const & castedSource = dynamicCast< Wrapper< T >
const & >( source );
203 m_ownsData = castedSource.m_ownsData;
204 m_default = castedSource.m_default;
205 m_dimLabels = castedSource.m_dimLabels;
209 virtual const std::type_info &
getTypeId() const noexcept
override
223 "Invalid downcast to Wrapper< " << LvArray::system::demangleType< T >() <<
" >" );
236 "Invalid downcast to Wrapper< " << LvArray::system::demangleType< T >() <<
" >" );
244 return wrapperHelpers::numArrayDims(
reference() );
249 return wrapperHelpers::numArrayComp(
reference() );
254 m_dimLabels.set( dim, labels );
260 return m_dimLabels.get( dim );
285 return bufferOps::can_memcpy< T >;
289 return bufferOps::is_packable< T >;
302 unpackedSize += bufferOps::Unpack( buffer, name );
309 unpackedSize += wrapperHelpers::UnpackDevice( buffer,
referenceAsView(), events );
313 unpackedSize += wrapperHelpers::UnpackDataDevice( buffer,
referenceAsView(), events );
318 unpackedSize += bufferOps::Unpack( buffer, *m_data );
330 parallelDeviceEvents & events,
331 MPI_Op op )
override final
338 unpackedSize += bufferOps::Unpack( buffer, name );
345 unpackedSize += wrapperHelpers::UnpackByIndexDevice( buffer,
referenceAsView(), unpackIndices, events, op );
349 unpackedSize += wrapperHelpers::UnpackDataByIndexDevice( buffer,
referenceAsView(), unpackIndices, events, op );
354 unpackedSize += wrapperHelpers::UnpackByIndex( buffer, *m_data, unpackIndices );
364 {
return wrapperHelpers::dataPtr( *m_data ); }
368 {
return wrapperHelpers::byteSizeOfElement< T >(); }
372 return m_isClone ? 0 : wrapperHelpers::byteSize< T >( *m_data );
386 {
return wrapperHelpers::size( *m_data ); }
391 wrapperHelpers::move( *m_data, hostMemorySpace,
true );
392 wrapperHelpers::resizeDimensions( *m_data, ndims, dims );
398 wrapperHelpers::move( *m_data, hostMemorySpace,
true );
399 wrapperHelpers::reserve(
reference(), newCapacity );
406 return wrapperHelpers::capacity( *m_data );
412 wrapperHelpers::move( *m_data, hostMemorySpace,
true );
413 wrapperHelpers::resizeDefault(
reference(), newSize, m_default );
419 template<
typename U,
int NDIM,
typename PERMUTATION >
422 LvArray::forValuesInSliceWithIndices( array[ sourceIndex ],
423 [destIndex, &array]( U
const & sourceVal,
auto const ... indicesToErase )
425 array( destIndex, indicesToErase ... ) = sourceVal;
429 template<
typename U >
433 template<
typename U=T >
434 static std::enable_if_t< traits::hasCopyAssignmentOp< U > >
435 copyData( U & destinationData, U
const & sourceData )
437 destinationData = sourceData;
440 template<
typename U=T >
441 static std::enable_if_t< !traits::hasCopyAssignmentOp< U > >
450 copy_wrapper::copy(
reference(), sourceIndex, destIndex );
457 Wrapper< T > const & castedSource = dynamicCast< Wrapper< T >
const & >( source );
458 copy_wrapper::copyData( *m_data, *castedSource.m_data );
465 template<
typename TYPE >
466 static void erase(
TYPE &, std::set< localIndex >
const & )
469 template<
typename TYPE >
472 int oldSize = array.size( 0 );
473 int numToErase = indicesToErase.size();
474 int newSize = oldSize - numToErase;
475 std::set< localIndex >::iterator it = indicesToErase.begin();
482 if( offset < numToErase )
487 array[i-offset] = array[i];
489 array.resize( newSize );
492 template<
typename TYPE >
493 static void erase( array2d< TYPE > & array, std::set< localIndex >
const & indicesToErase )
495 int oldSize = array.size( 0 );
496 int numToErase = indicesToErase.size();
497 int newSize = oldSize - numToErase;
498 int dim1 = array.size( 1 );
499 std::set< localIndex >::iterator it = indicesToErase.begin();
506 if( offset < numToErase )
511 for(
int j=0; j<dim1; j++ )
513 array[i-offset][j] = array[i][j];
516 array.resize( newSize );
519 template<
typename TYPE >
520 static void erase( array3d< TYPE > & array, std::set< localIndex >
const & indicesToErase )
522 int oldSize = array.size( 0 );
523 int numToErase = indicesToErase.size();
524 int newSize = oldSize - numToErase;
525 int dim1 = array.size( 1 );
526 int dim2 = array.size( 2 );
527 std::set< localIndex >::iterator it = indicesToErase.begin();
534 if( offset < numToErase )
539 for(
int j=0; j<dim1; j++ )
541 for(
int k=0; k<dim2; k++ )
543 array[i-offset][j][k] = array[i][j][k];
547 array.resize( newSize );
554 void erase( std::set< localIndex >
const & indicesToErase )
override
556 GEOS_ERROR_IF( indicesToErase.size() == 0,
"Wrapper::erase() can only be called on a populated set of indices!" );
562 virtual void move( LvArray::MemorySpace
const space,
bool const touch )
const override
563 {
return wrapperHelpers::move( *m_data, space, touch ); }
599 template<
typename _T=T,
typename=std::enable_if_t< traits::HasMemberFunction_toView< _T > > >
601 {
return m_data->toView(); }
606 template<
typename _T=T,
typename=std::enable_if_t< !traits::HasMemberFunction_toView< _T > > >
613 template<
typename _T=T,
typename=std::enable_if_t< traits::HasMemberFunction_toView< _T > > >
615 {
return m_data->toViewConst(); }
620 template<
typename _T=T,
typename=std::enable_if_t< !traits::HasMemberFunction_toView< _T > > >
636 return m_default.has_default_value;
643 template<
typename U=T >
655 template<
typename U=T >
656 std::enable_if_t< DefaultValue< U >::has_default_value,
typename DefaultValue< U >::value_type
const & >
659 return m_default.value;
667 template<
typename U=T >
668 std::enable_if_t< DefaultValue< U >::has_default_value,
Wrapper< T > & >
671 m_default.value = defaultVal;
680 template<
typename U=T >
684 m_default.value = defaultVal;
685 *m_data = m_default.value;
694 template<
typename U=T >
698 m_default.value = defaultVal;
699 m_data->template setValues< serialPolicy >( m_default.value );
708 std::ostringstream ss;
729 GEOS_FMT(
"XML Node {} ({}) with name={} is missing required attribute '{}'."
730 "Available options are:\n {}\n For more details, please refer to documentation at:\n"
731 "http://geosx-geosx.readthedocs-hosted.com/en/latest/docs/sphinx/userGuide/Index.html",
732 targetNode.name(), nodePos.
toString(), targetNode.attribute(
"name" ).value(),
745 catch( std::exception
const & ex )
773 string const & topology,
774 std::vector< string >
const & componentNames = {} )
const override
775 { wrapperHelpers::addBlueprintField(
reference(), fields, name, topology, componentNames ); }
778 void populateMCArray( conduit::Node & node, std::vector< string >
const & componentNames = {} )
const override
779 { wrapperHelpers::populateMCArray(
reference(), node, componentNames ); }
784 auto ptr = wrapperHelpers::averageOverSecondDim(
reference() );
785 using U =
typename decltype( ptr )::element_type;
787 GEOS_ERROR_IF( ptr ==
nullptr,
"Failed to average over the second dimension of." );
789 auto ret = std::make_unique< Wrapper< U > >( name, group, std::move( ptr ) );
808 move( hostMemorySpace,
false );
812 wrapperHelpers::pushDataToConduitNode( *m_data,
m_conduitNode );
831 wrapperHelpers::pullDataFromConduitNode( *m_data,
m_conduitNode );
916 #if defined(USE_TOTALVIEW_OUTPUT)
917 virtual string totalviewTypeName()
const override
919 return LvArray::system::demangle(
typeid(
Wrapper< T > ).name() );
922 virtual int setTotalviewDisplay()
const override
925 WrapperBase::setTotalviewDisplay();
926 TV_ttf_add_row(
"m_ownsData",
"bool", &m_ownsData );
927 TV_ttf_add_row(
"m_data", LvArray::system::demangle< T >().c_str(), m_data );
928 TV_ttf_add_row(
"m_default", LvArray::system::demangle<
DefaultValue< T > >().c_str(), &m_default );
934 #if defined(GEOSX_USE_PYGEOSX)
935 virtual PyObject * createPythonObject( )
override
936 {
return wrapperHelpers::createPythonObject(
reference() ); }
954 template<
bool DO_PACKING >
958 parallelDeviceEvents & events )
const
963 { packedSize += bufferOps::Pack< DO_PACKING >( buffer,
getName() ); }
968 packedSize += wrapperHelpers::PackDevice< DO_PACKING >( buffer,
reference(), events );
972 packedSize += wrapperHelpers::PackDataDevice< DO_PACKING >( buffer,
reference(), events );
977 packedSize += bufferOps::Pack< DO_PACKING >( buffer, *m_data );
996 template<
bool DO_PACKING >
998 arrayView1d< localIndex const >
const & packList,
1001 parallelDeviceEvents & events )
const
1006 { packedSize += bufferOps::Pack< DO_PACKING >( buffer,
getName() ); }
1011 packedSize += wrapperHelpers::PackByIndexDevice< DO_PACKING >( buffer,
reference(), packList, events );
1015 packedSize += wrapperHelpers::PackDataByIndexDevice< DO_PACKING >( buffer,
reference(), packList, events );
1020 packedSize += wrapperHelpers::PackByIndex< DO_PACKING >( buffer, *m_data, packList );
1032 parallelDeviceEvents & events )
const override final
1034 return this->packImpl< true >( buffer, withMetadata, onDevice, events );
1041 arrayView1d< localIndex const >
const & packList,
1044 parallelDeviceEvents & events )
const override final
1046 return this->packByIndexImpl< true >( buffer, packList, withMetadata, onDevice, events );
1052 localIndex packSizePrivate(
bool withMetadata,
1054 parallelDeviceEvents & events )
const override final
1057 return this->packImpl< false >( dummy, withMetadata, onDevice, events );
1063 localIndex packByIndexSizePrivate( arrayView1d< localIndex const >
const & packList,
1066 parallelDeviceEvents & events )
const override final
1069 return this->packByIndexImpl< false >( dummy, packList, withMetadata, onDevice, events );
1082 DefaultValue< T > m_default;
1085 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.
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 )
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.
Static class to manage the type selection of types at runtime and obtain the regexes of these types....
@ 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.
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.
GEOSX_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
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.
The regular expression data for validating inputs. Use rtTypes to get the regex of a type,...