Skip to content

Commit 7f9e082

Browse files
committed
Draft svm_allocator for mempool
1 parent d893cd7 commit 7f9e082

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

src/wrap_mempool.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,60 @@ namespace
299299
// }}}
300300

301301

302+
// {{{ svm allocator
303+
304+
// FIXME: Does this need deferred and immediate just like the buffer-level
305+
// allocators? (I.e. can I tell whether I am out of memory just from allocations?)
306+
class svm_allocator
307+
{
308+
protected:
309+
std::shared_ptr<pyopencl::context> m_context;
310+
cl_uint m_alignment;
311+
cl_mem_flags m_flags;
312+
313+
public:
314+
svm_allocator(std::shared_ptr<pyopencl::context> const &ctx,
315+
cl_uint alignment, cl_mem_flags flags=CL_MEM_READ_WRITE)
316+
: m_context(ctx), m_alignment(alignment), m_flags(flags)
317+
{
318+
if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))
319+
throw pyopencl::error("Allocator", CL_INVALID_VALUE,
320+
"cannot specify USE_HOST_PTR or COPY_HOST_PTR flags");
321+
}
322+
323+
svm_allocator(svm_allocator const &src)
324+
: m_context(src.m_context), m_alignment(src.m_alignment),
325+
m_flags(src.m_flags)
326+
{ }
327+
328+
virtual ~svm_allocator()
329+
{ }
330+
331+
typedef void *pointer_type;
332+
typedef size_t size_type;
333+
334+
pointer_type allocate(size_type size)
335+
{
336+
if (size == 0)
337+
return nullptr;
338+
339+
PYOPENCL_PRINT_CALL_TRACE("clSVMalloc");
340+
return clSVMAlloc(m_context->data(), m_flags, size, m_alignment);
341+
}
342+
343+
void free(pointer_type p)
344+
{
345+
clSVMFree(m_context->data(), p);
346+
}
347+
348+
void try_release_blocks()
349+
{
350+
pyopencl::run_python_gc();
351+
}
352+
};
353+
354+
// }}}
355+
302356

303357

304358

0 commit comments

Comments
 (0)