From 81c31701bc737dbf86070d467b34f2c42b8968e1 Mon Sep 17 00:00:00 2001
From: CoreRasurae <luis.p.mendes@gmail.com>
Date: Thu, 26 Apr 2018 18:36:38 +0100
Subject: [PATCH] Update: * Support for OpenCLDevice configurator/configure API

---
 CHANGELOG.md                 |  2 ++
 CONTRIBUTORS.md              |  3 ++-
 src/cpp/JNIHelper.cpp        | 20 ++++++++++++++++++++
 src/cpp/JNIHelper.h          |  1 +
 src/cpp/invoke/OpenCLJNI.cpp |  6 ++++++
 5 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 026e608..e2d7620 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,7 +3,9 @@ jni# Aparapi jni Changelog
 ## 1.3.2
 
 * Fixed local arrays handling 1D and ND, to cope with arrays resizing across kernel executions
+* Significant speed-up on discrete GPUs with dedicated memory - OpenCLDevice.setSharedMemory(false)
 * Fixed aparapi now supports efficient execution on discrete GPU and other devices with dedicated memory
+* Support for OpenCLDevice configurator/configure API
 
 ## 1.3.1
 
diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md
index 5e51ce3..0997bd2 100644
--- a/CONTRIBUTORS.md
+++ b/CONTRIBUTORS.md
@@ -29,4 +29,5 @@ Below are some of the specific details of various contributions.
 & lgalluci for his fix for issue #121 (incorrect toString for 3D ranges) July 6th 2013
 * Luis Mendes Issue #51 JVM crash when using multi-dimensional local arrays (refs #51)
 * Luis Mendes submitted local arrays handling 1D and ND, to cope with arrays resizing across kernel executions
-* Luis Mendes submitted #107 aparapi now supports efficient execution on discrete GPU and other devices
\ No newline at end of file
+* Luis Mendes submitted #107 aparapi now supports efficient execution on discrete GPU and other devices
+* Luis Mendes submitted - Support for OpenCLDevice configurator/configure API
diff --git a/src/cpp/JNIHelper.cpp b/src/cpp/JNIHelper.cpp
index 470282e..b7cd47b 100644
--- a/src/cpp/JNIHelper.cpp
+++ b/src/cpp/JNIHelper.cpp
@@ -55,6 +55,26 @@
 #define JNI_SOURCE
 #include "JNIHelper.h"
 
+void JNIHelper::callVoidWithException(JNIEnv *jenv, jobject instance, const char *methodName){
+   try {
+      jclass theClass = jenv->GetObjectClass(instance);
+      if (theClass == NULL ||  jenv->ExceptionCheck())
+         throw std::string("bummer! getting class from instance");
+
+      jmethodID methodId= jenv->GetMethodID(theClass,methodName,"()V");
+      if (methodId == NULL || jenv->ExceptionCheck())
+         throw std::string("bummer getting method '") + methodName + "', '()V' from instance";
+
+      jenv->CallVoidMethod(instance, methodId);
+      if (jenv->ExceptionCheck())
+         throw std::string("bummer calling '") + methodName + "', '()V'";
+
+   } catch(std::string& s) {
+      jenv->ExceptionClear();
+      throw s;
+   }
+}
+
 void JNIHelper::callVoid(JNIEnv *jenv, jobject instance, const char *methodName, const char *methodSignature, ...){
    try {
       jclass theClass = jenv->GetObjectClass(instance);
diff --git a/src/cpp/JNIHelper.h b/src/cpp/JNIHelper.h
index 7166fb9..71c6b8e 100644
--- a/src/cpp/JNIHelper.h
+++ b/src/cpp/JNIHelper.h
@@ -171,6 +171,7 @@ class JNIHelper {
 
 
    public:
+      static void callVoidWithException(JNIEnv *jenv, jobject instance, const char *methodName);
       static void callVoid(JNIEnv *jenv, jobject instance, const char *methodName, const char *methodSignature, ...);
       static jlong callLong(JNIEnv *jenv, jobject instance, const char *methodName, const char *methodSignature, ...);
       static jobject callObject(JNIEnv *jenv, jobject instance, const char *methodName, const char *methodSignature, ...);
diff --git a/src/cpp/invoke/OpenCLJNI.cpp b/src/cpp/invoke/OpenCLJNI.cpp
index 00127b2..f318d7b 100644
--- a/src/cpp/invoke/OpenCLJNI.cpp
+++ b/src/cpp/invoke/OpenCLJNI.cpp
@@ -581,6 +581,12 @@ JNI_JAVA(jobject, OpenCLJNI, getPlatforms)
                         value = (char*) malloc(valueSize);
                         clGetDeviceInfo(deviceIds[deviceIdx], CL_DEVICE_NAME, valueSize, value, NULL);
                         JNIHelper::callVoid(jenv, deviceInstance, "setName", ArgsVoidReturn(StringClassArg), jenv->NewStringUTF(value));
+
+                        try {
+                            JNIHelper::callVoidWithException(jenv, deviceInstance, "configure");
+                        } catch (std::string &s) {
+                            fprintf(stderr, "Failed to call OpenClDevice.configure() - method not available in Aparapi<1.9.0\n");
+                        }
                      }
 
                   }
-- 
GitLab