Handling std::vector in target regions

Is this the expected way of as of today, creating a vector that can be used both on host and device?

extern "C" {
  void* llvm_omp_target_alloc_shared(size_t, int);

class my_resource : public std::pmr::memory_resource {
  void* do_allocate(
    std::size_t bytes,
        std::size_t alignment
    )                             override {
    auto ptr = (void*)llvm_omp_target_alloc_shared(bytes,0);

    return ptr;


  void do_deallocate(
        void*  p,
        size_t bytes,
        size_t alignment
    )                             override {

    omp_target_free(p, 0);

    bool do_is_equal(
        std::pmr::memory_resource const& other
    ) const noexcept              override {
        return this == &other;


This requires user use std::pmr::vector though, seems to be working.

You can use omp_alloc for this right now which might make it a little easier, hopefully in the future this will be standardized. E.g.

  int *shared_ptr = omp_alloc(N * sizeof(int), llvm_omp_target_shared_mem_alloc);    

  omp_free(shared_ptr, llvm_omp_target_shared_mem_alloc);    
1 Like