├── .gitignore ├── CMakeLists.txt ├── LICENSE.TXT ├── README.md ├── Vagrantfile ├── pom.xml └── src ├── main ├── java │ └── org │ │ └── jocl │ │ ├── BuildProgramFunction.java │ │ ├── CL.java │ │ ├── CLException.java │ │ ├── CreateContextFunction.java │ │ ├── EnqueueNativeKernelFunction.java │ │ ├── EventCallbackFunction.java │ │ ├── LibInitializer.java │ │ ├── LibTracker.java │ │ ├── LibUtils.java │ │ ├── MemObjectDestructorCallbackFunction.java │ │ ├── NativePointerObject.java │ │ ├── Pointer.java │ │ ├── PrintfCallbackFunction.java │ │ ├── SVMFreeFunction.java │ │ ├── Sizeof.java │ │ ├── cl_abstract_properties.java │ │ ├── cl_buffer_region.java │ │ ├── cl_command_queue.java │ │ ├── cl_context.java │ │ ├── cl_context_properties.java │ │ ├── cl_device_id.java │ │ ├── cl_device_partition_property.java │ │ ├── cl_event.java │ │ ├── cl_image_desc.java │ │ ├── cl_image_format.java │ │ ├── cl_kernel.java │ │ ├── cl_mem.java │ │ ├── cl_pipe_properties.java │ │ ├── cl_platform_id.java │ │ ├── cl_program.java │ │ ├── cl_queue_properties.java │ │ ├── cl_sampler.java │ │ └── cl_sampler_properties.java └── native │ ├── CLFunctions.cpp │ ├── CLFunctions.hpp │ ├── FunctionPointerUtils.cpp │ ├── FunctionPointerUtils.hpp │ ├── FunctionPointerUtils_Linux.cpp │ ├── FunctionPointerUtils_Win.cpp │ ├── JOCL.cpp │ ├── JOCL.hpp │ └── Sizeof.cpp └── test └── java └── org └── jocl └── test ├── JOCLAbstractTest.java ├── JOCLBasicTest.java ├── JOCLMinimalPlatformTest.java ├── SizeofTest.java └── TestNonBlockingConstraints.java /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | JOCL.ncb 3 | JOCL.sln 4 | JOCL.suo 5 | JOCL.vcproj.* 6 | nativeLibraries/ 7 | JOCL.build/ 8 | build/ 9 | .*.sw[po] 10 | /.project 11 | /.classpath 12 | /.settings 13 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) 2 | 3 | ############################################################################# 4 | # Overriding the CMake flags to use static runtime libraries 5 | # See http://www.cmake.org/Wiki/CMake_FAQ# 6 | # How_can_I_build_my_MSVC_application_with_a_static_runtime.3F 7 | set(CMAKE_USER_MAKE_RULES_OVERRIDE 8 | ${CMAKE_CURRENT_SOURCE_DIR}/../JOCLCommon/CMake_c_flag_overrides.cmake) 9 | set(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX 10 | ${CMAKE_CURRENT_SOURCE_DIR}/../JOCLCommon/CMake_cxx_flag_overrides.cmake) 11 | 12 | project(JOCL) 13 | 14 | ############################################################################# 15 | # Add the JNI and OpenGL dependencies 16 | if(NOT ANDROID) 17 | find_package(JNI REQUIRED) 18 | find_package(OpenGL REQUIRED) 19 | link_directories(${OpenGL_LIBRARY_DIRS}) 20 | include_directories(${OpenGL_INCLUDE_DIR}) 21 | endif() 22 | 23 | # Add the JOCLCommon project as a dependency 24 | add_subdirectory(../JOCLCommon 25 | ${CMAKE_CURRENT_BINARY_DIR}/JOCLCommon) 26 | 27 | ############################################################################# 28 | # Set the variables that are later used to build the name of the native 29 | # library, e.g. "JOCL-0_2_0-windows-x86_64.dll" 30 | 31 | set(JOCL_VERSION "2_0_5") 32 | 33 | if(ANDROID) 34 | set(JOCL_TARGET_OS "android") 35 | elseif(CMAKE_HOST_WIN32) 36 | set(JOCL_TARGET_OS "windows") 37 | elseif(CMAKE_HOST_APPLE) 38 | set(JOCL_TARGET_OS "apple") 39 | set(CMAKE_SKIP_RPATH FALSE) 40 | elseif(CMAKE_HOST_UNIX) 41 | set(JOCL_TARGET_OS "linux") 42 | endif() 43 | 44 | if(ANDROID) 45 | # Possible ANDROID_ABI values: 46 | # { 47 | # armeabi, armeabi-v7a, 48 | # armeabi-v7a with NEON, armeabi-v7a with VFPV3, armeabi-v6 with VFP, 49 | # arm64-v8a, 50 | # x86, x86_64, 51 | # mips, mips64 52 | # } 53 | # TODO: Support more than just "arm" in LibUtils 54 | if("${ANDROID_ABI}" MATCHES "^armeabi.*") 55 | set(JOCL_TARGET_ARCH "arm") 56 | elseif("${ANDROID_ABI}" MATCHES "^arm64.*") 57 | set(JOCL_TARGET_ARCH "arm64") 58 | else() 59 | set(JOCL_TARGET_ARCH ANDROID_ABI) 60 | endif() 61 | elseif(CMAKE_OSX_ARCHITECTURES STREQUAL "arm64") 62 | set(JOCL_TARGET_ARCH "arm64") 63 | elseif(CMAKE_SIZEOF_VOID_P EQUAL 8) 64 | set(JOCL_TARGET_ARCH "x86_64") 65 | else() 66 | set(JOCL_TARGET_ARCH "x86") 67 | endif() 68 | 69 | 70 | ############################################################################# 71 | # Compiler settings 72 | 73 | if(MSVC) 74 | set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Wall /wd4514 /wd4820 /wd4710 /wd4711 /wd4350 /wd4668") 75 | endif() 76 | 77 | set(BUILD_SHARED_LIBS ON) 78 | 79 | 80 | ############################################################################# 81 | # Output directories 82 | 83 | if(ANDROID) 84 | set(JOCL_OUTPUT_DIR nativeLibraries/${ANDROID_ABI}) 85 | else() 86 | set(JOCL_OUTPUT_DIR nativeLibraries) 87 | endif() 88 | 89 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/${JOCL_OUTPUT_DIR}/Debug) 90 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/${JOCL_OUTPUT_DIR}) 91 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/${JOCL_OUTPUT_DIR}) 92 | 93 | 94 | ############################################################################# 95 | # Define the include directories and source files 96 | 97 | include_directories( 98 | src/main/native 99 | src/main/include/ 100 | ${JOCLCommon_SOURCE_DIR}/src/main/include 101 | ${JOCLCommon_SOURCE_DIR}/src/main/native 102 | ${JNI_INCLUDE_DIRS} 103 | ) 104 | 105 | add_library(JOCL_${JOCL_VERSION}-${JOCL_TARGET_OS}-${JOCL_TARGET_ARCH} 106 | src/main/native/JOCL.cpp 107 | src/main/native/CLFunctions.cpp 108 | src/main/native/FunctionPointerUtils.cpp 109 | src/main/native/FunctionPointerUtils_Linux.cpp 110 | src/main/native/FunctionPointerUtils_Win.cpp 111 | src/main/native/Sizeof.cpp 112 | ) 113 | 114 | target_link_libraries( 115 | JOCL_${JOCL_VERSION}-${JOCL_TARGET_OS}-${JOCL_TARGET_ARCH} 116 | JOCLCommon) 117 | 118 | ############################################################################# 119 | # Enable C++11 features 120 | set_property(TARGET JOCL_${JOCL_VERSION}-${JOCL_TARGET_OS}-${JOCL_TARGET_ARCH} PROPERTY CXX_STANDARD 11) 121 | -------------------------------------------------------------------------------- /LICENSE.TXT: -------------------------------------------------------------------------------- 1 | JOCL - Java bindings for OpenCL 2 | 3 | Copyright (c) 2009-2016 Marco Hutter - http://www.jocl.org 4 | 5 | Permission is hereby granted, free of charge, to any person 6 | obtaining a copy of this software and associated documentation 7 | files (the "Software"), to deal in the Software without 8 | restriction, including without limitation the rights to use, 9 | copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the 11 | Software is furnished to do so, subject to the following 12 | conditions: 13 | 14 | The above copyright notice and this permission notice shall be 15 | included in all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 19 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 | OTHER DEALINGS IN THE SOFTWARE. 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JOCL 2 | 3 | JOCL - Java Bindings for OpenCL - http://jocl.org 4 | 5 | ## Maven Coordinates 6 | 7 | JOCL is available in [Maven Central](http://search.maven.org/#search|ga|1|g%3A%22org.jocl%22) under the following coordinates: 8 | 9 | 10 | org.jocl 11 | jocl 12 | 2.0.5 13 | 14 | 15 | 16 | 17 | ## Build instructions 18 | 19 | In order to build JOCL, create a local working directory, e.g. 20 | `C:\JOCLRoot`, and clone the required repositories into this 21 | directory: 22 | 23 | git clone https://github.com/gpu/JOCL.git 24 | git clone https://github.com/gpu/JOCLCommon.git 25 | 26 | 27 | ### Building the native libraries 28 | 29 | The native libraries of JOCL can be built with [CMake](http://www.cmake.org/) 30 | and any compatible target compiler (e.g. Visual Studio or GCC): 31 | 32 | * Start `cmake-gui`, 33 | * Set the directory containing the sources of the `JOCL` project, e.g. `C:\JOCLRoot\JOCL` 34 | * Set the directory for the build files: e.g. `C:\JOCLRoot\JOCL.build` 35 | * Press "Configure" (and select the appropriate compiler) 36 | * Press "Generate" 37 | 38 | Then, `C:\JOCLRoot\JOCL.build` will contain the build files, e.g. the 39 | GCC makefiles or the Visual Studio project files. Compiling the project 40 | with these makefiles will place the binaries into a `nativeLibraries` 41 | subdirectory of the project, e.g. into 42 | `C:\JOCLRoot\JOCL\nativeLibraries`. 43 | 44 | 45 | ### Building and packaging the external native library dependencies 46 | 47 | JOCL itself does not have external dependencies, except for the run-time 48 | dependency to the OpenCL implementation. If you only want to compile 49 | JOCL, then you can skip this section. 50 | 51 | This section only refers to libraries like 52 | [JOCLBLAS](https://github.com/gpu/JOCLBLAS) and 53 | [JOCLBlast](https://github.com/gpu/JOCLBlast), which require installations of 54 | external libraries ([clBLAS](https://github.com/clMathLibraries/clBLAS/) and 55 | [CLBlast](https://github.com/CNugteren/CLBlast/), respectively). The following 56 | JOCLBlast and CLBlast, but the same process applies to all other external 57 | dependencies. 58 | 59 | Compiling and installing CLBlast (as described in its documentation) will 60 | result in directories that contain the header files and the native libraries. 61 | In order to build JOCLBlast, the paths to these header directories and 62 | library files have to be entered in the CMake GUI, as the values of the 63 | `CLBLAST_INSTALL_DIR` and `CLBlast_LIBRARY`. 64 | 65 | After this information has been entered, the JOCLBlast library may be 66 | compiled as described above, under **Building the native libraries**. 67 | The resulting native JOCLBlast library will be placed into 68 | the `nativeLibraries` directory of the project, for example, into 69 | `C:\JOCLRoot\JOCLBlast\nativeLibraries` 70 | 71 | This library is *dynamically* linked against the actual CLBlast library. 72 | In order to be able to load this library, the CLBlast library must 73 | be in a specific subdirectory of the `nativeLibraries` folder. 74 | The name of this subdirectory is determined by the operating system and 75 | architecture that the library is compiled for. The general pattern here is 76 | 77 | `C:\JOCLRoot\JOCLBlast\nativeLibraries\OS\ARCHITECTURE` 78 | 79 | The `OS` here refers to the operating system, and will usually be `windows`, 80 | `linux` or `apple`. The `ARCHITECTURE` refers to the processor architecture 81 | of the target system, and will usually be `x86` (for 32 bit system) or 82 | `x86_64` (for 64 bit systems). 83 | 84 | For example, on 64 bit Windows, the `clblast.dll` will have to be placed 85 | into the directory 86 | 87 | `C:\JOCLRoot\JOCLBlast\nativeLibraries\windows\x86_64\` 88 | 89 | There, it will be picked up by the Maven and integrated into the JAR, 90 | as described in the next section. 91 | 92 | 93 | 94 | 95 | 96 | ### Building the Java libraries 97 | 98 | The actual Java libraries can be built with 99 | [Apache Maven](https://maven.apache.org/). After the native libraries 100 | have been built as described above, change into the `JOCL` directory 101 | and execute 102 | 103 | mvn clean install 104 | 105 | This will compile the Java libraries, run the unit tests, assemble the 106 | classes (together with the native libraries), sources and JavaDocs into 107 | JAR files, and finally place all libraries into the 108 | `C:\JOCLRoot\JOCL\target` directory. 109 | 110 | 111 | ### Building for Android 112 | 113 | Compiling native code for Android is a bit of a pain, so we use [android-cmake](https://github.com/taka-no-me/android-cmake) 114 | to make our lives a bit easier. We first begin by installing the android-cmake 115 | toolchain file into our cmake modules path. On Linux, this is likely 116 | `/usr/share/cmake-3.2/Modules/`. 117 | 118 | cd /usr/share/cmake-3.2/Modules 119 | sudo wget https://github.com/taka-no-me/android-cmake/raw/master/android.toolchain.cmake 120 | 121 | Next, we want to configure the build for our particular Android target. 122 | 123 | cd JOCL 124 | mkdir build 125 | cd build 126 | cmake -DCMAKE_TOOLCHAIN_FILE=android.toolchain \ 127 | -DANDROID_ABI=armeabi-v7a \ 128 | -DANDROID_NATIVE_API_LEVEL=21 \ 129 | -DCMAKE_BUILD_TYPE=Release \ 130 | .. 131 | 132 | This should be enough to get you started. For more advanced configuration, 133 | refer to the [android-cmake](https://github.com/taka-no-me/android-cmake) 134 | documentation. 135 | 136 | Finally, when building the final .jar file, we would like to avoid running the 137 | local tests, as the Android native libraries won't run on your local machine. 138 | 139 | mvn clean install -DskipTests 140 | 141 | 142 | 143 | ## Building on a virtual machine 144 | 145 | **Note:** This section has to be updated. See [issue 23](https://github.com/gpu/JOCL/issues/23). 146 | 147 | JOCL for Linux can be built in a virtual machine using Vagrant. 148 | [Lewis Cowles](https://github.com/Lewiscowles1986) and 149 | [Alex Zhukov](https://github.com/zhuker) have provided 150 | a Vagrantfile that contains the complete JOCL build in its 151 | provisioning script. 152 | 153 | **Preparing to build JOCL using Vagrant:** 154 | 155 | - Install [VirtualBox](https://www.virtualbox.org/) (tested with version 5.2.10) 156 | - Install [Vagrant](https://www.vagrantup.com/) (tested with version 2.0.1) 157 | - Install the vagrant Virtual Box Guest additions plugin with 158 | 159 | vagrant plugin install vagrant-vbguest 160 | 161 | 162 | **Building JOCL using Vagrant:** 163 | 164 | Copy the [Vagrantfile](Vagrantfile) into a working directory and execute 165 | 166 | vagrant up 167 | 168 | The provisioning script will install all required packages, clone 169 | the latest state of JOCL from GitHub, build the native library 170 | using CMake and gcc, and the JARs using Maven. 171 | 172 | The resulting JAR files will be placed in the working directory. 173 | 174 | In order to remove the machine that has been created for building 175 | JOCL, execute 176 | 177 | vagrant destroy 178 | 179 | -------------------------------------------------------------------------------- /Vagrantfile: -------------------------------------------------------------------------------- 1 | Vagrant.configure("2") do |config| 2 | config.vm.box = "centos/7" 3 | config.vm.synced_folder ".", "/vagrant", type: "nfs" 4 | config.vm.provider "virtualbox" do |vb| 5 | vb.memory = "2048" 6 | vb.cpus = 2 7 | end 8 | 9 | config.vm.provision "shell", inline: <<-SHELL 10 | 11 | curl http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm >epel-release-latest-7.noarch.rpm 12 | rpm -ivh epel-release-latest-7.noarch.rpm 13 | 14 | yum install -y cmake3 gcc-c++ git maven mesa-libGL-devel ocl-icd-devel 15 | 16 | cd /tmp 17 | git clone https://github.com/gpu/JOCL 18 | git clone https://github.com/gpu/JOCLCommon 19 | 20 | export JAVA_HOME=/etc/alternatives/java_sdk 21 | 22 | cmake3 JOCL 23 | make 24 | 25 | cd JOCL 26 | mvn clean package -DskipTests 27 | 28 | cp target/*.jar /vagrant 29 | 30 | SHELL 31 | 32 | end -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | org.sonatype.oss 5 | oss-parent 6 | 9 7 | 8 | 9 | 4.0.0 10 | 11 | org.jocl 12 | jocl 13 | 2.0.6-SNAPSHOT 14 | 15 | jar 16 | 17 | JOCL 18 | Java bindings for OpenCL 19 | http://www.jocl.org 20 | 21 | 22 | 23 | MIT 24 | http://jocl.org/downloads/LICENSE.TXT 25 | repo 26 | 27 | 28 | 29 | 30 | 31 | Marco Hutter 32 | jocl@jocl.org 33 | 34 | developer 35 | 36 | 37 | 38 | 39 | 40 | scm:git:git@github.com:gpu/JOCL.git 41 | scm:git:git@github.com:gpu/JOCL.git 42 | git@github.com:gpu/JOCL.git 43 | 44 | 45 | 46 | package 47 | 48 | 49 | 50 | 51 | org.apache.maven.plugins 52 | maven-compiler-plugin 53 | 2.3.2 54 | 55 | 1.6 56 | 1.6 57 | 58 | 59 | 60 | 61 | org.apache.maven.plugins 62 | maven-resources-plugin 63 | 2.4.3 64 | 65 | 66 | UTF-8 67 | 68 | 69 | 70 | 71 | 72 | copy-resources 73 | prepare-package 74 | 75 | copy-resources 76 | 77 | 78 | ${project.build.outputDirectory}/lib 79 | 80 | 81 | ${project.basedir}/nativeLibraries/ 82 | false 83 | 84 | **/*.dll 85 | **/*.jnilib 86 | **/*.dylib 87 | **/*.so 88 | 89 | 90 | 91 | ${basedir}/../../nativeLibraries/ 92 | false 93 | 94 | **/*.dll 95 | **/*.jnilib 96 | **/*.dylib 97 | **/*.so 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | org.apache.maven.plugins 112 | maven-source-plugin 113 | 2.1.2 114 | 115 | 116 | attach-sources 117 | 118 | jar 119 | 120 | 121 | 122 | 123 | 124 | 125 | org.apache.maven.plugins 126 | maven-javadoc-plugin 127 | 2.7 128 | 129 | 130 | attach-javadocs 131 | 132 | jar 133 | 134 | 135 | 136 | 137 | 138 | 139 | org.apache.maven.plugins 140 | maven-surefire-plugin 141 | 2.18.1 142 | 143 | once 144 | -Djava.library.path=${project.basedir}/nativeLibraries 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | junit 154 | junit 155 | 4.13.1 156 | test 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | doclint-java8-disable 165 | 166 | [1.8,) 167 | 168 | 169 | 170 | 171 | org.apache.maven.plugins 172 | maven-javadoc-plugin 173 | 174 | -Xdoclint:none 175 | 176 | 177 | 178 | org.apache.maven.plugins 179 | maven-site-plugin 180 | 3.3 181 | 182 | 183 | 184 | org.apache.maven.plugins 185 | maven-javadoc-plugin 186 | 187 | -Xdoclint:none 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/BuildProgramFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | 31 | /** 32 | * Emulation of a function pointer for functions that may be passed to the 33 | * {@link CL#clBuildProgram(cl_program, int, cl_device_id[], String, BuildProgramFunction, Object) 34 | * clBuildProgram} method. 35 | * 36 | * @see CL#clBuildProgram(cl_program, int, cl_device_id[], String, BuildProgramFunction, Object) 37 | */ 38 | public interface BuildProgramFunction 39 | { 40 | /** 41 | * The function that will be called. 42 | * 43 | * @param program The program. 44 | * @param user_data The user data. 45 | */ 46 | void function(cl_program program, Object user_data); 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/CLException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | /** 31 | * An exception that may be thrown due to a OpenCL error.
32 | *
33 | * Exceptions may be enabled or disabled using 34 | * {@link org.jocl.CL#setExceptionsEnabled(boolean) CL#setExceptionsEnabled(boolean)}. 35 | * If exceptions are enabled, the JOCL methods will throw a 36 | * CLException if the OpenCL function did not return CL_SUCCESS.
37 | */ 38 | public class CLException extends RuntimeException 39 | { 40 | /** 41 | * The serial version UID 42 | */ 43 | private static final long serialVersionUID = 1587809813906124159L; 44 | 45 | /** 46 | * The status code from OpenCL 47 | */ 48 | private final int status; 49 | 50 | /** 51 | * Creates a new CLException with the given error message. 52 | * 53 | * @param message The error message for this CLException 54 | */ 55 | public CLException(String message) 56 | { 57 | super(message); 58 | this.status = CL.CL_JOCL_INTERNAL_ERROR; 59 | } 60 | 61 | /** 62 | * Creates a new CLException with the given error message. 63 | * 64 | * @param message The error message for this CLException 65 | * @param status The status code from OpenCL 66 | */ 67 | public CLException(String message, int status) 68 | { 69 | super(message); 70 | this.status = status; 71 | } 72 | 73 | /** 74 | * Creates a new CLException with the given error message. 75 | * 76 | * @param message The error message for this CLException 77 | * @param cause The throwable that caused this exception 78 | */ 79 | public CLException(String message, Throwable cause) 80 | { 81 | super(message, cause); 82 | this.status = CL.CL_JOCL_INTERNAL_ERROR; 83 | } 84 | 85 | /** 86 | * Creates a new CLException with the given error message. 87 | * 88 | * @param message The error message for this CLException 89 | * @param cause The throwable that caused this exception 90 | * @param status The status code from OpenCL 91 | */ 92 | public CLException(String message, Throwable cause, int status) 93 | { 94 | super(message, cause); 95 | this.status = status; 96 | } 97 | 98 | /** 99 | * Returns the status code from OpenCL that caused this exception. 100 | * For example, the value of {@link CL#CL_INVALID_DEVICE} 101 | * 102 | * @return The OpenCL status code 103 | */ 104 | public int getStatus() 105 | { 106 | return status; 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/CreateContextFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | /** 31 | * Emulation of a function pointer for functions that may be passed to the 32 | * {@link CL#clCreateContext(cl_context_properties, int, cl_device_id[], CreateContextFunction, Object, int[]) clCreateContext} and 33 | * {@link CL#clCreateContextFromType(cl_context_properties, long, CreateContextFunction, Object, int[]) clCreateContextFromType} methods. 34 | * 35 | * @see CL#clCreateContext(cl_context_properties, int, cl_device_id[], CreateContextFunction, Object, int[]) 36 | * @see CL#clCreateContextFromType(cl_context_properties, long, CreateContextFunction, Object, int[]) 37 | */ 38 | public interface CreateContextFunction 39 | { 40 | /** 41 | * The function that will be called. 42 | * 43 | * @param errinfo The error info. 44 | * @param private_info The private info data. 45 | * @param cb The The size of the private info data. 46 | * @param user_data The user data. 47 | */ 48 | void function(String errinfo, Pointer private_info, long cb, Object user_data); 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/EnqueueNativeKernelFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | /** 31 | * Emulation of a function pointer for functions that may be passed to the 32 | * {@link CL#clEnqueueNativeKernel(cl_command_queue, EnqueueNativeKernelFunction, 33 | * Object, long, int, cl_mem[], Pointer[], int, cl_event[], cl_event) clEnqueueNativeKernel} 34 | * method. 35 | * 36 | * @see CL#clEnqueueNativeKernel(cl_command_queue, EnqueueNativeKernelFunction, Object, 37 | * long, int, cl_mem[], Pointer[], int, cl_event[], cl_event) 38 | */ 39 | public interface EnqueueNativeKernelFunction 40 | { 41 | /** 42 | * The function that will be called 43 | * 44 | * @param args The arguments 45 | */ 46 | void function(Object args); 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/EventCallbackFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | /** 31 | * Emulation of a function pointer for functions that may be passed to the 32 | * {@link CL#clSetEventCallback(cl_event, int, EventCallbackFunction, Object) 33 | * clSetEventCallback} method. 34 | * 35 | * @see CL#clSetEventCallback(cl_event, int, EventCallbackFunction, Object) 36 | */ 37 | public interface EventCallbackFunction 38 | { 39 | /** 40 | * The function that will be called 41 | * 42 | * @param event The event 43 | * @param command_exec_callback_type The callback type 44 | * @param user_data The user data 45 | */ 46 | void function(cl_event event, int command_exec_callback_type, Object user_data); 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/LibInitializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2015 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package org.jocl; 28 | 29 | import java.util.logging.Level; 30 | import java.util.logging.Logger; 31 | 32 | import org.jocl.LibUtils.OSType; 33 | 34 | /** 35 | * Utility class for initializing the OpenCL implementation library 36 | */ 37 | class LibInitializer 38 | { 39 | /** 40 | * The logger used in this class 41 | */ 42 | private final static Logger logger = 43 | Logger.getLogger(LibTracker.class.getName()); 44 | 45 | /** 46 | * The default log level 47 | */ 48 | private static final Level level = Level.FINE; 49 | 50 | /** 51 | * Initialize the native library by passing the name of the OpenCL 52 | * implementation to the {@link CL#initNativeLibrary(String)} 53 | * method. 54 | * 55 | * @throws UnsatisfiedLinkError If the implementation library 56 | * could not be loaded. 57 | */ 58 | static void initNativeLibrary() 59 | { 60 | String[] libCandidates = createImplementationNameCandidates(); 61 | 62 | boolean initialized = false; 63 | for (int i = 0; i < libCandidates.length && !initialized; i++) 64 | { 65 | logger.log(level, "Trying library candidate: " + libCandidates[i]); 66 | initialized = CL.initNativeLibrary(libCandidates[i]); 67 | if (initialized) 68 | { 69 | break; 70 | } 71 | } 72 | if (!initialized) 73 | { 74 | throw new UnsatisfiedLinkError( 75 | "Could not initialize native OpenCL library. Implementation " + 76 | "library could not be loaded"); 77 | } 78 | } 79 | 80 | /** 81 | * Create a list of OpenCL shared library candidates that will be 82 | * passed to the dlopen/LoadLibrary call on native side. For Windows 83 | * and Linux, this will be the name of the OpenCL library itself. 84 | * For MacOS, it will be the path to the OpenCL framework. For Android, 85 | * this will be an absolute path to the shared library. 86 | * 87 | * @return A list of candidate paths / names. 88 | */ 89 | private static String[] createImplementationNameCandidates() 90 | { 91 | String defaultLibName = LibUtils.createLibraryFileName("OpenCL"); 92 | OSType osType = LibUtils.calculateOS(); 93 | if (OSType.APPLE.equals(osType)) 94 | { 95 | return new String[] { 96 | "/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL", 97 | defaultLibName 98 | }; 99 | } 100 | if (OSType.ANDROID.equals(osType)) 101 | { 102 | return new String[] 103 | { 104 | "libpocl.so", // portablecl.org 105 | "/system/vendor/lib/libOpenCL.so", // Qualcomm 106 | "/system/vendor/lib/egl/libGLES_mali.so", // ARM MALI SDK 107 | "/system/vendor/lib/libPVROCL.so", // PowerVR SDK 108 | "/system/lib/libOpenCL.so", 109 | "/system/lib/egl/libGLES_mali.so", 110 | "/system/lib/libPVROCL.so", 111 | defaultLibName 112 | }; 113 | } 114 | return new String[] 115 | { 116 | defaultLibName 117 | }; 118 | } 119 | 120 | /** 121 | * Private constructor to prevent instantiation 122 | */ 123 | private LibInitializer() 124 | { 125 | // Private constructor to prevent instantiation 126 | } 127 | 128 | } 129 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/LibTracker.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2015 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | package org.jocl; 28 | 29 | import java.io.File; 30 | import java.lang.reflect.*; 31 | import java.util.*; 32 | import java.util.logging.Logger; 33 | 34 | /** 35 | * Utility class for tracking a set of loaded libraries and 36 | * deleting them at program exit.
37 | *
38 | * Note: The current default setting in JOCL is that 39 | * this class is NOT used.
40 | *
41 | * This class contains some ugly reflection hacks, attempting 42 | * to alleviate the problem that (temporary) native library 43 | * files on Windows can not be deleted when they have been 44 | * loaded.
45 | *
46 | * However, the default setting in JOCL is that it assumes that 47 | * the name of the native library is fixed, and it will not be 48 | * necessary to create temporary files. 49 | */ 50 | class LibTracker 51 | { 52 | /** 53 | * The logger used in this class 54 | */ 55 | private final static Logger logger = 56 | Logger.getLogger(LibTracker.class.getName()); 57 | 58 | /** 59 | * The tracked library files that will be unloaded 60 | * and deleted when the application exits 61 | */ 62 | private static final Set libraryFiles = new LinkedHashSet(); 63 | 64 | /** 65 | * The shutdown hook thread that will attempt to unload 66 | * and delete the library files 67 | */ 68 | private static Thread shutdownHook = null; 69 | 70 | /** 71 | * Add the given native library file to be tracked by this class. 72 | * When the application exits, the native library will be unloaded 73 | * and the given file will be deleted. 74 | * 75 | * @param libraryFile The library file 76 | */ 77 | static synchronized void track(File libraryFile) 78 | { 79 | if (libraryFile == null) 80 | { 81 | return; 82 | } 83 | if (shutdownHook == null) 84 | { 85 | logger.fine("Initializing library shutdown hook"); 86 | shutdownHook = new Thread() 87 | { 88 | @Override 89 | public void run() 90 | { 91 | shutdown(); 92 | } 93 | }; 94 | try 95 | { 96 | Runtime.getRuntime().addShutdownHook(shutdownHook); 97 | } 98 | catch (SecurityException e) 99 | { 100 | // Ignored 101 | } 102 | } 103 | logger.fine("Tracking library file "+libraryFile); 104 | libraryFiles.add(libraryFile); 105 | } 106 | 107 | /** 108 | * Will be called by the shutdown hook 109 | */ 110 | private static void shutdown() 111 | { 112 | freeNativeLibraries(); 113 | deleteNativeLibraries(); 114 | } 115 | 116 | /** 117 | * Free the native libraries that have been registered in this class. 118 | * Any errors will be ignored. 119 | */ 120 | private static void freeNativeLibraries() 121 | { 122 | ClassLoader classLoader = LibTracker.class.getClassLoader(); 123 | Object nativeLibrariesObject = 124 | getFieldValueOptional(classLoader, "nativeLibraries"); 125 | if (nativeLibrariesObject == null) 126 | { 127 | return; 128 | } 129 | if (!(nativeLibrariesObject instanceof List)) 130 | { 131 | return; 132 | } 133 | List nativeLibraries = (List)nativeLibrariesObject; 134 | for (Object nativeLibrary : nativeLibraries) 135 | { 136 | freeNativeLibrary(nativeLibrary); 137 | } 138 | } 139 | 140 | /** 141 | * Free the given library object by invoking its "finalize(Class)" 142 | * method. Any errors will be ignored. 143 | * 144 | * @param library The library 145 | */ 146 | private static void freeNativeLibrary(final Object library) 147 | { 148 | Object nameObject = getFieldValueOptional(library, "name"); 149 | if (nameObject == null) 150 | { 151 | return; 152 | } 153 | String name = String.valueOf(nameObject); 154 | File file = new File(name); 155 | if (libraryFiles.contains(file)) 156 | { 157 | invokeFinalizeOptional(library); 158 | } 159 | } 160 | 161 | /** 162 | * Try to delete all library files that are tracked by this class. 163 | * Any errors will be ignored. 164 | */ 165 | private static void deleteNativeLibraries() 166 | { 167 | for (File libraryFile : libraryFiles) 168 | { 169 | if (libraryFile.exists()) 170 | { 171 | try 172 | { 173 | boolean deleted = libraryFile.delete(); 174 | logger.fine("Deleting " + libraryFile + " " + 175 | (deleted ? "DONE" : "FAILED")); 176 | } 177 | catch (SecurityException e) 178 | { 179 | // Ignored 180 | } 181 | } 182 | } 183 | libraryFiles.clear(); 184 | 185 | } 186 | 187 | 188 | /** 189 | * Returns the value of the (potentially private) field with the 190 | * given name from the given object. If there is any error, then 191 | * null will be returned. 192 | * 193 | * @param object The object 194 | * @param fieldName The name of the field whose value to obtain 195 | * @return The field value, or null if the value 196 | * could not be obtained 197 | */ 198 | private static Object getFieldValueOptional( 199 | Object object, String fieldName) 200 | { 201 | if (object == null) 202 | { 203 | return null; 204 | } 205 | Class c = object.getClass(); 206 | 207 | Field field = getDeclaredField(c, fieldName); 208 | if (field == null) 209 | { 210 | return null; 211 | } 212 | 213 | boolean wasAccessible = field.isAccessible(); 214 | try 215 | { 216 | field.setAccessible(true); 217 | try 218 | { 219 | return field.get(object); 220 | } 221 | catch (Exception e) 222 | { 223 | return null; 224 | } 225 | } 226 | finally 227 | { 228 | try 229 | { 230 | field.setAccessible(wasAccessible); 231 | } 232 | catch (SecurityException e) 233 | { 234 | // Ignored 235 | } 236 | } 237 | } 238 | 239 | /** 240 | * Returns the declared field with the given name in the given 241 | * class, or any of its superclasses. Returns null 242 | * if no such field could be found. 243 | * 244 | * @param c The class 245 | * @param fieldName The field name 246 | * @return The field 247 | */ 248 | private static Field getDeclaredField(Class c, String fieldName) 249 | { 250 | if (c == null) 251 | { 252 | return null; 253 | } 254 | try 255 | { 256 | Field field = c.getDeclaredField(fieldName); 257 | return field; 258 | } 259 | catch (Exception e) 260 | { 261 | return getDeclaredField(c.getSuperclass(), fieldName); 262 | } 263 | } 264 | 265 | /** 266 | * Tries to invoke a "finalize(Class)" method on the given object. 267 | * Any error will be ignored. 268 | * 269 | * @param object The object 270 | */ 271 | private static void invokeFinalizeOptional(Object object) 272 | { 273 | if (object == null) 274 | { 275 | return; 276 | } 277 | Method method = null; 278 | boolean wasAccessible = false; 279 | try 280 | { 281 | Class c = object.getClass(); 282 | method = c.getDeclaredMethod("finalize", new Class[0]); 283 | wasAccessible = method.isAccessible(); 284 | method.setAccessible(true); 285 | method.invoke(object, new Object[0]); 286 | } 287 | catch (Exception e) 288 | { 289 | // Ignored 290 | } 291 | finally 292 | { 293 | if (method != null) 294 | { 295 | try 296 | { 297 | method.setAccessible(wasAccessible); 298 | } 299 | catch (SecurityException e) 300 | { 301 | // Ignored 302 | } 303 | } 304 | } 305 | } 306 | 307 | /** 308 | * Private constructor to prevent instantiation 309 | */ 310 | private LibTracker() 311 | { 312 | // Private constructor to prevent instantiation 313 | } 314 | 315 | } 316 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/LibUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2015 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | import java.io.File; 31 | import java.io.FileOutputStream; 32 | import java.io.IOException; 33 | import java.io.InputStream; 34 | import java.io.OutputStream; 35 | import java.io.PrintWriter; 36 | import java.io.StringWriter; 37 | import java.util.Locale; 38 | import java.util.UUID; 39 | import java.util.logging.Level; 40 | import java.util.logging.Logger; 41 | 42 | /** 43 | * Utility class for detecting the operating system and architecture 44 | * types, and automatically loading the matching native library 45 | * as a resource or from a file.
46 | *
47 | * This class is not intended to be used by clients.
48 | *
49 | */ 50 | public final class LibUtils 51 | { 52 | // The architecture and OS detection has been adapted from 53 | // http://javablog.co.uk/2007/05/19/making-jni-cross-platform/ 54 | // and extended with http://lopica.sourceforge.net/os.html 55 | 56 | /** 57 | * The logger used in this class 58 | */ 59 | private static final Logger logger = 60 | Logger.getLogger(LibUtils.class.getName()); 61 | 62 | /** 63 | * The default log level 64 | */ 65 | private static final Level level = Level.FINE; 66 | 67 | /** 68 | * The directory where libraries are expected in JAR files, 69 | * when they are loaded as resources 70 | */ 71 | private static final String LIBRARY_PATH_IN_JAR = "/lib"; 72 | 73 | /** 74 | * Enumeration of common operating systems, independent of version 75 | * or architecture. 76 | */ 77 | enum OSType 78 | { 79 | ANDROID, APPLE, LINUX, SUN, WINDOWS, UNKNOWN 80 | } 81 | 82 | /** 83 | * Enumeration of common CPU architectures. 84 | */ 85 | enum ArchType 86 | { 87 | PPC, PPC_64, SPARC, X86, X86_64, ARM, ARM64, MIPS, MIPS64, RISC, UNKNOWN 88 | } 89 | 90 | /** 91 | * Private constructor to prevent instantiation. 92 | */ 93 | private LibUtils() 94 | { 95 | // Private constructor to prevent instantiation. 96 | } 97 | 98 | /** 99 | * Loads the specified library.
100 | *
101 | * The method will attempt to load the library using the usual 102 | * System.loadLibrary call. In this case, the specified 103 | * dependent libraries are ignored, because they are assumed to be 104 | * loaded automatically in the same way as the main library.
105 | *
106 | * If the library can not be loaded with the 107 | * System.loadLibrary call, then this method will attempt 108 | * to load the file as a resource (usually one that is contained in 109 | * a JAR file). In this case, the library is assumed to be located 110 | * in subdirectory called "/lib" inside the JAR file. 111 | * The method will try to load a resource that has the platform-specific 112 | * {@link #createLibraryFileName(String) library file name} from 113 | * this directory, extract it into the default directory for temporary 114 | * files, and load the library from there.
115 | *
116 | * In this case, the specified dependent libraries may also be loaded 117 | * as resources. They are assumed to be located in subdirectories 118 | * that are named according to the {@link #osString()} and 119 | * {@link #archString()} of the executing platform. For example, such 120 | * a library may be located in a directory inside the JAR that is 121 | * called "/lib/windows/x86_64". These dependent libraries 122 | * will be extracted and loaded before the main library is loaded. 123 | * 124 | * @param libraryName The name of the library (without a platform specific 125 | * prefix or file extension) 126 | * @param dependentLibraryNames The names of libraries that the library 127 | * to load depends on. If the library is loaded as a resource, then 128 | * it will be attempted to also load these libraries as resources, as 129 | * described above 130 | * @throws UnsatisfiedLinkError if the native library 131 | * could not be loaded. 132 | */ 133 | public static void loadLibrary( 134 | String libraryName, String ... dependentLibraryNames) 135 | { 136 | logger.log(level, "Loading library: " + libraryName); 137 | 138 | // First, try to load the specified library as a file 139 | // that is visible in the default search path 140 | Throwable throwableFromFile; 141 | try 142 | { 143 | logger.log(level, "Loading library as a file"); 144 | System.loadLibrary(libraryName); 145 | logger.log(level, "Loading library as a file DONE"); 146 | return; 147 | } 148 | catch (Throwable t) 149 | { 150 | logger.log(level, "Loading library as a file FAILED"); 151 | throwableFromFile = t; 152 | } 153 | 154 | // Now try to load the library by extracting the 155 | // corresponding resource from the JAR file 156 | try 157 | { 158 | logger.log(level, "Loading library as a resource"); 159 | loadLibraryResource(LIBRARY_PATH_IN_JAR, 160 | libraryName, "", dependentLibraryNames); 161 | logger.log(level, "Loading library as a resource DONE"); 162 | return; 163 | } 164 | catch (Throwable throwableFromResource) 165 | { 166 | logger.log(level, "Loading library as a resource FAILED", 167 | throwableFromResource); 168 | 169 | StringWriter sw = new StringWriter(); 170 | PrintWriter pw = new PrintWriter(sw); 171 | 172 | pw.println("Error while loading native library \"" + 173 | libraryName + "\""); 174 | pw.println("Operating system name: "+ 175 | System.getProperty("os.name")); 176 | pw.println("Architecture : "+ 177 | System.getProperty("os.arch")); 178 | pw.println("Architecture bit size: "+ 179 | System.getProperty("sun.arch.data.model")); 180 | 181 | pw.println("---(start of nested stack traces)---"); 182 | 183 | pw.println("Stack trace from the attempt to " + 184 | "load the library as a file:"); 185 | throwableFromFile.printStackTrace(pw); 186 | 187 | pw.println("Stack trace from the attempt to " + 188 | "load the library as a resource:"); 189 | throwableFromResource.printStackTrace(pw); 190 | 191 | pw.println("---(end of nested stack traces)---"); 192 | 193 | pw.close(); 194 | throw new UnsatisfiedLinkError(sw.toString()); 195 | } 196 | } 197 | 198 | 199 | 200 | 201 | /** 202 | * Load the library with the given name from a resource. 203 | * 204 | * @param resourceSubdirectoryName The subdirectory where the resource 205 | * is expected 206 | * @param libraryName The library name, e.g. "EXAMPLE-windows-x86" 207 | * @param tempSubdirectoryName The name for the subdirectory in the 208 | * temp directory, where the temporary files for dependent libraries 209 | * should be stored 210 | * @param dependentLibraryNames The names of libraries that the library 211 | * to load depends on, and that may have to be loaded as resources and 212 | * stored as temporary files as well 213 | * @throws Throwable If the library could not be loaded 214 | */ 215 | private static void loadLibraryResource( 216 | String resourceSubdirectoryName, 217 | String libraryName, 218 | String tempSubdirectoryName, 219 | String ... dependentLibraryNames) throws Throwable 220 | { 221 | // First try to load all dependent libraries, recursively 222 | for (String dependentLibraryName : dependentLibraryNames) 223 | { 224 | logger.log(level, 225 | "Library " + libraryName + 226 | " depends on " + dependentLibraryName); 227 | 228 | String dependentResourceSubdirectoryName = 229 | resourceSubdirectoryName + "/" + 230 | osString() + "/" + 231 | archString(); 232 | 233 | String dependentLibraryTempSubDirectoryName = 234 | libraryName+"_dependents" + File.separator + 235 | osString() + File.separator + 236 | archString() + File.separator; 237 | 238 | loadLibraryResource( 239 | dependentResourceSubdirectoryName, 240 | dependentLibraryName, 241 | dependentLibraryTempSubDirectoryName); 242 | } 243 | 244 | // Now, prepare loading the actual library 245 | String libraryFileName = createLibraryFileName(libraryName); 246 | File libraryTempFile; 247 | if (useUniqueLibraryNames()) 248 | { 249 | String uniqueLibraryFileName = 250 | createLibraryFileName(libraryName + "-" + UUID.randomUUID()); 251 | libraryTempFile = createTempFile( 252 | tempSubdirectoryName, uniqueLibraryFileName); 253 | } 254 | else 255 | { 256 | libraryTempFile = createTempFile( 257 | tempSubdirectoryName, libraryFileName); 258 | } 259 | 260 | // If the temporary file for the library does not exist, create it 261 | if (!libraryTempFile.exists()) 262 | { 263 | String libraryResourceName = 264 | resourceSubdirectoryName + "/" + libraryFileName; 265 | logger.log(level, 266 | "Writing resource " + libraryResourceName); 267 | logger.log(level, 268 | "to temporary file " + libraryTempFile); 269 | writeResourceToFile(libraryResourceName, libraryTempFile); 270 | if (trackCreatedTempFiles()) 271 | { 272 | LibTracker.track(libraryTempFile); 273 | } 274 | } 275 | 276 | // Finally, try to load the library from the temporary file 277 | logger.log(level, "Loading library " + libraryTempFile); 278 | System.load(libraryTempFile.toString()); 279 | logger.log(level, "Loading library " + libraryTempFile + " DONE"); 280 | } 281 | 282 | 283 | /** 284 | * Create a file object representing the file with the given name 285 | * in the specified subdirectory of the default "temp" directory. 286 | * If the specified subdirectory does not exist yet, it is created. 287 | * 288 | * @param name The file name 289 | * @return The file 290 | * @throws IOException If the subdirectory can not be created 291 | */ 292 | private static File createTempFile( 293 | String tempSubdirectoryName, String name) throws IOException 294 | { 295 | String tempDirName = System.getProperty("java.io.tmpdir"); 296 | File tempSubDirectory = 297 | new File(tempDirName + File.separator + tempSubdirectoryName); 298 | if (!tempSubDirectory.exists()) 299 | { 300 | boolean createdDirectory = tempSubDirectory.mkdirs(); 301 | if (!createdDirectory) 302 | { 303 | throw new IOException( 304 | "Could not create directory for temporary file: " + 305 | tempSubDirectory); 306 | } 307 | } 308 | String tempFileName = tempSubDirectory + File.separator + name; 309 | File tempFile = new File(tempFileName); 310 | return tempFile; 311 | } 312 | 313 | 314 | /** 315 | * Obtain an input stream to the resource with the given name, and write 316 | * it to the specified file (which may not be null, and 317 | * may not exist yet) 318 | * 319 | * @param resourceName The name of the resource 320 | * @param file The file to write to 321 | * @throws NullPointerException If the given file is null 322 | * @throws IllegalArgumentException If the given file already exists 323 | * @throws IOException If an IO error occurs 324 | */ 325 | private static void writeResourceToFile( 326 | String resourceName, File file) throws IOException 327 | { 328 | if (file == null) 329 | { 330 | throw new NullPointerException("Target file may not be null"); 331 | } 332 | if (file.exists()) 333 | { 334 | throw new IllegalArgumentException( 335 | "Target file already exists: "+file); 336 | } 337 | InputStream inputStream = 338 | LibUtils.class.getResourceAsStream(resourceName); 339 | if (inputStream == null) 340 | { 341 | throw new IOException( 342 | "No resource found with name '"+resourceName+"'"); 343 | } 344 | OutputStream outputStream = null; 345 | try 346 | { 347 | outputStream = new FileOutputStream(file); 348 | byte[] buffer = new byte[32768]; 349 | while (true) 350 | { 351 | int read = inputStream.read(buffer); 352 | if (read < 0) 353 | { 354 | break; 355 | } 356 | outputStream.write(buffer, 0, read); 357 | } 358 | outputStream.flush(); 359 | } 360 | finally 361 | { 362 | if (outputStream != null) 363 | { 364 | try 365 | { 366 | outputStream.close(); 367 | } 368 | catch (IOException e) 369 | { 370 | logger.log(Level.SEVERE, e.getMessage(), e); 371 | } 372 | } 373 | try 374 | { 375 | inputStream.close(); 376 | } 377 | catch (IOException e) 378 | { 379 | logger.log(Level.SEVERE, e.getMessage(), e); 380 | } 381 | } 382 | } 383 | 384 | 385 | /** 386 | * Returns whether the "uniqueLibaryNames" property was set, 387 | * and the temporary files for the native libraries that are 388 | * loaded as resources should receive a different name each 389 | * time that they are loaded.
390 | *
391 | * PRELIMINARY! 392 | * 393 | * @return Whether the temporary files should receive unique names 394 | */ 395 | private static boolean useUniqueLibraryNames() 396 | { 397 | String uniqueLibraryNames = 398 | System.getProperty("uniqueLibraryNames"); 399 | return "true".equals(uniqueLibraryNames); 400 | } 401 | 402 | /** 403 | * Returns whether all temporary files that are created should 404 | * be tracked with a {@link LibTracker} 405 | *
406 | * PRELIMINARY! 407 | * 408 | * @return Whether the files should be tracked 409 | */ 410 | private static boolean trackCreatedTempFiles() 411 | { 412 | // Currently, this is only done when unique library names are used 413 | return useUniqueLibraryNames(); 414 | } 415 | 416 | 417 | /** 418 | * Create the full library file name, including the extension 419 | * and prefix, for the given library name. For example, the 420 | * name "EXAMPLE" will become
421 | * EXAMPLE.dll on Windows
422 | * libEXAMPLE.so on Linux
423 | * EXAMPLE.dylib on MacOS
424 | * 425 | * @param libraryName The library name 426 | * @return The full library name, with extension 427 | */ 428 | public static String createLibraryFileName(String libraryName) 429 | { 430 | String libPrefix = createLibraryPrefix(); 431 | String libExtension = createLibraryExtension(); 432 | String fullName = libPrefix + libraryName + "." + libExtension; 433 | return fullName; 434 | } 435 | 436 | 437 | /** 438 | * Returns the extension for dynamically linked libraries on the 439 | * current OS. That is, returns "dylib" on Apple, 440 | * "so" on Linux and Sun, and "dll" 441 | * on Windows. 442 | * 443 | * @return The library extension 444 | */ 445 | private static String createLibraryExtension() 446 | { 447 | OSType osType = calculateOS(); 448 | switch (osType) 449 | { 450 | case APPLE: 451 | return "dylib"; 452 | case ANDROID: 453 | case LINUX: 454 | case SUN: 455 | return "so"; 456 | case WINDOWS: 457 | return "dll"; 458 | default: 459 | break; 460 | } 461 | return ""; 462 | } 463 | 464 | /** 465 | * Returns the prefix for dynamically linked libraries on the 466 | * current OS. That is, returns "lib" on Apple, 467 | * Linux and Sun, and the empty String on Windows. 468 | * 469 | * @return The library prefix 470 | */ 471 | private static String createLibraryPrefix() 472 | { 473 | OSType osType = calculateOS(); 474 | switch (osType) 475 | { 476 | case ANDROID: 477 | case APPLE: 478 | case LINUX: 479 | case SUN: 480 | return "lib"; 481 | case WINDOWS: 482 | return ""; 483 | default: 484 | break; 485 | } 486 | return ""; 487 | } 488 | 489 | 490 | /** 491 | * Creates the name for the native library with the given base name for 492 | * the current platform, by appending strings that indicate the current 493 | * operating system and architecture.
494 | *
495 | * The resulting name will be of the form
496 | * baseName-OSType-ArchType
497 | * where OSType and ArchType are the lower case Strings 498 | * of the respective {@link LibUtils.OSType OSType} and 499 | * {@link LibUtils.ArchType ArcType} enum constants.
500 | *
501 | * For example, the library name with the base name "EXAMPLE" may be
502 | * EXAMPLE-windows-x86
503 | *
504 | * Note that the resulting name will not include any platform specific 505 | * prefixes or extensions for the actual name. 506 | * 507 | * @param baseName The base name of the library 508 | * @return The library name 509 | */ 510 | public static String createPlatformLibraryName(String baseName) 511 | { 512 | return baseName + "-" + osString() + "-" + archString(); 513 | } 514 | 515 | /** 516 | * Returns a the lower case String representation of 517 | * the {@link #calculateOS() OSType} of this platform. E.g. 518 | * "windows". 519 | * 520 | * @return The string describing the operating system 521 | */ 522 | private static String osString() 523 | { 524 | OSType osType = calculateOS(); 525 | return osType.toString().toLowerCase(Locale.ENGLISH); 526 | } 527 | 528 | /** 529 | * Returns a the lower case String representation of 530 | * the {@link #calculateArch() ArchType} of this platform. E.g. 531 | * "x86_64". 532 | * 533 | * @return The string describing the architecture 534 | */ 535 | private static String archString() 536 | { 537 | ArchType archType = calculateArch(); 538 | return archType.toString().toLowerCase(Locale.ENGLISH); 539 | } 540 | 541 | /** 542 | * Calculates the current OSType 543 | * 544 | * @return The current OSType 545 | */ 546 | static OSType calculateOS() 547 | { 548 | String vendor = System.getProperty("java.vendor"); 549 | if ("The Android Project".equals(vendor)) 550 | { 551 | return OSType.ANDROID; 552 | } 553 | String osName = System.getProperty("os.name"); 554 | osName = osName.toLowerCase(Locale.ENGLISH); 555 | if (osName.startsWith("mac os")) 556 | { 557 | return OSType.APPLE; 558 | } 559 | if (osName.startsWith("windows")) 560 | { 561 | return OSType.WINDOWS; 562 | } 563 | if (osName.startsWith("linux")) 564 | { 565 | return OSType.LINUX; 566 | } 567 | if (osName.startsWith("sun")) 568 | { 569 | return OSType.SUN; 570 | } 571 | return OSType.UNKNOWN; 572 | } 573 | 574 | 575 | /** 576 | * Calculates the current ARCHType 577 | * 578 | * @return The current ARCHType 579 | */ 580 | private static ArchType calculateArch() 581 | { 582 | String osArch = System.getProperty("os.arch"); 583 | osArch = osArch.toLowerCase(Locale.ENGLISH); 584 | if ("i386".equals(osArch) || 585 | "x86".equals(osArch) || 586 | "i686".equals(osArch)) 587 | { 588 | return ArchType.X86; 589 | } 590 | if (osArch.startsWith("amd64") || osArch.startsWith("x86_64")) 591 | { 592 | return ArchType.X86_64; 593 | } 594 | if (osArch.startsWith("arm64") || osArch.equals("aarch64")) 595 | { 596 | return ArchType.ARM64; 597 | } 598 | if (osArch.startsWith("arm")) 599 | { 600 | return ArchType.ARM; 601 | } 602 | if ("ppc".equals(osArch) || "powerpc".equals(osArch)) 603 | { 604 | return ArchType.PPC; 605 | } 606 | if (osArch.startsWith("ppc")) 607 | { 608 | return ArchType.PPC_64; 609 | } 610 | if (osArch.startsWith("sparc")) 611 | { 612 | return ArchType.SPARC; 613 | } 614 | if (osArch.startsWith("mips64")) 615 | { 616 | return ArchType.MIPS64; 617 | } 618 | if (osArch.startsWith("mips")) 619 | { 620 | return ArchType.MIPS; 621 | } 622 | if (osArch.contains("risc")) 623 | { 624 | return ArchType.RISC; 625 | } 626 | return ArchType.UNKNOWN; 627 | } 628 | } 629 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/MemObjectDestructorCallbackFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | /** 31 | * Emulation of a function pointer for functions that may be passed to the 32 | * {@link CL#clSetMemObjectDestructorCallback(cl_mem, MemObjectDestructorCallbackFunction, Object) 33 | * clSetMemObjectDestructorCallback} method. 34 | * 35 | * @see CL#clSetMemObjectDestructorCallback(cl_mem, MemObjectDestructorCallbackFunction, Object) 36 | */ 37 | public interface MemObjectDestructorCallbackFunction 38 | { 39 | /** 40 | * The function that will be called 41 | * 42 | * @param memobj The memory object 43 | * @param user_data The user data 44 | */ 45 | void function(cl_mem memobj, Object user_data); 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/NativePointerObject.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | import java.nio.Buffer; 31 | import java.util.Arrays; 32 | 33 | /** 34 | * Base class for all classes that store a native pointer 35 | */ 36 | public class NativePointerObject 37 | { 38 | /** 39 | * The native pointer, written by native methods 40 | */ 41 | private long nativePointer; 42 | 43 | 44 | // These fields are only for the use in the Pointer class, 45 | // but stored here to treat NativePointerObjects and 46 | // Pointers equally on JNI side: 47 | 48 | /** 49 | * The offset from the nativePointer, in bytes 50 | */ 51 | private long byteOffset; 52 | 53 | /** 54 | * The buffer the pointer points to 55 | */ 56 | private Buffer buffer; 57 | 58 | /** 59 | * The array of pointers this pointer points to 60 | */ 61 | private NativePointerObject pointers[]; 62 | 63 | 64 | /** 65 | * Creates a new (null) Pointer 66 | */ 67 | protected NativePointerObject() 68 | { 69 | buffer = null; 70 | pointers = null; 71 | byteOffset = 0; 72 | } 73 | 74 | /** 75 | * Creates a Pointer to the given Buffer 76 | * 77 | * @param buffer The buffer to point to 78 | */ 79 | NativePointerObject(Buffer buffer) 80 | { 81 | this.buffer = buffer; 82 | pointers = null; 83 | byteOffset = 0; 84 | } 85 | 86 | /** 87 | * Creates a Pointer to the given array of pointers 88 | * 89 | * @param pointers The array the pointer points to 90 | */ 91 | NativePointerObject(NativePointerObject pointers[]) 92 | { 93 | buffer = null; 94 | this.pointers = pointers; 95 | byteOffset = 0; 96 | } 97 | 98 | /** 99 | * Copy constructor 100 | * 101 | * @param other The other Pointer 102 | */ 103 | NativePointerObject(NativePointerObject other) 104 | { 105 | this.buffer = other.buffer; 106 | this.pointers = other.pointers; 107 | this.byteOffset = other.byteOffset; 108 | } 109 | 110 | /** 111 | * Creates a copy of the given pointer, with an 112 | * additional byte offset 113 | * 114 | * @param other The other pointer 115 | * @param byteOffset The additional byte offset 116 | */ 117 | NativePointerObject(NativePointerObject other, long byteOffset) 118 | { 119 | this(other); 120 | this.byteOffset += byteOffset; 121 | } 122 | 123 | /** 124 | * Method to obtain the native pointer value. 125 | * 126 | * Clients should usually not use this pointer value directly. 127 | * It is only intended for interoperability with other JNI based 128 | * libraries. 129 | * 130 | * @return The native pointer value 131 | */ 132 | public long getNativePointer() 133 | { 134 | return nativePointer; 135 | } 136 | 137 | /** 138 | * Returns the byte offset 139 | * 140 | * @return The byte offset 141 | */ 142 | long getByteOffset() 143 | { 144 | return byteOffset; 145 | } 146 | 147 | // TODO: --> Only used for cl_context_properties 148 | /** 149 | * Set the given buffer as the contents of this Pointer 150 | * 151 | * @param buffer The buffer to set 152 | */ 153 | void setBuffer(Buffer buffer) 154 | { 155 | this.buffer = buffer; 156 | pointers = null; 157 | byteOffset = 0; 158 | } 159 | 160 | /** 161 | * Returns the Buffer of this Pointer 162 | * 163 | * @return The Buffer of this Pointer 164 | */ 165 | Buffer getBuffer() 166 | { 167 | return buffer; 168 | } 169 | // TODO: <-- Only used for cl_context_properties 170 | 171 | 172 | /** 173 | * Returns a new pointer with an offset of the given number 174 | * of bytes 175 | * 176 | * @param byteOffset The byte offset for the pointer 177 | * @return The new pointer with the given byte offset 178 | */ 179 | NativePointerObject withByteOffset(long byteOffset) 180 | { 181 | return new NativePointerObject(this, byteOffset); 182 | } 183 | 184 | 185 | /** 186 | * Returns a String representation of this object. 187 | * 188 | * @return A String representation of this object. 189 | */ 190 | @Override 191 | public String toString() 192 | { 193 | if (buffer != null) 194 | { 195 | return "NativePointerObject["+ 196 | "buffer="+buffer+","+ 197 | "byteOffset="+byteOffset+"]"; 198 | 199 | } 200 | else if (pointers != null) 201 | { 202 | return "NativePointerObject["+ 203 | "pointers="+Arrays.toString(pointers)+","+ 204 | "byteOffset="+byteOffset+"]"; 205 | } 206 | else 207 | { 208 | return "NativePointerObject["+ 209 | "nativePointer=0x"+Long.toHexString(getNativePointer())+","+ 210 | "byteOffset="+byteOffset+"]"; 211 | } 212 | } 213 | 214 | 215 | @Override 216 | public int hashCode() 217 | { 218 | final int prime = 31; 219 | int result = 1; 220 | result = prime * result + ((buffer == null) ? 0 : buffer.hashCode()); 221 | result = prime * result + (int) (byteOffset ^ (byteOffset >>> 32)); 222 | result = prime * result + (int) (nativePointer ^ (nativePointer >>> 32)); 223 | result = prime * result + Arrays.hashCode(pointers); 224 | return result; 225 | } 226 | 227 | @Override 228 | public boolean equals(Object obj) 229 | { 230 | if (this == obj) 231 | return true; 232 | if (obj == null) 233 | return false; 234 | if (getClass() != obj.getClass()) 235 | return false; 236 | final NativePointerObject other = (NativePointerObject) obj; 237 | if (buffer == null) 238 | { 239 | if (other.buffer != null) 240 | return false; 241 | } 242 | else if (!buffer.equals(other.buffer)) 243 | return false; 244 | if (byteOffset != other.byteOffset) 245 | return false; 246 | if (nativePointer != other.nativePointer) 247 | return false; 248 | if (!Arrays.equals(pointers, other.pointers)) 249 | return false; 250 | return true; 251 | } 252 | 253 | 254 | } 255 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/Pointer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2015 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | import java.nio.*; 31 | 32 | /** 33 | * A Java representation of a void pointer. 34 | */ 35 | public final class Pointer extends NativePointerObject 36 | { 37 | /** 38 | * The error message that will be part of the IllegalArgumentException 39 | * when a null buffer is passed to the {@link #to(Buffer)} 40 | * or {@link #toBuffer(Buffer)} method 41 | */ 42 | private static final String BUFFER_MAY_NOT_BE_NULL = 43 | "The buffer may not be null"; 44 | 45 | /** 46 | * The error message that will be part of the IllegalArgumentException 47 | * when a buffer is passed to the {@link #toBuffer(Buffer)} method 48 | * neither has an array nor is direct 49 | */ 50 | private static final String BUFFER_MUST_HAVE_ARRAY_OR_BE_DIRECT = 51 | "The buffer must have an array or be direct"; 52 | 53 | /** 54 | * Creates a new (null) Pointer 55 | */ 56 | public Pointer() 57 | { 58 | super(); 59 | } 60 | 61 | /** 62 | * Creates a Pointer to the given Buffer 63 | * 64 | * @param buffer The buffer to point to 65 | */ 66 | protected Pointer(Buffer buffer) 67 | { 68 | super(buffer); 69 | } 70 | 71 | /** 72 | * Creates a Pointer to the given array of pointers 73 | * 74 | * @param pointers The array the pointer points to 75 | */ 76 | private Pointer(NativePointerObject[] pointers) 77 | { 78 | super(pointers); 79 | } 80 | 81 | /** 82 | * Copy constructor 83 | * 84 | * @param other The other Pointer 85 | */ 86 | protected Pointer(Pointer other) 87 | { 88 | super(other); 89 | } 90 | 91 | /** 92 | * Creates a copy of the given pointer, with an 93 | * additional byte offset 94 | * 95 | * @param other The other pointer 96 | * @param byteOffset The additional byte offset 97 | */ 98 | protected Pointer(Pointer other, long byteOffset) 99 | { 100 | super(other, byteOffset); 101 | } 102 | 103 | /** 104 | * Creates a new Pointer to the given values. 105 | * The values may not be null. 106 | * 107 | * @param values The values the pointer should point to 108 | * @return The pointer 109 | */ 110 | public static Pointer to(byte[] values) 111 | { 112 | return new Pointer(ByteBuffer.wrap(values)); 113 | } 114 | 115 | /** 116 | * Creates a new Pointer to the given values. 117 | * The values may not be null. 118 | * 119 | * @param values The values the pointer should point to 120 | * @return The pointer 121 | */ 122 | public static Pointer to(char[] values) 123 | { 124 | return new Pointer(CharBuffer.wrap(values)); 125 | } 126 | 127 | /** 128 | * Creates a new Pointer to the given values. 129 | * The values may not be null. 130 | * 131 | * @param values The values the pointer should point to 132 | * @return The pointer 133 | */ 134 | public static Pointer to(short[] values) 135 | { 136 | return new Pointer(ShortBuffer.wrap(values)); 137 | } 138 | 139 | /** 140 | * Creates a new Pointer to the given values. 141 | * The values may not be null. 142 | * 143 | * @param values The values the pointer should point to 144 | * @return The pointer 145 | */ 146 | public static Pointer to(int[] values) 147 | { 148 | return new Pointer(IntBuffer.wrap(values)); 149 | } 150 | 151 | /** 152 | * Creates a new Pointer to the given values. 153 | * The values may not be null. 154 | * 155 | * @param values The values the pointer should point to 156 | * @return The pointer 157 | */ 158 | public static Pointer to(float[] values) 159 | { 160 | return new Pointer(FloatBuffer.wrap(values)); 161 | } 162 | 163 | /** 164 | * Creates a new Pointer to the given values. 165 | * The values may not be null. 166 | * 167 | * @param values The values the pointer should point to 168 | * @return The pointer 169 | */ 170 | public static Pointer to(long[] values) 171 | { 172 | return new Pointer(LongBuffer.wrap(values)); 173 | } 174 | 175 | /** 176 | * Creates a new Pointer to the given values. 177 | * The values may not be null. 178 | * 179 | * @param values The values the pointer should point to 180 | * @return The pointer 181 | */ 182 | public static Pointer to(double[] values) 183 | { 184 | return new Pointer(DoubleBuffer.wrap(values)); 185 | } 186 | 187 | 188 | 189 | /** 190 | * NOTE: This method does not take into account the position 191 | * and array offset of the given buffer. In order to create a 192 | * pointer that takes the position and array offset into account, 193 | * use the {@link #toBuffer(Buffer)} method.
194 | *
195 | * 196 | * If the given buffer has a backing array, then the returned 197 | * pointer will in any case point to the start of the array, 198 | * even if the buffer has been created using the slice 199 | * method (like {@link ByteBuffer#slice()}). If the buffer is 200 | * direct, then this method will return a Pointer to the address 201 | * of the direct buffer. If the buffer has been created using the 202 | * slice method, then this will be the actual start 203 | * of the slice. Although this implies a different treatment of 204 | * direct- and non direct buffers, the method is kept for 205 | * backward compatibility.
206 | *
207 | * In both cases, for direct and array-based buffers, this method 208 | * does not take into account the position of the given buffer.
209 | *
210 | * The buffer must not be null, and either be a direct buffer, or 211 | * have a backing array 212 | * 213 | * @param buffer The buffer the pointer should point to 214 | * @return The pointer 215 | * @throws IllegalArgumentException If the given buffer 216 | * is null or is neither direct nor has a backing array 217 | */ 218 | public static Pointer to(Buffer buffer) 219 | { 220 | if (buffer == null) 221 | { 222 | throw new IllegalArgumentException(BUFFER_MAY_NOT_BE_NULL); 223 | } 224 | if (!buffer.isDirect() && !buffer.hasArray()) 225 | { 226 | throw new IllegalArgumentException( 227 | BUFFER_MUST_HAVE_ARRAY_OR_BE_DIRECT); 228 | } 229 | return new Pointer(buffer); 230 | } 231 | 232 | /** 233 | * Creates a new Pointer to the given buffer.
234 | *
235 | * Note that this method takes into account the array offset and position 236 | * of the given buffer, in contrast to the {@link #to(Buffer)} method. 237 | * 238 | * @param buffer The buffer 239 | * @return The new pointer 240 | * @throws IllegalArgumentException If the given buffer 241 | * is null or is neither direct nor has a backing array 242 | */ 243 | public static Pointer toBuffer(Buffer buffer) 244 | { 245 | if (buffer == null) 246 | { 247 | throw new IllegalArgumentException(BUFFER_MAY_NOT_BE_NULL); 248 | } 249 | if (buffer instanceof ByteBuffer) 250 | { 251 | return computePointer((ByteBuffer)buffer); 252 | } 253 | if (buffer instanceof ShortBuffer) 254 | { 255 | return computePointer((ShortBuffer)buffer); 256 | } 257 | if (buffer instanceof IntBuffer) 258 | { 259 | return computePointer((IntBuffer)buffer); 260 | } 261 | if (buffer instanceof LongBuffer) 262 | { 263 | return computePointer((LongBuffer)buffer); 264 | } 265 | if (buffer instanceof FloatBuffer) 266 | { 267 | return computePointer((FloatBuffer)buffer); 268 | } 269 | if (buffer instanceof DoubleBuffer) 270 | { 271 | return computePointer((DoubleBuffer)buffer); 272 | } 273 | throw new IllegalArgumentException( 274 | "Unknown buffer type: "+buffer); 275 | 276 | } 277 | 278 | /** 279 | * Creates a new Pointer to the given buffer, taking into 280 | * account the position and array offset of the given buffer. 281 | * 282 | * @param buffer The buffer 283 | * @return The pointer 284 | * @throws IllegalArgumentException If the given buffer 285 | * is neither direct nor has a backing array 286 | */ 287 | private static Pointer computePointer(ByteBuffer buffer) 288 | { 289 | Pointer result; 290 | if (buffer.isDirect()) 291 | { 292 | int oldPosition = buffer.position(); 293 | buffer.position(0); 294 | result = Pointer.to(buffer.slice()).withByteOffset( 295 | oldPosition * Sizeof.cl_char); 296 | buffer.position(oldPosition); 297 | } 298 | else if (buffer.hasArray()) 299 | { 300 | ByteBuffer t = ByteBuffer.wrap(buffer.array()); 301 | int elementOffset = buffer.position() + buffer.arrayOffset(); 302 | result = Pointer.to(t).withByteOffset( 303 | elementOffset * Sizeof.cl_char); 304 | } 305 | else 306 | { 307 | throw new IllegalArgumentException( 308 | BUFFER_MUST_HAVE_ARRAY_OR_BE_DIRECT); 309 | } 310 | return result; 311 | } 312 | 313 | 314 | /** 315 | * Creates a new Pointer to the given buffer, taking into 316 | * account the position and array offset of the given buffer. 317 | * 318 | * @param buffer The buffer 319 | * @return The pointer 320 | * @throws IllegalArgumentException If the given buffer 321 | * is neither direct nor has a backing array 322 | */ 323 | private static Pointer computePointer(ShortBuffer buffer) 324 | { 325 | Pointer result; 326 | if (buffer.isDirect()) 327 | { 328 | int oldPosition = buffer.position(); 329 | buffer.position(0); 330 | result = Pointer.to(buffer.slice()).withByteOffset( 331 | oldPosition * Sizeof.cl_short); 332 | buffer.position(oldPosition); 333 | } 334 | else if (buffer.hasArray()) 335 | { 336 | ShortBuffer t = ShortBuffer.wrap(buffer.array()); 337 | int elementOffset = buffer.position() + buffer.arrayOffset(); 338 | result = Pointer.to(t).withByteOffset( 339 | elementOffset * Sizeof.cl_short); 340 | } 341 | else 342 | { 343 | throw new IllegalArgumentException( 344 | BUFFER_MUST_HAVE_ARRAY_OR_BE_DIRECT); 345 | } 346 | return result; 347 | } 348 | 349 | 350 | /** 351 | * Creates a new Pointer to the given buffer, taking into 352 | * account the position and array offset of the given buffer. 353 | * 354 | * @param buffer The buffer 355 | * @return The pointer 356 | * @throws IllegalArgumentException If the given buffer 357 | * is neither direct nor has a backing array 358 | */ 359 | private static Pointer computePointer(IntBuffer buffer) 360 | { 361 | Pointer result; 362 | if (buffer.isDirect()) 363 | { 364 | int oldPosition = buffer.position(); 365 | buffer.position(0); 366 | result = Pointer.to(buffer.slice()).withByteOffset( 367 | oldPosition * Sizeof.cl_int); 368 | buffer.position(oldPosition); 369 | } 370 | else if (buffer.hasArray()) 371 | { 372 | IntBuffer t = IntBuffer.wrap(buffer.array()); 373 | int elementOffset = buffer.position() + buffer.arrayOffset(); 374 | result = Pointer.to(t).withByteOffset( 375 | elementOffset * Sizeof.cl_int); 376 | } 377 | else 378 | { 379 | throw new IllegalArgumentException( 380 | BUFFER_MUST_HAVE_ARRAY_OR_BE_DIRECT); 381 | } 382 | return result; 383 | } 384 | 385 | 386 | /** 387 | * Creates a new Pointer to the given buffer, taking into 388 | * account the position and array offset of the given buffer. 389 | * 390 | * @param buffer The buffer 391 | * @return The pointer 392 | * @throws IllegalArgumentException If the given buffer 393 | * is neither direct nor has a backing array 394 | */ 395 | private static Pointer computePointer(LongBuffer buffer) 396 | { 397 | Pointer result; 398 | if (buffer.isDirect()) 399 | { 400 | int oldPosition = buffer.position(); 401 | buffer.position(0); 402 | result = Pointer.to(buffer.slice()).withByteOffset( 403 | oldPosition * Sizeof.cl_long); 404 | buffer.position(oldPosition); 405 | } 406 | else if (buffer.hasArray()) 407 | { 408 | LongBuffer t = LongBuffer.wrap(buffer.array()); 409 | int elementOffset = buffer.position() + buffer.arrayOffset(); 410 | result = Pointer.to(t).withByteOffset( 411 | elementOffset * Sizeof.cl_long); 412 | } 413 | else 414 | { 415 | throw new IllegalArgumentException( 416 | BUFFER_MUST_HAVE_ARRAY_OR_BE_DIRECT); 417 | } 418 | return result; 419 | } 420 | 421 | 422 | /** 423 | * Creates a new Pointer to the given buffer, taking into 424 | * account the position and array offset of the given buffer. 425 | * 426 | * @param buffer The buffer 427 | * @return The pointer 428 | * @throws IllegalArgumentException If the given buffer 429 | * is neither direct nor has a backing array 430 | */ 431 | private static Pointer computePointer(FloatBuffer buffer) 432 | { 433 | Pointer result; 434 | if (buffer.isDirect()) 435 | { 436 | int oldPosition = buffer.position(); 437 | buffer.position(0); 438 | result = Pointer.to(buffer.slice()).withByteOffset( 439 | oldPosition * Sizeof.cl_float); 440 | buffer.position(oldPosition); 441 | } 442 | else if (buffer.hasArray()) 443 | { 444 | FloatBuffer t = FloatBuffer.wrap(buffer.array()); 445 | int elementOffset = buffer.position() + buffer.arrayOffset(); 446 | result = Pointer.to(t).withByteOffset( 447 | elementOffset * Sizeof.cl_float); 448 | } 449 | else 450 | { 451 | throw new IllegalArgumentException( 452 | BUFFER_MUST_HAVE_ARRAY_OR_BE_DIRECT); 453 | } 454 | return result; 455 | } 456 | 457 | 458 | /** 459 | * Creates a new Pointer to the given buffer, taking into 460 | * account the position and array offset of the given buffer. 461 | * 462 | * @param buffer The buffer 463 | * @return The pointer 464 | * @throws IllegalArgumentException If the given buffer 465 | * is neither direct nor has a backing array 466 | */ 467 | private static Pointer computePointer(DoubleBuffer buffer) 468 | { 469 | Pointer result; 470 | if (buffer.isDirect()) 471 | { 472 | int oldPosition = buffer.position(); 473 | buffer.position(0); 474 | result = Pointer.to(buffer.slice()).withByteOffset( 475 | oldPosition * Sizeof.cl_double); 476 | buffer.position(oldPosition); 477 | } 478 | else if (buffer.hasArray()) 479 | { 480 | DoubleBuffer t = DoubleBuffer.wrap(buffer.array()); 481 | int elementOffset = buffer.position() + buffer.arrayOffset(); 482 | result = Pointer.to(t).withByteOffset( 483 | elementOffset * Sizeof.cl_double); 484 | } 485 | else 486 | { 487 | throw new IllegalArgumentException( 488 | BUFFER_MUST_HAVE_ARRAY_OR_BE_DIRECT); 489 | } 490 | return result; 491 | } 492 | 493 | 494 | 495 | 496 | /** 497 | * Creates a new Pointer to the given Pointer. The pointer 498 | * may not be null. 499 | * 500 | * @param pointer The pointer the pointer should point to 501 | * @return The new pointer 502 | * @throws IllegalArgumentException If the given pointer 503 | * is null 504 | */ 505 | public static Pointer to(NativePointerObject pointer) 506 | { 507 | if (pointer == null) 508 | { 509 | throw new IllegalArgumentException( 510 | "Pointer may not point to null objects"); 511 | } 512 | return new Pointer(new NativePointerObject[]{pointer}); 513 | } 514 | 515 | /** 516 | * Creates a new Pointer to the given Pointers. The array 517 | * of pointers may not be null, and may not contain null 518 | * elements. 519 | * 520 | * @param pointers The pointers the pointer should point to 521 | * @return The new pointer 522 | * @throws IllegalArgumentException If the given array 523 | * is null 524 | */ 525 | public static Pointer to(NativePointerObject ... pointers) 526 | { 527 | if (pointers == null) 528 | { 529 | throw new IllegalArgumentException( 530 | "Pointer may not point to null objects"); 531 | } 532 | return new Pointer(pointers); 533 | } 534 | 535 | 536 | /** 537 | * Returns whether this Pointer is a Pointer to a direct Buffer. 538 | * 539 | * @return Whether this pointer is a Pointer to a direct Buffer 540 | */ 541 | boolean isDirectBufferPointer() 542 | { 543 | return getBuffer() != null && getBuffer().isDirect(); 544 | } 545 | 546 | 547 | /** 548 | * Returns a new pointer with an offset of the given number 549 | * of bytes 550 | * 551 | * @param byteOffset The byte offset for the pointer 552 | * @return The new pointer with the given byte offset 553 | */ 554 | public Pointer withByteOffset(long byteOffset) 555 | { 556 | return new Pointer(this, byteOffset); 557 | } 558 | 559 | 560 | 561 | /** 562 | * Returns a ByteBuffer that corresponds to the specified 563 | * segment of the memory that this pointer points to.
564 | *
565 | * This function is solely intended for pointers that that 566 | * have been allocated with {@link CL#clSVMAlloc}. 567 | *
568 | * (It will work for all pointers to ByteBuffers, but for 569 | * other pointer types, null will be returned) 570 | * 571 | * @param byteOffset The offset in bytes 572 | * @param byteSize The size of the byte buffer, in bytes 573 | * @return The byte buffer 574 | */ 575 | public ByteBuffer getByteBuffer(long byteOffset, long byteSize) 576 | { 577 | Buffer buffer = getBuffer(); 578 | if (buffer == null) 579 | { 580 | return null; 581 | } 582 | if (!(buffer instanceof ByteBuffer)) 583 | { 584 | return null; 585 | } 586 | ByteBuffer byteBuffer = (ByteBuffer)buffer; 587 | byteBuffer.limit((int)(byteOffset + byteSize)); 588 | byteBuffer.position((int)byteOffset); 589 | return byteBuffer.slice(); 590 | } 591 | 592 | 593 | } 594 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/PrintfCallbackFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | /** 31 | * Emulation of a function pointer that may be passed to the 32 | * {@link CL#clSetPrintfCallback(cl_context, PrintfCallbackFunction, Object)} 33 | * method. 34 | * 35 | * @see CL#clSetPrintfCallback(cl_context, PrintfCallbackFunction, Object) 36 | */ 37 | public interface PrintfCallbackFunction 38 | { 39 | /** 40 | * The function that will be called 41 | * 42 | * @param context The context 43 | * @param printf_data_len The length of the printf data 44 | * @param printf_data_ptr The printf data 45 | * @param user_data The user data 46 | */ 47 | void function(cl_context context , int printf_data_len , String printf_data_ptr , Object user_data); 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/SVMFreeFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2014 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | /** 31 | * Emulation of a function pointer that may be passed to the 32 | * {@link CL#clEnqueueSVMFree(cl_command_queue, int, Pointer[], SVMFreeFunction, 33 | * Object, int, cl_event[], cl_event)} method 34 | * 35 | * @see CL#clEnqueueSVMFree 36 | */ 37 | public interface SVMFreeFunction 38 | { 39 | /** 40 | * The function that will be called 41 | * 42 | * @param queue The command queue 43 | * @param num_svm_pointers The number of pointers 44 | * @param svm_pointers The pointers 45 | * @param user_data The user data 46 | */ 47 | void function(cl_command_queue queue, int num_svm_pointers, Pointer svm_pointers[], Object user_data); 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/Sizeof.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | 31 | import java.util.logging.Level; 32 | import java.util.logging.Logger; 33 | 34 | /** 35 | * Size constants for scalar and vector data types. 36 | */ 37 | public final class Sizeof 38 | { 39 | 40 | /** 41 | * The logger used in this class 42 | */ 43 | private static final Logger logger = 44 | Logger.getLogger(Sizeof.class.getName()); 45 | 46 | static 47 | { 48 | CL.loadNativeLibrary(); 49 | } 50 | 51 | /** 52 | * Size of a cl_char, in bytes. 53 | */ 54 | public static final int cl_char = 8 / 8; 55 | 56 | /** 57 | * Size of a cl_uchar, in bytes. 58 | */ 59 | public static final int cl_uchar = 8 / 8; 60 | 61 | /** 62 | * Size of a cl_short, in bytes. 63 | */ 64 | public static final int cl_short = 16 / 8; 65 | 66 | /** 67 | * Size of a cl_ushort, in bytes. 68 | */ 69 | public static final int cl_ushort = 16 / 8; 70 | 71 | /** 72 | * Size of a cl_int, in bytes. 73 | */ 74 | public static final int cl_int = 32 / 8; 75 | 76 | /** 77 | * Size of a cl_uint, in bytes. 78 | */ 79 | public static final int cl_uint = 32 / 8; 80 | 81 | /** 82 | * Size of a cl_long, in bytes. 83 | */ 84 | public static final int cl_long = 64 / 8; 85 | 86 | /** 87 | * Size of a cl_ulong, in bytes. 88 | */ 89 | public static final int cl_ulong = 64 / 8; 90 | 91 | /** 92 | * Size of a cl_half, in bytes. 93 | */ 94 | public static final int cl_half = 16 / 8; 95 | 96 | /** 97 | * Size of a cl_float, in bytes. 98 | */ 99 | public static final int cl_float = 32 / 8; 100 | 101 | /** 102 | * Size of a cl_double, in bytes. 103 | */ 104 | public static final int cl_double = 64 / 8; 105 | 106 | /** 107 | * Size of a cl_char2, in bytes 108 | */ 109 | public static final int cl_char2 = 8 * 2 / 8; 110 | 111 | /** 112 | * Size of a cl_char4, in bytes 113 | */ 114 | public static final int cl_char4 = 8 * 4 / 8; 115 | 116 | /** 117 | * Size of a cl_char8, in bytes 118 | */ 119 | public static final int cl_char8 = 8 * 8 / 8; 120 | 121 | /** 122 | * Size of a cl_char16, in bytes 123 | */ 124 | public static final int cl_char16 = 8 * 16 / 8; 125 | 126 | /** 127 | * Size of a cl_char3, in bytes.
128 | *
129 | * This is the same as the size of a cl_char4, according to the OpenCL 130 | * specification, version 1.1, section 6.1.5 131 | */ 132 | public static final int cl_char3 = cl_char4; 133 | 134 | /** 135 | * Size of a cl_uchar2, in bytes 136 | */ 137 | public static final int cl_uchar2 = 8 * 2 / 8; 138 | 139 | /** 140 | * Size of a cl_uchar4, in bytes 141 | */ 142 | public static final int cl_uchar4 = 8 * 4 / 8; 143 | 144 | /** 145 | * Size of a cl_uchar8, in bytes 146 | */ 147 | public static final int cl_uchar8 = 8 * 8 / 8; 148 | 149 | /** 150 | * Size of a cl_uchar16, in bytes 151 | */ 152 | public static final int cl_uchar16 = 8 * 16 / 8; 153 | 154 | /** 155 | * Size of a cl_uchar3, in bytes.
156 | *
157 | * This is the same as the size of a cl_uchar4, according to the OpenCL 158 | * specification, version 1.1, section 6.1.5 159 | */ 160 | public static final int cl_uchar3 = cl_uchar4; 161 | 162 | /** 163 | * Size of a cl_short2, in bytes 164 | */ 165 | public static final int cl_short2 = 16 * 2 / 8; 166 | 167 | /** 168 | * Size of a cl_short4, in bytes 169 | */ 170 | public static final int cl_short4 = 16 * 4 / 8; 171 | 172 | /** 173 | * Size of a cl_short8, in bytes 174 | */ 175 | public static final int cl_short8 = 16 * 8 / 8; 176 | 177 | /** 178 | * Size of a cl_short16, in bytes 179 | */ 180 | public static final int cl_short16 = 16 * 16 / 8; 181 | 182 | /** 183 | * Size of a cl_short3, in bytes.
184 | *
185 | * This is the same as the size of a cl_short4, according to the OpenCL 186 | * specification, version 1.1, section 6.1.5 187 | */ 188 | public static final int cl_short3 = cl_short4; 189 | 190 | /** 191 | * Size of a cl_ushort2, in bytes 192 | */ 193 | public static final int cl_ushort2 = 16 * 2 / 8; 194 | 195 | /** 196 | * Size of a cl_ushort4, in bytes 197 | */ 198 | public static final int cl_ushort4 = 16 * 4 / 8; 199 | 200 | /** 201 | * Size of a cl_ushort8, in bytes 202 | */ 203 | public static final int cl_ushort8 = 16 * 8 / 8; 204 | 205 | /** 206 | * Size of a cl_ushort16, in bytes 207 | */ 208 | public static final int cl_ushort16 = 16 * 16 / 8; 209 | 210 | /** 211 | * Size of a cl_ushort3, in bytes.
212 | *
213 | * This is the same as the size of a cl_ushort4, according to the OpenCL 214 | * specification, version 1.1, section 6.1.5 215 | */ 216 | public static final int cl_ushort3 = cl_ushort4; 217 | 218 | /** 219 | * Size of a cl_int2, in bytes 220 | */ 221 | public static final int cl_int2 = 32 * 2 / 8; 222 | 223 | /** 224 | * Size of a cl_int4, in bytes 225 | */ 226 | public static final int cl_int4 = 32 * 4 / 8; 227 | 228 | /** 229 | * Size of a cl_int8, in bytes 230 | */ 231 | public static final int cl_int8 = 32 * 8 / 8; 232 | 233 | /** 234 | * Size of a cl_int16, in bytes 235 | */ 236 | public static final int cl_int16 = 32 * 16 / 8; 237 | 238 | /** 239 | * Size of a cl_int3, in bytes.
240 | *
241 | * This is the same as the size of a cl_int4, according to the OpenCL 242 | * specification, version 1.1, section 6.1.5 243 | */ 244 | public static final int cl_int3 = cl_int4; 245 | 246 | /** 247 | * Size of a cl_uint2, in bytes 248 | */ 249 | public static final int cl_uint2 = 32 * 2 / 8; 250 | 251 | /** 252 | * Size of a cl_uint4, in bytes 253 | */ 254 | public static final int cl_uint4 = 32 * 4 / 8; 255 | 256 | /** 257 | * Size of a cl_uint8, in bytes 258 | */ 259 | public static final int cl_uint8 = 32 * 8 / 8; 260 | 261 | /** 262 | * Size of a cl_uint16, in bytes 263 | */ 264 | public static final int cl_uint16 = 32 * 16 / 8; 265 | 266 | /** 267 | * Size of a cl_uint3, in bytes.
268 | *
269 | * This is the same as the size of a cl_uint4, according to the OpenCL 270 | * specification, version 1.1, section 6.1.5 271 | */ 272 | public static final int cl_uint3 = cl_uint4; 273 | 274 | /** 275 | * Size of a cl_long2, in bytes 276 | */ 277 | public static final int cl_long2 = 64 * 2 / 8; 278 | 279 | /** 280 | * Size of a cl_long4, in bytes 281 | */ 282 | public static final int cl_long4 = 64 * 4 / 8; 283 | 284 | /** 285 | * Size of a cl_long8, in bytes 286 | */ 287 | public static final int cl_long8 = 64 * 8 / 8; 288 | 289 | /** 290 | * Size of a cl_long16, in bytes 291 | */ 292 | public static final int cl_long16 = 64 * 16 / 8; 293 | 294 | /** 295 | * Size of a cl_long3, in bytes.
296 | *
297 | * This is the same as the size of a cl_long4, according to the OpenCL 298 | * specification, version 1.1, section 6.1.5 299 | */ 300 | public static final int cl_long3 = cl_long4; 301 | 302 | /** 303 | * Size of a cl_ulong2, in bytes 304 | */ 305 | public static final int cl_ulong2 = 64 * 2 / 8; 306 | 307 | /** 308 | * Size of a cl_ulong4, in bytes 309 | */ 310 | public static final int cl_ulong4 = 64 * 4 / 8; 311 | 312 | /** 313 | * Size of a cl_ulong8, in bytes 314 | */ 315 | public static final int cl_ulong8 = 64 * 8 / 8; 316 | 317 | /** 318 | * Size of a cl_ulong16, in bytes 319 | */ 320 | public static final int cl_ulong16 = 64 * 16 / 8; 321 | 322 | /** 323 | * Size of a cl_ulong3, in bytes.
324 | *
325 | * This is the same as the size of a cl_ulong4, according to the OpenCL 326 | * specification, version 1.1, section 6.1.5 327 | */ 328 | public static final int cl_ulong3 = cl_ulong4; 329 | 330 | /** 331 | * Size of a cl_float2, in bytes 332 | */ 333 | public static final int cl_float2 = 32 * 2 / 8; 334 | 335 | /** 336 | * Size of a cl_float4, in bytes 337 | */ 338 | public static final int cl_float4 = 32 * 4 / 8; 339 | 340 | /** 341 | * Size of a cl_float8, in bytes 342 | */ 343 | public static final int cl_float8 = 32 * 8 / 8; 344 | 345 | /** 346 | * Size of a cl_float16, in bytes 347 | */ 348 | public static final int cl_float16 = 32 * 16 / 8; 349 | 350 | /** 351 | * Size of a cl_float3, in bytes.
352 | *
353 | * This is the same as the size of a cl_float4, according to the OpenCL 354 | * specification, version 1.1, section 6.1.5 355 | */ 356 | public static final int cl_float3 = cl_float4; 357 | 358 | /** 359 | * Size of a cl_double2, in bytes 360 | */ 361 | public static final int cl_double2 = 64 * 2 / 8; 362 | 363 | /** 364 | * Size of a cl_double4, in bytes 365 | */ 366 | public static final int cl_double4 = 64 * 4 / 8; 367 | 368 | /** 369 | * Size of a cl_double8, in bytes 370 | */ 371 | public static final int cl_double8 = 64 * 8 / 8; 372 | 373 | /** 374 | * Size of a cl_double16, in bytes 375 | */ 376 | public static final int cl_double16 = 64 * 16 / 8; 377 | 378 | /** 379 | * Size of a cl_double3, in bytes.
380 | *
381 | * This is the same as the size of a cl_double4, according to the OpenCL 382 | * specification, version 1.1, section 6.1.5 383 | */ 384 | public static final int cl_double3 = cl_double4; 385 | 386 | /** 387 | * Size of a native pointer, in bytes 388 | */ 389 | public static final int POINTER = computePointerSize(); 390 | 391 | /** 392 | * Size of a size_t, in bytes 393 | */ 394 | // TODO: This is assumed to be the same as the pointer size, 395 | // although this is not clearly specified in the C standard. 396 | public static final int size_t = POINTER; 397 | 398 | /** 399 | * Size of a cl_platform_id, in bytes 400 | */ 401 | public static final int cl_platform_id = POINTER; 402 | 403 | /** 404 | * Size of a cl_device_id, in bytes 405 | */ 406 | public static final int cl_device_id = POINTER; 407 | 408 | /** 409 | * Size of a cl_context, in bytes 410 | */ 411 | public static final int cl_context = POINTER; 412 | 413 | /** 414 | * Size of a cl_command_queue, in bytes 415 | */ 416 | public static final int cl_command_queue = POINTER; 417 | 418 | /** 419 | * Size of a cl_mem, in bytes 420 | */ 421 | public static final int cl_mem = POINTER; 422 | 423 | /** 424 | * Size of a cl_program, in bytes 425 | */ 426 | public static final int cl_program = POINTER; 427 | 428 | /** 429 | * Size of a cl_kernel, in bytes 430 | */ 431 | public static final int cl_kernel = POINTER; 432 | 433 | /** 434 | * Size of a cl_event, in bytes 435 | */ 436 | public static final int cl_event = POINTER; 437 | 438 | /** 439 | * Size of a cl_sampler, in bytes 440 | */ 441 | public static final int cl_sampler = POINTER; 442 | 443 | 444 | /** 445 | * Computes the size of a pointer, in bytes 446 | * 447 | * @return The size of a pointer, in bytes 448 | */ 449 | private static int computePointerSize() 450 | { 451 | // Android does not have the "sun.arch.data.model" system property, 452 | // so the pointer size is computed with a native method 453 | if (LibUtils.calculateOS() == LibUtils.OSType.ANDROID) 454 | { 455 | return computePointerSizeNative(); 456 | } 457 | 458 | // Desktop systems may fail to load the native library for the 459 | // initialization of the (constant) fields in this class, so 460 | // the pointer size is determined without native methods here. 461 | // See https://github.com/gpu/JOCL/issues/5 462 | String bits = System.getProperty("sun.arch.data.model"); 463 | if ("32".equals(bits)) 464 | { 465 | return 4; 466 | } 467 | if ("64".equals(bits)) 468 | { 469 | return 8; 470 | } 471 | logger.log(Level.SEVERE, 472 | "Unknown value for sun.arch.data.model - assuming 32 bits"); 473 | return 4; 474 | } 475 | private static native int computePointerSizeNative(); 476 | } 477 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/cl_abstract_properties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | import java.nio.LongBuffer; 31 | 32 | /** 33 | * Abstract base class for CL properties, like cl_context_properties 34 | * and cl_device_partition_property 35 | */ 36 | abstract class cl_abstract_properties extends NativePointerObject 37 | { 38 | /** 39 | * Creates new, empty cl_abstract_properties 40 | */ 41 | cl_abstract_properties() 42 | { 43 | super(LongBuffer.wrap(new long[]{0})); 44 | } 45 | 46 | /** 47 | * Add the specified property to these properties 48 | * 49 | * @param id The property ID 50 | * @param value The property value 51 | */ 52 | public void addProperty(long id, long value) 53 | { 54 | LongBuffer oldBuffer = (LongBuffer)getBuffer(); 55 | long newArray[] = new long[oldBuffer.capacity()+2]; 56 | oldBuffer.get(newArray, 0, oldBuffer.capacity()); 57 | newArray[oldBuffer.capacity()-1] = id; 58 | newArray[oldBuffer.capacity()+0] = value; 59 | newArray[oldBuffer.capacity()+1] = 0; 60 | setBuffer(LongBuffer.wrap(newArray)); 61 | } 62 | 63 | /** 64 | * Returns the string identifying the given property 65 | * @param value The property value 66 | * @return The string representation 67 | */ 68 | protected abstract String propertyString(long value); 69 | 70 | /** 71 | * Returns a String containing the contents of these properties 72 | * 73 | * @return A String representation of the contents of these properties 74 | */ 75 | protected String buildString() 76 | { 77 | StringBuilder result = new StringBuilder(); 78 | LongBuffer buffer = (LongBuffer)getBuffer(); 79 | int entries = buffer.capacity() / 2; 80 | for (int i=0; icl_mem_object_type 37 | * and must be either CL_MEM_OBJECT_IMAGE1D, 38 | * CL_MEM_OBJECT_IMAGE1D_BUFFER, CL_MEM_OBJECT_IMAGE1D_ARRAY, 39 | * CL_MEM_OBJECT_IMAGE2D, CL_MEM_OBJECT_IMAGE2D_ARRAY or 40 | * CL_MEM_OBJECT_IMAGE3D. 41 | */ 42 | public int image_type; 43 | 44 | /** 45 | * The width of the image in pixels. For a 2D image and image array, 46 | * the image width must be <= CL_DEVICE_IMAGE2D_MAX_WIDTH. For a 3D 47 | * image, the image width must be <= CL_DEVICE_IMAGE3D_MAX_WIDTH. 48 | * For a 1D image buffer, the image width must be <= 49 | * CL_DEVICE_IMAGE_MAX_BUFFER_SIZE. For a 1D image and 1D image array, 50 | * the image width must be <= CL_DEVICE_IMAGE2D_MAX_WIDTH. 51 | */ 52 | public long image_width; 53 | 54 | /** 55 | * Height of the image in pixels. This is only used if the image is a 56 | * 2D, 3D or 2D image array. For a 2D image or image array, the image 57 | * height must be <= CL_DEVICE_IMAGE2D_MAX_HEIGHT. For a 3D image, 58 | * the image height must be <= CL_DEVICE_IMAGE3D_MAX_HEIGHT. 59 | */ 60 | public long image_height; 61 | 62 | /** 63 | * The depth of the image in pixels. This is only used if the image 64 | * is a 3D image and must be a value >= 1 and <= 65 | * CL_DEVICE_IMAGE3D_MAX_DEPTH. 66 | */ 67 | public long image_depth; 68 | 69 | /** 70 | * The number of images in the image array. This is only used if the 71 | * image is a 1D or 2D image array. The values for image_array_size, 72 | * if specified, must be a value >= 1 and <= 73 | * CL_DEVICE_IMAGE_MAX_ARRAY_SIZE. 74 | */ 75 | public long image_array_size; 76 | 77 | /** 78 | * The scan-line pitch in bytes. This must be 0 if host_ptr is 79 | * NULL and can be either 0 or >= image_width * size of element 80 | * in bytes if host_ptr is not NULL. If host_ptr is not NULL and 81 | * image_row_pitch = 0, image_row_pitch is calculated as 82 | * image_width * size of element in bytes. If image_row_pitch is 83 | * not 0, it must be a multiple of the image element size in bytes 84 | */ 85 | public long image_row_pitch; 86 | 87 | /** 88 | * The size in bytes of each 2D slice in the 3D image or the size 89 | * in bytes of each image in a 1D or 2D image array. This must be 90 | * 0 if host_ptr is NULL. If host_ptr is not NULL, image_slice_pitch 91 | * can be either 0 or >= image_row_pitch * image_height for a 2D 92 | * image array or 3D image and can be either 0 or >= 93 | * image_row_pitch for a 1D image array. If host_ptr is not NULL and 94 | * image_slice_pitch = 0, image_slice_pitch is calculated as 95 | * image_row_pitch * image_height for a 2D image array or 3D image 96 | * and image_row_pitch for a 1D image array. If image_slice_pitch 97 | * is not 0, it must be a multiple of the image_row_pitch 98 | */ 99 | public long image_slice_pitch; 100 | 101 | /** 102 | * Must be 0. 103 | */ 104 | public int num_mip_levels; 105 | 106 | /** 107 | * Must be 0. 108 | */ 109 | public int num_samples; 110 | 111 | /** 112 | * buffer refers to a valid buffer memory object if image_type is 113 | * CL_MEM_OBJECT_IMAGE1D_BUFFER. Otherwise it must be NULL. For a 114 | * 1D image buffer object, the image pixels are taken from the 115 | * buffer object's data store. When the contents of a buffer 116 | * object's data store are modified, those changes are reflected 117 | * in the contents of the 1D image buffer object and vice-versa 118 | * at corresponding synchronization points. The image_width * size 119 | * of element in bytes must be <= size of buffer object data store. 120 | */ 121 | public cl_mem buffer; 122 | 123 | /** 124 | * Creates a new, uninitialized cl_image_desc 125 | */ 126 | public cl_image_desc() 127 | { 128 | } 129 | 130 | /** 131 | * Returns a String representation of this object. 132 | * 133 | * @return A String representation of this object. 134 | */ 135 | @Override 136 | public String toString() 137 | { 138 | return "cl_image_desc["+ 139 | "image_type="+CL.stringFor_cl_mem_object_type(image_type)+","+ 140 | "image_width="+image_width+","+ 141 | "image_height="+image_height+","+ 142 | "image_depth="+image_depth+","+ 143 | "image_array_size="+image_array_size+","+ 144 | "image_row_pitch="+image_row_pitch+","+ 145 | "image_slice_pitch="+image_slice_pitch+","+ 146 | "num_mip_levels="+num_mip_levels+","+ 147 | "num_samples="+num_samples+","+ 148 | "buffer="+buffer+ 149 | "]"; 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/cl_image_format.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | /** 31 | * Java port of a cl_image_format 32 | */ 33 | public final class cl_image_format 34 | { 35 | public int image_channel_order; 36 | public int image_channel_data_type; 37 | 38 | /** 39 | * Creates a new, uninitialized cl_image_format 40 | */ 41 | public cl_image_format() 42 | { 43 | } 44 | 45 | /** 46 | * Returns a String representation of this object. 47 | * 48 | * @return A String representation of this object. 49 | */ 50 | @Override 51 | public String toString() 52 | { 53 | return "cl_image_format["+ 54 | "image_channel_order="+CL.stringFor_cl_channel_order(image_channel_order)+","+ 55 | "image_channel_data_type="+CL.stringFor_cl_channel_type(image_channel_data_type)+ 56 | "]"; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/cl_kernel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | /** 31 | * Java port of a cl_kernel. 32 | */ 33 | public final class cl_kernel extends NativePointerObject 34 | { 35 | /** 36 | * Creates a new, uninitialized cl_kernel 37 | */ 38 | public cl_kernel() 39 | { 40 | } 41 | 42 | /** 43 | * Returns a String representation of this object. 44 | * 45 | * @return A String representation of this object. 46 | */ 47 | @Override 48 | public String toString() 49 | { 50 | return "cl_kernel[0x"+Long.toHexString(getNativePointer())+"]"; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/cl_mem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | /** 31 | * Java port of a cl_mem. 32 | */ 33 | public final class cl_mem extends NativePointerObject 34 | { 35 | /** 36 | * Creates a new, uninitialized cl_mem 37 | */ 38 | public cl_mem() 39 | { 40 | } 41 | 42 | /** 43 | * Returns a String representation of this object. 44 | * 45 | * @return A String representation of this object. 46 | */ 47 | @Override 48 | public String toString() 49 | { 50 | return "cl_mem[0x"+Long.toHexString(getNativePointer())+"]"; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/cl_pipe_properties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2014 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | /** 31 | * Java port of cl_pipe_properties. 32 | * 33 | * In OpenCL 2.0, the properties object must always be "null". 34 | */ 35 | public final class cl_pipe_properties extends cl_abstract_properties 36 | { 37 | /** 38 | * Creates new, empty cl_pipe_properties 39 | */ 40 | public cl_pipe_properties() 41 | { 42 | } 43 | 44 | @Override 45 | protected String propertyString(long value) 46 | { 47 | return "(unknown)"; 48 | } 49 | 50 | /** 51 | * Returns a String representation of this object. 52 | * 53 | * @return A String representation of this object. 54 | */ 55 | @Override 56 | public String toString() 57 | { 58 | StringBuilder result = new StringBuilder("cl_pipe_properties["); 59 | result.append(buildString()); 60 | result.append("]"); 61 | return result.toString(); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/cl_platform_id.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | /** 31 | * Java port of a cl_platform_id. 32 | */ 33 | public final class cl_platform_id extends NativePointerObject 34 | { 35 | /** 36 | * Creates a new, uninitialized cl_platform_id 37 | */ 38 | public cl_platform_id() 39 | { 40 | } 41 | 42 | /** 43 | * Returns a String representation of this object. 44 | * 45 | * @return A String representation of this object. 46 | */ 47 | @Override 48 | public String toString() 49 | { 50 | return "cl_platform_id[0x"+Long.toHexString(getNativePointer())+"]"; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/cl_program.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | /** 31 | * Java port of a cl_program. 32 | */ 33 | public final class cl_program extends NativePointerObject 34 | { 35 | /** 36 | * Creates a new, uninitialized cl_program 37 | */ 38 | public cl_program() 39 | { 40 | } 41 | 42 | /** 43 | * Returns a String representation of this object. 44 | * 45 | * @return A String representation of this object. 46 | */ 47 | @Override 48 | public String toString() 49 | { 50 | return "cl_program[0x"+Long.toHexString(getNativePointer())+"]"; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/cl_queue_properties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2014 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | /** 31 | * Java port of cl_queue_properties. 32 | */ 33 | public final class cl_queue_properties extends cl_abstract_properties 34 | { 35 | /** 36 | * Creates new, empty cl_queue_properties 37 | */ 38 | public cl_queue_properties() 39 | { 40 | } 41 | 42 | @Override 43 | protected String propertyString(long value) 44 | { 45 | if (value == CL.CL_QUEUE_PROPERTIES) 46 | { 47 | return "CL_QUEUE_PROPERTIES"; 48 | } 49 | if (value == CL.CL_QUEUE_SIZE) 50 | { 51 | return "CL_QUEUE_SIZE"; 52 | } 53 | return "(unknown)"; 54 | } 55 | 56 | /** 57 | * Returns a String representation of this object. 58 | * 59 | * @return A String representation of this object. 60 | */ 61 | @Override 62 | public String toString() 63 | { 64 | StringBuilder result = new StringBuilder("cl_queue_properties["); 65 | result.append(buildString()); 66 | result.append("]"); 67 | return result.toString(); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/cl_sampler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | /** 31 | * Java port of a cl_sampler. 32 | */ 33 | public final class cl_sampler extends NativePointerObject 34 | { 35 | /** 36 | * Creates a new, uninitialized cl_sampler 37 | */ 38 | public cl_sampler() 39 | { 40 | } 41 | 42 | /** 43 | * Returns a String representation of this object. 44 | * 45 | * @return A String representation of this object. 46 | */ 47 | @Override 48 | public String toString() 49 | { 50 | return "cl_sampler[0x"+Long.toHexString(getNativePointer())+"]"; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/jocl/cl_sampler_properties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2014 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | package org.jocl; 29 | 30 | /** 31 | * Java port of cl_sampler_properties. 32 | */ 33 | public final class cl_sampler_properties extends cl_abstract_properties 34 | { 35 | /** 36 | * Creates new, empty cl_sampler_properties 37 | */ 38 | public cl_sampler_properties() 39 | { 40 | } 41 | 42 | /** 43 | * Add the specified property to these properties 44 | * 45 | * @param id The property ID 46 | * @param value The property value 47 | */ 48 | public void addProperty(long id, boolean value) 49 | { 50 | addProperty(id, value ? 1 : 0); 51 | } 52 | 53 | /** 54 | * Add the specified property to these properties 55 | * 56 | * @param id The property ID 57 | * @param value The property value 58 | */ 59 | public void addProperty(long id, float value) 60 | { 61 | addProperty(id, Float.floatToIntBits(value)); 62 | } 63 | 64 | @Override 65 | protected String propertyString(long value) 66 | { 67 | return CL.stringFor_cl_sampler_info((int)value); 68 | } 69 | 70 | /** 71 | * Returns a String representation of this object. 72 | * 73 | * @return A String representation of this object. 74 | */ 75 | @Override 76 | public String toString() 77 | { 78 | StringBuilder result = new StringBuilder("cl_sampler_properties["); 79 | result.append(buildString()); 80 | result.append("]"); 81 | return result.toString(); 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /src/main/native/CLFunctions.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | #include "CLFunctions.hpp" 29 | 30 | // The pointers to the CL functions. The types of these pointers 31 | // are defined in the CLFunction.hpp header. The values will be 32 | // assigned to these pointers when the native library is 33 | // initialized with a call to Java_org_jocl_CL_initNativeLibrary, 34 | // in the FunctionPointerUtils::initFunctionPointers function. 35 | 36 | clGetPlatformIDsFunctionPointerType clGetPlatformIDsFP = NULL; 37 | clGetPlatformInfoFunctionPointerType clGetPlatformInfoFP = NULL; 38 | clGetDeviceIDsFunctionPointerType clGetDeviceIDsFP = NULL; 39 | clGetDeviceInfoFunctionPointerType clGetDeviceInfoFP = NULL; 40 | clCreateSubDevicesFunctionPointerType clCreateSubDevicesFP = NULL; 41 | clRetainDeviceFunctionPointerType clRetainDeviceFP = NULL; 42 | clReleaseDeviceFunctionPointerType clReleaseDeviceFP = NULL; 43 | clCreateContextFunctionPointerType clCreateContextFP = NULL; 44 | clCreateContextFromTypeFunctionPointerType clCreateContextFromTypeFP = NULL; 45 | clRetainContextFunctionPointerType clRetainContextFP = NULL; 46 | clReleaseContextFunctionPointerType clReleaseContextFP = NULL; 47 | clGetContextInfoFunctionPointerType clGetContextInfoFP = NULL; 48 | clCreateCommandQueueWithPropertiesFunctionPointerType clCreateCommandQueueWithPropertiesFP = NULL; 49 | clRetainCommandQueueFunctionPointerType clRetainCommandQueueFP = NULL; 50 | clReleaseCommandQueueFunctionPointerType clReleaseCommandQueueFP = NULL; 51 | clGetCommandQueueInfoFunctionPointerType clGetCommandQueueInfoFP = NULL; 52 | clCreateBufferFunctionPointerType clCreateBufferFP = NULL; 53 | clCreateSubBufferFunctionPointerType clCreateSubBufferFP = NULL; 54 | clCreateImageFunctionPointerType clCreateImageFP = NULL; 55 | clCreatePipeFunctionPointerType clCreatePipeFP = NULL; 56 | clRetainMemObjectFunctionPointerType clRetainMemObjectFP = NULL; 57 | clReleaseMemObjectFunctionPointerType clReleaseMemObjectFP = NULL; 58 | clGetSupportedImageFormatsFunctionPointerType clGetSupportedImageFormatsFP = NULL; 59 | clGetMemObjectInfoFunctionPointerType clGetMemObjectInfoFP = NULL; 60 | clGetImageInfoFunctionPointerType clGetImageInfoFP = NULL; 61 | clGetPipeInfoFunctionPointerType clGetPipeInfoFP = NULL; 62 | clSetMemObjectDestructorCallbackFunctionPointerType clSetMemObjectDestructorCallbackFP = NULL; 63 | clSVMAllocFunctionPointerType clSVMAllocFP = NULL; 64 | clSVMFreeFunctionPointerType clSVMFreeFP = NULL; 65 | clCreateSamplerWithPropertiesFunctionPointerType clCreateSamplerWithPropertiesFP = NULL; 66 | clRetainSamplerFunctionPointerType clRetainSamplerFP = NULL; 67 | clReleaseSamplerFunctionPointerType clReleaseSamplerFP = NULL; 68 | clGetSamplerInfoFunctionPointerType clGetSamplerInfoFP = NULL; 69 | clCreateProgramWithSourceFunctionPointerType clCreateProgramWithSourceFP = NULL; 70 | clCreateProgramWithBinaryFunctionPointerType clCreateProgramWithBinaryFP = NULL; 71 | clCreateProgramWithBuiltInKernelsFunctionPointerType clCreateProgramWithBuiltInKernelsFP = NULL; 72 | clRetainProgramFunctionPointerType clRetainProgramFP = NULL; 73 | clReleaseProgramFunctionPointerType clReleaseProgramFP = NULL; 74 | clBuildProgramFunctionPointerType clBuildProgramFP = NULL; 75 | clCompileProgramFunctionPointerType clCompileProgramFP = NULL; 76 | clLinkProgramFunctionPointerType clLinkProgramFP = NULL; 77 | clUnloadPlatformCompilerFunctionPointerType clUnloadPlatformCompilerFP = NULL; 78 | clGetProgramInfoFunctionPointerType clGetProgramInfoFP = NULL; 79 | clGetProgramBuildInfoFunctionPointerType clGetProgramBuildInfoFP = NULL; 80 | clCreateKernelFunctionPointerType clCreateKernelFP = NULL; 81 | clCreateKernelsInProgramFunctionPointerType clCreateKernelsInProgramFP = NULL; 82 | clRetainKernelFunctionPointerType clRetainKernelFP = NULL; 83 | clReleaseKernelFunctionPointerType clReleaseKernelFP = NULL; 84 | clSetKernelArgFunctionPointerType clSetKernelArgFP = NULL; 85 | clSetKernelArgSVMPointerFunctionPointerType clSetKernelArgSVMPointerFP = NULL; 86 | clSetKernelExecInfoFunctionPointerType clSetKernelExecInfoFP = NULL; 87 | clGetKernelInfoFunctionPointerType clGetKernelInfoFP = NULL; 88 | clGetKernelArgInfoFunctionPointerType clGetKernelArgInfoFP = NULL; 89 | clGetKernelWorkGroupInfoFunctionPointerType clGetKernelWorkGroupInfoFP = NULL; 90 | clWaitForEventsFunctionPointerType clWaitForEventsFP = NULL; 91 | clGetEventInfoFunctionPointerType clGetEventInfoFP = NULL; 92 | clCreateUserEventFunctionPointerType clCreateUserEventFP = NULL; 93 | clRetainEventFunctionPointerType clRetainEventFP = NULL; 94 | clReleaseEventFunctionPointerType clReleaseEventFP = NULL; 95 | clSetUserEventStatusFunctionPointerType clSetUserEventStatusFP = NULL; 96 | clSetEventCallbackFunctionPointerType clSetEventCallbackFP = NULL; 97 | clGetEventProfilingInfoFunctionPointerType clGetEventProfilingInfoFP = NULL; 98 | clFlushFunctionPointerType clFlushFP = NULL; 99 | clFinishFunctionPointerType clFinishFP = NULL; 100 | clEnqueueReadBufferFunctionPointerType clEnqueueReadBufferFP = NULL; 101 | clEnqueueReadBufferRectFunctionPointerType clEnqueueReadBufferRectFP = NULL; 102 | clEnqueueWriteBufferFunctionPointerType clEnqueueWriteBufferFP = NULL; 103 | clEnqueueWriteBufferRectFunctionPointerType clEnqueueWriteBufferRectFP = NULL; 104 | clEnqueueFillBufferFunctionPointerType clEnqueueFillBufferFP = NULL; 105 | clEnqueueCopyBufferFunctionPointerType clEnqueueCopyBufferFP = NULL; 106 | clEnqueueCopyBufferRectFunctionPointerType clEnqueueCopyBufferRectFP = NULL; 107 | clEnqueueReadImageFunctionPointerType clEnqueueReadImageFP = NULL; 108 | clEnqueueWriteImageFunctionPointerType clEnqueueWriteImageFP = NULL; 109 | clEnqueueFillImageFunctionPointerType clEnqueueFillImageFP = NULL; 110 | clEnqueueCopyImageFunctionPointerType clEnqueueCopyImageFP = NULL; 111 | clEnqueueCopyImageToBufferFunctionPointerType clEnqueueCopyImageToBufferFP = NULL; 112 | clEnqueueCopyBufferToImageFunctionPointerType clEnqueueCopyBufferToImageFP = NULL; 113 | clEnqueueMapBufferFunctionPointerType clEnqueueMapBufferFP = NULL; 114 | clEnqueueMapImageFunctionPointerType clEnqueueMapImageFP = NULL; 115 | clEnqueueUnmapMemObjectFunctionPointerType clEnqueueUnmapMemObjectFP = NULL; 116 | clEnqueueMigrateMemObjectsFunctionPointerType clEnqueueMigrateMemObjectsFP = NULL; 117 | clEnqueueNDRangeKernelFunctionPointerType clEnqueueNDRangeKernelFP = NULL; 118 | clEnqueueNativeKernelFunctionPointerType clEnqueueNativeKernelFP = NULL; 119 | clEnqueueMarkerWithWaitListFunctionPointerType clEnqueueMarkerWithWaitListFP = NULL; 120 | clEnqueueBarrierWithWaitListFunctionPointerType clEnqueueBarrierWithWaitListFP = NULL; 121 | clEnqueueSVMFreeFunctionPointerType clEnqueueSVMFreeFP = NULL; 122 | clEnqueueSVMMemcpyFunctionPointerType clEnqueueSVMMemcpyFP = NULL; 123 | clEnqueueSVMMemFillFunctionPointerType clEnqueueSVMMemFillFP = NULL; 124 | clEnqueueSVMMapFunctionPointerType clEnqueueSVMMapFP = NULL; 125 | clEnqueueSVMUnmapFunctionPointerType clEnqueueSVMUnmapFP = NULL; 126 | clGetExtensionFunctionAddressForPlatformFunctionPointerType clGetExtensionFunctionAddressForPlatformFP = NULL; 127 | clSetCommandQueuePropertyFunctionPointerType clSetCommandQueuePropertyFP = NULL; 128 | clCreateImage2DFunctionPointerType clCreateImage2DFP = NULL; 129 | clCreateImage3DFunctionPointerType clCreateImage3DFP = NULL; 130 | clEnqueueMarkerFunctionPointerType clEnqueueMarkerFP = NULL; 131 | clEnqueueWaitForEventsFunctionPointerType clEnqueueWaitForEventsFP = NULL; 132 | clEnqueueBarrierFunctionPointerType clEnqueueBarrierFP = NULL; 133 | clUnloadCompilerFunctionPointerType clUnloadCompilerFP = NULL; 134 | clGetExtensionFunctionAddressFunctionPointerType clGetExtensionFunctionAddressFP = NULL; 135 | clCreateCommandQueueFunctionPointerType clCreateCommandQueueFP = NULL; 136 | clCreateSamplerFunctionPointerType clCreateSamplerFP = NULL; 137 | clEnqueueTaskFunctionPointerType clEnqueueTaskFP = NULL; 138 | 139 | clCreateFromGLBufferFunctionPointerType clCreateFromGLBufferFP = NULL; 140 | clCreateFromGLTextureFunctionPointerType clCreateFromGLTextureFP = NULL; 141 | clCreateFromGLRenderbufferFunctionPointerType clCreateFromGLRenderbufferFP = NULL; 142 | clGetGLObjectInfoFunctionPointerType clGetGLObjectInfoFP = NULL; 143 | clGetGLTextureInfoFunctionPointerType clGetGLTextureInfoFP = NULL; 144 | clEnqueueAcquireGLObjectsFunctionPointerType clEnqueueAcquireGLObjectsFP = NULL; 145 | clEnqueueReleaseGLObjectsFunctionPointerType clEnqueueReleaseGLObjectsFP = NULL; 146 | clCreateFromGLTexture2DFunctionPointerType clCreateFromGLTexture2DFP = NULL; 147 | clCreateFromGLTexture3DFunctionPointerType clCreateFromGLTexture3DFP = NULL; 148 | 149 | clGetGLContextInfoAPPLEFunctionPointerType clGetGLContextInfoAPPLEFP = NULL; 150 | -------------------------------------------------------------------------------- /src/main/native/FunctionPointerUtils.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | #include "FunctionPointerUtils.hpp" 29 | 30 | #include "CLFunctions.hpp" 31 | 32 | /** 33 | * Template method that obtains the pointer to the function 34 | * with the given name, and stores it in the given pointer. 35 | */ 36 | template void initFunctionPointer(T* pointer, const char* name) 37 | { 38 | T result = (T)obtainFunctionPointer(name); 39 | *pointer = result; 40 | } 41 | 42 | /** 43 | * Initialize all pointers to OpenCL functions 44 | */ 45 | void initFunctionPointers() 46 | { 47 | initFunctionPointer(&clGetPlatformIDsFP, "clGetPlatformIDs"); 48 | initFunctionPointer(&clGetPlatformInfoFP, "clGetPlatformInfo"); 49 | initFunctionPointer(&clGetDeviceIDsFP, "clGetDeviceIDs"); 50 | initFunctionPointer(&clGetDeviceInfoFP, "clGetDeviceInfo"); 51 | initFunctionPointer(&clCreateSubDevicesFP, "clCreateSubDevices"); 52 | initFunctionPointer(&clRetainDeviceFP, "clRetainDevice"); 53 | initFunctionPointer(&clReleaseDeviceFP, "clReleaseDevice"); 54 | initFunctionPointer(&clCreateContextFP, "clCreateContext"); 55 | initFunctionPointer(&clCreateContextFromTypeFP, "clCreateContextFromType"); 56 | initFunctionPointer(&clRetainContextFP, "clRetainContext"); 57 | initFunctionPointer(&clReleaseContextFP, "clReleaseContext"); 58 | initFunctionPointer(&clGetContextInfoFP, "clGetContextInfo"); 59 | initFunctionPointer(&clCreateCommandQueueWithPropertiesFP, "clCreateCommandQueueWithProperties"); 60 | initFunctionPointer(&clRetainCommandQueueFP, "clRetainCommandQueue"); 61 | initFunctionPointer(&clReleaseCommandQueueFP, "clReleaseCommandQueue"); 62 | initFunctionPointer(&clGetCommandQueueInfoFP, "clGetCommandQueueInfo"); 63 | initFunctionPointer(&clCreateBufferFP, "clCreateBuffer"); 64 | initFunctionPointer(&clCreateSubBufferFP, "clCreateSubBuffer"); 65 | initFunctionPointer(&clCreateImageFP, "clCreateImage"); 66 | initFunctionPointer(&clCreatePipeFP, "clCreatePipe"); 67 | initFunctionPointer(&clRetainMemObjectFP, "clRetainMemObject"); 68 | initFunctionPointer(&clReleaseMemObjectFP, "clReleaseMemObject"); 69 | initFunctionPointer(&clGetSupportedImageFormatsFP, "clGetSupportedImageFormats"); 70 | initFunctionPointer(&clGetMemObjectInfoFP, "clGetMemObjectInfo"); 71 | initFunctionPointer(&clGetImageInfoFP, "clGetImageInfo"); 72 | initFunctionPointer(&clGetPipeInfoFP, "clGetPipeInfo"); 73 | initFunctionPointer(&clSetMemObjectDestructorCallbackFP, "clSetMemObjectDestructorCallback"); 74 | initFunctionPointer(&clSVMAllocFP, "clSVMAlloc"); 75 | initFunctionPointer(&clSVMFreeFP, "clSVMFree"); 76 | initFunctionPointer(&clCreateSamplerWithPropertiesFP, "clCreateSamplerWithProperties"); 77 | initFunctionPointer(&clRetainSamplerFP, "clRetainSampler"); 78 | initFunctionPointer(&clReleaseSamplerFP, "clReleaseSampler"); 79 | initFunctionPointer(&clGetSamplerInfoFP, "clGetSamplerInfo"); 80 | initFunctionPointer(&clCreateProgramWithSourceFP, "clCreateProgramWithSource"); 81 | initFunctionPointer(&clCreateProgramWithBinaryFP, "clCreateProgramWithBinary"); 82 | initFunctionPointer(&clCreateProgramWithBuiltInKernelsFP, "clCreateProgramWithBuiltInKernels"); 83 | initFunctionPointer(&clRetainProgramFP, "clRetainProgram"); 84 | initFunctionPointer(&clReleaseProgramFP, "clReleaseProgram"); 85 | initFunctionPointer(&clBuildProgramFP, "clBuildProgram"); 86 | initFunctionPointer(&clCompileProgramFP, "clCompileProgram"); 87 | initFunctionPointer(&clLinkProgramFP, "clLinkProgram"); 88 | initFunctionPointer(&clUnloadPlatformCompilerFP, "clUnloadPlatformCompiler"); 89 | initFunctionPointer(&clGetProgramInfoFP, "clGetProgramInfo"); 90 | initFunctionPointer(&clGetProgramBuildInfoFP, "clGetProgramBuildInfo"); 91 | initFunctionPointer(&clCreateKernelFP, "clCreateKernel"); 92 | initFunctionPointer(&clCreateKernelsInProgramFP, "clCreateKernelsInProgram"); 93 | initFunctionPointer(&clRetainKernelFP, "clRetainKernel"); 94 | initFunctionPointer(&clReleaseKernelFP, "clReleaseKernel"); 95 | initFunctionPointer(&clSetKernelArgFP, "clSetKernelArg"); 96 | initFunctionPointer(&clSetKernelArgSVMPointerFP, "clSetKernelArgSVMPointer"); 97 | initFunctionPointer(&clSetKernelExecInfoFP, "clSetKernelExecInfo"); 98 | initFunctionPointer(&clGetKernelInfoFP, "clGetKernelInfo"); 99 | initFunctionPointer(&clGetKernelArgInfoFP, "clGetKernelArgInfo"); 100 | initFunctionPointer(&clGetKernelWorkGroupInfoFP, "clGetKernelWorkGroupInfo"); 101 | initFunctionPointer(&clWaitForEventsFP, "clWaitForEvents"); 102 | initFunctionPointer(&clGetEventInfoFP, "clGetEventInfo"); 103 | initFunctionPointer(&clCreateUserEventFP, "clCreateUserEvent"); 104 | initFunctionPointer(&clRetainEventFP, "clRetainEvent"); 105 | initFunctionPointer(&clReleaseEventFP, "clReleaseEvent"); 106 | initFunctionPointer(&clSetUserEventStatusFP, "clSetUserEventStatus"); 107 | initFunctionPointer(&clSetEventCallbackFP, "clSetEventCallback"); 108 | initFunctionPointer(&clGetEventProfilingInfoFP, "clGetEventProfilingInfo"); 109 | initFunctionPointer(&clFlushFP, "clFlush"); 110 | initFunctionPointer(&clFinishFP, "clFinish"); 111 | initFunctionPointer(&clEnqueueReadBufferFP, "clEnqueueReadBuffer"); 112 | initFunctionPointer(&clEnqueueReadBufferRectFP, "clEnqueueReadBufferRect"); 113 | initFunctionPointer(&clEnqueueWriteBufferFP, "clEnqueueWriteBuffer"); 114 | initFunctionPointer(&clEnqueueWriteBufferRectFP, "clEnqueueWriteBufferRect"); 115 | initFunctionPointer(&clEnqueueFillBufferFP, "clEnqueueFillBuffer"); 116 | initFunctionPointer(&clEnqueueCopyBufferFP, "clEnqueueCopyBuffer"); 117 | initFunctionPointer(&clEnqueueCopyBufferRectFP, "clEnqueueCopyBufferRect"); 118 | initFunctionPointer(&clEnqueueReadImageFP, "clEnqueueReadImage"); 119 | initFunctionPointer(&clEnqueueWriteImageFP, "clEnqueueWriteImage"); 120 | initFunctionPointer(&clEnqueueFillImageFP, "clEnqueueFillImage"); 121 | initFunctionPointer(&clEnqueueCopyImageFP, "clEnqueueCopyImage"); 122 | initFunctionPointer(&clEnqueueCopyImageToBufferFP, "clEnqueueCopyImageToBuffer"); 123 | initFunctionPointer(&clEnqueueCopyBufferToImageFP, "clEnqueueCopyBufferToImage"); 124 | initFunctionPointer(&clEnqueueMapBufferFP, "clEnqueueMapBuffer"); 125 | initFunctionPointer(&clEnqueueMapImageFP, "clEnqueueMapImage"); 126 | initFunctionPointer(&clEnqueueUnmapMemObjectFP, "clEnqueueUnmapMemObject"); 127 | initFunctionPointer(&clEnqueueMigrateMemObjectsFP, "clEnqueueMigrateMemObjects"); 128 | initFunctionPointer(&clEnqueueNDRangeKernelFP, "clEnqueueNDRangeKernel"); 129 | initFunctionPointer(&clEnqueueNativeKernelFP, "clEnqueueNativeKernel"); 130 | initFunctionPointer(&clEnqueueMarkerWithWaitListFP, "clEnqueueMarkerWithWaitList"); 131 | initFunctionPointer(&clEnqueueBarrierWithWaitListFP, "clEnqueueBarrierWithWaitList"); 132 | initFunctionPointer(&clEnqueueSVMFreeFP, "clEnqueueSVMFree"); 133 | initFunctionPointer(&clEnqueueSVMMemcpyFP, "clEnqueueSVMMemcpy"); 134 | initFunctionPointer(&clEnqueueSVMMemFillFP, "clEnqueueSVMMemFill"); 135 | initFunctionPointer(&clEnqueueSVMMapFP, "clEnqueueSVMMap"); 136 | initFunctionPointer(&clEnqueueSVMUnmapFP, "clEnqueueSVMUnmap"); 137 | initFunctionPointer(&clGetExtensionFunctionAddressForPlatformFP, "clGetExtensionFunctionAddressForPlatform"); 138 | initFunctionPointer(&clSetCommandQueuePropertyFP, "clSetCommandQueueProperty"); 139 | initFunctionPointer(&clCreateImage2DFP, "clCreateImage2D"); 140 | initFunctionPointer(&clCreateImage3DFP, "clCreateImage3D"); 141 | initFunctionPointer(&clEnqueueMarkerFP, "clEnqueueMarker"); 142 | initFunctionPointer(&clEnqueueWaitForEventsFP, "clEnqueueWaitForEvents"); 143 | initFunctionPointer(&clEnqueueBarrierFP, "clEnqueueBarrier"); 144 | initFunctionPointer(&clUnloadCompilerFP, "clUnloadCompiler"); 145 | initFunctionPointer(&clGetExtensionFunctionAddressFP, "clGetExtensionFunctionAddress"); 146 | initFunctionPointer(&clCreateCommandQueueFP, "clCreateCommandQueue"); 147 | initFunctionPointer(&clCreateSamplerFP, "clCreateSampler"); 148 | initFunctionPointer(&clEnqueueTaskFP, "clEnqueueTask"); 149 | 150 | initFunctionPointer(&clCreateFromGLBufferFP, "clCreateFromGLBuffer"); 151 | initFunctionPointer(&clCreateFromGLTextureFP, "clCreateFromGLTexture"); 152 | initFunctionPointer(&clCreateFromGLRenderbufferFP, "clCreateFromGLRenderbuffer"); 153 | initFunctionPointer(&clGetGLObjectInfoFP, "clGetGLObjectInfo"); 154 | initFunctionPointer(&clGetGLTextureInfoFP, "clGetGLTextureInfo"); 155 | initFunctionPointer(&clEnqueueAcquireGLObjectsFP, "clEnqueueAcquireGLObjects"); 156 | initFunctionPointer(&clEnqueueReleaseGLObjectsFP, "clEnqueueReleaseGLObjects"); 157 | initFunctionPointer(&clCreateFromGLTexture2DFP, "clCreateFromGLTexture2D"); 158 | initFunctionPointer(&clCreateFromGLTexture3DFP, "clCreateFromGLTexture3D"); 159 | 160 | initFunctionPointer(&clGetGLContextInfoAPPLEFP, "clGetGLContextInfoAPPLE"); 161 | 162 | 163 | } 164 | -------------------------------------------------------------------------------- /src/main/native/FunctionPointerUtils.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | #ifndef FUNCTION_POINTER_UTILS_HPP 29 | #define FUNCTION_POINTER_UTILS_HPP 30 | 31 | // Visual Studio <2010 is missing stdint.h, 32 | // but defines intptr_t in another file 33 | #ifdef _WIN32 34 | #include 35 | #else 36 | #include 37 | #endif 38 | 39 | intptr_t obtainFunctionPointer(const char* name); 40 | bool loadImplementationLibrary(const char *libraryName); 41 | void initFunctionPointers(); 42 | bool unloadImplementationLibrary(); 43 | 44 | 45 | #endif // FUNCTION_POINTER_UTILS_HPP 46 | -------------------------------------------------------------------------------- /src/main/native/FunctionPointerUtils_Linux.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | // The dlfcn.h header file should also be available on MacOS X >=10.3, 29 | // according to https://developer.apple.com/library/mac/documentation/ 30 | // developertools/Reference/MachOReference/Reference/reference.html 31 | 32 | #ifndef _WIN32 33 | 34 | #include "FunctionPointerUtils.hpp" 35 | #include "Logger.hpp" 36 | 37 | // NOTE: Link with -ldl. 38 | 39 | #include 40 | #include 41 | 42 | void* libraryHandle; 43 | 44 | /** 45 | * The address of the function with the given name is obtained from 46 | * the library and returned 47 | */ 48 | intptr_t obtainFunctionPointer(const char* name) 49 | { 50 | dlerror(); 51 | intptr_t result = (intptr_t)dlsym(libraryHandle, name); 52 | char *lastError = dlerror(); 53 | if (lastError != NULL) 54 | { 55 | //printf("Function pointer %p for %s - ERROR: %s\n", result, name, lastError); 56 | } 57 | else 58 | { 59 | //printf("Function pointer %p for %s\n", result, name); 60 | } 61 | return result; 62 | } 63 | 64 | /** 65 | * Prepare the initialization of the function pointers by loading 66 | * the specified library. Returns whether this initialization 67 | * succeeded. 68 | */ 69 | bool loadImplementationLibrary(const char *libraryName) 70 | { 71 | dlerror(); 72 | libraryHandle = dlopen(libraryName, RTLD_LAZY); 73 | if (libraryHandle == NULL) 74 | { 75 | char *lastError = dlerror(); 76 | Logger::log(LOG_ERROR, "Could not load %s, error %s\n", libraryName, lastError); 77 | return false; 78 | } 79 | return true; 80 | } 81 | 82 | /** 83 | * Unload the OpenCL library 84 | */ 85 | bool unloadImplementationLibrary() 86 | { 87 | if (libraryHandle != NULL) 88 | { 89 | int lastError = dlclose(libraryHandle); 90 | if (lastError != 0) 91 | { 92 | Logger::log(LOG_ERROR, "Could not unload implementation library, error %d\n", lastError); 93 | return false; 94 | } 95 | } 96 | return true; 97 | } 98 | 99 | #endif // _WIN32 -------------------------------------------------------------------------------- /src/main/native/FunctionPointerUtils_Win.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2012 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | #ifdef _WIN32 29 | 30 | #include "FunctionPointerUtils.hpp" 31 | #include "Logger.hpp" 32 | 33 | #include 34 | 35 | HMODULE libraryHandle = NULL; 36 | 37 | /** 38 | * The address of the function with the given name is obtained from 39 | * the library and returned 40 | */ 41 | intptr_t obtainFunctionPointer(const char* name) 42 | { 43 | SetLastError(0); 44 | intptr_t result = (intptr_t)GetProcAddress(libraryHandle, name); 45 | DWORD lastError = GetLastError(); 46 | if (lastError != 0) 47 | { 48 | //printf("Function pointer %p for %s - ERROR %d\n", result, name, lastError); 49 | SetLastError(0); 50 | } 51 | else 52 | { 53 | //printf("Function pointer %p for %s\n", result, name); 54 | } 55 | return result; 56 | } 57 | 58 | /** 59 | * Prepare the initialization of the function pointers by loading 60 | * the specified library. Returns whether this initialization 61 | * succeeded. 62 | */ 63 | bool loadImplementationLibrary(const char *libraryName) 64 | { 65 | SetLastError(0); 66 | libraryHandle = LoadLibrary(libraryName); 67 | if (libraryHandle == NULL) 68 | { 69 | DWORD lastError = GetLastError(); 70 | SetLastError(0); 71 | Logger::log(LOG_ERROR, "Could not load %s, error %ld\n", libraryName, lastError); 72 | return false; 73 | } 74 | return true; 75 | } 76 | 77 | /** 78 | * Unload the OpenCL library 79 | */ 80 | bool unloadImplementationLibrary() 81 | { 82 | if (libraryHandle != NULL) 83 | { 84 | SetLastError(0); 85 | FreeLibrary(libraryHandle); 86 | DWORD lastError = GetLastError(); 87 | if (lastError != 0) 88 | { 89 | Logger::log(LOG_ERROR, "Could not unload implementation library, error %ld\n", lastError); 90 | SetLastError(0); 91 | return false; 92 | } 93 | } 94 | return true; 95 | } 96 | 97 | 98 | #endif // _WIN32 -------------------------------------------------------------------------------- /src/main/native/Sizeof.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * JOCL - Java bindings for OpenCL 3 | * 4 | * Copyright (c) 2009-2016 Marco Hutter - http://www.jocl.org 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the 12 | * Software is furnished to do so, subject to the following 13 | * conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | * OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | #include 29 | #include "JOCLCommon.hpp" 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | JNIEXPORT jint JNICALL Java_org_jocl_Sizeof_computePointerSizeNative( 36 | JNIEnv* UNUSED(env), jclass UNUSED(cls)) 37 | { 38 | int ptr_size = sizeof((void*) 0); 39 | return (jint) ptr_size; 40 | } 41 | 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | -------------------------------------------------------------------------------- /src/test/java/org/jocl/test/JOCLAbstractTest.java: -------------------------------------------------------------------------------- 1 | package org.jocl.test; 2 | 3 | import static org.jocl.CL.CL_CONTEXT_PLATFORM; 4 | import static org.jocl.CL.CL_DEVICE_TYPE_ALL; 5 | import static org.jocl.CL.clBuildProgram; 6 | import static org.jocl.CL.clCreateCommandQueue; 7 | import static org.jocl.CL.clCreateContext; 8 | import static org.jocl.CL.clCreateKernel; 9 | import static org.jocl.CL.clCreateProgramWithSource; 10 | import static org.jocl.CL.clGetDeviceIDs; 11 | import static org.jocl.CL.clGetPlatformIDs; 12 | import static org.jocl.CL.clReleaseCommandQueue; 13 | import static org.jocl.CL.clReleaseKernel; 14 | import static org.jocl.CL.clReleaseProgram; 15 | 16 | import org.jocl.CL; 17 | import org.jocl.cl_command_queue; 18 | import org.jocl.cl_context; 19 | import org.jocl.cl_context_properties; 20 | import org.jocl.cl_device_id; 21 | import org.jocl.cl_kernel; 22 | import org.jocl.cl_platform_id; 23 | import org.jocl.cl_program; 24 | 25 | /** 26 | * Abstract base class for JOCL test cases 27 | */ 28 | public abstract class JOCLAbstractTest 29 | { 30 | // The default platform, device type and device index 31 | protected static final int defaultPlatformIndex = 0; 32 | protected static final long defaultDeviceType = CL_DEVICE_TYPE_ALL; 33 | protected static final int defaultDeviceIndex = 0; 34 | 35 | protected cl_context context; 36 | protected cl_command_queue commandQueue; 37 | protected cl_kernel kernel; 38 | 39 | 40 | /** 41 | * Default initialization of the context and the command queue 42 | * 43 | * @param platformIndex The platform to use 44 | * @param deviceType The device type 45 | * @param deviceIndex The device index 46 | * @return Whether the initialization was successful 47 | */ 48 | protected final boolean initCL( 49 | int platformIndex, long deviceType, int deviceIndex) 50 | { 51 | // Enable exceptions and subsequently omit error checks in this sample 52 | CL.setExceptionsEnabled(true); 53 | 54 | // Obtain the number of platforms 55 | int numPlatformsArray[] = new int[1]; 56 | clGetPlatformIDs(0, null, numPlatformsArray); 57 | int numPlatforms = numPlatformsArray[0]; 58 | 59 | if (numPlatforms == 0) 60 | { 61 | System.err.println("No OpenCL platforms available"); 62 | return false; 63 | } 64 | 65 | // Obtain a platform ID 66 | cl_platform_id platforms[] = new cl_platform_id[numPlatforms]; 67 | clGetPlatformIDs(platforms.length, platforms, null); 68 | cl_platform_id platform = platforms[platformIndex]; 69 | 70 | // Initialize the context properties 71 | cl_context_properties contextProperties = new cl_context_properties(); 72 | contextProperties.addProperty(CL_CONTEXT_PLATFORM, platform); 73 | 74 | // Obtain the number of devices for the platform 75 | int numDevicesArray[] = new int[1]; 76 | clGetDeviceIDs(platform, deviceType, 0, null, numDevicesArray); 77 | int numDevices = numDevicesArray[0]; 78 | 79 | if (numDevices == 0) 80 | { 81 | System.err.println("No devices with type " + 82 | CL.stringFor_cl_device_type(deviceType)+ 83 | " on platform "+platformIndex); 84 | return false; 85 | } 86 | 87 | // Obtain a device ID 88 | cl_device_id devices[] = new cl_device_id[numDevices]; 89 | clGetDeviceIDs(platform, deviceType, numDevices, devices, null); 90 | cl_device_id device = devices[deviceIndex]; 91 | 92 | // Create a context for the selected device 93 | context = clCreateContext( 94 | contextProperties, 1, new cl_device_id[]{device}, 95 | null, null, null); 96 | 97 | // Create a command-queue for the selected device 98 | commandQueue = clCreateCommandQueue( 99 | context, device, 0, null); 100 | 101 | return true; 102 | } 103 | 104 | /** 105 | * Initialize the kernel with the given name and source 106 | * 107 | * @param kernelName The kernel name 108 | * @param programSource The program source 109 | */ 110 | protected final void initKernel(String kernelName, String programSource) 111 | { 112 | // Create the program from the source code 113 | cl_program program = clCreateProgramWithSource(context, 114 | 1, new String[]{ programSource }, null, null); 115 | 116 | // Build the program 117 | clBuildProgram(program, 0, null, null, null, null); 118 | 119 | // Create the kernel 120 | kernel = clCreateKernel(program, kernelName, null); 121 | 122 | clReleaseProgram(program); 123 | 124 | } 125 | 126 | /** 127 | * Release the kernel that has been created in 128 | * {@link #initKernel(String, String)} 129 | */ 130 | protected final void shutdownKernel() 131 | { 132 | if (kernel != null) 133 | { 134 | clReleaseKernel(kernel); 135 | } 136 | } 137 | 138 | 139 | /** 140 | * Shut down OpenCL by freeing all resources that have 141 | * been allocated in {@link #initCL()} 142 | */ 143 | protected final void shutdownCL() 144 | { 145 | if (commandQueue != null) 146 | { 147 | clReleaseCommandQueue(commandQueue); 148 | } 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /src/test/java/org/jocl/test/JOCLBasicTest.java: -------------------------------------------------------------------------------- 1 | package org.jocl.test; 2 | 3 | import static org.jocl.CL.*; 4 | import static org.junit.Assert.assertTrue; 5 | 6 | import org.jocl.*; 7 | import org.junit.Test; 8 | 9 | /** 10 | * A basic vector addition test 11 | */ 12 | public class JOCLBasicTest extends JOCLAbstractTest 13 | { 14 | /** 15 | * The source code of the OpenCL program to execute 16 | */ 17 | private static String programSource = 18 | "__kernel void "+ 19 | "sampleKernel(__global const float *a,"+ 20 | " __global const float *b,"+ 21 | " __global float *c)"+ 22 | "{"+ 23 | " int gid = get_global_id(0);"+ 24 | " c[gid] = a[gid] + b[gid];"+ 25 | "}"; 26 | 27 | @Test 28 | public void basicTest() 29 | { 30 | initCL(defaultPlatformIndex, defaultDeviceType, defaultDeviceIndex); 31 | initKernel("sampleKernel", programSource); 32 | 33 | 34 | // Create input- and output data 35 | int n = 10; 36 | float srcArrayA[] = new float[n]; 37 | float srcArrayB[] = new float[n]; 38 | float dstArray[] = new float[n]; 39 | for (int i=0; i 0); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/org/jocl/test/SizeofTest.java: -------------------------------------------------------------------------------- 1 | package org.jocl.test; 2 | 3 | import static org.junit.Assert.assertTrue; 4 | 5 | import org.jocl.Sizeof; 6 | import org.junit.Test; 7 | 8 | /** 9 | * Test whether the native methods in the Sizeof class can be called without 10 | * using any of the CL functions first 11 | */ 12 | public class SizeofTest 13 | { 14 | @Test 15 | public void sizeofTest() 16 | { 17 | assertTrue(Sizeof.POINTER != 0); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /src/test/java/org/jocl/test/TestNonBlockingConstraints.java: -------------------------------------------------------------------------------- 1 | package org.jocl.test; 2 | 3 | import static org.jocl.CL.CL_MEM_READ_ONLY; 4 | import static org.jocl.CL.CL_MEM_WRITE_ONLY; 5 | import static org.jocl.CL.CL_NON_BLOCKING; 6 | import static org.jocl.CL.clCreateBuffer; 7 | import static org.jocl.CL.clEnqueueReadBuffer; 8 | import static org.jocl.CL.clEnqueueWriteBuffer; 9 | import static org.jocl.CL.clFinish; 10 | import static org.jocl.CL.clFlush; 11 | 12 | import java.nio.ByteBuffer; 13 | 14 | import org.jocl.Pointer; 15 | import org.jocl.Sizeof; 16 | import org.jocl.cl_mem; 17 | import org.junit.Rule; 18 | import org.junit.Test; 19 | import org.junit.rules.ExpectedException; 20 | 21 | /** 22 | * Test the constraints for non-blocking operations, and whether the 23 | * IllegalArgumentException is thrown accordingly: 24 | * A non-blocking operation will 25 | * - throw for a pointer to a Java array 26 | * - throw for a pointer to a Java byte buffer 27 | * - NOT throw for a pointer to a DIRECT byte buffer 28 | */ 29 | public class TestNonBlockingConstraints extends JOCLAbstractTest 30 | { 31 | @Rule 32 | public final ExpectedException exception = ExpectedException.none(); 33 | 34 | @Test 35 | public void testNonBlockingReadWithJavaArrayThrows() 36 | { 37 | initCL(defaultPlatformIndex, defaultDeviceType, defaultDeviceIndex); 38 | 39 | cl_mem mem = clCreateBuffer(context, CL_MEM_READ_ONLY, 40 | 10 * Sizeof.cl_float, null, null); 41 | 42 | Pointer pointer = Pointer.to(new float[10]); 43 | 44 | exception.expect(IllegalArgumentException.class); 45 | clEnqueueReadBuffer(commandQueue, mem, CL_NON_BLOCKING, 0, 46 | 10 * Sizeof.cl_float, pointer, 0, null, null); 47 | 48 | clFlush(commandQueue); 49 | clFinish(commandQueue); 50 | } 51 | 52 | @Test 53 | public void testNonBlockingReadWithBufferThrows() 54 | { 55 | initCL(defaultPlatformIndex, defaultDeviceType, defaultDeviceIndex); 56 | 57 | cl_mem mem = clCreateBuffer(context, CL_MEM_READ_ONLY, 58 | 10 * Sizeof.cl_float, null, null); 59 | 60 | Pointer pointer = Pointer.to(ByteBuffer.allocate(10 * Sizeof.cl_float)); 61 | 62 | exception.expect(IllegalArgumentException.class); 63 | clEnqueueReadBuffer(commandQueue, mem, CL_NON_BLOCKING, 0, 64 | 10 * Sizeof.cl_float, pointer, 0, null, null); 65 | 66 | clFlush(commandQueue); 67 | clFinish(commandQueue); 68 | } 69 | 70 | @Test 71 | public void testNonBlockingReadWithDirectBuffer() 72 | { 73 | initCL(defaultPlatformIndex, defaultDeviceType, defaultDeviceIndex); 74 | 75 | cl_mem mem = clCreateBuffer(context, CL_MEM_READ_ONLY, 76 | 10 * Sizeof.cl_float, null, null); 77 | 78 | Pointer pointer = Pointer.to( 79 | ByteBuffer.allocateDirect(10 * Sizeof.cl_float)); 80 | 81 | clEnqueueReadBuffer(commandQueue, mem, CL_NON_BLOCKING, 0, 82 | 10 * Sizeof.cl_float, pointer, 0, null, null); 83 | 84 | clFlush(commandQueue); 85 | clFinish(commandQueue); 86 | } 87 | 88 | 89 | @Test 90 | public void testNonBlockingWriteWithJavaArrayThrows() 91 | { 92 | initCL(defaultPlatformIndex, defaultDeviceType, defaultDeviceIndex); 93 | 94 | cl_mem mem = clCreateBuffer(context, CL_MEM_WRITE_ONLY, 95 | 10 * Sizeof.cl_float, null, null); 96 | 97 | Pointer pointer = Pointer.to(new float[10]); 98 | 99 | exception.expect(IllegalArgumentException.class); 100 | clEnqueueWriteBuffer(commandQueue, mem, CL_NON_BLOCKING, 0, 101 | 10 * Sizeof.cl_float, pointer, 0, null, null); 102 | 103 | clFlush(commandQueue); 104 | clFinish(commandQueue); 105 | } 106 | 107 | @Test 108 | public void testNonBlockingWriteWithBufferThrows() 109 | { 110 | initCL(defaultPlatformIndex, defaultDeviceType, defaultDeviceIndex); 111 | 112 | cl_mem mem = clCreateBuffer(context, CL_MEM_WRITE_ONLY, 113 | 10 * Sizeof.cl_float, null, null); 114 | 115 | Pointer pointer = Pointer.to(ByteBuffer.allocate(10 * Sizeof.cl_float)); 116 | 117 | exception.expect(IllegalArgumentException.class); 118 | clEnqueueWriteBuffer(commandQueue, mem, CL_NON_BLOCKING, 0, 119 | 10 * Sizeof.cl_float, pointer, 0, null, null); 120 | 121 | clFlush(commandQueue); 122 | clFinish(commandQueue); 123 | } 124 | 125 | @Test 126 | public void testNonBlockingWriteWithDirectBuffer() 127 | { 128 | initCL(defaultPlatformIndex, defaultDeviceType, defaultDeviceIndex); 129 | 130 | cl_mem mem = clCreateBuffer(context, CL_MEM_READ_ONLY, 131 | 10 * Sizeof.cl_float, null, null); 132 | 133 | Pointer pointer = Pointer.to( 134 | ByteBuffer.allocateDirect(10 * Sizeof.cl_float)); 135 | 136 | clEnqueueWriteBuffer(commandQueue, mem, CL_NON_BLOCKING, 0, 137 | 10 * Sizeof.cl_float, pointer, 0, null, null); 138 | 139 | clFlush(commandQueue); 140 | clFinish(commandQueue); 141 | } 142 | 143 | } 144 | --------------------------------------------------------------------------------