171:
public AbstractArrayBase
175 typedef typename ArrayTraits<T>::ConstReferenceType ConstReferenceType;
176 typedef typename ArrayTraits<T>::IsPODType IsPODType;
201 typedef std::reverse_iterator<iterator> reverse_iterator;
202 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
231 static constexpr Int64 typeSize() {
return static_cast<Int64
>(
sizeof(T)); }
232 AllocatedMemoryInfo _currentMemoryInfo()
const
234 return AllocatedMemoryInfo(m_ptr, m_md->size * typeSize(), m_md->capacity * typeSize());
249 _createRange(0, asize, view.
data());
264 void* pre_allocated_buffer =
nullptr)
266 _directFirstAllocateWithAllocator(acapacity, o, pre_allocated_buffer);
276 _internalDeallocate();
280 if (options.allocator() != m_md->_allocator())
281 _directFirstAllocateWithAllocator(0, options);
291 operator Span<const T>()
const
293 return Span<const T>(m_ptr, m_md->
size);
295 operator SmallSpan<const T>()
const
297 return SmallSpan<const T>(m_ptr, ARCCORE_CAST_SMALL_SIZE(
size()));
303 Integer size()
const {
return ARCCORE_CAST_SMALL_SIZE(m_md->size); }
315 bool empty()
const {
return m_md->size == 0; }
319 const T* ptr = m_ptr;
320 for (
Int64 i = 0, n = m_md->size; i < n; ++i) {
332 ARCCORE_CHECK_AT(i, m_md->size);
338 ARCCORE_CHECK_AT(i, m_md->size);
347 m_md->_setMemoryLocationHint(new_hint, m_ptr,
sizeof(T));
358 m_md->m_host_device_memory_location = location;
364 return m_md->m_host_device_memory_location;
376 return !(rhs == lhs);
379 friend bool operator==(
const AbstractArray<T>& rhs,
const Span<const T>& lhs)
381 return operator==(Span<const T>(rhs), lhs);
384 friend bool operator!=(
const AbstractArray<T>& rhs,
const Span<const T>& lhs)
386 return !(rhs == lhs);
389 friend bool operator==(
const Span<const T>& rhs,
const AbstractArray<T>& lhs)
391 return operator==(rhs, Span<const T>(lhs));
394 friend bool operator!=(
const Span<const T>& rhs,
const AbstractArray<T>& lhs)
396 return !(rhs == lhs);
399 friend std::ostream& operator<<(std::ostream& o,
const AbstractArray<T>& val)
401 o << Span<const T>(val);
407 using AbstractArrayBase::m_meta_data;
421 if (new_capacity <= m_md->
capacity) {
424 if (m_meta_data.is_collective_allocator) {
440 if (_isSharedNull()) {
441 if (new_capacity != 0 || m_meta_data.is_collective_allocator)
442 _internalAllocate(new_capacity, queue);
446 Int64 acapacity = new_capacity;
447 if (compute_capacity) {
448 acapacity = m_md->capacity;
450 while (new_capacity > acapacity)
451 acapacity = (acapacity == 0) ? 4 : (acapacity + 1 + acapacity / 2);
457 if (m_meta_data.is_collective_allocator) {
458 _internalReallocate(m_md->capacity, queue);
462 _internalReallocate(acapacity, queue);
465 void _internalReallocate(
Int64 new_capacity,
RunQueue* queue)
467 if constexpr (std::is_trivially_copyable_v<T>) {
470 _directReAllocate(new_capacity, queue);
471 bool update = (new_capacity < old_capacity) || (m_ptr != old_ptr);
478 ArrayMetaData* old_md = m_md;
479 AllocatedMemoryInfo old_mem_info = _currentMemoryInfo();
480 Int64 old_size = m_md->size;
481 _directAllocate(new_capacity, queue);
482 if (m_ptr != old_ptr) {
483 for (
Int64 i = 0; i < old_size; ++i) {
484 new (m_ptr + i) T(old_ptr[i]);
487 m_md->nb_ref = old_md->nb_ref;
488 m_md->_deallocate(old_mem_info, queue);
495 void _internalDeallocate(RunQueue* queue =
nullptr)
499 if (!_isSharedNull())
500 m_md->_deallocate(_currentMemoryInfo(), queue);
501 if (m_md->is_not_null)
502 _deallocateMetaData(m_md);
504 void _internalAllocate(
Int64 new_capacity, RunQueue* queue)
506 _directAllocate(new_capacity, queue);
511 void _copyFromMemory(
const T* source)
513 m_md->_copyFromMemory(m_ptr, source,
sizeof(T),
_nullRunQueue());
526 void _directFirstAllocateWithAllocator(
Int64 new_capacity, MemoryAllocationOptions options,
527 void* pre_allocated_buffer =
nullptr)
529 IMemoryAllocator* wanted_allocator = options.allocator();
530 if (!wanted_allocator) {
531 wanted_allocator = ArrayMetaData::_defaultAllocator();
532 options.setAllocator(wanted_allocator);
535 m_md->allocation_options = options;
536 if (new_capacity > 0) {
537 if (!pre_allocated_buffer)
538 _allocateMP(new_capacity,
nullptr);
540 _setMPCast(pre_allocated_buffer);
547 void _directAllocate(
Int64 new_capacity, RunQueue* queue)
549 if (!m_md->is_not_null)
551 _allocateMP(new_capacity, queue);
554 void _allocateMP(
Int64 new_capacity, RunQueue* queue)
556 _setMPCast(m_md->_allocate(new_capacity, typeSize(), queue));
559 void _directReAllocate(
Int64 new_capacity, RunQueue* queue)
561 _setMPCast(m_md->_reallocate(_currentMemoryInfo(), new_capacity, typeSize(), queue));
566 void changeAllocator(
const MemoryAllocationOptions& options, RunQueue* queue)
568 _setMPCast(m_md->_changeAllocator(options, _currentMemoryInfo(), typeSize(), queue));
572 void changeAllocator(
const MemoryAllocationOptions& options)
574 _setMPCast(m_md->_changeAllocator(options, _currentMemoryInfo(), typeSize(),
_nullRunQueue()));
592 Int64 s = m_md->size;
593 if ((s + n) > m_md->capacity)
595 for (
Int64 i = 0; i < n; ++i)
596 new (m_ptr + s + i) T(val);
604 const T* ptr = val.
data();
605 Int64 s = m_md->size;
606 if ((s + n) > m_md->capacity)
608 _createRange(s, s + n, ptr);
615 if (m_md->nb_ref == 0) {
622 _destroyRange(0, m_md->
size, IsPODType());
624 void _destroyRange(Int64, Int64, TrueType)
628 void _destroyRange(
Int64 abegin,
Int64 aend, FalseType)
632 for (
Int64 i = abegin; i < aend; ++i)
635 void _createRangeDefault(
Int64,
Int64, TrueType)
638 void _createRangeDefault(
Int64 abegin,
Int64 aend, FalseType)
642 for (
Int64 i = abegin; i < aend; ++i)
645 void _createRange(
Int64 abegin,
Int64 aend, ConstReferenceType value, TrueType)
649 for (
Int64 i = abegin; i < aend; ++i)
652 void _createRange(
Int64 abegin,
Int64 aend, ConstReferenceType value, FalseType)
656 for (
Int64 i = abegin; i < aend; ++i)
657 new (m_ptr + i) T(value);
659 void _createRange(
Int64 abegin,
Int64 aend,
const T* values)
663 for (
Int64 i = abegin; i < aend; ++i) {
664 new (m_ptr + i) T(*values);
668 void _fill(ConstReferenceType value)
670 for (
Int64 i = 0, n =
size(); i < n; ++i)
673 void _clone(
const ThatClassType& orig_array)
675 Int64 that_size = orig_array.size();
677 m_md->size = that_size;
678 m_md->dim1_size = orig_array.m_md->dim1_size;
679 m_md->dim2_size = orig_array.m_md->dim2_size;
680 _createRange(0, that_size, orig_array.m_ptr);
682 template <
typename PodType>
683 void _resizeHelper(
Int64 s, PodType pod_type, RunQueue* queue)
687 if (s > m_md->size) {
689 this->_createRangeDefault(m_md->size, s, pod_type);
692 this->_destroyRange(s, m_md->size, pod_type);
693 if (m_meta_data.is_collective_allocator) {
699 void _resize(
Int64 s)
706 _resizeHelper(s,
TrueType{}, queue);
710 this->_destroyRange(0, m_md->
size, IsPODType());
718 if (s > m_md->size) {
720 this->_createRange(m_md->size, s, value, IsPODType());
723 this->_destroyRange(s, m_md->size, IsPODType());
724 if (m_meta_data.is_collective_allocator) {
730 void _copy(
const T* rhs_begin,
TrueType)
732 _copyFromMemory(rhs_begin);
734 void _copy(
const T* rhs_begin, FalseType)
736 for (Int64 i = 0, n = m_md->
size; i < n; ++i)
737 m_ptr[i] = rhs_begin[i];
739 void _copy(
const T* rhs_begin)
741 _copy(rhs_begin, IsPODType());
754 const T* rhs_begin = rhs.
data();
756 const Int64 current_size = m_md->size;
759 if (abegin >= rhs_begin && abegin < (rhs_begin + rhs_size))
760 ArrayMetaData::overlapError(abegin, m_md->size, rhs_begin, rhs_size);
762 if (rhs_size > current_size) {
765 this->_createRange(m_md->size, rhs_size, rhs_begin + current_size);
768 m_md->size = rhs_size;
771 this->_destroyRange(rhs_size, current_size, IsPODType{});
772 m_md->size = rhs_size;
784 void _move(ThatClassType& rhs) ARCCORE_NOEXCEPT
808 void _swap(ThatClassType& rhs) ARCCORE_NOEXCEPT
810 std::swap(m_ptr, rhs.m_ptr);
820 void _shrink(Int64 new_capacity)
825 if (new_capacity > this->
capacity())
827 if (new_capacity < 4)
845 if (asize > max_size)
847 asize =
static_cast<Integer>(max_size);
856 Span<const T> rhs_span(rhs);
857 if (rhs.allocator() == this->allocator()) {
871 void _setMP(TrueImpl* new_mp)
876 void _setMP2(TrueImpl* new_mp, ArrayMetaData* new_md)
882 if (!m_md->is_allocated_by_new)
888 return m_ptr ==
nullptr;
893 void _setToSharedNull()
896 m_meta_data = ArrayMetaData();
899 void _setMPCast(
void* p)
901 _setMP(
reinterpret_cast<TrueImpl*
>(p));