JOCL v2.6.0-rc-20250722
JOCL, OpenCL® API Binding for Java™ (public API).
CLTLInfoAccessor.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 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.impl;
30
31import java.nio.Buffer;
32import java.nio.ByteBuffer;
33
34import com.jogamp.common.nio.Buffers;
35import com.jogamp.common.nio.PointerBuffer;
36import com.jogamp.common.os.Platform;
37import com.jogamp.common.util.Bitstream;
38import com.jogamp.opencl.CLException;
39import com.jogamp.opencl.spi.CLInfoAccessor;
40import com.jogamp.opencl.util.CLUtil;
41
42/**
43 * Internal utility for common OpenCL clGetFooInfo calls.
44 * Threadsafe, threadlocal implementation.
45 * @author Michael Bien, et al.
46 */
47public abstract class CLTLInfoAccessor implements CLInfoAccessor {
48
49 private static final int BB_SIZE = 512;
50
51 protected final static ThreadLocal<ByteBuffer> localBB = new ThreadLocal<ByteBuffer>() {
52
53 @Override
54 protected ByteBuffer initialValue() {
55 return Buffers.newDirectByteBuffer(BB_SIZE);
56 }
57
58 };
59 protected final static ThreadLocal<PointerBuffer> localNSB = new ThreadLocal<PointerBuffer>() {
60
61 @Override
62 protected PointerBuffer initialValue() {
63 return PointerBuffer.allocateDirect(1);
64 }
65
66 };
67
68 @Override
69 public final long getUInt32Long(final int key) {
70 final ByteBuffer buffer = getBB(4).putInt(0, 0);
71 final int ret = getInfo(key, 4, buffer, null);
72 CLException.checkForError(ret, "error while asking for info value");
73 return Bitstream.toUInt32Long(buffer.getInt(0));
74 }
75
76 @Override
77 public final long getLong(final int key) {
78
79 final ByteBuffer buffer = getBB(8).putLong(0, 0);
80 final int ret = getInfo(key, 8, buffer, null);
81 CLException.checkForError(ret, "error while asking for info value");
82
83 return buffer.getLong(0);
84 }
85
86 @Override
87 public final String getString(final int key) {
88
89 final PointerBuffer sizeBuffer = getNSB();
90 int ret = getInfo(key, 0, null, sizeBuffer);
91 CLException.checkForError(ret, "error while asking for info string");
92
93 final int clSize = (int)sizeBuffer.get(0);
94 final ByteBuffer buffer = getBB(clSize);
95
96 ret = getInfo(key, buffer.capacity(), buffer, null);
97 CLException.checkForError(ret, "error while asking for info string");
98
99 final byte[] array = new byte[clSize];
100 buffer.get(array).rewind();
101
102 return CLUtil.clString2JavaString(array, clSize);
103
104 }
105
106 @Override
107 public final int[] getInts(final int key, final int n) {
108 // FIXME: Really 8 bytes per int on 64bit platforms ?
109 final ByteBuffer buffer = getBB(n * (Platform.is32Bit()?4:8));
110 final int ret = getInfo(key, buffer.capacity(), buffer, null);
111 CLException.checkForError(ret, "error while asking for info value");
112
113 final int[] array = new int[n];
114 for(int i = 0; i < array.length; i++) {
115 if(Platform.is32Bit()) {
116 array[i] = buffer.getInt();
117 }else{
118 array[i] = (int)buffer.getLong();
119 }
120 }
121 buffer.rewind();
122
123 return array;
124 }
125
126 protected ByteBuffer getBB(final int minCapacity) {
127 if(minCapacity > BB_SIZE) {
128 return Buffers.newDirectByteBuffer(minCapacity);
129 }else{
130 return localBB.get();
131 }
132 }
133
134 protected PointerBuffer getNSB() {
135 return localNSB.get();
136 }
137
138 protected abstract int getInfo(int name, long valueSize, Buffer value, PointerBuffer valueSizeRet);
139
140
141}
Main Exception type for runtime OpenCL errors and failed function calls (e.g.
static void checkForError(final int status, final String message)
Throws a CLException when status != CL_SUCCESS.
Internal utility for common OpenCL clGetFooInfo calls.
abstract int getInfo(int name, long valueSize, Buffer value, PointerBuffer valueSizeRet)
final String getString(final int key)
Returns the String value for the given key.
final int[] getInts(final int key, final int n)
ByteBuffer getBB(final int minCapacity)
final long getLong(final int key)
Returns the long value for the given key.
static final ThreadLocal< PointerBuffer > localNSB
final long getUInt32Long(final int key)
Returns the uint32_t value for the given key, reinterpreted as a long value.
static final ThreadLocal< ByteBuffer > localBB
static String clString2JavaString(final byte[] chars, int clLength)
Definition: CLUtil.java:51
Internal utility for common OpenCL clGetFooInfo calls.