Skip to content

Commit

Permalink
introduced CLMemory as superclass for all memory objects.
Browse files Browse the repository at this point in the history
added CLImage, CLImage2d and CLImage3d.
  • Loading branch information
mbien committed Jan 18, 2010
1 parent c4aeea2 commit 09ac312
Show file tree
Hide file tree
Showing 16 changed files with 503 additions and 229 deletions.
4 changes: 2 additions & 2 deletions resources/cl-if.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ Ignore CL_GL_.*|cl.*GL.*
Ignore clCreateContext
CustomJavaCode CL
CustomJavaCode CL /** Interface to C language function: <br> <code> cl_context {@native clCreateContext}(intptr_t * , uint32_t, cl_device_id * , void (*pfn_notify)(const char *, const void *, size_t, void *), void *, int32_t * ); </code> */
CustomJavaCode CL public long clCreateContext(LongBuffer properties, long[] devices, CreateContextCallback pfn_notify, Object userData, IntBuffer errcode_ret);
CustomJavaCode CL public long clCreateContext(java.nio.Buffer properties, long[] devices, CreateContextCallback pfn_notify, Object userData, IntBuffer errcode_ret);

Ignore clCreateContextFromType
CustomJavaCode CL
CustomJavaCode CL /** Interface to C language function: <br> <code> cl_context {@native clCreateContextFromType}(cl_context_properties *properties, cl_device_type device_type, void (*pfn_notify)(const char *errinfo, const void *private_info, size_t cb, void *user_data), void *user_data, cl_int *errcode_ret) ; </code> */
CustomJavaCode CL public long clCreateContextFromType(LongBuffer properties, long device_type, CreateContextCallback pfn_notify, Object userData, IntBuffer errcode_ret);
CustomJavaCode CL public long clCreateContextFromType(java.nio.Buffer properties, long device_type, CreateContextCallback pfn_notify, Object userData, IntBuffer errcode_ret);

Ignore clBuildProgram
CustomJavaCode CL
Expand Down
6 changes: 3 additions & 3 deletions resources/clImplCustomCode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

public long clCreateContext(LongBuffer properties, long[] devices, CreateContextCallback pfn_notify, Object userData, IntBuffer errcode_ret) {
public long clCreateContext(java.nio.Buffer properties, long[] devices, CreateContextCallback pfn_notify, Object userData, IntBuffer errcode_ret) {

if(pfn_notify != null)
throw new RuntimeException("asynchronous execution with callback is not yet implemented, pass null through this method to block until complete.");
Expand All @@ -18,7 +18,7 @@ public long clCreateContext(LongBuffer properties, long[] devices, CreateContext
private native long clCreateContext1(Object cl_context_properties, int props_offset, int deviceCount, long[] devices, CreateContextCallback pfn_notify, Object userData, Object errcode_ret, int err_offset);


public long clCreateContextFromType(LongBuffer properties, long device_type, CreateContextCallback pfn_notify, Object userData, IntBuffer errcode_ret) {
public long clCreateContextFromType(java.nio.Buffer properties, long device_type, CreateContextCallback pfn_notify, Object userData, IntBuffer errcode_ret) {

if(pfn_notify != null)
throw new RuntimeException("asynchronous execution with callback is not yet implemented, pass null through this method to block until complete.");
Expand Down Expand Up @@ -61,4 +61,4 @@ private final static void convert32To64(long[] values) {
values[i-1] = temp>>>32;
values[i ] = temp & 0x00000000FFFFFFFFL;
}
}
}
178 changes: 15 additions & 163 deletions src/com/mbien/opencl/CLBuffer.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,31 @@
package com.mbien.opencl;

import com.sun.gluegen.runtime.BufferFactory;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;

import static com.mbien.opencl.CLException.*;
import static com.mbien.opencl.CL.*;

/**
*
* @author Michael Bien
*/
public class CLBuffer<B extends Buffer> implements CLResource {
public final class CLBuffer<B extends Buffer> extends CLMemory<B> {

public final B buffer;
public final long ID;

private final CLContext context;
private final CL cl;

CLBuffer(CLContext context, B directBuffer, int flags) {
this(context, directBuffer, 0, flags);
private CLBuffer(CLContext context, B directBuffer, long id) {
super(context, directBuffer, id);
}

CLBuffer(CLContext context, B directBuffer, int glBuffer, int flags) {
static <B extends Buffer> CLBuffer<B> create(CLContext context, B directBuffer, int flags) {
return create(context, directBuffer, flags, 0);
}

static <B extends Buffer> CLBuffer<B> create(CLContext context, B directBuffer, int flags, int glBuffer) {

if(!directBuffer.isDirect())
throw new IllegalArgumentException("buffer is not a direct buffer");

this.buffer = directBuffer;
this.context = context;
this.cl = context.cl;
CL cl = context.cl;
long id;

int[] result = new int[1];

Expand All @@ -43,162 +34,23 @@ public class CLBuffer<B extends Buffer> implements CLResource {
if(isHostPointerFlag(flags)) {
host_ptr = directBuffer;
}
this.ID = cl.clCreateBuffer(context.ID, flags,
sizeOfBufferElem(directBuffer)*directBuffer.capacity(), host_ptr, result, 0);
id = cl.clCreateBuffer(context.ID, flags, sizeOfBufferElem(directBuffer)*directBuffer.capacity(), host_ptr, result, 0);
}else{
if(isHostPointerFlag(flags)) {
throw new IllegalArgumentException(
"CL_MEM_COPY_HOST_PTR or CL_MEM_USE_HOST_PTR can not be used with OpenGL Buffers.");
}
CLGLI clgli = (CLGLI)cl;
this.ID = clgli.clCreateFromGLBuffer(context.ID, flags, glBuffer, result, 0);
id = clgli.clCreateFromGLBuffer(context.ID, flags, glBuffer, result, 0);
}
checkForError(result[0], "can not create cl buffer");

return new CLBuffer<B>(context, directBuffer, id);
}

private final boolean isHostPointerFlag(int flags) {
return (flags & CL_MEM_COPY_HOST_PTR) != 0 || (flags & CL_MEM_USE_HOST_PTR) != 0;
}

public void release() {
int ret = cl.clReleaseMemObject(ID);
context.onBufferReleased(this);
checkForError(ret, "can not release mem object");
}

//stolen from JOGL project... think about merging
private final int sizeOfBufferElem(Buffer buffer) {

if (buffer instanceof ByteBuffer) {
return BufferFactory.SIZEOF_BYTE;
} else if (buffer instanceof IntBuffer) {
return BufferFactory.SIZEOF_INT;
} else if (buffer instanceof ShortBuffer) {
return BufferFactory.SIZEOF_SHORT;
} else if (buffer instanceof FloatBuffer) {
return BufferFactory.SIZEOF_FLOAT;
} else if (buffer instanceof DoubleBuffer) {
return BufferFactory.SIZEOF_DOUBLE;
}
throw new RuntimeException("Unexpected buffer type " + buffer.getClass().getName());
}

int getSizeInBytes() {
return sizeOfBufferElem(buffer)*buffer.capacity();
}


@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final CLBuffer<?> other = (CLBuffer<?>) obj;
if (this.buffer != other.buffer && (this.buffer == null || !this.buffer.equals(other.buffer))) {
return false;
}
if (this.context.ID != other.context.ID) {
return false;
}
return true;
}

@Override
public int hashCode() {
int hash = 3;
hash = 29 * hash + (this.buffer != null ? this.buffer.hashCode() : 0);
hash = 29 * hash + (int) (this.context.ID ^ (this.context.ID >>> 32));
return hash;
}

/**
* Memory settings for configuring CLBuffers.
*/
public enum Mem {

/**
* This flag specifies that the memory object will be read and
* written by a kernel.
*/
READ_WRITE(CL_MEM_READ_WRITE),

/**
* This flags specifies that the memory object will be written
* but not read by a kernel.
* Reading from a buffer or image object created with WRITE_ONLY
* inside a kernel is undefined.
*/
WRITE_ONLY(CL_MEM_WRITE_ONLY),

/**
* This flag specifies that the memory object is a read-only memory
* object when used inside a kernel. Writing to a buffer or image object
* created withREAD_ONLY inside a kernel is undefined.
*/
READ_ONLY(CL_MEM_READ_ONLY),

/**
* Enum representing CL.CL_MEM_USE_HOST_PTR.
* If specified, it indicates that the application wants the OpenCL
* implementation to use memory referenced by host_ptr as the storage
* bits for the memory object. OpenCL implementations are allowed
* to cache the buffer contents pointed to by host_ptr in device memory.
* This cached copy can be used when kernels are executed on a device.
*/
USE_BUFFER(CL_MEM_USE_HOST_PTR),

// ALLOC_HOST_PTR(CL_MEM_ALLOC_HOST_PTR), // this is the default in java world anyway

/**
* Enum representing CL.CL_MEM_COPY_HOST_PTR.
* If CL_MEM_COPY_HOST_PTR specified, it indicates that the application
* wants the OpenCL implementation to allocate memory for the memory object
* and copy the data from memory referenced by host_ptr.<br/>
* COPY_HOST_PTR and USE_HOST_PTR are mutually exclusive.
*/
COPY_BUFFER(CL_MEM_COPY_HOST_PTR);

/**
* Value of wrapped OpenCL flag.
*/
public final int CONFIG;

private Mem(int config) {
this.CONFIG = config;
}

public static Mem valueOf(int bufferFlag) {
switch(bufferFlag) {
case(CL_MEM_READ_WRITE):
return READ_WRITE;
case(CL_MEM_READ_ONLY):
return READ_ONLY;
case(CL_MEM_USE_HOST_PTR):
return USE_BUFFER;
// case(CL_MEM_ALLOC_HOST_PTR):
// return ALLOC_HOST_PTR;
case(CL_MEM_COPY_HOST_PTR):
return COPY_BUFFER;
}
return null;
}

static int flagsToInt(Mem[] flags) {
int clFlags = 0;
if(flags != null) {
for (int i = 0; i < flags.length; i++) {
clFlags |= flags[i].CONFIG;
}
}
if(clFlags == 0)
clFlags = CL_MEM_READ_WRITE;
return clFlags;
}

public <T extends Buffer> CLBuffer<T> cloneWith(T directBuffer) {
return new CLBuffer<T>(context, directBuffer, ID);
}

}
4 changes: 2 additions & 2 deletions src/com/mbien/opencl/CLCommandQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public CLCommandQueue putWriteBuffer(CLBuffer<?> writeBuffer, boolean blockingWr

int ret = cl.clEnqueueWriteBuffer(
ID, writeBuffer.ID, blockingWrite ? CL_TRUE : CL_FALSE,
0, writeBuffer.getSizeInBytes(), writeBuffer.buffer,
0, writeBuffer.getSize(), writeBuffer.buffer,
0, null, events==null ? null : events.IDs);

if(ret != CL_SUCCESS)
Expand All @@ -77,7 +77,7 @@ public CLCommandQueue putReadBuffer(CLBuffer<?> readBuffer, boolean blockingRead

int ret = cl.clEnqueueReadBuffer(
ID, readBuffer.ID, blockingRead ? CL_TRUE : CL_FALSE,
0, readBuffer.getSizeInBytes(), readBuffer.buffer,
0, readBuffer.getSize(), readBuffer.buffer,
0, null, events==null ? null : events.IDs);

if(ret != CL_SUCCESS)
Expand Down
Loading

0 comments on commit 09ac312

Please sign in to comment.