JOCL v2.6.0-rc-20250722
JOCL, OpenCL® API Binding for Java™ (public API).
CLTLAccessorFactory.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 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
29/*
30 * Created on Wednesday, May 25 2011 00:57
31 */
32
33package com.jogamp.opencl.impl;
34
35import java.nio.IntBuffer;
36
37import com.jogamp.common.nio.AbstractBuffer;
38import com.jogamp.common.nio.PointerBuffer;
39import com.jogamp.opencl.llb.CL;
40import com.jogamp.opencl.spi.CLAccessorFactory;
41import com.jogamp.opencl.spi.CLInfoAccessor;
42import com.jogamp.opencl.spi.CLPlatformInfoAccessor;
43import java.nio.Buffer;
44
45import static com.jogamp.opencl.CLException.*;
46
47/**
48 *
49 * @author Michael Bien, et al.
50 */
51public class CLTLAccessorFactory implements CLAccessorFactory {
52
53 @Override
54 public CLInfoAccessor createDeviceInfoAccessor(final CL cl, final long id) {
55 return new CLDeviceInfoAccessor(cl, id);
56 }
57
58 @Override
59 public CLPlatformInfoAccessor createPlatformInfoAccessor(final CL cl, final long id) {
60 return new CLTLPlatformInfoAccessor(cl, id);
61 }
62
63 private final static class CLDeviceInfoAccessor extends CLTLInfoAccessor {
64
65 private final CL cl;
66 private final long ID;
67
68 private CLDeviceInfoAccessor(final CL cl, final long id) {
69 this.cl = cl;
70 this.ID = id;
71 }
72
73 @Override
74 public int getInfo(final int name, final long valueSize, final Buffer value, final PointerBuffer valueSizeRet) {
75 return cl.clGetDeviceInfo(ID, name, valueSize, value, valueSizeRet);
76 }
77
78 }
79
80 private final static class CLTLPlatformInfoAccessor extends CLTLInfoAccessor implements CLPlatformInfoAccessor {
81
82 private final long ID;
83 private final CL cl;
84
85 private CLTLPlatformInfoAccessor(final CL cl, final long id) {
86 this.ID = id;
87 this.cl = cl;
88 }
89
90 @Override
91 public int getInfo(final int name, final long valueSize, final Buffer value, final PointerBuffer valueSizeRet) {
92 return cl.clGetPlatformInfo(ID, name, valueSize, value, valueSizeRet);
93 }
94
95 @Override
96 public long[] getDeviceIDs(final long type) {
97
98 final IntBuffer buffer = getBB(4).asIntBuffer();
99 int ret = cl.clGetDeviceIDs(ID, type, 0, null, buffer);
100 final int count = buffer.get(0);
101
102 // return an empty buffer rather than throwing an exception
103 if(ret == CL.CL_DEVICE_NOT_FOUND || count == 0) {
104 return new long[0];
105 }else{
106 checkForError(ret, "error while enumerating devices");
107
108 final PointerBuffer deviceIDs = PointerBuffer.wrap(getBB(count*AbstractBuffer.POINTER_SIZE));
109 ret = cl.clGetDeviceIDs(ID, type, count, deviceIDs, null);
110 checkForError(ret, "error while enumerating devices");
111
112 final long[] ids = new long[count];
113 for (int i = 0; i < ids.length; i++) {
114 ids[i] = deviceIDs.get(i);
115 }
116 return ids;
117 }
118
119 }
120
121 }
122
123}
CLInfoAccessor createDeviceInfoAccessor(final CL cl, final long id)
CLPlatformInfoAccessor createPlatformInfoAccessor(final CL cl, final long id)
Internal utility for common OpenCL clGetFooInfo calls.
ByteBuffer getBB(final int minCapacity)
Java bindings to OpenCL, the Open Computing Language.
Definition: CL.java:26
int clGetDeviceInfo(long device, int param_name, long param_value_size, Buffer param_value, PointerBuffer param_value_size_ret)
Interface to C language function: cl_int {@native clGetDeviceInfo}(cl_device_id device,...
int clGetPlatformInfo(long platform, int param_name, long param_value_size, Buffer param_value, PointerBuffer param_value_size_ret)
Interface to C language function: cl_int {@native clGetPlatformInfo}(cl_platform_id platform,...
Implementations of this interface are factories responsible for creating CLAccessors.
Internal utility for common OpenCL clGetFooInfo calls.