JOCL v2.6.0-rc-20250722
JOCL, OpenCL® API Binding for Java™ (public API).
CLEventList.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 com.jogamp.common.nio.AbstractBuffer;
32import com.jogamp.common.nio.CachedBufferFactory;
33import com.jogamp.common.nio.PointerBuffer;
34import java.util.Iterator;
35
36/**
37 * Fixed size list for storing CLEvents.
38 * @author Michael Bien, et al.
39 */
40public final class CLEventList implements CLResource, AutoCloseable, Iterable<CLEvent> {
41
42 private final CLEvent[] events;
43
44 /**
45 * stores event ids for fast access.
46 */
47 final PointerBuffer IDs;
48
49 /**
50 * Points always to the first element of the id buffer.
51 */
52 final PointerBuffer IDsView;
53
54 int size;
55
56 public CLEventList(final int capacity) {
57 this(null, capacity);
58 }
59
60 public CLEventList(final CLEvent... events) {
61 this(null, events);
62 }
63
64 public CLEventList(final CachedBufferFactory factory, final int capacity) {
65 this.events = new CLEvent[capacity];
66 this.IDs = initIDBuffer(factory, capacity);
67 this.IDsView = IDs.duplicate();
68 }
69
70 public CLEventList(final CachedBufferFactory factory, final CLEvent... events) {
71 this.events = events;
72 this.IDs = initIDBuffer(factory, events.length);
73 this.IDsView = IDs.duplicate();
74
75 for (final CLEvent event : events) {
76 if(event == null) {
77 throw new IllegalArgumentException("event list containes null element.");
78 }
79 IDs.put(event.ID);
80 }
81 IDs.rewind();
82 size = events.length;
83 }
84
85 private PointerBuffer initIDBuffer(final CachedBufferFactory factory, final int size) {
86 if(factory == null) {
87 return PointerBuffer.allocateDirect(size);
88 }else{
89 return PointerBuffer.wrap(factory.newDirectByteBuffer(size*AbstractBuffer.POINTER_SIZE));
90 }
91 }
92
93 void createEvent(final CLContext context) {
94
95 if(events[size] != null)
96 events[size].release();
97
98 events[size] = new CLEvent(context, IDs.get());
99 size++;
100 }
101
102 PointerBuffer getEventBuffer(final int index) {
103 return IDs.duplicate().position(index);
104 }
105
106 /**
107 * Waits for all events in this list to occur.
108 * If this list is empty this method won't do anything.
109 */
110 public void waitForEvents() {
111 if(size > 0) {
112 events[0].getPlatform().getCLBinding().clWaitForEvents(size, IDsView);
113 }
114 }
115
116 /**
117 * Waits for all events of the specified region in this list to occur.
118 * Will throw IndexOutOfBoundsException if indices are out of bounds.
119 */
120 public void waitForEvents(final int start, final int range) {
121 if(start+range < size || range <= 0) {
122 throw new IndexOutOfBoundsException("args: [start: "+start+" range: "+range+"], eventcount: "+size);
123 }
124
125 final PointerBuffer view = getEventBuffer(start);
126 getEvent(start).getPlatform().getCLBinding().clWaitForEvents(range, view);
127 }
128
129 /**
130 * Waits for the event with the given index in this list to occur.
131 */
132 public void waitForEvent(final int index) {
133 final PointerBuffer view = getEventBuffer(index);
135 }
136
137 /**
138 * Releases all CLEvents in this list.
139 */
140 @Override
141 public void release() {
142 for (int i = 0; i < size; i++) {
143 events[i].release();
144 events[i] = null;
145 }
146 size = 0;
147 IDs.rewind();
148 }
149
150 /**
151 * @deprecated use {@link #release()} instead.
152 */
153 @Deprecated
154 @Override
155 public final void close() {
156 release();
157 }
158
159 public CLEvent getEvent(final int index) {
160 if(index >= size)
161 throw new IndexOutOfBoundsException("list contains "+size+" events, can not return event with index "+index);
162 return events[index];
163 }
164
165 /**
166 * Returns the current size of this list.
167 */
168 public int size() {
169 return size;
170 }
171
172 /**
173 * Returns the maximum size of this list.
174 */
175 public int capacity() {
176 return events.length;
177 }
178
179 @Override
180 public boolean isReleased() {
181 return size == 0;
182 }
183
184 @Override
185 public Iterator<CLEvent> iterator() {
186 return new EventIterator(events, size);
187 }
188
189 @Override
190 public String toString() {
191 final StringBuilder sb = new StringBuilder();
192 sb.append(getClass().getName()).append('[');
193 for (int i = 0; i < size; i++) {
194 sb.append(events[i].toString());
195 if(i+1 != size) {
196 sb.append(", ");
197 }
198 }
199 return sb.append(']').toString();
200 }
201
202 private static class EventIterator implements Iterator<CLEvent> {
203
204 private final CLEvent[] events;
205 private final int size;
206 private int index;
207
208 private EventIterator(final CLEvent[] events, final int size) {
209 this.events = events;
210 this.size = size;
211 }
212
213 @Override
214 public boolean hasNext() {
215 return index < size;
216 }
217
218 @Override
219 public CLEvent next() {
220 if(hasNext())
221 return events[index++];
222 else
223 return null;
224 }
225
226 @Override
227 public void remove() {
228 throw new UnsupportedOperationException("remove() not supported.");
229 }
230
231 }
232
233}
Fixed size list for storing CLEvents.
int size()
Returns the current size of this list.
void waitForEvents(final int start, final int range)
Waits for all events of the specified region in this list to occur.
boolean isReleased()
Returns true if release() has been called.
CLEventList(final CLEvent... events)
Iterator< CLEvent > iterator()
void waitForEvent(final int index)
Waits for the event with the given index in this list to occur.
int capacity()
Returns the maximum size of this list.
CLEventList(final CachedBufferFactory factory, final CLEvent... events)
void waitForEvents()
Waits for all events in this list to occur.
CLEventList(final int capacity)
void release()
Releases all CLEvents in this list.
CLEvent getEvent(final int index)
CLEventList(final CachedBufferFactory factory, final int capacity)
Event objects can be used for synchronizing command queues, e.g you can wait until a event occurs or ...
Definition: CLEvent.java:48
void release()
Releases the OpenCL resource.
Definition: CLEvent.java:78
CLPlatform getPlatform()
Returns the platform for this OpenCL object.
Definition: CLObject.java:65
import of JDK7's ARM interface allowing JDK6 backwards compatibility.
Releasable OpenCL resource.
Definition: CLResource.java:35
int clWaitForEvents(int num_events, PointerBuffer event_list)
Interface to C language function: cl_int {@native clWaitForEvents}(cl_uint num_events,...