JOCL v2.6.0-rc-20250722
JOCL, OpenCL® API Binding for Java™ (public API).
CLUtil.java
Go to the documentation of this file.
1/*
2 * Copyright 2009 - 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.opencl.util;
30
31import com.jogamp.common.JogampRuntimeException;
32import com.jogamp.opencl.llb.CL;
33import com.jogamp.opencl.CLDevice;
34import com.jogamp.opencl.CLPlatform;
35import com.jogamp.opencl.CLProperty;
36import java.lang.annotation.Annotation;
37import java.lang.reflect.InvocationTargetException;
38import java.lang.reflect.Method;
39import java.nio.ByteBuffer;
40import java.util.ArrayList;
41import java.util.LinkedHashMap;
42import java.util.List;
43import java.util.Map;
44
45/**
46 *
47 * @author Michael Bien
48 */
49public class CLUtil {
50
51 public static String clString2JavaString(final byte[] chars, int clLength) {
52
53 // certain char queries on windows always claim to have a fixed length
54 // e.g. (clDeviceInfo(CL_DEVICE_NAME) is always 64.. but luckily they are 0 terminated)
55 while(clLength > 0 && chars[--clLength] == 0);
56
57 return clLength==0 ? "" : new String(chars, 0, clLength+1);
58 }
59
60 public static String clString2JavaString(final ByteBuffer chars, final int clLength) {
61 if (clLength==0) {
62 return "";
63 }else{
64 final byte[] array = new byte[clLength];
65 chars.get(array).rewind();
66 return clString2JavaString(array, clLength);
67 }
68 }
69
70 /**
71 * Returns true if clBoolean == CL.CL_TRUE.
72 */
73 public static boolean clBoolean(final int clBoolean) {
74 return clBoolean == CL.CL_TRUE;
75 }
76
77 /**
78 * Returns b ? CL.CL_TRUE : CL.CL_FALSE
79 */
80 public static int clBoolean(final boolean b) {
81 return b ? CL.CL_TRUE : CL.CL_FALSE;
82 }
83
84 /**
85 * Reads all platform properties and returns them as key-value map.
86 */
87 public static Map<String, String> obtainPlatformProperties(final CLPlatform platform) {
88 return readCLProperties(platform);
89 }
90
91 /**
92 * Reads all device properties and returns them as key-value map.
93 */
94 public static Map<String, String> obtainDeviceProperties(final CLDevice dev) {
95 return readCLProperties(dev);
96 }
97
98 private static Map<String, String> readCLProperties(final Object obj) {
99 try {
100 return invoke(listMethods(obj.getClass()), obj);
101 } catch (final IllegalArgumentException ex) {
102 throw new JogampRuntimeException(ex);
103 } catch (final IllegalAccessException ex) {
104 throw new JogampRuntimeException(ex);
105 }
106 }
107
108 static Map<String, String> invoke(final List<Method> methods, final Object obj) throws IllegalArgumentException, IllegalAccessException {
109 final Map<String, String> map = new LinkedHashMap<String, String>();
110 for (final Method method : methods) {
111 Object info = null;
112 try {
113 info = method.invoke(obj);
114 } catch (final InvocationTargetException ex) {
115 info = ex.getTargetException();
116 }
117
118 if(info.getClass().isArray()) {
119 info = asList(info);
120 }
121
122 final String value = method.getAnnotation(CLProperty.class).value();
123 map.put(value, info.toString());
124 }
125 return map;
126 }
127
128 static List<Method> listMethods(final Class<?> clazz) throws SecurityException {
129 final List<Method> list = new ArrayList<Method>();
130 for (final Method method : clazz.getDeclaredMethods()) {
131 final Annotation[] annotations = method.getDeclaredAnnotations();
132 for (final Annotation annotation : annotations) {
133 if (annotation instanceof CLProperty) {
134 list.add(method);
135 }
136 }
137 }
138 return list;
139 }
140
141 private static List<Number> asList(final Object info) {
142 final List<Number> list = new ArrayList<Number>();
143 if(info instanceof int[]) {
144 final int[] array = (int[]) info;
145 for (final int i : array) {
146 list.add(i);
147 }
148 }else if(info instanceof long[]) {
149 final long[] array = (long[]) info;
150 for (final long i : array) {
151 list.add(i);
152 }
153 }else if(info instanceof float[]) {
154 final float[] array = (float[]) info;
155 for (final float i : array) {
156 list.add(i);
157 }
158 }else if(info instanceof double[]) {
159 final double[] array = (double[]) info;
160 for (final double i : array) {
161 list.add(i);
162 }
163 }
164 return list;
165 }
166
167}
This object represents an OpenCL device.
Definition: CLDevice.java:53
CLPlatfrorm representing a OpenCL implementation (e.g.
Definition: CLPlatform.java:99
static int clBoolean(final boolean b)
Returns b ? CL.CL_TRUE : CL.CL_FALSE.
Definition: CLUtil.java:80
static String clString2JavaString(final byte[] chars, int clLength)
Definition: CLUtil.java:51
static Map< String, String > obtainPlatformProperties(final CLPlatform platform)
Reads all platform properties and returns them as key-value map.
Definition: CLUtil.java:87
static String clString2JavaString(final ByteBuffer chars, final int clLength)
Definition: CLUtil.java:60
static Map< String, String > obtainDeviceProperties(final CLDevice dev)
Reads all device properties and returns them as key-value map.
Definition: CLUtil.java:94
static boolean clBoolean(final int clBoolean)
Returns true if clBoolean == CL.CL_TRUE.
Definition: CLUtil.java:73
Java bindings to OpenCL, the Open Computing Language.
Definition: CL.java:26
static final int CL_TRUE
Define "CL_TRUE" with expression '1', CType: int.
Definition: CL.java:211
static final int CL_FALSE
Define "CL_FALSE" with expression '0', CType: int.
Definition: CL.java:801