Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/af/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ namespace af
dim_t dims(unsigned dim) const;
unsigned numdims() const;
size_t bytes() const;
size_t allocated() const;
array copy() const;
bool isempty() const;
bool isscalar() const;
Expand Down Expand Up @@ -586,6 +587,12 @@ namespace af
*/
size_t bytes() const;

/**
Get the size of the array in memory. This will return the parent's
bytes() if the array is indexed.
*/
size_t allocated() const;

/**
Perform deep copy of the array
*/
Expand Down
11 changes: 11 additions & 0 deletions include/af/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ extern "C"
AFAPI af_err af_is_owner(bool *result, const af_array arr);
#endif

#if AF_API_VERSION >= 35
/**
\param[out] bytes the size of the physical allocated bytes. This will return the size
of the parent/owner if the \p arr is an indexed array.
\param[in] arr the input array.

\ingroup internal_func_allocatedbytes
*/
AFAPI af_err af_get_allocated_bytes(size_t *bytes, const af_array arr);
#endif

#ifdef __cplusplus
}
#endif
29 changes: 29 additions & 0 deletions src/api/c/internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,32 @@ af_err af_is_owner(bool *result, const af_array arr)
CATCHALL;
return AF_SUCCESS;
}

af_err af_get_allocated_bytes(size_t *bytes, const af_array arr)
{
try {
af_dtype ty = getInfo(arr).getType();

size_t res = 0;

switch (ty) {
case f32: res = getArray<float >(arr).getAllocatedBytes(); break;
case f64: res = getArray<double >(arr).getAllocatedBytes(); break;
case c32: res = getArray<cfloat >(arr).getAllocatedBytes(); break;
case c64: res = getArray<cdouble>(arr).getAllocatedBytes(); break;
case u32: res = getArray<uint >(arr).getAllocatedBytes(); break;
case s32: res = getArray<int >(arr).getAllocatedBytes(); break;
case u64: res = getArray<uintl >(arr).getAllocatedBytes(); break;
case s64: res = getArray<intl >(arr).getAllocatedBytes(); break;
case u16: res = getArray<ushort >(arr).getAllocatedBytes(); break;
case s16: res = getArray<short >(arr).getAllocatedBytes(); break;
case b8 : res = getArray<char >(arr).getAllocatedBytes(); break;
case u8 : res = getArray<uchar >(arr).getAllocatedBytes(); break;
default: TYPE_ERROR(6, ty);
}

std::swap(*bytes, res);
}
CATCHALL;
return AF_SUCCESS;
}
9 changes: 9 additions & 0 deletions src/api/cpp/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <af/device.h>
#include <af/gfor.h>
#include <af/algorithm.h>
#include <af/internal.h>
#include "error.hpp"

namespace af
Expand Down Expand Up @@ -266,6 +267,13 @@ namespace af
return nElements * getSizeOf(type());
}

size_t array::allocated() const
{
size_t result = 0;
AF_THROW(af_get_allocated_bytes(&result, get()));
return result;
}

array array::copy() const
{
af_array other = 0;
Expand Down Expand Up @@ -597,6 +605,7 @@ namespace af
MEM_FUNC(dim4 , dims)
MEM_FUNC(unsigned , numdims)
MEM_FUNC(size_t , bytes)
MEM_FUNC(size_t , allocated)
MEM_FUNC(array , copy)
MEM_FUNC(bool , isempty)
MEM_FUNC(bool , isscalar)
Expand Down
6 changes: 6 additions & 0 deletions src/api/unified/internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ af_err af_is_owner(bool *result, const af_array arr)
CHECK_ARRAYS(arr);
return CALL(result, arr);
}

af_err af_get_allocated_bytes(size_t *bytes, const af_array arr)
{
CHECK_ARRAYS(arr);
return CALL(bytes, arr);
}
5 changes: 5 additions & 0 deletions src/backend/cpu/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ namespace cpu
data_dims = new_dims;
}

size_t getAllocatedBytes() const
{
return data_dims.elements() * sizeof(T);
}

T* device();

T* device() const
Expand Down
5 changes: 5 additions & 0 deletions src/backend/cuda/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ namespace cuda
data_dims = new_dims;
}

size_t getAllocatedBytes() const
{
return data_dims.elements() * sizeof(T);
}

T* device();

T* device() const
Expand Down
5 changes: 5 additions & 0 deletions src/backend/opencl/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ namespace opencl
data_dims = new_dims;
}

size_t getAllocatedBytes() const
{
return data_dims.elements() * sizeof(T);
}

operator Param() const
{
KParam info = {{dims()[0], dims()[1], dims()[2], dims()[3]},
Expand Down
33 changes: 33 additions & 0 deletions test/internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,36 @@ TEST(Internal, Linear)
ASSERT_EQ(isOwner(c), false);
}
}

TEST(Internal, Allocated)
{
af::array a = af::randu(10, 8);
const size_t aBytes = a.bytes();

// b is just pointing to same underlying data
// b is an owner;
af::array b = a;
ASSERT_EQ(b.allocated(), aBytes);
ASSERT_EQ(b.bytes(), aBytes);

// C is considered sub array
// C will not be an owner
af::array c = a(af::span);
ASSERT_EQ(c.allocated(), aBytes);
ASSERT_EQ(c.bytes(), aBytes);

af::array d = a.col(1);
ASSERT_EQ(d.allocated(), aBytes);
ASSERT_EQ(d.bytes(), (size_t)10 * 4);

a = af::randu(20);
b = af::randu(20);

// Even though a, b are reallocated and c, d are not owners
// the allocated and bytes should remain the same
ASSERT_EQ(c.allocated(), aBytes);
ASSERT_EQ(c.bytes(), aBytes);

ASSERT_EQ(d.allocated(), aBytes);
ASSERT_EQ(d.bytes(), (size_t)10 * 4);
}