Array
- Holds the data allocated inside it or references to the external data. The data are organized as one homogeneous and contiguous memory block.
- Contains information about the memory block’s size.
- Supports both immutable and mutable data.
- Provides an ability to change the data state from immutable to mutable one.
- Holds ownership information on the data (see the dm data ownership requirements section).
- Ownership information on the data can be shared between several arrays. It is possible to create a new array from another one without any data copies.
Usage example
#include <CL/sycl.hpp>
#include <iostream>
#include <string>
#include "oneapi/dal/array.hpp"
using namespace oneapi;
void print_property(const std::string& description, const auto& property) {
std::cout << description << ": " << property << std::endl;
}
int main() {
sycl::queue queue { sycl::default_selector() };
constexpr std::int64_t data_count = 4;
const float data[] = { 1.0f, 2.0f, 3.0f, 4.0f };
// Creating an array from immutable user-defined memory
auto arr_data = dal::array<float>::wrap(data, data_count);
// Creating an array from internally allocated memory filled by ones
auto arr_ones = dal::array<float>::full(queue, data_count, 1.0f);
print_property("Is arr_data mutable", arr_data.has_mutable_data()); // false
print_property("Is arr_ones mutable", arr_ones.has_mutable_data()); // true
// Creating new array from arr_data without data copy - they share ownership information.
dal::array<float> arr_mdata = arr_data;
print_property("arr_mdata elements count", arr_mdata.get_count()); // equal to data_count
print_property("Is arr_mdata mutable", arr_mdata.has_mutable_data()); // false
/// Copying data inside arr_mdata to new mutable memory block.
/// arr_data still refers to the original data pointer.
arr_mdata.need_mutable_data(queue);
print_property("Is arr_data mutable", arr_data.has_mutable_data()); // false
print_property("Is arr_mdata mutable", arr_mdata.has_mutable_data()); // true
queue.submit([&](sycl::handler& cgh){
auto mdata = arr_mdata.get_mutable_data();
auto cones = arr_ones.get_data();
cgh.parallel_for<class array_addition>(sycl::range<1>(data_count), [=](sycl::id<1> idx) {
mdata[idx[0]] += cones[idx[0]];
});
}).wait();
std::cout << "arr_mdata values: ";
for(std::int64_t i = 0; i < arr_mdata.get_count(); i++) {
std::cout << arr_mdata[i] << ", ";
}
std::cout << std::endl;
return 0;
}
Data ownership requirements
- An array owns two properties representing raw pointers to the data:
- datafor a pointer to immutable data block
- mutable_datafor a pointer to mutable data block (see the array programming interface)
- If an array owns mutable data, both properties point to the same memory block.
- If an array owns immutable data,mutable_dataisnullptr.
- An array stores the number of elements in the block it owns and updates thecountproperty when a new memory block is assigned to the array.
- An array stores a pointer to theownership structureof the data:
- Thereference countindicating how many array objects refer to the same memory block.
- Thedeleterobject used to free the memory block when reference count is zero.
- An array creates the ownership structure for a new memory block not associated with such structure.
- An array decrements the number of references to the memory block when the array goes out of the scope. If the number of references is zero, the array calls the deleter on this memory block and free the ownership structure.
- An array stores the pointer to the ownership structure created by another array when they share the data. An array increments the reference count for it to be equal to the number of array objects sharing the same data.
Programming interface
- Constructors that are used to create an array from external, mutable or immutable memory.
- Constructors and assignment operators that are used to create an array that shares its data with another one.
- The group ofreset()methods that are used to re-assign an array to another external memory block.
- The group ofreset()methods that are used to re-assign an array to an internally allocated memory block.
- The methods that are used to access the data.
- Static methods that provide simplified ways to create an array either from external memory or by allocating it within a new object.
- template<typenameT>classarray
- Template Parameters
- T– The type of the memory block elements within the array.
can represent any type.
Public Static Methods- staticarray<T>empty(std::int64_tcount)
- Allocates a new memory block for mutable data, does not initialize it, creates a new array instance by passing a pointer to the memory block. The array owns the memory block (for details, see dm data ownership requirements).
- Parameters
- count– The number of elements of type
to allocate memory for.
- Preconditions
count > 0
- Allocates a new memory block for mutable data, fills it with a scalar value, creates a new array instance by passing a pointer to the memory block. The array owns the memory block (for details, see dm data ownership requirements).
- Parameters
- count– The number of elements of type
to allocate memory for.
- element– The value that is used to fill a memory block.
- Preconditions
count > 0
Elements of type T are constructible from the Element type.
- staticarray<T>zeros(std::int64_tcount)
- Allocates a new memory block on mutable data, fills it with zeros, creates a new array instance by passing a pointer to the memory block. The array owns the memory block (for details, see dm data ownership requirements).
- Parameters
- count– The number of elements of type
to allocate memory for.
- Preconditions
count > 0
- Creates a new array instance by passing the pointer to externally-allocated memory block for mutable data. It is the responsibility of the calling application to free the memory block as the array does not free it when the reference count is zero.
- Parameters
- data– The pointer to externally-allocated memory block.
- count– The number of elements of type
in the memory block.
- Preconditions
data != nullptr
count > 0
Constructors- array()
- Creates a new instance of the class without memory allocation:mutable_dataanddatapointers should be set tonullptr,countshould be zero; the pointer to the ownership structure should be set tonullptr.
- Creates a new array instance that shares an ownership with
on its memory block.
- Movesdata,mutable_datapointers,count, and pointer to the ownership structure in
to the new array instance.
- Creates a new array instance which owns a memory block of externally-allocated mutable data. The ownership structure is created for a block, the input
is assigned to it.
- Template Parameters
- Deleter– The type of a deleter used to free the
. The deleter provides
void operator()(Data*)member function. - Parameters
- data– The pointer to externally-allocated memory block.
- count– The number of elements of type
in the memory block.
- deleter– The object used to free
.
- Creates a new array instance which owns a memory block of externally-allocated immutable data. The ownership structure is created for a block, the input
is assigned to it.
- Template Parameters
- ConstDeleter– The type of a deleter used to free the
. The deleter implements
void operator()(const Data*)member function. - Parameters
- data– The pointer to externally-allocated memory block.
- count– The number of elements of type
in the
.
- deleter– The object used to free
.
- An aliasing constructor: creates a new array instance that stores
pointer, assigns the pointer to the ownership structure of
to the new instance. Array returns
pointer as its mutable or immutable block depending on the
type.
- Parameters
- ref– The array which shares ownership structure with created one.
- data– Mutable or immutable unmanaged pointer hold by created array.
- count– The number of elements of type
in the
.
- Preconditions
std::is_same_v || std::is_same_v
Public Methods- array<T>operator=(constarray<T> &other)
- Replaces thedata,mutable_datapointers,count, and pointer to the ownership structure in the array instance by the values in
.
- Postconditions
data == other.data
mutable_data == other.mutable_data
count == other.count
- array<T>operator=(array<T> &&other)
- Swaps the values ofdata,mutable_datapointers,count, and pointer to the ownership structure in the array instance and
.
- boolhas_mutable_data()constnoexcept
- Returns whether array containsmutable_dataor not.
- array &need_mutable_data()
- Returns mutable_data, if array contains it. Otherwise, allocates a memory block for mutable data and fills it with the data stored atdata. Creates the ownership structure for allocated memory block and stores the pointer.
- Postconditions
has_mutable_data() == true
- voidreset()
- Resets ownership structure pointer tonullptr, setscountto zero,dataandmutable_datatonullptr.
- voidreset(std::int64_tcount)
- Allocates a new memory block for mutable data, does not initialize it, creates ownership structure for this block, assigns the structure inside the array. The array owns allocated memory block.
- Parameters
- count– The number of elements of type
to allocate memory for.
- Creates the ownership structure for memory block of externally-allocated mutable data, assigns input
object to it, sets and
mutable_datapointers to this block.- Template Parameters
- Deleter– The type of a deleter used to free the
. The deleter implements
void operator()(Data*)member function. - Parameters
- data– The mutable memory block pointer to be assigned inside the array.
- count– The number of elements of type
into the block.
- deleter– The object used to free
.
- template<typenameConstDeleter> voidreset(constT *data, std::int64_tcount, ConstDeleter &&deleter)
- Creates the ownership structure for memory block of externally-allocated immutable data, assigns input
object to it, sets pointer to this block.
- Template Parameters
- ConstDeleter– The type of a deleter used to free. The deleter implementsvoid operator()(const Data*)`member function.
- Parameters
- data– The immutable memory block pointer to be assigned inside the array.
- count– The number of elements of type
into the block.
- deleter– The object used to free
.
- Parameters
- ref– The array which is used to share ownership structure with current one.
- data– Mutable unmanaged pointer to be assigned to the array.
- count– The number of elements of type
in the
.
- Parameters
- ref– The array which is used to share ownership structure with current one.
- data– Immutable unmanaged pointer to be assigned to the array.
- count– The number of elements of type
in the
.
- constT &operator[](std::int64_tindex)constnoexcept
- Provides a read-only access to the elements of array. Does not perform boundary checks.
Properties- T *mutable_data
- The pointer to the memory block holding mutable data.
- Getter & Setter
T * get_mutable_data() const
- Invariants
mutable_data != nullptr if has_mutable_data() && count > 0
- constT *data
- The pointer to the memory block holding immutable data.
- Getter & Setter
const T * get_data() const noexcept
- Invariants
data != nullptr if count > 0
if has_mutable_data() == true then data == mutable_data
- std::int64_tcount
- The number of elements of type
in a memory block.
- Getter & Setter
std::int64_t get_count() const noexcept
- std::int64_tsize
- The size of memory block in bytes.
- Getter & Setter
std::int64_t get_size() const noexcept
- Invariants
size == count * sizeof(T)