Commit 28879b5d authored by Mohaned's avatar Mohaned

Merge branch 'master' of https://github.com/aparapi/aparapi

parents dce00f59 6319738b
......@@ -422,10 +422,10 @@ void updateBuffer(JNIEnv* jenv, JNIContext* jniContext, KernelArg* arg, int& arg
fprintf(stderr, "runKernel arg %d %s, length = %d\n", argIdx, arg->name, buffer->lens[i]);
}
argPos++;
status = clSetKernelArg(jniContext->kernel, argPos, sizeof(cl_uint), &(buffer->dims[i]));
if(status != CL_SUCCESS) throw CLException(status,"clSetKernelArg (buffer dimension)");
status = clSetKernelArg(jniContext->kernel, argPos, sizeof(cl_uint), &(buffer->offsets[i]));
if(status != CL_SUCCESS) throw CLException(status,"clSetKernelArg (buffer offset)");
if (config->isVerbose()){
fprintf(stderr, "runKernel arg %d %s, dim = %d\n", argIdx, arg->name, buffer->dims[i]);
fprintf(stderr, "runKernel arg %d %s, offsets = %d\n", argIdx, arg->name, buffer->offsets[i]);
}
}
}
......@@ -469,7 +469,7 @@ void processArray(JNIEnv* jenv, JNIContext* jniContext, KernelArg* arg, int& arg
arg->pin(jenv);
if (config->isVerbose()) {
fprintf(stderr, "runKernel: arrayOrBuf ref %p, oldAddr=%p, newAddr=%p, ref.mem=%p isCopy=%s\n",
fprintf(stderr, "runKernel: array ref %p, oldAddr=%p, newAddr=%p, ref.mem=%p isCopy=%s\n",
arg->arrayBuffer->javaArray,
prevAddr,
arg->arrayBuffer->addr,
......@@ -528,8 +528,11 @@ void processBuffer(JNIEnv* jenv, JNIContext* jniContext, KernelArg* arg, int& ar
arg->aparapiBuffer->write.valid = false;
}
// TODO: check if the object was moved and required re-flatten
arg->aparapiBuffer->flatten(jenv,arg);
if (config->isVerbose()) {
fprintf(stderr, "runKernel: arrayOrBuf addr=%p, ref.mem=%p\n",
fprintf(stderr, "runKernel: Buf addr=%p, ref.mem=%p\n",
arg->aparapiBuffer->data,
arg->aparapiBuffer->mem);
fprintf(stderr, "at memory addr %p, contents: ", arg->aparapiBuffer->data);
......
......@@ -70,10 +70,12 @@ private:
return (type&com_amd_aparapi_internal_jni_KernelRunnerJNI_ARG_SHORT);
}
void buildBuffer(void* _data, cl_uint* _dims, cl_uint _numDims, long _lengthInBytes, jobject _javaObject);
public:
jobject javaObject; // The java array that this arg is mapped to
cl_uint numDims; // sizes of dimensions of the object (array lengths for ND arrays)
cl_uint* dims; // sizes of offsets of the object (first element offset in ND arrays)
cl_uint numDims; // number of dimensions of the object (array lengths for ND arrays)
cl_uint* offsets; // offsets of the next element in ND arrays)
cl_uint* lens; // sizes of dimensions of the object (array lengths for ND arrays)
jint lengthInBytes; // bytes in the array or directBuf
cl_mem mem; // the opencl buffer
......@@ -87,25 +89,25 @@ public:
void deleteBuffer(KernelArg* arg);
static AparapiBuffer* flatten(JNIEnv *env, jobject arg, int type);
static AparapiBuffer* flattenBoolean2D(JNIEnv *env, jobject arg);
static AparapiBuffer* flattenChar2D(JNIEnv *env, jobject arg);
static AparapiBuffer* flattenByte2D(JNIEnv *env, jobject arg);
static AparapiBuffer* flattenShort2D(JNIEnv *env, jobject arg);
static AparapiBuffer* flattenInt2D(JNIEnv *env, jobject arg);
static AparapiBuffer* flattenLong2D(JNIEnv *env, jobject arg);
static AparapiBuffer* flattenFloat2D(JNIEnv *env, jobject arg);
static AparapiBuffer* flattenDouble2D(JNIEnv *env, jobject arg);
static AparapiBuffer* flattenBoolean3D(JNIEnv *env, jobject arg);
static AparapiBuffer* flattenChar3D(JNIEnv *env, jobject arg);
static AparapiBuffer* flattenByte3D(JNIEnv *env, jobject arg);
static AparapiBuffer* flattenShort3D(JNIEnv *env, jobject arg);
static AparapiBuffer* flattenInt3D(JNIEnv *env, jobject arg);
static AparapiBuffer* flattenLong3D(JNIEnv *env, jobject arg);
static AparapiBuffer* flattenFloat3D(JNIEnv *env, jobject arg);
static AparapiBuffer* flattenDouble3D(JNIEnv *env, jobject arg);
void flatten(JNIEnv *env, KernelArg* arg);
void flattenBoolean2D(JNIEnv *env, KernelArg* arg);
void flattenChar2D(JNIEnv *env, KernelArg* arg);
void flattenByte2D(JNIEnv *env, KernelArg* arg);
void flattenShort2D(JNIEnv *env, KernelArg* arg);
void flattenInt2D(JNIEnv *env, KernelArg* arg);
void flattenLong2D(JNIEnv *env, KernelArg* arg);
void flattenFloat2D(JNIEnv *env, KernelArg* arg);
void flattenDouble2D(JNIEnv *env, KernelArg* arg);
void flattenBoolean3D(JNIEnv *env, KernelArg* arg);
void flattenChar3D(JNIEnv *env, KernelArg* arg);
void flattenByte3D(JNIEnv *env, KernelArg* arg);
void flattenShort3D(JNIEnv *env, KernelArg* arg);
void flattenInt3D(JNIEnv *env, KernelArg* arg);
void flattenLong3D(JNIEnv *env, KernelArg* arg);
void flattenFloat3D(JNIEnv *env, KernelArg* arg);
void flattenDouble3D(JNIEnv *env, KernelArg* arg);
void inflate(JNIEnv *env, KernelArg* arg);
......
......@@ -21,8 +21,7 @@ JNIContext::JNIContext(JNIEnv *jenv, jobject _kernelObject, jobject _openCLDevic
deviceId = OpenCLDevice::getDeviceId(jenv, openCLDeviceObject);
cl_device_type returnedDeviceType;
clGetDeviceInfo(deviceId, CL_DEVICE_TYPE, sizeof(returnedDeviceType), &returnedDeviceType, NULL);
//fprintf(stderr, "device[%d] CL_DEVICE_TYPE = %x\n", deviceId, returnedDeviceType);
//fprintf(stderr, "device[%p] CL_DEVICE_TYPE = %x\n", deviceId, returnedDeviceType);
cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platformId, 0 };
cl_context_properties* cprops = (NULL == platformId) ? NULL : cps;
......@@ -34,7 +33,7 @@ JNIContext::JNIContext(JNIEnv *jenv, jobject _kernelObject, jobject _openCLDevic
}
void JNIContext::dispose(JNIEnv *jenv, Config* config) {
//fprintf(stdout, "dispose()\n");
//fprintf(stderr, "dispose()\n");
cl_int status = CL_SUCCESS;
jenv->DeleteGlobalRef(kernelObject);
jenv->DeleteGlobalRef(kernelClass);
......@@ -69,21 +68,41 @@ void JNIContext::dispose(JNIEnv *jenv, Config* config) {
for (int i=0; i< argc; i++){
KernelArg *arg = args[i];
if (!arg->isPrimitive()){
if (arg->arrayBuffer != NULL){
if (arg->arrayBuffer->mem != 0){
if (config->isTrackingOpenCLResources()){
memList.remove((cl_mem)arg->arrayBuffer->mem, __LINE__, __FILE__);
if (arg->isArray()) {
if (arg->arrayBuffer != NULL){
if (arg->arrayBuffer->mem != 0){
if (config->isTrackingOpenCLResources()){
memList.remove((cl_mem)arg->arrayBuffer->mem, __LINE__, __FILE__);
}
status = clReleaseMemObject((cl_mem)arg->arrayBuffer->mem);
//fprintf(stdout, "dispose arg %d %0lx\n", i, arg->arrayBuffer->mem);
CLException::checkCLError(status, "clReleaseMemObject()");
arg->arrayBuffer->mem = (cl_mem)0;
}
if (arg->arrayBuffer->javaArray != NULL) {
jenv->DeleteWeakGlobalRef((jweak) arg->arrayBuffer->javaArray);
}
status = clReleaseMemObject((cl_mem)arg->arrayBuffer->mem);
//fprintf(stdout, "dispose arg %d %0lx\n", i, arg->arrayBuffer->mem);
CLException::checkCLError(status, "clReleaseMemObject()");
arg->arrayBuffer->mem = (cl_mem)0;
delete arg->arrayBuffer;
arg->arrayBuffer = NULL;
}
if (arg->arrayBuffer->javaArray != NULL) {
jenv->DeleteWeakGlobalRef((jweak) arg->arrayBuffer->javaArray);
} else if (arg->isAparapiBuffer()) {
if (arg->aparapiBuffer != NULL){
if (arg->aparapiBuffer->mem != 0){
if (config->isTrackingOpenCLResources()){
memList.remove((cl_mem)arg->aparapiBuffer->mem, __LINE__, __FILE__);
}
status = clReleaseMemObject((cl_mem)arg->aparapiBuffer->mem);
//fprintf(stdout, "dispose arg %d %0lx\n", i, arg->aparapiBuffer->mem);
CLException::checkCLError(status, "clReleaseMemObject()");
arg->aparapiBuffer->mem = (cl_mem)0;
}
if (arg->aparapiBuffer->javaObject != NULL) {
jenv->DeleteWeakGlobalRef((jweak) arg->aparapiBuffer->javaObject);
}
delete arg->aparapiBuffer;
arg->aparapiBuffer = NULL;
}
delete arg->arrayBuffer;
arg->arrayBuffer = NULL;
}
}
if (arg->name != NULL){
......@@ -93,7 +112,7 @@ void JNIContext::dispose(JNIEnv *jenv, Config* config) {
jenv->DeleteGlobalRef((jobject) arg->javaArg);
}
delete arg; arg=args[i]=NULL;
}
} // for
delete[] args; args=NULL;
// do we need to call clReleaseEvent on any of these that are still retained....
......
......@@ -35,7 +35,7 @@ KernelArg::KernelArg(JNIEnv *jenv, JNIContext *jniContext, jobject argObj):
if (isArray()){
arrayBuffer = new ArrayBuffer();
} else if(isAparapiBuffer()) {
aparapiBuffer = AparapiBuffer::flatten(jenv, argObj, type);
aparapiBuffer = new AparapiBuffer();
}
}
......
......@@ -64,7 +64,7 @@ class KernelArg{
static jfieldID javaArrayFieldID;
public:
JNIContext *jniContext;
jobject argObj; // the Java KernelRunner.KernelArg object that we are mirroring.
jobject argObj; // the Java KernelRunner.KernelArg object that we are mirroring. Do not use it outside constructor due to GC. Use javaArg instead.
jobject javaArg; // global reference to the corresponding java KernelArg object we grabbed our own global reference so that the object won't be collected until we dispose!
char *name; // used for debugging printfs
jint type; // a bit mask determining the type of this arg
......
......@@ -986,6 +986,34 @@ public class KernelRunner extends KernelRunnerJNI{
arg.setArray(newArrayRef);
assert arg.getArray() != null : "null array ref";
} else if ((arg.getType() & ARG_APARAPI_BUFFER) != 0) {
// TODO: check if the 2D/3D array is changed.
// can Arrays.equals help?
needsSync = true; // Always need syn
Object buffer = new Object();
try {
buffer = arg.getField().get(kernel);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
int numDims = arg.getNumDims();
Object subBuffer = buffer;
int[] dims = new int[numDims];
for (int d = 0; d < numDims - 1; d++) {
dims[d] = Array.getLength(subBuffer);
subBuffer = Array.get(subBuffer, 0);
}
dims[numDims - 1] = Array.getLength(subBuffer);
arg.setDims(dims);
int primitiveSize = getPrimitiveSize(arg.getType());
int totalElements = 1;
for (int d = 0; d < numDims; d++) {
totalElements *= dims[d];
}
arg.setJavaBuffer(buffer);
arg.setSizeInBytes(totalElements * primitiveSize);
arg.setArray(buffer);
}
} catch (final IllegalArgumentException e) {
e.printStackTrace();
......@@ -1619,53 +1647,32 @@ public class KernelRunner extends KernelRunnerJNI{
while (type.getName().charAt(numDims) == '[') {
numDims++;
}
Object buffer = new Object();
try {
buffer = arg.getField().get(kernel);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
arg.setJavaBuffer(buffer);
arg.setNumDims(numDims);
Object subBuffer = buffer;
int[] dims = new int[numDims];
for (int i = 0; i < numDims - 1; i++) {
dims[i] = Array.getLength(subBuffer);
subBuffer = Array.get(subBuffer, 0);
arg.setJavaBuffer(null); // will get updated in updateKernelArrayRefs
arg.setArray(null); // will get updated in updateKernelArrayRefs
Class<?> elementType = arg.getField().getType();
while (elementType.isArray()) {
elementType = elementType.getComponentType();
}
dims[numDims - 1] = Array.getLength(subBuffer);
arg.setDims(dims);
if (subBuffer.getClass().isAssignableFrom(float[].class)) {
if (elementType.isAssignableFrom(float.class)) {
arg.setType(arg.getType() | ARG_FLOAT);
}
if (subBuffer.getClass().isAssignableFrom(int[].class)) {
} else if (elementType.isAssignableFrom(int.class)) {
arg.setType(arg.getType() | ARG_INT);
}
if (subBuffer.getClass().isAssignableFrom(boolean[].class)) {
} else if (elementType.isAssignableFrom(boolean.class)) {
arg.setType(arg.getType() | ARG_BOOLEAN);
}
if (subBuffer.getClass().isAssignableFrom(byte[].class)) {
} else if (elementType.isAssignableFrom(byte.class)) {
arg.setType(arg.getType() | ARG_BYTE);
}
if (subBuffer.getClass().isAssignableFrom(char[].class)) {
} else if (elementType.isAssignableFrom(char.class)) {
arg.setType(arg.getType() | ARG_CHAR);
}
if (subBuffer.getClass().isAssignableFrom(double[].class)) {
} else if (elementType.isAssignableFrom(double.class)) {
arg.setType(arg.getType() | ARG_DOUBLE);
}
if (subBuffer.getClass().isAssignableFrom(long[].class)) {
} else if (elementType.isAssignableFrom(long.class)) {
arg.setType(arg.getType() | ARG_LONG);
}
if (subBuffer.getClass().isAssignableFrom(short[].class)) {
} else if (elementType.isAssignableFrom(short.class)) {
arg.setType(arg.getType() | ARG_SHORT);
}
int primitiveSize = getPrimitiveSize(arg.getType());
int totalElements = 1;
for (int i = 0; i < numDims; i++) {
totalElements *= dims[i];
}
arg.setSizeInBytes(totalElements * primitiveSize);
}
private final Set<Object> puts = new HashSet<Object>();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment