Intel® oneAPI Data Parallel C++
Support for Intel® oneAPI DPC++ Compiler, Intel® oneAPI DPC++ Library, Intel ICX Compiler , Intel® DPC++ Compatibility Tool, and GDB*
584 Discussions

Multi-dimensional data to USM Allocator class

Suraj
Beginner
1,521 Views

Hello

I am trying to pass a vector of array to the USM allocator class and am trying to follow the syntax given in one of the webinars which goes like:
usm_allocator<int, usm::alloc::shared> alloc(q.get_context(), q.get_device());
std::vector<int, decltype(alloc)> v(size, initial value, alloc);

I have attached the code I am trying this in.  I am getting the output but also an error as follows:

Device: Intel(R) Gen9
terminate called after throwing an instance of 'cl::sycl::runtime_error'
what(): Native API failed. Native API returns: -30 (CL_INVALID_VALUE) -30 (CL_INVALID_VALUE)
Aborted (core dumped)

System spec: Ubuntu 18.04.4 LTS, Intel® HD Graphics 630 (Kaby Lake GT2)
oneAPI version beta 07, running on default L0 BE

Please kindly advise on the error and syntax I have used for the vector of array passed to USM alloc class.

Labels (1)
0 Kudos
1 Solution
AbhishekD_Intel
Moderator
1,505 Views

Hi Suraj,

 

Your syntax of vector allocation is correct. In your code(attached) you are trying to free a double-pointer which is not allocated by any of the SYCL allocation function so there will not be any SYCL context associated with the pointer. This is the reason for the error.

You can directly clear the vector of array instead of freeing the pointer to the vector.

Please find the below code for more details.

#include <CL/sycl.hpp>
#include <vector>
#include <iostream>
#include <array>

using namespace sycl;
static const int N = 5;

int main(){

  gpu_selector device_selector;

  queue q(device_selector);

  std::cout << "Device: " << q.get_device().get_info<info::device::name>() << std::endl;

  auto ctx = q.get_context();
  usm_allocator<std::array<double,3>, usm::alloc::shared> alloc(q.get_context(), q.get_device());
  std::vector<std::array<double,3>,decltype(alloc) > arrVec(N,alloc);

  //std::array<std::vector<double>,3> arrVec({std::vector<double>(5,alloc),std::vector<double>(11), std::vector<double>(12)});

  for(int i=0; i<N; i++)
    for(int j=0; j<3; j++)
      arrVec[i][j] = (i+1)*10 + j + 1;

  double** ptr0 = (double**)(&arrVec[0][0]);


  {
   q.submit([&](handler &h) {
       h.parallel_for(range<2>(N,3), [=] (id<2> i){
           // ptr[i] *= ptr1[i];
           ptr0[i[0]][i[1]] += 1;
         });
       }).wait_and_throw();
  }


  std::cout<<"O/P"<<std::endl;
  for(int i=0; i<N; i++)
    std::cout << arrVec[i][0] <<" "<< arrVec[i][1] << std::endl;

  arrVec.clear();

  return 0;
}

 

 

 

Warm Regards,

Abhishek

 

View solution in original post

0 Kudos
3 Replies
AbhishekD_Intel
Moderator
1,506 Views

Hi Suraj,

 

Your syntax of vector allocation is correct. In your code(attached) you are trying to free a double-pointer which is not allocated by any of the SYCL allocation function so there will not be any SYCL context associated with the pointer. This is the reason for the error.

You can directly clear the vector of array instead of freeing the pointer to the vector.

Please find the below code for more details.

#include <CL/sycl.hpp>
#include <vector>
#include <iostream>
#include <array>

using namespace sycl;
static const int N = 5;

int main(){

  gpu_selector device_selector;

  queue q(device_selector);

  std::cout << "Device: " << q.get_device().get_info<info::device::name>() << std::endl;

  auto ctx = q.get_context();
  usm_allocator<std::array<double,3>, usm::alloc::shared> alloc(q.get_context(), q.get_device());
  std::vector<std::array<double,3>,decltype(alloc) > arrVec(N,alloc);

  //std::array<std::vector<double>,3> arrVec({std::vector<double>(5,alloc),std::vector<double>(11), std::vector<double>(12)});

  for(int i=0; i<N; i++)
    for(int j=0; j<3; j++)
      arrVec[i][j] = (i+1)*10 + j + 1;

  double** ptr0 = (double**)(&arrVec[0][0]);


  {
   q.submit([&](handler &h) {
       h.parallel_for(range<2>(N,3), [=] (id<2> i){
           // ptr[i] *= ptr1[i];
           ptr0[i[0]][i[1]] += 1;
         });
       }).wait_and_throw();
  }


  std::cout<<"O/P"<<std::endl;
  for(int i=0; i<N; i++)
    std::cout << arrVec[i][0] <<" "<< arrVec[i][1] << std::endl;

  arrVec.clear();

  return 0;
}

 

 

 

Warm Regards,

Abhishek

 

0 Kudos
Suraj
Beginner
1,499 Views

Thank you very much. I understand. Silly mistake on my part. The issue is resolved for me. 

0 Kudos
AbhishekD_Intel
Moderator
1,482 Views

Hi Suraj,


Thank you for the confirmation, please post a new thread if you have any other issues.


Warm Regards,

Abhishek


0 Kudos
Reply