JOCL v2.6.0-rc-20250722
JOCL, OpenCL® API Binding for Java™ (public API).
CLContext.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;
30
31import java.io.BufferedReader;
32import java.io.IOException;
33import java.io.InputStream;
34import java.io.InputStreamReader;
35import java.nio.Buffer;
36import java.nio.ByteBuffer;
37import java.nio.DoubleBuffer;
38import java.nio.FloatBuffer;
39import java.nio.IntBuffer;
40import java.nio.LongBuffer;
41import java.nio.ShortBuffer;
42import java.util.ArrayList;
43import java.util.Collection;
44import java.util.Collections;
45import java.util.HashMap;
46import java.util.HashSet;
47import java.util.List;
48import java.util.Map;
49import java.util.Set;
50
51import com.jogamp.common.nio.Buffers;
52import com.jogamp.common.nio.PointerBuffer;
53import com.jogamp.common.os.Platform;
54import com.jogamp.opencl.CLDevice.Type;
55import com.jogamp.opencl.CLMemory.Mem;
56import com.jogamp.opencl.CLSampler.AddressingMode;
57import com.jogamp.opencl.CLSampler.FilteringMode;
58import com.jogamp.opencl.llb.CL;
59import com.jogamp.opencl.llb.impl.CLImageFormatImpl;
60
61/**
62 * CLContext is responsible for managing objects such as command-queues, memory,
63 * program and kernel objects and for executing kernels on one or more devices
64 * specified in the context.
65 * <p>
66 * Must be released if no longer used to free native resources. {@link #release()} will
67 * also free all associated {@link CLResource} like programs, samplers, command queues and memory
68 * objects.
69 * </p>
70 * <p>
71 * For a code example see {@link CLPlatform}.
72 * <p/>
73 *
74 * concurrency:<br/>
75 * CLContext is threadsafe.
76 *
77 * @author Michael Bien, et al.
78 */
79public class CLContext extends CLObjectResource {
80
81 protected CLDevice[] devices;
82
83 protected final Set<CLProgram> programs;
84 protected final Set<CLSampler> samplers;
85 protected final Set<CLMemory<? extends Buffer>> memoryObjects;
86
88
89 protected final CLPlatform platform;
90
91 private final ErrorDispatcher errorHandler;
92
93 protected CLContext(final CLPlatform platform, final long contextID, final ErrorDispatcher dispatcher) {
94 super(contextID);
95 this.platform = platform;
96
97 this.programs = Collections.synchronizedSet(new HashSet<CLProgram>());
98 this.samplers = Collections.synchronizedSet(new HashSet<CLSampler>());
99 this.memoryObjects = Collections.synchronizedSet(new HashSet<CLMemory<? extends Buffer>>());
100
101 this.queuesMap = new HashMap<CLDevice, List<CLCommandQueue>>();
102
103 this.errorHandler = dispatcher;
104
105 /*
106 addCLErrorHandler(new CLErrorHandler() {
107 public void onError(String errinfo, ByteBuffer private_info, long cb) {
108 java.util.logging.Logger.getLogger(getClass().getName()).warning(errinfo);
109 }
110 });
111 */
112
113 }
114
115 private synchronized void initDevices(final CL cl) {
116
117 if (devices == null) {
118
119 final PointerBuffer deviceCount = PointerBuffer.allocateDirect(1);
120
121 int ret = cl.clGetContextInfo(ID, CL.CL_CONTEXT_DEVICES, 0, null, deviceCount);
122 CLException.checkForError(ret, "can not enumerate devices");
123
124 final ByteBuffer deviceIDs = Buffers.newDirectByteBuffer((int)deviceCount.get());
125 ret = cl.clGetContextInfo(ID, CL.CL_CONTEXT_DEVICES, deviceIDs.capacity(), deviceIDs, null);
126 CLException.checkForError(ret, "can not enumerate devices");
127
128 devices = new CLDevice[deviceIDs.capacity() / (Platform.is32Bit() ? 4 : 8)];
129 for (int i = 0; i < devices.length; i++) {
130 devices[i] = new CLDevice(this, Platform.is32Bit() ? deviceIDs.getInt() : deviceIDs.getLong());
131 }
132 }
133 }
134
135 /**
136 * Creates a context on all available devices (CL_DEVICE_TYPE_ALL).
137 * The platform to be used is implementation dependent.
138 */
139 public static CLContext create() {
140 return create((CLPlatform)null, Type.ALL);
141 }
142
143 /**
144 * Creates a context on the specified device types.
145 * The platform to be used is implementation dependent.
146 */
147 public static CLContext create(final Type... deviceTypes) {
148 return create(null, deviceTypes);
149 }
150
151 /**
152 * Creates a context on the specified platform on all available devices (CL_DEVICE_TYPE_ALL).
153 */
154 public static CLContext create(final CLPlatform platform) {
155 return create(platform, Type.ALL);
156 }
157
158 /**
159 * Creates a context on the specified platform and with the specified
160 * device types.
161 */
162 public static CLContext create(CLPlatform platform, final Type... deviceTypes) {
163
164 if(platform == null) {
166 }
167
168 final long type = toDeviceBitmap(deviceTypes);
169
170 final PointerBuffer properties = setupContextProperties(platform);
171 final ErrorDispatcher dispatcher = new ErrorDispatcher();
172 return new CLContext(platform, createContextFromType(platform, dispatcher, properties, type), dispatcher);
173 }
174
175 /**
176 * Creates a context on the specified devices.
177 */
178 public static CLContext create(final CLDevice... devices) {
179
180 if(devices == null) {
181 throw new IllegalArgumentException("no devices specified");
182 }else if(devices[0] == null) {
183 throw new IllegalArgumentException("first device was null");
184 }
185
187
188 final PointerBuffer properties = setupContextProperties(platform);
189 final ErrorDispatcher dispatcher = new ErrorDispatcher();
190 final CLContext context = new CLContext(platform, createContext(platform, dispatcher, properties, devices), dispatcher);
191 if(devices != null) {
192 for (int i = 0; i < devices.length; i++) {
193 devices[i].setContext(context);
194 }
195 }
196 return context;
197 }
198
199 protected static long createContextFromType(final CLPlatform platform, final CLErrorHandler handler, final PointerBuffer properties, final long deviceType) {
200 final IntBuffer status = Buffers.newDirectIntBuffer(1);
201 final CL cl = platform.getCLBinding();
202 final long context = cl.clCreateContextFromType(properties, deviceType, handler, status);
203
204 CLException.checkForError(status.get(), "can not create CL context");
205
206 return context;
207 }
208
209 protected static long createContext(final CLPlatform platform, final CLErrorHandler handler, final PointerBuffer properties, final CLDevice... devices) {
210 final IntBuffer status = Buffers.newDirectIntBuffer(1);
211 PointerBuffer pb = null;
212 if(devices != null && devices.length != 0) {
213 pb = PointerBuffer.allocateDirect(devices.length);
214 for (int i = 0; i < devices.length; i++) {
215 final CLDevice device = devices[i];
216 if(device == null) {
217 throw new IllegalArgumentException("device at index "+i+" was null.");
218 }
219 pb.put(i, device.ID);
220 }
221 }
222 final CL cl = platform.getCLBinding();
223 final long context = cl.clCreateContext(properties, pb, handler, status);
224
225 CLException.checkForError(status.get(), "can not create CL context");
226
227 return context;
228 }
229
230 private static PointerBuffer setupContextProperties(final CLPlatform platform) {
231 if(platform == null) {
232 throw new RuntimeException("no OpenCL installation found");
233 }
234
235 return PointerBuffer.allocateDirect(3).put(CL.CL_CONTEXT_PLATFORM)
236 .put(platform.ID).put(0) // 0 terminated array
237 .rewind();
238 }
239
240 /**
241 * Creates a program from the given sources, the returned program is not build yet.
242 */
243 public CLProgram createProgram(final String src) {
244 final CLProgram program = CLProgram.create(this, src);
245 programs.add(program);
246 return program;
247 }
248
249 /**
250 * Creates a program and reads the source from stream, the returned program is not build yet.
251 * The InputStream is automatically closed after the sources have been read.
252 * @throws IOException when a IOException occurred while reading or closing the stream.
253 */
254 public CLProgram createProgram(final InputStream source) throws IOException {
255
256 if(source == null)
257 throw new IllegalArgumentException("input stream for program source must not be null");
258
259 final BufferedReader reader = new BufferedReader(new InputStreamReader(source));
260 final StringBuilder sb = new StringBuilder(2048);
261
262 String line;
263 try {
264 while ((line = reader.readLine()) != null)
265 sb.append(line).append("\n");
266 } finally {
267 reader.close();
268 }
269
270 return createProgram(sb.toString());
271 }
272
273 /**
274 * Creates a program from the given binaries, the program is not build yet.
275 * <br/>Creating a program will fail if:<br/>
276 * <ul>
277 * <li>the submitted binaries are invalid or can not be loaded from the OpenCL driver</li>
278 * <li>the binaries do not fit to the CLDevices associated with this context</li>
279 * <li>binaries are missing for one or more CLDevices</li>
280 * </ul>
281 */
282 public CLProgram createProgram(final Map<CLDevice, byte[]> binaries) {
283 final CLProgram program = CLProgram.create(this, binaries);
284 program.setNoSource();
285 programs.add(program);
286 return program;
287 }
288
289 /**
290 * Creates a CLBuffer with the specified flags and element count. No flags creates a MEM.READ_WRITE buffer.
291 */
292 public final CLBuffer<ShortBuffer> createShortBuffer(final int size, final Mem... flags) {
293 return createBuffer(Buffers.newDirectShortBuffer(size), flags);
294 }
295
296 /**
297 * Creates a CLBuffer with the specified flags and element count. No flags creates a MEM.READ_WRITE buffer.
298 */
299 public final CLBuffer<IntBuffer> createIntBuffer(final int size, final Mem... flags) {
300 return createBuffer(Buffers.newDirectIntBuffer(size), flags);
301 }
302
303 /**
304 * Creates a CLBuffer with the specified flags and element count. No flags creates a MEM.READ_WRITE buffer.
305 */
306 public final CLBuffer<LongBuffer> createLongBuffer(final int size, final Mem... flags) {
307 return createBuffer(Buffers.newDirectLongBuffer(size), flags);
308 }
309
310 /**
311 * Creates a CLBuffer with the specified flags and element count. No flags creates a MEM.READ_WRITE buffer.
312 */
313 public final CLBuffer<FloatBuffer> createFloatBuffer(final int size, final Mem... flags) {
314 return createBuffer(Buffers.newDirectFloatBuffer(size), flags);
315 }
316
317 /**
318 * Creates a CLBuffer with the specified flags and element count. No flags creates a MEM.READ_WRITE buffer.
319 */
320 public final CLBuffer<DoubleBuffer> createDoubleBuffer(final int size, final Mem... flags) {
321 return createBuffer(Buffers.newDirectDoubleBuffer(size), flags);
322 }
323
324 /**
325 * Creates a CLBuffer with the specified flags and buffer size in bytes. No flags creates a MEM.READ_WRITE buffer.
326 */
327 public final CLBuffer<ByteBuffer> createByteBuffer(final int size, final Mem... flags) {
328 return createByteBuffer(size, Mem.flagsToInt(flags));
329 }
330
331 /**
332 * Creates a CLBuffer with the specified flags and buffer size in bytes.
333 */
334 public final CLBuffer<ByteBuffer> createByteBuffer(final int size, final int flags) {
335 return createBuffer(Buffers.newDirectByteBuffer(size), flags);
336 }
337
338 /**
339 * Creates a CLBuffer with the specified flags. No flags creates a MEM.READ_WRITE buffer.
340 */
341 public final CLBuffer<?> createBuffer(final int size, final Mem... flags) {
342 return createBuffer(size, Mem.flagsToInt(flags));
343 }
344
345 /**
346 * Creates a CLBuffer with the specified flags.
347 */
348 public final CLBuffer<?> createBuffer(final int size, final int flags) {
349 final CLBuffer<?> buffer = CLBuffer.create(this, size, flags);
350 memoryObjects.add(buffer);
351 return buffer;
352 }
353
354 /**
355 * Creates a CLBuffer with the specified flags. No flags creates a MEM.READ_WRITE buffer.
356 */
357 public final <B extends Buffer> CLBuffer<B> createBuffer(final B directBuffer, final Mem... flags) {
358 return createBuffer(directBuffer, Mem.flagsToInt(flags));
359 }
360
361 /**
362 * Creates a CLBuffer with the specified flags.
363 */
364 public final <B extends Buffer> CLBuffer<B> createBuffer(final B directBuffer, final int flags) {
365 final CLBuffer<B> buffer = CLBuffer.create(this, directBuffer, flags);
366 memoryObjects.add(buffer);
367 return buffer;
368 }
369
370 /**
371 * Creates a CLImage2d with the specified format, dimension and flags.
372 */
373 public final CLImage2d<?> createImage2d(final int width, final int height, final CLImageFormat format, final Mem... flags) {
374 return createImage2d(null, width, height, 0, format, flags);
375 }
376
377 /**
378 * Creates a CLImage2d with the specified format, dimension and flags.
379 */
380 public final CLImage2d<?> createImage2d(final int width, final int height, final int rowPitch, final CLImageFormat format, final Mem... flags) {
381 return createImage2d(null, width, height, rowPitch, format, flags);
382 }
383
384 /**
385 * Creates a CLImage2d with the specified format, dimension and flags.
386 */
387 public final <B extends Buffer> CLImage2d<B> createImage2d(final B directBuffer, final int width, final int height, final CLImageFormat format, final Mem... flags) {
388 return createImage2d(directBuffer, width, height, 0, format, flags);
389 }
390
391 /**
392 * Creates a CLImage2d with the specified format, dimension and flags.
393 */
394 public final <B extends Buffer> CLImage2d<B> createImage2d(final B directBuffer, final int width, final int height, final int rowPitch, final CLImageFormat format, final Mem... flags) {
395 final CLImage2d<B> image = CLImage2d.createImage(this, directBuffer, width, height, rowPitch, format, Mem.flagsToInt(flags));
396 memoryObjects.add(image);
397 return image;
398 }
399
400 /**
401 * Creates a CLImage3d with the specified format, dimension and flags.
402 */
403 public final CLImage3d<?> createImage3d(final int width, final int height, final int depth, final CLImageFormat format, final Mem... flags) {
404 return createImage3d(null, width, height, depth, format, flags);
405 }
406
407 /**
408 * Creates a CLImage3d with the specified format, dimension and flags.
409 */
410 public final CLImage3d<?> createImage3d(final int width, final int height, final int depth, final int rowPitch, final int slicePitch, final CLImageFormat format, final Mem... flags) {
411 return createImage3d(null, width, height, depth, rowPitch, slicePitch, format, flags);
412 }
413
414 /**
415 * Creates a CLImage3d with the specified format, dimension and flags.
416 */
417 public final <B extends Buffer> CLImage3d<B> createImage3d(final B directBuffer, final int width, final int height, final int depth, final CLImageFormat format, final Mem... flags) {
418 return createImage3d(directBuffer, width, height, depth, 0, 0, format, flags);
419 }
420
421 /**
422 * Creates a CLImage3d with the specified format, dimension and flags.
423 */
424 public final <B extends Buffer> CLImage3d<B> createImage3d(final B directBuffer, final int width, final int height, final int depth, final int rowPitch, final int slicePitch, final CLImageFormat format, final Mem... flags) {
425 final CLImage3d<B> image = CLImage3d.createImage(this, directBuffer, width, height, depth, rowPitch, slicePitch, format, Mem.flagsToInt(flags));
426 memoryObjects.add(image);
427 return image;
428 }
429
430 CLCommandQueue createCommandQueue(final CLDevice device, final long properties) {
431
432 final CLCommandQueue queue = CLCommandQueue.create(this, device, properties);
433
434 synchronized(queuesMap) {
435 List<CLCommandQueue> list = queuesMap.get(device);
436 if(list == null) {
437 list = new ArrayList<CLCommandQueue>();
438 queuesMap.put(device, list);
439 }
440 list.add(queue);
441 }
442
443 return queue;
444 }
445
446 public CLSampler createSampler(final AddressingMode addrMode, final FilteringMode filtMode, final boolean normalizedCoords) {
447 final CLSampler sampler = CLSampler.create(this, addrMode, filtMode, normalizedCoords);
448 samplers.add(sampler);
449 return sampler;
450 }
451
452 void onProgramReleased(final CLProgram program) {
453 programs.remove(program);
454 }
455
456 void onMemoryReleased(final CLMemory<?> buffer) {
457 memoryObjects.remove(buffer);
458 }
459
460 void onCommandQueueReleased(final CLDevice device, final CLCommandQueue queue) {
461 synchronized(queuesMap) {
462 final List<CLCommandQueue> list = queuesMap.get(device);
463 list.remove(queue);
464 // remove empty lists from map
465 if(list.isEmpty())
466 queuesMap.remove(device);
467 }
468 }
469
470 void onSamplerReleased(final CLSampler sampler) {
471 samplers.remove(sampler);
472 }
473
474 public void addCLErrorHandler(final CLErrorHandler handler) {
475 errorHandler.addHandler(handler);
476 }
477
478 public void removeCLErrorHandler(final CLErrorHandler handler) {
479 errorHandler.removeHandler(handler);
480 }
481
482 private void release(final Collection<? extends CLResource> resources) {
483 // resources remove themselves when released, see above
484 while(!resources.isEmpty()) {
485 resources.iterator().next().release();
486 }
487 }
488
489 /**
490 * Releases this context and all resources.
491 */
492 @Override
493 public synchronized void release() {
494 super.release();
495
496 try{
497 //release all resources
501
502 synchronized(queuesMap) {
503 final Collection<List<CLCommandQueue>> queuesList = queuesMap.values();
504 while(!queuesList.isEmpty())
505 release(queuesList.iterator().next());
506 }
507
508 } finally {
509 final int ret = platform.getCLBinding().clReleaseContext(ID);
510 CLException.checkForError(ret, "error releasing context");
511 }
512
513 }
514
515 protected void overrideContext(final CLDevice device) {
516 device.setContext(this);
517 }
518
519 private CLImageFormat[] getSupportedImageFormats(final int flags, final int type) {
520
521 final CL binding = platform.getCLBinding();
522
523 final int[] entries = new int[1];
524 int ret = binding.clGetSupportedImageFormats(ID, flags, type, 0, null, entries, 0);
525 if(ret != CL.CL_SUCCESS) {
526 throw CLException.newException(ret, "error calling clGetSupportedImageFormats");
527 }
528
529 final int count = entries[0];
530 if(count == 0) {
531 return new CLImageFormat[0];
532 }
533
534 final CLImageFormat[] formats = new CLImageFormat[count];
535 final CLImageFormatImpl impl = CLImageFormatImpl.create(Buffers.newDirectByteBuffer(count * CLImageFormatImpl.size()));
536 ret = binding.clGetSupportedImageFormats(ID, flags, type, count, impl, null);
537 if(ret != CL.CL_SUCCESS) {
538 throw CLException.newException(ret, "error calling clGetSupportedImageFormats");
539 }
540
541 final ByteBuffer buffer = impl.getBuffer();
542 for (int i = 0; i < formats.length; i++) {
543 formats[i] = new CLImageFormat(CLImageFormatImpl.create(buffer.slice()));
544 buffer.position(i*CLImageFormatImpl.size());
545 }
546
547 return formats;
548
549 }
550
551 /**
552 * Returns all supported 2d image formats with the (optional) memory allocation flags.
553 */
555 return getSupportedImageFormats(flags==null?0:Mem.flagsToInt(flags), CL.CL_MEM_OBJECT_IMAGE2D);
556 }
557
558 /**
559 * Returns all supported 3d image formats with the (optional) memory allocation flags.
560 */
562 return getSupportedImageFormats(flags==null?0:Mem.flagsToInt(flags), CL.CL_MEM_OBJECT_IMAGE3D);
563 }
564
565 /**
566 * Returns the CLPlatform this context is running on.
567 */
568 @Override
570 return platform;
571 }
572
573 @Override
575 return this;
576 }
577
578 /**
579 * Returns a read only shapshot of all programs associated with this context.
580 */
581 public List<CLProgram> getPrograms() {
582 synchronized(programs) {
583 return Collections.unmodifiableList(new ArrayList<CLProgram>(programs));
584 }
585 }
586
587 /**
588 * Returns a read only shapshot of all allocated memory objects associated with this context.
589 */
590 public List<CLMemory<? extends Buffer>> getMemoryObjects() {
591 synchronized(memoryObjects) {
592 return Collections.unmodifiableList(new ArrayList<CLMemory<? extends Buffer>>(memoryObjects));
593 }
594 }
595
596 /**
597 * Returns a read only shapshot of all samplers associated with this context.
598 */
599 public List<CLSampler> getSamplers() {
600 synchronized(samplers) {
601 return Collections.unmodifiableList(new ArrayList<CLSampler>(samplers));
602 }
603 }
604
605 /**
606 * Returns the device with maximal FLOPS from this context.
607 * The device speed is estimated by calculating the product of
608 * MAX_COMPUTE_UNITS and MAX_CLOCK_FREQUENCY.
609 * @see #getMaxFlopsDevice(com.jogamp.opencl.CLDevice.Type)
610 */
612 return CLPlatform.findMaxFlopsDevice(getDevices());
613 }
614
615 /**
616 * Returns the device with maximal FLOPS of the specified device type from this context.
617 * The device speed is estimated by calculating the product of
618 * MAX_COMPUTE_UNITS and MAX_CLOCK_FREQUENCY.
619 */
621 return CLPlatform.findMaxFlopsDevice(getDevices(), type);
622 }
623
624 /**
625 * Returns the maximum {@link CLDevice#getMemBaseAddrAlign()} of all devices.
626 */
628 long maxAlignment = 0;
629 for (final CLDevice device : getDevices()) {
630 maxAlignment = Math.max(maxAlignment, device.getMemBaseAddrAlign());
631 }
632 return maxAlignment;
633 }
634
635 /**
636 * Returns all devices associated with this CLContext.
637 */
638 public CLDevice[] getDevices() {
639 initDevices(platform.getCLBinding());
640 return devices;
641 }
642
643 /**
644 * Return the low level OpenCL interface.
645 */
646 public CL getCL() {
647 return getPlatform().getCLBinding();
648 }
649
650 CLDevice getDevice(final long dID) {
651 final CLDevice[] deviceArray = getDevices();
652 for (int i = 0; i < deviceArray.length; i++) {
653 if(dID == deviceArray[i].ID)
654 return deviceArray[i];
655 }
656 return null;
657 }
658
659 protected static long toDeviceBitmap(final Type[] deviceTypes) {
660 long bitmap = 0;
661 if (deviceTypes != null) {
662 for (int i = 0; i < deviceTypes.length; i++) {
663 final Type type = deviceTypes[i];
664 if(type == null) {
665 throw new IllegalArgumentException("Device type at index "+i+" was null.");
666 }
667 bitmap |= type.TYPE;
668 }
669 }
670 return bitmap;
671 }
672
673 @Override
674 public String toString() {
675 return getClass().getSimpleName()+" [id: " + ID
676 + ", platform: " + getPlatform().getName()
677 + ", profile: " + getPlatform().getProfile()
678 + ", devices: " + getDevices().length
679 + "]";
680 }
681
682 @Override
683 public boolean equals(final Object obj) {
684 if (obj == null) {
685 return false;
686 }
687 if (getClass() != obj.getClass()) {
688 return false;
689 }
690 final CLContext other = (CLContext) obj;
691 if (this.ID != other.ID) {
692 return false;
693 }
694 return true;
695 }
696
697 @Override
698 public int hashCode() {
699 int hash = 7;
700 hash = 23 * hash + (int) (this.ID ^ (this.ID >>> 32));
701 return hash;
702 }
703
705 return new ErrorDispatcher();
706 }
707
708 protected static class ErrorDispatcher implements CLErrorHandler {
709
710 private CLErrorHandler[] clientHandlers = new CLErrorHandler[0];
711
712 @Override
713 public synchronized void onError(final String errinfo, final ByteBuffer private_info, final long cb) {
714 final CLErrorHandler[] handlers = this.clientHandlers;
715 for (int i = 0; i < handlers.length; i++) {
716 handlers[i].onError(errinfo, private_info, cb);
717 }
718 }
719
720 private synchronized void addHandler(final CLErrorHandler handler) {
721
722 if(handler == null) {
723 throw new IllegalArgumentException("handler was null.");
724 }
725
726 final CLErrorHandler[] handlers = new CLErrorHandler[clientHandlers.length+1];
727 System.arraycopy(clientHandlers, 0, handlers, 0, clientHandlers.length);
728 handlers[handlers.length-1] = handler;
729 clientHandlers = handlers;
730 }
731
732 private synchronized void removeHandler(final CLErrorHandler handler) {
733
734 if(handler == null) {
735 throw new IllegalArgumentException("handler was null.");
736 }
737
738 for (int i = 0; i < clientHandlers.length; i++) {
739 if(handler.equals(clientHandlers[i])) {
740 final CLErrorHandler[] handlers = new CLErrorHandler[clientHandlers.length-1];
741 System.arraycopy(clientHandlers, 0, handlers, 0, i);
742 System.arraycopy(clientHandlers, i, handlers, 0, handlers.length-i);
743 clientHandlers = handlers;
744 return;
745 }
746 }
747 }
748
749
750 }
751
752
753}
OpenCL buffer object wrapping an optional NIO buffer.
Definition: CLBuffer.java:46
The command queue is used to queue a set of operations for a specific CLDevice.
synchronized void onError(final String errinfo, final ByteBuffer private_info, final long cb)
Definition: CLContext.java:713
CLContext is responsible for managing objects such as command-queues, memory, program and kernel obje...
Definition: CLContext.java:79
void overrideContext(final CLDevice device)
Definition: CLContext.java:515
List< CLSampler > getSamplers()
Returns a read only shapshot of all samplers associated with this context.
Definition: CLContext.java:599
static long createContextFromType(final CLPlatform platform, final CLErrorHandler handler, final PointerBuffer properties, final long deviceType)
Definition: CLContext.java:199
CLContext(final CLPlatform platform, final long contextID, final ErrorDispatcher dispatcher)
Definition: CLContext.java:93
CLImageFormat[] getSupportedImage2dFormats(final Mem... flags)
Returns all supported 2d image formats with the (optional) memory allocation flags.
Definition: CLContext.java:554
final< B extends Buffer > CLImage2d< B > createImage2d(final B directBuffer, final int width, final int height, final int rowPitch, final CLImageFormat format, final Mem... flags)
Creates a CLImage2d with the specified format, dimension and flags.
Definition: CLContext.java:394
final Set< CLProgram > programs
Definition: CLContext.java:83
List< CLMemory<? extends Buffer > > getMemoryObjects()
Returns a read only shapshot of all allocated memory objects associated with this context.
Definition: CLContext.java:590
CLDevice[] getDevices()
Returns all devices associated with this CLContext.
Definition: CLContext.java:638
void addCLErrorHandler(final CLErrorHandler handler)
Definition: CLContext.java:474
final CLBuffer< ShortBuffer > createShortBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and element count.
Definition: CLContext.java:292
CLProgram createProgram(final String src)
Creates a program from the given sources, the returned program is not build yet.
Definition: CLContext.java:243
static CLContext create()
Creates a context on all available devices (CL_DEVICE_TYPE_ALL).
Definition: CLContext.java:139
final< B extends Buffer > CLImage3d< B > createImage3d(final B directBuffer, final int width, final int height, final int depth, final int rowPitch, final int slicePitch, final CLImageFormat format, final Mem... flags)
Creates a CLImage3d with the specified format, dimension and flags.
Definition: CLContext.java:424
final CLBuffer< ByteBuffer > createByteBuffer(final int size, final int flags)
Creates a CLBuffer with the specified flags and buffer size in bytes.
Definition: CLContext.java:334
final CLImage3d<?> createImage3d(final int width, final int height, final int depth, final int rowPitch, final int slicePitch, final CLImageFormat format, final Mem... flags)
Creates a CLImage3d with the specified format, dimension and flags.
Definition: CLContext.java:410
void removeCLErrorHandler(final CLErrorHandler handler)
Definition: CLContext.java:478
static CLContext create(CLPlatform platform, final Type... deviceTypes)
Creates a context on the specified platform and with the specified device types.
Definition: CLContext.java:162
final CLBuffer< DoubleBuffer > createDoubleBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and element count.
Definition: CLContext.java:320
final CLBuffer< IntBuffer > createIntBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and element count.
Definition: CLContext.java:299
final CLBuffer<?> createBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags.
Definition: CLContext.java:341
CLProgram createProgram(final InputStream source)
Creates a program and reads the source from stream, the returned program is not build yet.
Definition: CLContext.java:254
CLContext getContext()
Returns the context for this OpenCL object.
Definition: CLContext.java:574
final< B extends Buffer > CLImage2d< B > createImage2d(final B directBuffer, final int width, final int height, final CLImageFormat format, final Mem... flags)
Creates a CLImage2d with the specified format, dimension and flags.
Definition: CLContext.java:387
final< B extends Buffer > CLImage3d< B > createImage3d(final B directBuffer, final int width, final int height, final int depth, final CLImageFormat format, final Mem... flags)
Creates a CLImage3d with the specified format, dimension and flags.
Definition: CLContext.java:417
final Set< CLMemory<? extends Buffer > > memoryObjects
Definition: CLContext.java:85
CLProgram createProgram(final Map< CLDevice, byte[]> binaries)
Creates a program from the given binaries, the program is not build yet.
Definition: CLContext.java:282
final< B extends Buffer > CLBuffer< B > createBuffer(final B directBuffer, final int flags)
Creates a CLBuffer with the specified flags.
Definition: CLContext.java:364
static long createContext(final CLPlatform platform, final CLErrorHandler handler, final PointerBuffer properties, final CLDevice... devices)
Definition: CLContext.java:209
CLDevice getMaxFlopsDevice()
Returns the device with maximal FLOPS from this context.
Definition: CLContext.java:611
final CLBuffer< ByteBuffer > createByteBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and buffer size in bytes.
Definition: CLContext.java:327
static CLContext create(final CLDevice... devices)
Creates a context on the specified devices.
Definition: CLContext.java:178
final CLBuffer< LongBuffer > createLongBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and element count.
Definition: CLContext.java:306
static long toDeviceBitmap(final Type[] deviceTypes)
Definition: CLContext.java:659
CLImageFormat[] getSupportedImage3dFormats(final Mem... flags)
Returns all supported 3d image formats with the (optional) memory allocation flags.
Definition: CLContext.java:561
static ErrorDispatcher createErrorHandler()
Definition: CLContext.java:704
final Map< CLDevice, List< CLCommandQueue > > queuesMap
Definition: CLContext.java:87
boolean equals(final Object obj)
Definition: CLContext.java:683
List< CLProgram > getPrograms()
Returns a read only shapshot of all programs associated with this context.
Definition: CLContext.java:581
CLSampler createSampler(final AddressingMode addrMode, final FilteringMode filtMode, final boolean normalizedCoords)
Definition: CLContext.java:446
static CLContext create(final CLPlatform platform)
Creates a context on the specified platform on all available devices (CL_DEVICE_TYPE_ALL).
Definition: CLContext.java:154
CL getCL()
Return the low level OpenCL interface.
Definition: CLContext.java:646
final CLBuffer< FloatBuffer > createFloatBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and element count.
Definition: CLContext.java:313
final< B extends Buffer > CLBuffer< B > createBuffer(final B directBuffer, final Mem... flags)
Creates a CLBuffer with the specified flags.
Definition: CLContext.java:357
final CLImage2d<?> createImage2d(final int width, final int height, final int rowPitch, final CLImageFormat format, final Mem... flags)
Creates a CLImage2d with the specified format, dimension and flags.
Definition: CLContext.java:380
long getMaxMemBaseAddrAlign()
Returns the maximum CLDevice#getMemBaseAddrAlign() of all devices.
Definition: CLContext.java:627
final CLImage2d<?> createImage2d(final int width, final int height, final CLImageFormat format, final Mem... flags)
Creates a CLImage2d with the specified format, dimension and flags.
Definition: CLContext.java:373
synchronized void release()
Releases this context and all resources.
Definition: CLContext.java:493
final Set< CLSampler > samplers
Definition: CLContext.java:84
final CLImage3d<?> createImage3d(final int width, final int height, final int depth, final CLImageFormat format, final Mem... flags)
Creates a CLImage3d with the specified format, dimension and flags.
Definition: CLContext.java:403
static CLContext create(final Type... deviceTypes)
Creates a context on the specified device types.
Definition: CLContext.java:147
final CLPlatform platform
Definition: CLContext.java:89
final CLBuffer<?> createBuffer(final int size, final int flags)
Creates a CLBuffer with the specified flags.
Definition: CLContext.java:348
CLDevice getMaxFlopsDevice(final CLDevice.Type type)
Returns the device with maximal FLOPS of the specified device type from this context.
Definition: CLContext.java:620
CLPlatform getPlatform()
Returns the CLPlatform this context is running on.
Definition: CLContext.java:569
This object represents an OpenCL device.
Definition: CLDevice.java:53
CLPlatform getPlatform()
Returns the platform for this OpenCL object.
Definition: CLDevice.java:102
Main Exception type for runtime OpenCL errors and failed function calls (e.g.
static CLException newException(final int status, final String message)
Returns a CLException specific to the error code.
static void checkForError(final int status, final String message)
Throws a CLException when status != CL_SUCCESS.
Represents the OpenCL image format with its channeltype and order.
Common superclass for all OpenCL memory types.
Definition: CLMemory.java:49
final long ID
The OpenCL object handle.
Definition: CLObject.java:41
CLPlatfrorm representing a OpenCL implementation (e.g.
Definition: CLPlatform.java:99
String getName()
Returns the platform name.
final long ID
OpenCL platform id for this platform.
String getProfile()
Returns the platform profile.
static CLPlatform getDefault()
Returns the default OpenCL platform or null when no platform found.
Represents a OpenCL program executed on one or more CLDevices.
Definition: CLProgram.java:64
void setNoSource()
Must set this if the program is created from binary so we know not to call getSource(),...
Definition: CLProgram.java:601
Object representing an OpenCL sampler.
Definition: CLSampler.java:46
Enumeration for the type of a device.
Definition: CLDevice.java:798
final long TYPE
Value of wrapped OpenCL device type.
Definition: CLDevice.java:825
Configures the mapping process.
Definition: CLMemory.java:402
Memory settings for configuring CLMemory.
Definition: CLMemory.java:289
static int flagsToInt(final Mem[] flags)
Definition: CLMemory.java:382
Experimental: the api may change in future, feedback appreciated.
void onError(String errinfo, ByteBuffer private_info, long cb)
Java bindings to OpenCL, the Open Computing Language.
Definition: CL.java:26
int clReleaseContext(long context)
Interface to C language function: cl_int {@native clReleaseContext}(cl_context context)
int clGetContextInfo(long context, int param_name, long param_value_size, Buffer param_value, PointerBuffer param_value_size_ret)
Interface to C language function: cl_int {@native clGetContextInfo}(cl_context context,...
static final int CL_CONTEXT_DEVICES
Define "CL_CONTEXT_DEVICES" with expression '0x1081', CType: int.
Definition: CL.java:691
int clGetSupportedImageFormats(long context, long flags, int image_type, int num_entries, CLImageFormatImpl image_formats, IntBuffer num_image_formats)
Interface to C language function: cl_int {@native clGetSupportedImageFormats}(cl_context context,...
static final int CL_MEM_OBJECT_IMAGE3D
Define "CL_MEM_OBJECT_IMAGE3D" with expression '0x10F2', CType: int.
Definition: CL.java:681
static final int CL_SUCCESS
Define "CL_SUCCESS" with expression '0', CType: int.
Definition: CL.java:325
static final int CL_MEM_OBJECT_IMAGE2D
Define "CL_MEM_OBJECT_IMAGE2D" with expression '0x10F1', CType: int.
Definition: CL.java:699
long clCreateContextFromType(PointerBuffer properties, long device_type, CLErrorHandler pfn_notify, IntBuffer errcode_ret)
Interface to C language function: cl_context {@native clCreateContextFromType}(cl_context_properti...
long clCreateContext(PointerBuffer properties, PointerBuffer devices, CLErrorHandler pfn_notify, IntBuffer errcode_ret)
Interface to C language function: cl_context {@native clCreateContext}(intptr_t * ,...