Skip to content

Commit d465242

Browse files
authored
Remove m_singleParameterResizeIndex from ArrayView to reduce memory. (#311)
1 parent 24d8d5c commit d465242

12 files changed

Lines changed: 65 additions & 234 deletions

docs/sphinx/Array.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Resizing a ``LvArray::Array``
6969

7070
It is important to note that unless the array being resized is one dimensional the resize methods above do not preserve the values in the array. That is if you have a two dimensional array ``A`` of size :math:`M \times N` and you resize it to :math:`P \times Q` using any of the methods above then you cannot rely on ``A( i, j )`` having the same value it did before the resize.
7171

72-
There is also a method ``resize`` which takes a single parameter and will resize the dimension given by ``getSingleParameterResizeIndex``. Unlike the previous methods this will preserve the values in the array. By default the first dimension is resized but you can choose the dimension with ``setSingleParameterResizeIndex``.
72+
There is also a method ``resize`` which takes a single parameter and will resize the first dimension of the array.
7373

7474
.. literalinclude:: ../../examples/exampleArray.cpp
7575
:language: c++

docs/sphinx/developmentAids.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Example below demonstrates the difference between pretty printer output and raw
6363
(gdb) p dofNumber
6464
$1 = const geos::arrayView1d & of size [10] = {gdb_view = {0, 5, 10, 15, 20, 25, 30, 35, 40, 45}}
6565
(gdb) p /r dofNumber
66-
$2 = (const geos::arrayView1d &) @0x7ffcda2a8ab0: {static NDIM = 1, static USD = 0, m_dims = {data = {10}}, m_strides = {data = {1}}, m_dataBuffer = {static hasShallowCopy = <optimized out>, m_pointer = 0x55bec1de4860, m_capacity = 10, m_pointerRecord = 0x55bec1dfa5c0}, m_singleParameterResizeIndex = 0}
66+
$2 = (const geos::arrayView1d &) @0x7ffcda2a8ab0: {static NDIM = 1, static USD = 0, m_dims = {data = {10}}, m_strides = {data = {1}}, m_dataBuffer = {static hasShallowCopy = <optimized out>, m_pointer = 0x55bec1de4860, m_capacity = 10, m_pointerRecord = 0x55bec1dfa5c0}}
6767
6868
This is how the variable is viewed in a debugging session in CLion IDE:
6969

@@ -86,7 +86,6 @@ This is how the variable is viewed in a debugging session in CLion IDE:
8686
m_dims = {LvArray::typeManipulation::CArray<long, 1>}
8787
m_strides = {LvArray::typeManipulation::CArray<long, 1>}
8888
m_dataBuffer = {LvArray::ChaiBuffer<long long const>}
89-
m_singleParameterResizeIndex = {int} 0
9089
9190
.. warning::
9291

examples/exampleArray.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -221,24 +221,6 @@ TEST( Array, resizeSingleDimension )
221221
}
222222
}
223223
}
224-
225-
// Shrink the second dimension from 6 to 3;
226-
array.setSingleParameterResizeIndex( 1 );
227-
array.resize( 3 );
228-
for( std::ptrdiff_t i = 0; i < array.size( 0 ); ++i )
229-
{
230-
for( std::ptrdiff_t j = 0; j < array.size( 1 ); ++j )
231-
{
232-
if( i < 5 )
233-
{
234-
EXPECT_EQ( array( i, j ), 6 * i + j );
235-
}
236-
else
237-
{
238-
EXPECT_EQ( array( i, j ), 0 );
239-
}
240-
}
241-
}
242224
}
243225
// Sphinx end before resizeSingleDimension
244226

src/Array.hpp

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ class Array : public ArrayView< T,
180180
this->m_strides[ i ] = rhs.m_strides[ i ];
181181
}
182182

183-
setSingleParameterResizeIndex( rhs.getSingleParameterResizeIndex() );
184183
return *this;
185184
}
186185

@@ -202,7 +201,6 @@ class Array : public ArrayView< T,
202201
this->m_strides[ i ] = strides[ i ];
203202
}
204203

205-
setSingleParameterResizeIndex( rhs.getSingleParameterResizeIndex() );
206204
return *this;
207205
}
208206

@@ -438,52 +436,38 @@ class Array : public ArrayView< T,
438436
}
439437

440438
/**
441-
* @brief Resize the default dimension of the Array.
442-
* @param newdim the new size of the default dimension.
439+
* @brief Resize the first dimension of the Array.
440+
* @param newdim the new size of the first dimension.
443441
* @note This preserves the values in the Array.
444-
* @note The default dimension is given by m_singleParameterResizeIndex.
445442
*/
446443
LVARRAY_HOST_DEVICE
447444
void resize( INDEX_TYPE const newdim )
448445
{ resizeDefaultDimension( newdim ); }
449446

450447
/**
451-
* @brief Resize the default dimension of the Array.
452-
* @param newdim the new size of the default dimension.
448+
* @brief Resize the first dimension of the Array.
449+
* @param newdim the new size of the first dimension.
453450
* @param defaultValue the value to initialize the new values with.
454451
* @note This preserves the values in the Array.
455-
* @note The default dimension is given by m_singleParameterResizeIndex.
456452
*/
457453
LVARRAY_HOST_DEVICE
458454
void resizeDefault( INDEX_TYPE const newdim, T const & defaultValue )
459455
{ resizeDefaultDimension( newdim, defaultValue ); }
460456

461457
/**
462458
* @brief Sets the size of the Array to zero and destroys all the values.
463-
* @details Sets the size of m_singleParameterResizeIndex to 0 but leaves the
459+
* @details Sets the size of the first dimension to 0 but leaves the
464460
* size of the other dimensions untouched. Equivalent to resize( 0 ).
465461
*/
466462
void clear()
467463
{
468464
bufferManipulation::resize( this->m_dataBuffer, this->size(), 0 );
469465

470-
this->m_dims[ this->getSingleParameterResizeIndex() ] = 0;
466+
this->m_dims[ 0 ] = 0;
471467

472468
this->m_strides = indexing::calculateStrides< PERMUTATION >( this->m_dims );
473469
}
474470

475-
/**
476-
* @brief Set the default resize dimension.
477-
* @param index The new default dimension.
478-
*/
479-
LVARRAY_HOST_DEVICE
480-
inline void setSingleParameterResizeIndex( int const index )
481-
{
482-
LVARRAY_ERROR_IF_LT( index, 0 );
483-
LVARRAY_ERROR_IF_GE( index, NDIM );
484-
this->m_singleParameterResizeIndex = index;
485-
}
486-
487471
///@}
488472

489473
/**
@@ -608,7 +592,6 @@ class Array : public ArrayView< T,
608592
* @param newDimLength the new size of the default dimension.
609593
* @param args arguments to initialize the new values with.
610594
* @note This preserves the values in the Array.
611-
* @note The default dimension is given by m_singleParameterResizeIndex.
612595
*/
613596
DISABLE_HD_WARNING
614597
template< typename ... ARGS >
@@ -617,25 +600,25 @@ class Array : public ArrayView< T,
617600
{
618601
LVARRAY_ERROR_IF_LT( newDimLength, 0 );
619602

620-
// If m_singleParameterResizeIndex is the first dimension in memory than a simple 1D resizing is sufficient. The
603+
// If the first dimension in memory is 0 then a simple 1D resizing is sufficient. The
621604
// check if NDIM == 1 is to give the compiler compile time knowledge that this path is always taken for 1D arrays.
622-
if( NDIM == 1 || typeManipulation::asArray( PERMUTATION {} )[ 0 ] == this->m_singleParameterResizeIndex )
605+
if( NDIM == 1 || typeManipulation::asArray( PERMUTATION {} )[ 0 ] == 0 )
623606
{
624607
INDEX_TYPE const oldSize = this->size();
625-
this->m_dims[ this->m_singleParameterResizeIndex ] = newDimLength;
608+
this->m_dims[ 0 ] = newDimLength;
626609
this->m_strides = indexing::calculateStrides< PERMUTATION >( this->m_dims );
627610

628611
bufferManipulation::resize( this->m_dataBuffer, oldSize, this->size(), std::forward< ARGS >( args )... );
629612
return;
630613
}
631614

632615
// Get the current length and stride of the dimension as well as the size of the whole Array.
633-
INDEX_TYPE const curDimLength = this->m_dims[ this->m_singleParameterResizeIndex ];
634-
INDEX_TYPE const curDimStride = this->m_strides[ this->m_singleParameterResizeIndex ];
616+
INDEX_TYPE const curDimLength = this->m_dims[ 0 ];
617+
INDEX_TYPE const curDimStride = this->m_strides[ 0 ];
635618
INDEX_TYPE const curSize = this->size();
636619

637620
// Set the size of the dimension, recalculate the strides and get the new total size.
638-
this->m_dims[ this->m_singleParameterResizeIndex ] = newDimLength;
621+
this->m_dims[ 0 ] = newDimLength;
639622
this->m_strides = indexing::calculateStrides< PERMUTATION >( this->m_dims );
640623

641624
INDEX_TYPE const newSize = this->size();

src/ArrayView.hpp

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ class ArrayView
131131
ArrayView( ArrayView const & source ) noexcept:
132132
m_dims{ source.m_dims },
133133
m_strides{ source.m_strides },
134-
m_dataBuffer{ source.m_dataBuffer, source.size() },
135-
m_singleParameterResizeIndex( source.m_singleParameterResizeIndex )
134+
m_dataBuffer{ source.m_dataBuffer, source.size() }
136135
{}
137136

138137
/**
@@ -155,8 +154,7 @@ class ArrayView
155154
explicit ArrayView( ArrayView< U, NDIM, USD, INDEX_TYPE, BUFFER_TYPE > const & source ):
156155
m_dims{ source.dimsArray() },
157156
m_strides{ source.stridesArray() },
158-
m_dataBuffer{ source.dataBuffer() },
159-
m_singleParameterResizeIndex( source.getSingleParameterResizeIndex() )
157+
m_dataBuffer{ source.dataBuffer() }
160158
{
161159
m_dims[ USD ] = typeManipulation::convertSize< T, U >( m_dims[ USD ] );
162160

@@ -191,18 +189,15 @@ class ArrayView
191189
* @brief Construct a new ArrayView from existing components.
192190
* @param dims The array of dimensions.
193191
* @param strides The array of strides.
194-
* @param singleParameterResizeIndex The single parameter resize index.
195192
* @param buffer The buffer to copy construct.
196193
*/
197194
inline LVARRAY_HOST_DEVICE constexpr explicit
198195
ArrayView( typeManipulation::CArray< INDEX_TYPE, NDIM > const & dims,
199196
typeManipulation::CArray< INDEX_TYPE, NDIM > const & strides,
200-
int const singleParameterResizeIndex,
201197
BUFFER_TYPE< T > const & buffer ):
202198
m_dims( dims ),
203199
m_strides( strides ),
204-
m_dataBuffer( buffer ),
205-
m_singleParameterResizeIndex( singleParameterResizeIndex )
200+
m_dataBuffer( buffer )
206201
{}
207202

208203
/// The default destructor.
@@ -231,7 +226,6 @@ class ArrayView
231226
ArrayView & operator=( ArrayView && rhs )
232227
{
233228
m_dataBuffer = std::move( rhs.m_dataBuffer );
234-
m_singleParameterResizeIndex = rhs.m_singleParameterResizeIndex;
235229
for( int i = 0; i < NDIM; ++i )
236230
{
237231
m_dims[ i ] = rhs.m_dims[ i ];
@@ -250,7 +244,6 @@ class ArrayView
250244
ArrayView & operator=( ArrayView const & rhs ) noexcept
251245
{
252246
m_dataBuffer = rhs.m_dataBuffer;
253-
m_singleParameterResizeIndex = rhs.m_singleParameterResizeIndex;
254247
for( int i = 0; i < NDIM; ++i )
255248
{
256249
m_dims[ i ] = rhs.m_dims[ i ];
@@ -272,7 +265,7 @@ class ArrayView
272265
*/
273266
inline LVARRAY_HOST_DEVICE constexpr
274267
ArrayView toView() const &
275-
{ return ArrayView( m_dims, m_strides, m_singleParameterResizeIndex, m_dataBuffer ); }
268+
{ return ArrayView( m_dims, m_strides, m_dataBuffer ); }
276269

277270
/**
278271
* @return Return a new ArrayView where @c T is @c const.
@@ -282,7 +275,6 @@ class ArrayView
282275
{
283276
return ViewTypeConst( m_dims,
284277
m_strides,
285-
m_singleParameterResizeIndex,
286278
m_dataBuffer );
287279
}
288280

@@ -440,13 +432,6 @@ class ArrayView
440432
INDEX_TYPE capacity() const
441433
{ return LvArray::integerConversion< INDEX_TYPE >( m_dataBuffer.capacity() ); }
442434

443-
/**
444-
* @return Return the default resize dimension.
445-
*/
446-
LVARRAY_HOST_DEVICE inline constexpr
447-
int getSingleParameterResizeIndex() const
448-
{ return m_singleParameterResizeIndex; }
449-
450435
/**
451436
* @tparam INDICES A variadic pack of integral types.
452437
* @return Return the linear index from a multidimensional index.
@@ -710,7 +695,6 @@ class ArrayView
710695
TV_ttf_add_row( "m_dims", totalview::format< INDEX_TYPE, int >( 1, &ndim ).c_str(), (av->m_dims) );
711696
TV_ttf_add_row( "m_strides", totalview::format< INDEX_TYPE, int >( 1, &ndim ).c_str(), (av->m_strides) );
712697
TV_ttf_add_row( "m_dataBuffer", LvArray::system::demangle< BUFFER_TYPE< T > >().c_str(), &(av->m_dataBuffer) );
713-
TV_ttf_add_row( "m_singleParameterResizeIndex", "int", &(av->m_singleParameterResizeIndex) );
714698
}
715699
return 0;
716700
}
@@ -744,8 +728,7 @@ class ArrayView
744728
ArrayView( BUFFER_TYPE< T > && buffer ) noexcept:
745729
m_dims{ 0 },
746730
m_strides{ 0 },
747-
m_dataBuffer{ std::move( buffer ) },
748-
m_singleParameterResizeIndex{ 0 }
731+
m_dataBuffer{ std::move( buffer ) }
749732
{}
750733

751734
/// the dimensions of the array.
@@ -756,10 +739,6 @@ class ArrayView
756739

757740
/// this data member contains the actual data for the array.
758741
BUFFER_TYPE< T > m_dataBuffer;
759-
760-
/// this data member specifies the dimension that will be resized as a result of a call to the
761-
/// single dimension resize method.
762-
int m_singleParameterResizeIndex = 0;
763742
};
764743

765744
/**

src/MallocBuffer.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,15 @@ class MallocBuffer : public bufferManipulation::VoidBuffer
131131
{
132132
LVARRAY_ERROR_IF_NE( space, MemorySpace::host );
133133

134+
#if (defined(__GNUC__) && (__GNUC__ == 8))
135+
// This is a workaround for a GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87544
136+
std::size_t constexpr maxSize = (NumericLimits< std::size_t >::max / sizeof( T )) >> 1;
137+
std::size_t newSpaceSize = math::min( static_cast< std::size_t >(newCapacity), maxSize ) * sizeof( T );
138+
#else
139+
std::size_t newSpaceSize = newCapacity * sizeof( T );
140+
#endif
134141
// TODO: If std::is_trivially_copyable_v< T > then we could use std::realloc.
135-
T * const newPtr = reinterpret_cast< T * >( std::malloc( newCapacity * sizeof( T ) ) );
142+
T * const newPtr = reinterpret_cast< T * >( std::malloc( newSpaceSize ) );
136143

137144
std::ptrdiff_t const overlapAmount = math::min( newCapacity, size );
138145
arrayManipulation::uninitializedMove( newPtr, overlapAmount, m_data );

src/python/PyArray.cpp

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -90,43 +90,6 @@ static PyObject * PyArray_repr( PyObject * const obj )
9090
return PyUnicode_FromString( repr.c_str() );
9191
}
9292

93-
static constexpr char const * PyArray_getSingleParameterResizeIndexDocString =
94-
"get_single_parameter_resize_index(self)\n"
95-
"--\n\n"
96-
"Return the default resize dimension.\n";
97-
static PyObject * PyArray_getSingleParameterResizeIndex( PyArray * const self, PyObject * const args )
98-
{
99-
LVARRAY_UNUSED_VARIABLE( args );
100-
VERIFY_NON_NULL_SELF( self );
101-
VERIFY_INITIALIZED( self );
102-
103-
return PyLong_FromLongLong( self->array->getSingleParameterResizeIndex() );
104-
}
105-
106-
static constexpr char const * PyArray_setSingleParameterResizeIndexDocString =
107-
"set_single_parameter_resize_index(self, dim)\n"
108-
"--\n\n"
109-
"Set the default resize dimension.";
110-
static PyObject * PyArray_setSingleParameterResizeIndex( PyArray * const self, PyObject * const args )
111-
{
112-
VERIFY_NON_NULL_SELF( self );
113-
VERIFY_INITIALIZED( self );
114-
VERIFY_RESIZEABLE( self );
115-
116-
int dim;
117-
if( !PyArg_ParseTuple( args, "i", &dim ) )
118-
{
119-
return nullptr;
120-
}
121-
122-
PYTHON_ERROR_IF( dim < 0 || dim >= self->array->ndim(), PyExc_ValueError,
123-
"argument out of bounds", nullptr );
124-
125-
self->array->setSingleParameterResizeIndex( dim );
126-
127-
Py_RETURN_NONE;
128-
}
129-
13093
static constexpr char const * PyArray_resizeDocString =
13194
"resize(self, size)\n"
13295
"--\n\n"
@@ -247,8 +210,6 @@ static PyMemberDef PyArray_members[] = {
247210
};
248211

249212
static PyMethodDef PyArray_methods[] = {
250-
{ "get_single_parameter_resize_index", (PyCFunction) PyArray_getSingleParameterResizeIndex, METH_NOARGS, PyArray_getSingleParameterResizeIndexDocString },
251-
{ "set_single_parameter_resize_index", (PyCFunction) PyArray_setSingleParameterResizeIndex, METH_VARARGS, PyArray_setSingleParameterResizeIndexDocString },
252213
{ "resize", (PyCFunction) PyArray_resize, METH_VARARGS, PyArray_resizeDocString },
253214
{ "resize_all", (PyCFunction) PyArray_resizeAll, METH_VARARGS, PyArray_resizeAllDocString },
254215
{ "to_numpy", (PyCFunction) PyArray_toNumPy, METH_VARARGS, PyArray_toNumPyDocString },

src/python/PyArray.hpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,6 @@ class PyArrayWrapperBase
8383
*/
8484
virtual int ndim() const = 0;
8585

86-
/**
87-
* @brief Return the single parameter resize index of the Array.
88-
* @return the single parameter resize index of the array.
89-
* @note This wraps Array::getSingleParameterResizeIndex().
90-
*/
91-
virtual int getSingleParameterResizeIndex() const = 0;
92-
93-
/**
94-
* @brief Set the single parameter resize index of the Array.
95-
* @param dim The dimension to make the single parameter resize index.
96-
* @note This wraps Array::setSingleParameterResizeIndex( int ).
97-
*/
98-
virtual void setSingleParameterResizeIndex( int const dim ) const = 0;
99-
10086
/**
10187
* @brief Resize the default dimension of the Array.
10288
* @param newSize The new size of the default dimension.
@@ -172,14 +158,6 @@ class PyArrayWrapper final : public PyArrayWrapperBase
172158
virtual int ndim() const final override
173159
{ return NDIM; }
174160

175-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
176-
virtual int getSingleParameterResizeIndex() const final override
177-
{ return m_array.getSingleParameterResizeIndex(); }
178-
179-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
180-
virtual void setSingleParameterResizeIndex( int const dim ) const final override
181-
{ m_array.setSingleParameterResizeIndex( dim ); }
182-
183161
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
184162
virtual void resize( long long const newSize ) final override
185163
{ m_array.resize( integerConversion< INDEX_TYPE >( newSize ) ); }

unitTests/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ set( testSources
4040
testArray_copyAssignmentOperator.cpp
4141
testArray_copyConstructor.cpp
4242
testArray_defaultConstructor.cpp
43-
testArray_getSetSingleParameterResizeIndex.cpp
4443
testArray_indexing.cpp
4544
testArray_moveAssignmentOperator.cpp
4645
testArray_moveConstructor.cpp

0 commit comments

Comments
 (0)