Allocating host memory

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Allocating host memory

Emily Leiviskä
I'm trying to allocated pinned memory (floats) on the host as per page 10 of https://www.nvidia.com/content/cudazone/CUDABrowser/downloads/papers/NVIDIA_OpenCL_BestPracticesGuide.pdf .

So I want to get a direct FloatBuffer that is backed by memory allocated from clCreateBuffer with CL_MEM_ALLOC_HOST_PTR.

I scoured through the source of CLContext and found that all createXBuffer calls eventually boil down to:

(1)
    public final CLBuffer<?> createBuffer(final int size, final int flags) {
        final CLBuffer<?> buffer = CLBuffer.create(this, size, flags);
        memoryObjects.add(buffer);
        return buffer;
    }

or

(2)
    public final  CLBuffer createBuffer(final B directBuffer, final int flags) {
        final CLBuffer buffer = CLBuffer.create(this, directBuffer, flags);
        memoryObjects.add(buffer);
        return buffer;
    }

My understanding is that (1) will get me a CLBuffer which has a null Buffer, so this is not what I want. And (2) will not work with a null argument for directBuffer as it calls CLBuffer.create:

(3)
    static  CLBuffer create(final CLContext context, final B directBuffer, final int flags) {

        if(!directBuffer.isDirect()) <---------------------------------------------------- NullPointerException here
            throw new IllegalArgumentException("buffer is not direct");

        B host_ptr = null;
        if(isHostPointerFlag(flags)) {
            host_ptr = directBuffer;
        }

        final CLBufferBinding binding = context.getPlatform().getBufferBinding();
        final int[] result = new int[1];
        final int size = Buffers.sizeOfBufferElem(directBuffer) * directBuffer.capacity();
        final long id = binding.clCreateBuffer(context.ID, flags, size, host_ptr, result, 0);
        CLException.checkForError(result[0], "can not create cl buffer");

        return new CLBuffer(context, directBuffer, size, id, flags);
    }

As a side note I don't think that (2) or (3) should accept Mem.ALLOCATE_BUFFER as the buffer passed is already allocated... or am I missing something?

How would I go about getting a NIO buffer into a memory block allocated by clCreateBuffer with CL_MEM_ALLOC_HOST_PTR?
Reply | Threaded
Open this post in threaded view
|

Re: Allocating host memory

gouessej
Administrator