sycl::usm_allocator

Since SYCL 2020

template <typename T, sycl::usm::alloc AllocKind, size_t Alignment = 0>
class usm_allocator;

Allocator suitable for use with a C++ standard library container.

A usm_allocator enables using USM allocation for standard library containers. It is typically passed as template parameter when declaring standard library containers (e.g. vector).

Template parameters

T

Type of allocated element

AllocKind

Type of allocation, see o

Alignment

Alignment of the allocation

Example

 1#include <vector>
 2
 3#include <CL/sycl.hpp>
 4
 5using namespace sycl;
 6
 7const int size = 10;
 8
 9int main() {
10  queue q;
11
12  // USM allocator for data of type int in shared memory
13  typedef usm_allocator<int, usm::alloc::shared> vec_alloc;
14  // Create allocator for device associated with q
15  vec_alloc myAlloc(q);
16  // Create std vectors with the allocator
17  std::vector<int, vec_alloc >
18    a(size, myAlloc),
19    b(size, myAlloc),
20    c(size, myAlloc);
21
22  // Get pointer to vector data for access in kernel
23  auto A = a.data();
24  auto B = b.data();
25  auto C = c.data();
26  
27  for (int i = 0; i < size; i++) {
28    a[i] = i;
29    b[i] = i;
30    c[i] = i;
31  }
32
33  q.submit([&](handler &h) {
34      h.parallel_for(range<1>(size),
35		     [=](id<1> idx) {
36		       C[idx] = A[idx] + B[idx];
37		     });
38    }).wait();
39
40  for (int i = 0; i < size; i++) std::cout << c[i] << std::endl;
41  return 0;
42}

Member types

value_type

See also

SYCL Specification Section 4.8.4

(constructors)

usm_allocator(const sycl::context &ctxt, const sycl::device &dev) noexcept;
usm_allocator(const sycl::queue &q) noexcept;
usm_allocator(const sycl::usm_allocator &other) noexcept;
template <class U>
usm_allocator(sycl::usm_allocator<U, AllocKind, Alignment> const &) noexcept;

allocate

T *allocate(size_t Size);

Allocates memory

deallocate

void deallocate(T *Ptr, size_t size);

Deallocates memory

construct

template <
    sycl::usm::alloc AllocT = AllocKind,
    typename std::enable_if<AllocT != sycl::usm::alloc::device, int>::type = 0,
    class U, class... ArgTs>
void construct(U *Ptr, ArgTs &&... Args);
template <
    sycl::usm::alloc AllocT = AllocKind,
    typename std::enable_if<AllocT == sycl::usm::alloc::device, int>::type = 0,
    class U, class... ArgTs>
void construct(U *Ptr, ArgTs &&... Args);

Constructs an object on memory pointed by Ptr.

destroy

template <
    sycl::usm::alloc AllocT = AllocKind,
    typename std::enable_if<AllocT != sycl::usm::alloc::device, int>::type = 0>
void destroy(T *Ptr);

/// Throws an error when trying to destroy a device allocation
/// on the host
template <
    sycl::usm::alloc AllocT = AllocKind,
    typename std::enable_if<AllocT == sycl::usm::alloc::device, int>::type = 0>
void destroy(T *Ptr);

Destroys an object.

(operators)

template <class T, sycl::usm::alloc AllocKindT, size_t AlignmentT, class U,
          sycl::usm::alloc AllocKindU, size_t AlignmentU>
bool operator==(const sycl::usm_allocator<T, AllocKindT, AlignmentT> &,
                const sycl::usm_allocator<U, AllocKindU, AlignmentU> &) noexcept;
template <class T, class U, sycl::usm::alloc AllocKind, size_t Alignment = 0>
bool operator!=(const sycl:usm_allocator<T, AllocKind, Alignment> &allocT,
                const sycl::usm_allocator<U, AllocKind, Alignment> &allocU) noexcept;

Allocators only compare equal if they are of the same USM kind, alignment, context, and device (when kind is not host).