diff --git a/src/chai/ManagedArray.hpp b/src/chai/ManagedArray.hpp index 467494e0..55abbd7f 100644 --- a/src/chai/ManagedArray.hpp +++ b/src/chai/ManagedArray.hpp @@ -134,6 +134,8 @@ class ManagedArray : public CHAICopyable /*! * \brief Allocate data for the ManagedArray in the specified space. * + * Once a ManagedArray is allocated, it will have a valid resource manager. + * * \param elems Number of elements to allocate. * \param space Execution space in which to allocate data. * \param cback User defined callback for memory events (alloc, free, move) @@ -289,6 +291,7 @@ class ManagedArray : public CHAICopyable * \brief Return the value of element i in the ManagedArray. * ExecutionSpace space to the current one * + * \pre ManagedArray must be allocated * \param index The index of the element to be fetched * \param space The index of the element to be fetched * \return The value of the i-th element in the ManagedArray. @@ -299,6 +302,7 @@ class ManagedArray : public CHAICopyable /*! * \brief Set the value of element i in the ManagedArray to be val. * + * \pre ManagedArray must be allocated * \param index The index of the element to be set * \param val Source location of the value * \tparam T The type of data value in ManagedArray. diff --git a/src/chai/ManagedArray.inl b/src/chai/ManagedArray.inl index c769be56..967b2669 100644 --- a/src/chai/ManagedArray.inl +++ b/src/chai/ManagedArray.inl @@ -25,7 +25,6 @@ CHAI_HOST_DEVICE ManagedArray::ManagedArray(): m_is_slice(false) { #if !defined(CHAI_DEVICE_COMPILE) - m_resource_manager = ArrayManager::getInstance(); m_pointer_record = &ArrayManager::s_null_record; #endif } @@ -37,6 +36,7 @@ ManagedArray::ManagedArray( std::initializer_list allocators): ManagedArray() { + m_resource_manager = ArrayManager::getInstance(); m_pointer_record = new PointerRecord(); int i = 0; for (int s = CPU; s < NUM_EXECUTION_SPACES; ++s) { @@ -169,6 +169,9 @@ CHAI_HOST void ManagedArray::allocate( const UserCallback& cback) { if(!m_is_slice) { + if (m_resource_manager == nullptr) { + m_resource_manager = ArrayManager::getInstance(); + } if (elems > 0) { CHAI_LOG(Debug, "Allocating array of size " << elems << " in space " << space); @@ -300,6 +303,9 @@ template CHAI_INLINE CHAI_HOST void ManagedArray::reset() { + if (m_resource_manager == nullptr) { + return; + } m_resource_manager->resetTouch(m_pointer_record); } @@ -312,6 +318,9 @@ CHAI_HOST_DEVICE size_t ManagedArray::size() const { template CHAI_INLINE CHAI_HOST void ManagedArray::registerTouch(ExecutionSpace space) { + if (m_resource_manager == nullptr) { + return; + } if (m_active_pointer && (m_pointer_record == nullptr || m_pointer_record == &ArrayManager::s_null_record)) { CHAI_LOG(Warning,"registerTouch called on ManagedArray with nullptr pointer record."); m_pointer_record = m_resource_manager->makeManaged((void *)m_active_base_pointer,m_size,space,true); @@ -556,7 +565,7 @@ ManagedArray::operator= (std::nullptr_t) { m_offset = 0; #if !defined(CHAI_DEVICE_COMPILE) m_pointer_record = &ArrayManager::s_null_record; - m_resource_manager = ArrayManager::getInstance(); + m_resource_manager = nullptr; #else m_pointer_record = nullptr; m_resource_manager = nullptr;