简体   繁体   中英

SVM allocation in OpenCL C++

In the case of cl_context and cl::Context , we can do:

cl::Context context_ = cl::Context(device);
cl_context context = context_();

Now, I have an OpenCL program, with the following snippet in it:

...
void* svm_data = clSVMAlloc(context, svm_flags, svm_buffer_size, 0);
...

I would like to do something similar here to what we did with cl::Context above (ie extract the underlying variable from the header, or in this case, the underlying void pointer):

cl::SVMAllocator svm_data_ = cl::SVMAllocator<int, cl::SVMTraitAtomic<>>(context);
void* svm_data = svm_data_();

However, after looking through the docs , I have been unsuccessful in finding a method.

Anyone got some ideas?

Looks like you need to allocate the shared virtual memory using the allocator to get the pointer to it:

cl::SVMAllocator<int, cl::SVMTraitAtomic<>> svm_allocator(context);

std::size_t num_elements = 4;
int* svm_data = svm_allocator.allocate(num_elements);

The SVM allocator holds only the context and the flags that are passed to clSVMAlloc() and if you want to allocate the shared virtual memory, you need to call allocate() method directly. Also, if you want to deallocate the allocated memory, you need to use the allocator as well:

svm_allocator.deallocate(svm_data, num_elements);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM