Skip to content

Commit

Permalink
Draft svm_allocator for mempool
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Jun 30, 2022
1 parent 8db28d6 commit 01be96d
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/wrap_mempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,60 @@ namespace
// }}}


// {{{ svm allocator

// FIXME: Does this need deferred and immediate just like the buffer-level
// allocators? (I.e. can I tell whether I am out of memory just from allocations?)
class svm_allocator
{
protected:
std::shared_ptr<pyopencl::context> m_context;
cl_uint m_alignment;
cl_mem_flags m_flags;

public:
svm_allocator(std::shared_ptr<pyopencl::context> const &ctx,
cl_uint alignment, cl_mem_flags flags=CL_MEM_READ_WRITE)
: m_context(ctx), m_alignment(alignment), m_flags(flags)
{
if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))
throw pyopencl::error("Allocator", CL_INVALID_VALUE,
"cannot specify USE_HOST_PTR or COPY_HOST_PTR flags");
}

svm_allocator(svm_allocator const &src)
: m_context(src.m_context), m_alignment(src.m_alignment),
m_flags(src.m_flags)
{ }

virtual ~svm_allocator()
{ }

typedef void *pointer_type;
typedef size_t size_type;

pointer_type allocate(size_type size)
{
if (size == 0)
return nullptr;

PYOPENCL_PRINT_CALL_TRACE("clSVMalloc");
return clSVMAlloc(m_context->data(), m_flags, size, m_alignment);
}

void free(pointer_type p)
{
clSVMFree(m_context->data(), p);
}

void try_release_blocks()
{
pyopencl::run_python_gc();
}
};

// }}}




Expand Down

0 comments on commit 01be96d

Please sign in to comment.