# TODO: module deps?
# TODO: JAVAFLAGS / DEMOFLAGS / TESTFLAGS?
-TESTFLAGS=--add-exports jdk.incubator.foreign/jdk.incubator.foreign.unsafe=notzed.zcl
+TESTFLAGS=--enable-native-access=notzed.zcl -Djava.library.path=/usr/lib64
#central_JARS+=junit:junit:4.12 \
# org.hamcrest:hamcrest-core:1.3
TARGET ?= linux-amd64
-JAVA_HOME ?= /usr/local/jdk-13+33
-JAVAFX_HOME ?= /home/notzed/src/jfx/build/sdk
+JAVA_HOME ?= /usr/local/jdk
+JAVAFX_HOME ?= /usr/local/javafx-sdk
-JAVACFLAGS += -source 13
JAVACFLAGS += --add-exports jdk.incubator.foreign/jdk.incubator.foreign.unsafe=notzed.zcl
JAVA ?= $(JAVA_HOME)/bin/java
public class TestCopies {
final static VarHandle longHandle = MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder());
- final static VarHandle longVHandle = MemoryHandles.withStride(longHandle, 8);
+ final static SequenceLayout longVLayout = MemoryLayout.sequenceLayout(CLinker.C_LONG);
+ final static VarHandle longVHandle = longVLayout.varHandle(long.class, MemoryLayout.PathElement.sequenceElement());
public static long getLong(MemoryAddress p) {
return (long)longHandle.get(p);
longVHandle.set(p, i, v);
}
-
- static void copyLoop(long[] src, MemoryAddress dst) {
- for (int i=0;i<src.length;i++) {
- setLong(dst, i, src[i]);
+ static void copyLoop(long[] src, MemorySegment dst) {
+ MemoryAddress base = dst.address();
+ for (int i = 0; i < src.length; i++) {
+ setLong(base, i, src[i]);
}
}
- static void copyBulk(long[] src, MemoryAddress dst) {
- MemoryAddress.copy(MemorySegment.ofArray(src).baseAddress(), dst, src.length);
+ static void copyBulk(long[] src, MemorySegment dst) {
+ dst.copyFrom(MemorySegment.ofArray(src));
}
public static void main(String[] args) {
32,
64,
128,
- 256,
- };
- HashMap<String,Long> results = new HashMap<>();
+ 256,};
+ HashMap<String, Long> results = new HashMap<>();
- int X = 1024*1024;
+ int X = 1024 * 1024;
for (int c = 0; c < 5; c++) {
for (int len: lengths) {
long[] data = new long[len];
now = System.nanoTime();
for (int l = 0; l < X; l++) {
- try (MemorySegment seg = MemorySegment.allocateNative(data.length * 8)) {
- MemoryAddress add = seg.baseAddress();
- copyLoop(data, add);
+ try ( ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(data.length * 8, scope);
+ copyLoop(data, seg);
}
}
long t = System.nanoTime() - now;
System.out.printf(" %12.9f %3d copyLoop\n", t * 1E-9, len);
- results.compute(String.format("%3d copyLoop", len), (k, v)-> v == null ? t : min(v, t));
+ results.compute(String.format("%3d copyLoop", len), (k, v) -> v == null ? t : min(v, t));
now = System.nanoTime();
for (int l = 0; l < X; l++) {
- try (MemorySegment seg = MemorySegment.allocateNative(data.length * 8)) {
- MemoryAddress add = seg.baseAddress();
- copyBulk(data, add);
+ try ( ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(data.length * 8, scope);
+ copyBulk(data, seg);
}
}
long s = System.nanoTime() - now;
System.out.printf(" %12.9f %3d copyBulk\n", s * 1E-9, len);
- results.compute(String.format("%3d copyBulk", len), (k, v)-> v == null ? s : min(v, s));
+ results.compute(String.format("%3d copyBulk", len), (k, v) -> v == null ? s : min(v, s));
}
for (int len: lengths) {
long now;
now = System.nanoTime();
- try (MemorySegment seg = MemorySegment.allocateNative(data.length * 8)) {
- MemoryAddress add = seg.baseAddress();
+ try ( ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(data.length * 8, scope);
for (int l = 0; l < X; l++) {
- copyLoop(data, add);
+ copyLoop(data, seg);
}
}
long t = System.nanoTime() - now;
System.out.printf(" %12.9f %3d copyLoop pre-alloc\n", t * 1E-9, len);
- results.compute(String.format("%3d copyLoop pre-alloc", len), (k, v)-> v == null ? t : min(v, t));
+ results.compute(String.format("%3d copyLoop pre-alloc", len), (k, v) -> v == null ? t : min(v, t));
now = System.nanoTime();
- try (MemorySegment seg = MemorySegment.allocateNative(data.length * 8)) {
- MemoryAddress add = seg.baseAddress();
+ try ( ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(data.length * 8, scope);
for (int l = 0; l < X; l++) {
- copyBulk(data, add);
+ copyBulk(data, seg);
}
}
long s = System.nanoTime() - now;
System.out.printf(" %12.9f %3d copyBulk pre-alloc\n", s * 1E-9, len);
- results.compute(String.format("%3d copyBulk pre-alloc", len), (k, v)-> v == null ? s : min(v, s));
+ results.compute(String.format("%3d copyBulk pre-alloc", len), (k, v) -> v == null ? s : min(v, s));
}
// if have stack allocator:
now = System.nanoTime();
for (int l = 0; l < X; l++) {
- try (Allocator a = api.Memory.stack()) {
- MemoryAddress add = a.alloca(data.length * 8);
- copyLoop(data, add);
+ try ( Allocator a = api.Memory.stack()) {
+ MemorySegment seg = a.allocs(data.length * 8);
+ copyLoop(data, seg);
}
}
long t = System.nanoTime() - now;
System.out.printf(" %12.9f %3d copyLoop stack\n", t * 1E-9, len);
- results.compute(String.format("%3d copyLoop stack", len), (k, v)-> v == null ? t : min(v, t));
+ results.compute(String.format("%3d copyLoop stack", len), (k, v) -> v == null ? t : min(v, t));
now = System.nanoTime();
for (int l = 0; l < X; l++) {
- try (Allocator a = api.Memory.stack()) {
- MemoryAddress add = a.alloca(data.length * 8);
- copyBulk(data, add);
+ try ( Allocator a = api.Memory.stack()) {
+ MemorySegment seg = a.allocs(data.length * 8);
+ copyBulk(data, seg);
}
}
long s = System.nanoTime() - now;
System.out.printf(" %12.9f %3d copyBulk stack\n", s * 1E-9, len);
- results.compute(String.format("%3d copyBulk stack", len), (k, v)-> v == null ? s : min(v, s));
+ results.compute(String.format("%3d copyBulk stack", len), (k, v) -> v == null ? s : min(v, s));
}
System.out.println();
}
results.entrySet().stream().map((e) -> {
- return String.format("%s %12.9f %s", e.getKey().substring(0, 3), 1E-9 * e.getValue(), e.getKey().substring(4));
- }).sorted().forEach(System.out::println);
+ return String.format("%s %12.9f %s", e.getKey().substring(0, 3), 1E-9 * e.getValue(), e.getKey().substring(4));
+ }).sorted().forEach(System.out::println);
}
}
import java.nio.FloatBuffer;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemorySegment;
+import jdk.incubator.foreign.ResourceScope;
/**
* Some memory tests.
int len = (int)(seg.byteSize() >>> 2);
float sum = 0;
- MemoryAddress add = seg.baseAddress();
+ MemoryAddress add = seg.address();
for (int i = 0; i < len; i++)
sum += Native.getFloat(add, i);
Native.setFloat(add, sum);
for (int i = 0; i < sizes.length; i++) {
arrays[i] = new float[sizes[i]];
- segments[i] = MemorySegment.allocateNative(sizes[i] * 4, 16);
+ segments[i] = MemorySegment.allocateNative(sizes[i] * 4, 16, ResourceScope.globalScope());
buffers[i] = ByteBuffer.allocateDirect(sizes[i] * 4).order(ByteOrder.nativeOrder());
}
import java.nio.LongBuffer;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemorySegment;
+import jdk.incubator.foreign.ResourceScope;
/**
* Some memory tests.
int len = (int)(seg.byteSize() >>> 3);
long sum = 0;
- MemoryAddress add = seg.baseAddress();
+ MemoryAddress add = seg.address();
for (int i = 0; i < len; i++)
sum += Native.getLong(add, i);
Native.setLong(add, sum);
for (int i = 0; i < sizes.length; i++) {
arrays[i] = new long[sizes[i]];
- segments[i] = MemorySegment.allocateNative(sizes[i] * 8, 16);
+ segments[i] = MemorySegment.allocateNative(sizes[i] * 8, 16, ResourceScope.globalScope());
buffers[i] = ByteBuffer.allocateDirect(sizes[i] * 8).order(ByteOrder.nativeOrder());
}
*/
module notzed.zcl.demo {
requires notzed.zcl;
+ requires jdk.incubator.foreign;
exports au.notzed.zcl.tools;
}
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-
package api;
import jdk.incubator.foreign.*;
/**
* A utility library for memory operations including a stack allocator.
- *
+ * <p>
* The stack allocator works like this
- * try (Memory.Frame f = Memory.frame()) {
- * MemoryAddress a = Memory.alloca(size);
- * }
- * Any memory allocated is freed when the frame is closed.
- *
+ * try (Memory.Frame f = Memory.frame()) {
+ * MemoryAddress a = Memory.alloca(size);
+ * }
+ * Any memory allocated is freed when the frame is closed.
+ * <p>
* Note that unlike C's alloca() the frame scope is not per-function
* but application defined.
*/
private static final ThreadLocal<Allocator> stackAllocators = new ThreadLocal<>();
/* older idea */
-
// eh, this isn't really necessary, it's just a safety check/debug?
//private static final ThreadLocal<Frame> frames = new ThreadLocal<>();
-
/**
- * Alloc memory in the current frame. The size is not enforced.
+ * Alloc memory in the current frame. The size is not enforced.
*/
/*
public static MemoryAddress alloca(long size) {
throw new UnsupportedOperationException("Must have a frame");
return stack().alloca(size);
}*/
-
/**
- * Allocate a segment in the current frame. The segment must not be closed.
+ * Allocate a segment in the current frame. The segment must not be closed.
*/
-/*
+ /*
public static MemorySegment slicea(long size) {
if (frames.get() == null)
throw new UnsupportedOperationException("Must have a frame");
@Override
public void close();
}
-*/
+ */
static class Stack extends Native {
+
final MemorySegment stack;
final MemoryAddress base;
long sp;
super(p);
// TODO: use cleanup stuff, perhaps owner thread
- stack = MemorySegment.ofNativeRestricted(addr(), size, null, null, null);
- base = stack.baseAddress();
+ stack = MemorySegment.globalNativeSegment().asSlice(addr(), size);
+ base = stack.address();
sp = size;
origin = Thread.currentThread();
}
public MemoryAddress alloca(long size) {
- sp -= (size + 7 ) & ~7;
+ sp -= (size + 7) & ~7;
return base.addOffset(sp);
}
public MemorySegment allocs(long size) {
- sp -= (size + 7 ) & ~7;
+ sp -= (size + 7) & ~7;
return stack.asSlice(sp, size);
}
public MemorySegment slicea(long size) {
- sp -= (size + 7 ) & ~7;
+ sp -= (size + 7) & ~7;
return stack.asSlice(sp, size);
}
}
This allows different allocators to be used for functions that use one.
Individual memory cannot be freed.
*/
-
/**
* Create a stack allocator.
* The stack allocator uses thread-specific backing buffer.
Thread me = Thread.currentThread();
Allocator gnu = new Allocator() {
- public void close() {
- stack.sp = fp;
- stackAllocators.set(old);
- }
- public MemoryAddress alloca(long size) {
- if (stackAllocators.get() != this || me != Thread.currentThread())
- throw new IllegalStateException();
- return stack.alloca(size);
- }
- public MemorySegment allocs(long size) {
- if (stackAllocators.get() != this || me != Thread.currentThread())
- throw new IllegalStateException();
- return stack.allocs(size);
- }
- };
+ public void close() {
+ stack.sp = fp;
+ stackAllocators.set(old);
+ }
+
+ public MemoryAddress alloca(long size) {
+ if (stackAllocators.get() != this || me != Thread.currentThread())
+ throw new IllegalStateException();
+ return stack.alloca(size);
+ }
+
+ public MemorySegment allocs(long size) {
+ if (stackAllocators.get() != this || me != Thread.currentThread())
+ throw new IllegalStateException();
+ return stack.allocs(size);
+ }
+ };
stackAllocators.set(gnu);
return gnu;
}
-
static final MethodHandle malloc;
static final MethodHandle free;
static {
// So for whatever reason it's been decided that MemorySegment can't be freed normally on another thread
- SystemABI abi = SystemABI.getSystemABI();
- MethodHandles.Lookup lookup = MethodHandles.lookup();
- LibraryLookup libc = LibraryLookup.ofDefault();
+ CLinker abi = CLinker.getInstance();
+ SymbolLookup libc = CLinker.systemLookup();
try {
- malloc = abi.downcallHandle(libc.lookup("malloc"),
- MethodType.methodType(MemoryAddress.class,
- long.class),
- FunctionDescriptor.of(MemoryLayouts.SysV.C_POINTER,
- MemoryLayouts.SysV.C_ULONG));
- free = abi.downcallHandle(libc.lookup("free"),
- MethodType.methodType(void.class,
- MemoryAddress.class),
- FunctionDescriptor.ofVoid(MemoryLayouts.SysV.C_POINTER));
- } catch (NoSuchMethodException x) {
+ malloc = abi.downcallHandle(libc.lookup("malloc").get(),
+ MethodType.methodType(MemoryAddress.class,
+ long.class),
+ FunctionDescriptor.of(CLinker.C_POINTER,
+ CLinker.C_LONG));
+ free = abi.downcallHandle(libc.lookup("free").get(),
+ MethodType.methodType(void.class,
+ MemoryAddress.class),
+ FunctionDescriptor.ofVoid(CLinker.C_POINTER));
+ } catch (Exception x) {
throw new RuntimeException(x);
}
}
* Closing this segment has no effect on the original, and the original must be used for that.
*/
public static MemorySegment ofNative(MemoryAddress addr, long size) {
- return MemorySegment.ofNativeRestricted(addr, size, null, null, null);
+ return MemorySegment.globalNativeSegment().asSlice(addr, size);
}
/**
- * Get the physical address. I mean how is this an "offset"?
+ * Get the physical address. I mean how is this an "offset"?
*/
public static long toLong(MemoryAddress addr) {
return addr.toRawLongValue();
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-
package api;
import java.io.StringReader;
/**
* Base class for all native objects.
- *
+ * <p>
* Handles instantiation and provides helper functions for native access.
- *
+ * <p>
* Work in progress.
- *
+ * <p>
* For better safety the 'p' field should be the CHandle, and addr() would
- * call get(). Otherwise one must not release ANY object which might ever
- * be used again - including any objects returned by the getInfo(). However ...
+ * call get(). Otherwise one must not release ANY object which might ever
+ * be used again - including any objects returned by the getInfo(). However ...
* it's a trade-off and it's a lot of code to change.
- *
+ * <p>
+ * FIXME: there are MemorySegment based accessors for primitive types now, use those
*/
public class Native {
private final static boolean dolog = true;
- final static VarHandle byteHandle = MemoryHandles.varHandle(byte.class, ByteOrder.nativeOrder());
- final static VarHandle shortHandle = MemoryHandles.varHandle(short.class, ByteOrder.nativeOrder());
- final static VarHandle intHandle = MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder());
- final static VarHandle longHandle = MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder());
- final static VarHandle floatHandle = MemoryHandles.varHandle(float.class, ByteOrder.nativeOrder());
- final static VarHandle doubleHandle = MemoryHandles.varHandle(double.class, ByteOrder.nativeOrder());
- final static VarHandle addrHandle = MemoryHandles.asAddressVarHandle(MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder()));
-
- final static VarHandle byteVHandle = MemoryHandles.withStride(byteHandle, 1);
- final static VarHandle shortVHandle = MemoryHandles.withStride(shortHandle, 2);
- final static VarHandle intVHandle = MemoryHandles.withStride(intHandle, 4);
- final static VarHandle longVHandle = MemoryHandles.withStride(longHandle, 8);
- final static VarHandle floatVHandle = MemoryHandles.withStride(floatHandle, 4);
- final static VarHandle doubleVHandle = MemoryHandles.withStride(doubleHandle, 8);
- final static VarHandle addrVHandle = MemoryHandles.withStride(addrHandle, 8);
+ public final static VarHandle byteHandle = MemoryHandles.varHandle(byte.class, ByteOrder.nativeOrder());
+ public final static VarHandle shortHandle = MemoryHandles.varHandle(short.class, ByteOrder.nativeOrder());
+ //final static VarHandle intHandle = MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder());
+ public final static VarHandle intHandle = CLinker.C_INT.varHandle(int.class);
+ public final static VarHandle longHandle = MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder());
+ public final static VarHandle floatHandle = MemoryHandles.varHandle(float.class, ByteOrder.nativeOrder());
+ public final static VarHandle doubleHandle = MemoryHandles.varHandle(double.class, ByteOrder.nativeOrder());
+ public final static VarHandle addrHandle = MemoryHandles.asAddressVarHandle(MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder()));
+
+ final static SequenceLayout byteVLayout = MemoryLayout.sequenceLayout(CLinker.C_CHAR);
+ final static SequenceLayout shortVLayout = MemoryLayout.sequenceLayout(CLinker.C_SHORT);
+ final static SequenceLayout intVLayout = MemoryLayout.sequenceLayout(CLinker.C_INT);
+ final static SequenceLayout longVLayout = MemoryLayout.sequenceLayout(CLinker.C_LONG);
+ final static SequenceLayout floatVLayout = MemoryLayout.sequenceLayout(CLinker.C_FLOAT);
+ final static SequenceLayout doubleVLayout = MemoryLayout.sequenceLayout(CLinker.C_DOUBLE);
+ final static SequenceLayout addrVLayout = MemoryLayout.sequenceLayout(CLinker.C_POINTER);
+
+ public final static VarHandle byteVHandle = byteVLayout.varHandle(byte.class, MemoryLayout.PathElement.sequenceElement());
+ public final static VarHandle shortVHandle = shortVLayout.varHandle(short.class, MemoryLayout.PathElement.sequenceElement());
+ public final static VarHandle intVHandle = intVLayout.varHandle(int.class, MemoryLayout.PathElement.sequenceElement());
+ public final static VarHandle longVHandle = longVLayout.varHandle(long.class, MemoryLayout.PathElement.sequenceElement());
+ public final static VarHandle floatVHandle = floatVLayout.varHandle(float.class, MemoryLayout.PathElement.sequenceElement());
+ public final static VarHandle doubleVHandle = doubleVLayout.varHandle(double.class, MemoryLayout.PathElement.sequenceElement());
+ public final static VarHandle addrVHandle = addrVLayout.varHandle(long.class, MemoryLayout.PathElement.sequenceElement());
protected Native(MemoryAddress p) {
this.p = p;
}
public static MemoryAddress addr(MemorySegment o) {
- return o != null ? o.baseAddress() : MemoryAddress.NULL;
+ return o != null ? o.address() : MemoryAddress.NULL;
}
public static byte getByte(MemoryAddress p) {
}
/* helpers - java to native */
- public static <T extends Native> MemoryAddress toAddrV(Allocator frame, T[]array, int len) {
+ public static <T extends Native> MemoryAddress toAddrV(Allocator frame, T[] array, int len) {
MemoryAddress list = frame.alloca(8 * len);
- for (int i=0;i<len;i++)
+ for (int i = 0; i < len; i++)
setAddr(list, i, array[i].addr());
return list;
}
- public static <T extends Native> MemoryAddress toAddrV(Allocator frame, T[]array) {
+ public static <T extends Native> MemoryAddress toAddrV(Allocator frame, T[] array) {
return toAddrV(frame, array, array.length);
}
- public static <T extends Native> MemoryAddress toAddrV(Allocator frame, String[]array) {
- if (array != null) {
- MemoryAddress list = frame.alloca(8 * array.length);
+ public static <T extends Native> MemoryAddress toAddrV(Allocator frame, String[] array) {
+ if (array != null) {
+ MemoryAddress list = frame.alloca(8 * array.length);
- for (int i=0;i<array.length;i++)
- setAddr(list, i, toByteV(frame, array[i]));
+ for (int i = 0; i < array.length; i++)
+ setAddr(list, i, toByteV(frame, array[i]));
- return list;
- } else {
- return MemoryAddress.NULL;
- }
- }
+ return list;
+ } else {
+ return MemoryAddress.NULL;
+ }
+ }
- public static <T extends Native> MemoryAddress toLongV(Allocator frame, long[]array) {
+ public static <T extends Native> MemoryAddress toLongV(Allocator frame, long[] array) {
MemoryAddress list = frame.alloca(8 * array.length);
- for (int i=0;i<array.length;i++)
+ for (int i = 0; i < array.length; i++)
setLong(list, i, array[i]);
return list;
}
+ public static MemorySegment allocAddrV(ResourceScope frame, int len) {
+ return MemorySegment.allocateNative(CLinker.C_POINTER.byteSize() * len, CLinker.C_POINTER.byteAlignment(), frame);
+ }
+
+ public static MemorySegment allocV(ResourceScope frame, ValueLayout type, int len) {
+ return MemorySegment.allocateNative(type.byteSize() * len, type.byteAlignment(), frame);
+ }
+
+ public static <T extends Native> MemorySegment toAddrV(ResourceScope frame, T[] array, int len) {
+ MemorySegment list = MemorySegment.allocateNative(CLinker.C_POINTER.byteSize() * len, CLinker.C_POINTER.byteAlignment(), frame);
+
+ for (int i = 0; i < len; i++)
+ MemoryAccess.setAddressAtIndex(list, i, array[i].addr());
+
+ return list;
+ }
+
+ public static <T extends Native> MemorySegment toAddrV(ResourceScope frame, T[] array) {
+ return toAddrV(frame, array, array.length);
+ }
+
+ public static <T extends Native> MemorySegment toLongV(ResourceScope frame, long[] array) {
+ MemorySegment list = MemorySegment.allocateNative(CLinker.C_LONG.byteSize() * array.length, CLinker.C_LONG.byteAlignment(), frame);
+
+ for (int i = 0; i < array.length; i++)
+ MemoryAccess.setLongAtIndex(list, i, array[i]);
+
+ return list;
+ }
+
+ public static MemorySegment toCStringV(String[]array, ResourceScope frame) {
+ MemorySegment list = MemorySegment.allocateNative(CLinker.C_POINTER.byteSize() * array.length, CLinker.C_POINTER.byteAlignment(), frame);
+ for (int i=0;i<array.length;i++) {
+ MemoryAccess.setAddressAtIndex(list, i, CLinker.toCString(array[i], frame));
+ }
+ return list;
+ }
+
public static <T extends Native> MemoryAddress toByteV(Allocator frame, byte[] data) {
if (data != null) {
MemoryAddress list = frame.alloca(data.length);
- for (int i=0;i<data.length;i++)
+ for (int i = 0; i < data.length; i++)
setByte(list, i, data[i]);
return list;
byte[] data = string.getBytes();
MemoryAddress list = frame.alloca(data.length + 1);
- for (int i=0;i<data.length;i++)
+ for (int i = 0; i < data.length; i++)
setByte(list, i, data[i]);
setByte(list, data.length, (byte)0);
}
/* helpers - native to java */
- public static <T extends Native> T[] toObjectV(MemoryAddress list, T[] array, Function<MemoryAddress,T> create) {
- for (int i=0;i<array.length;i++)
- array[i] = Native.resolve(getAddr(list, i), create);
+ public static <T extends Native> T[] toObjectV(MemorySegment list, T[] array, Function<MemoryAddress, T> create) {
+ for (int i = 0; i < array.length; i++)
+ array[i] = Native.resolve(MemoryAccess.getAddressAtIndex(list, i), create);
return array;
}
- public static <T extends Native> T[] toObjectV(MemorySegment list, Function<MemoryAddress,T> create, IntFunction<T[]> createArray) {
- return toObjectV(list.baseAddress(), createArray.apply((int)(list.byteSize() >>> 3)), create);
+ public static <T extends Native> T[] toObjectV(MemorySegment list, Function<MemoryAddress, T> create, IntFunction<T[]> createArray) {
+ return toObjectV(list, createArray.apply((int)(list.byteSize() >>> 3)), create);
}
public static String toString(MemoryAddress cstr) {
- MemorySegment seg = MemorySegment.ofNativeRestricted(cstr, Integer.MAX_VALUE, null, null, null);
- MemoryAddress add = seg.baseAddress();
- byte[] data;
- int len = 0;
-
- while (getByte(add, len) != 0)
- len++;
-
- data = new byte[len];
- for (int i=0;i<data.length;i++)
- data[i] = getByte(add, i);
-
- return new String(data);
+ return CLinker.toJavaString(cstr);
}
public static long[] toLongV(MemorySegment valp) {
- MemoryAddress val = valp.baseAddress();
int len = (int)(valp.byteSize() >>> 3);
long[] list = new long[len];
- for (int i=0;i<list.length;i++)
- list[i] = getLong(val, i);
+ for (int i = 0; i < list.length; i++)
+ list[i] = MemoryAccess.getLongAtIndex(valp, i);
return list;
}
/* abi stuff */
-
- public static MethodHandle downcallHandle(LibraryLookup[] libs, String name, String signature) {
+ public static MethodHandle downcallHandle(SymbolLookup lib, String name, String signature) {
Signature sig = Signature.parse(signature);
int n = sig.classes.length;
- SystemABI abi = SystemABI.getSystemABI();
+ CLinker abi = CLinker.getInstance();
if (sig.classes.length != sig.layouts.length)
throw new RuntimeException("layout class mismatch");
- Class<?> resClass = sig.classes[n-1];
- MemoryLayout resLayout = sig.layouts[n-1];
-
- for (LibraryLookup lib: libs) {
- try {
- return abi.downcallHandle(lib.lookup(name),
- MethodType.methodType(resClass, Arrays.copyOf(sig.classes, n-1)),
- FunctionDescriptor.of(resLayout, Arrays.copyOf(sig.layouts, n-1)));
- } catch (NoSuchMethodException x) {
- }
+ Class<?> resClass = sig.classes[n - 1];
+ MemoryLayout resLayout = sig.layouts[n - 1];
+
+ if (resLayout != null) {
+ return lib.lookup(name).map(addr -> abi.downcallHandle(addr,
+ MethodType.methodType(resClass, Arrays.copyOf(sig.classes, n - 1)),
+ FunctionDescriptor.of(resLayout, Arrays.copyOf(sig.layouts, n - 1))))
+ .get();
+ } else {
+ return lib.lookup(name).map(addr -> abi.downcallHandle(addr,
+ MethodType.methodType(resClass, Arrays.copyOf(sig.classes, n - 1)),
+ FunctionDescriptor.ofVoid(Arrays.copyOf(sig.layouts, n - 1))))
+ .get();
}
- // or use some 'unsupportedoperation' one?
- System.err.println("not found: "+ name);
-
- return null;
}
- public static MethodHandle downcallHandle(Function<String,MemoryAddress> find, String name, String signature) {
+ public static MethodHandle downcallHandle(Function<String, MemoryAddress> find, String name, String signature) {
return downcallHandle(find.apply(name), signature);
}
public static MethodHandle downcallHandle(MemoryAddress addr, String signature) {
Signature sig = Signature.parse(signature);
int n = sig.classes.length;
- SystemABI abi = SystemABI.getSystemABI();
+ CLinker abi = CLinker.getInstance();
if (sig.classes.length != sig.layouts.length)
throw new RuntimeException("layout class mismatch");
- Class<?> resClass = sig.classes[n-1];
- MemoryLayout resLayout = sig.layouts[n-1];
+ Class<?> resClass = sig.classes[n - 1];
+ MemoryLayout resLayout = sig.layouts[n - 1];
return abi.downcallHandle(addr,
- MethodType.methodType(resClass, Arrays.copyOf(sig.classes, n-1)),
- FunctionDescriptor.of(resLayout, Arrays.copyOf(sig.layouts, n-1)));
+ MethodType.methodType(resClass, Arrays.copyOf(sig.classes, n - 1)),
+ FunctionDescriptor.of(resLayout, Arrays.copyOf(sig.layouts, n - 1)));
}
// instance must be of a functional interface
public static MemoryAddress upcallStub(MethodHandles.Lookup lookup, Object instance, String signature) {
Signature sig = Signature.parse(signature);
int n = sig.classes.length;
- SystemABI abi = SystemABI.getSystemABI();
+ CLinker abi = CLinker.getInstance();
if (sig.classes.length != sig.layouts.length)
throw new RuntimeException("layout class mismatch");
- Class<?> resClass = sig.classes[n-1];
- MemoryLayout resLayout = sig.layouts[n-1];
-
+ Class<?> resClass = sig.classes[n - 1];
+ MemoryLayout resLayout = sig.layouts[n - 1];
Method m = instance.getClass().getMethods()[0];
MethodType mt = MethodType.methodType(m.getReturnType(), m.getParameterTypes());
+ // FIXME: ResourceScope usage is broken here
//System.out.printf("instance %s\n", instance);
//System.out.printf("declaring class %s\n", m.getDeclaringClass());
-
try {
- return abi.upcallStub(lookup.findVirtual(
- m.getDeclaringClass(),
- m.getName(),
- mt)
- .bindTo(instance),
- FunctionDescriptor.of(resLayout, Arrays.copyOf(sig.layouts, n-1)));
+ if (resLayout != null) {
+ return abi.upcallStub(lookup.findVirtual(
+ m.getDeclaringClass(),
+ m.getName(),
+ mt)
+ .bindTo(instance),
+ FunctionDescriptor.of(resLayout, Arrays.copyOf(sig.layouts, n - 1)),
+ ResourceScope.newSharedScope());
+ } else {
+ return abi.upcallStub(lookup.findVirtual(
+ m.getDeclaringClass(),
+ m.getName(),
+ mt)
+ .bindTo(instance),
+ FunctionDescriptor.ofVoid(Arrays.copyOf(sig.layouts, n - 1)),
+ ResourceScope.newSharedScope());
+ }
} catch (NoSuchMethodException | IllegalAccessException x) {
throw new RuntimeException(x);
}
}
public static void freeUpcallStub(MemoryAddress addr) {
- SystemABI.getSystemABI().freeUpcallStub(addr);
+ // NOOP?
}
- public static LibraryLookup[] loadLibraries(String... libraries) {
- LibraryLookup[] libs = new LibraryLookup[libraries.length];
- MethodHandles.Lookup lookup = MethodHandles.lookup();
- for (int i=0;i<libraries.length;i++)
- libs[i] = LibraryLookup.ofLibrary(lookup, libraries[i]);
- return libs;
+ public static void loadLibraries(String... libraries) {
+ for (int i = 0; i < libraries.length; i++)
+ System.loadLibrary(libraries[i]);
}
public static MemoryLayout parseStruct(String layout) {
Signature sig = Signature.parse(layout);
- return MemoryLayout.ofStruct(sig.layouts);
+ return MemoryLayout.structLayout(sig.layouts);
}
public static MemoryLayout parseUnion(String layout) {
Signature sig = Signature.parse(layout);
- return MemoryLayout.ofUnion(sig.layouts);
+ return MemoryLayout.unionLayout(sig.layouts);
}
public static void dumpSignature(String signature) {
Signature sig = Signature.parse(signature);
- for (int i=0;i<sig.classes.length;i++) {
+ for (int i = 0; i < sig.classes.length; i++) {
System.out.printf(" %-40s %s\n", sig.classes[i].getName(), sig.layouts[i]);
}
}
static class Signature {
+
Class classes[];
MemoryLayout layouts[];
}
c = r.read();
- while ( c != -1) {
+ while (c != -1) {
switch (c) {
case 'u':
case 'i':
if (pointerDepth > 0) {
argClass.add(MemoryAddress.class);
- argLayout.add(MemoryLayouts.SysV.C_POINTER);
+ argLayout.add(CLinker.C_POINTER);
} else {
switch (c) {
case 'u':
switch (size) {
case 8:
argClass.add(byte.class);
- argLayout.add(MemoryLayouts.SysV.C_UCHAR);
+ argLayout.add(CLinker.C_CHAR);
break;
case 16:
// char.class?
argClass.add(short.class);
- argLayout.add(MemoryLayouts.SysV.C_USHORT);
+ argLayout.add(CLinker.C_SHORT);
break;
case 32:
argClass.add(int.class);
- argLayout.add(MemoryLayouts.SysV.C_UINT);
+ argLayout.add(CLinker.C_INT);
break;
case 64:
argClass.add(long.class);
- argLayout.add(MemoryLayouts.SysV.C_ULONG);
+ argLayout.add(CLinker.C_LONG);
break;
}
break;
switch (size) {
case 8:
argClass.add(byte.class);
- argLayout.add(MemoryLayouts.SysV.C_SCHAR);
+ argLayout.add(CLinker.C_CHAR);
break;
case 16:
argClass.add(short.class);
- argLayout.add(MemoryLayouts.SysV.C_SHORT);
+ argLayout.add(CLinker.C_SHORT);
break;
case 32:
argClass.add(int.class);
- argLayout.add(MemoryLayouts.SysV.C_INT);
+ argLayout.add(CLinker.C_INT);
break;
case 64:
argClass.add(long.class);
- argLayout.add(MemoryLayouts.SysV.C_LONG);
+ argLayout.add(CLinker.C_LONG);
break;
}
break;
switch (size) {
case 32:
argClass.add(float.class);
- argLayout.add(MemoryLayouts.SysV.C_FLOAT);
+ argLayout.add(CLinker.C_FLOAT);
break;
case 64:
argClass.add(double.class);
- argLayout.add(MemoryLayouts.SysV.C_DOUBLE);
+ argLayout.add(CLinker.C_DOUBLE);
break;
}
break;
//System.out.printf("void *=%d\n", pointerDepth);
if (pointerDepth > 0) {
argClass.add(MemoryAddress.class);
- argLayout.add(MemoryLayouts.SysV.C_POINTER);
+ argLayout.add(CLinker.C_POINTER);
} else {
// can only be return value
argClass.add(void.class);
//System.out.printf(" type: *=%d %s\n", pointerDepth, sb);
if (pointerDepth > 0) {
argClass.add(MemoryAddress.class);
- argLayout.add(MemoryLayouts.SysV.C_POINTER);
+ argLayout.add(CLinker.C_POINTER);
} else {
argClass.add(MemorySegment.class);
try {
}
return new Signature(argClass.stream().toArray(Class[]::new),
- argLayout.stream().toArray(MemoryLayout[]::new));
+ argLayout.stream().toArray(MemoryLayout[]::new));
}
}
/* ********************************************************************** */
- /* GC handling */
- /* ********************************************************************** */
-
+ /* GC handling */
+ /* ********************************************************************** */
/**
* Resource index.
*/
}
return o;
}*/
-
- public static <T extends Native> T resolve(MemoryAddress p, Function<MemoryAddress,T> create) {
+ public static <T extends Native> T resolve(MemoryAddress p, Function<MemoryAddress, T> create) {
T o;
boolean step = false;
//if (dolog)
// log().log(Level.DEBUG, () -> String.format(" resolv $%016x %s", Memory.toLong(p), create));
-
if (Memory.toLong(p) == 0)
return null;
// Instantiation needs to be synchronized for obvious reasons.
synchronized (map) {
- CHandle h = (CHandle) map.get(p);
+ CHandle h = (CHandle)map.get(p);
String fmt;
return o;
}*/
-
public void release() {
WeakReference<? extends Native> ref;
}
public static void release(Native... list) {
- for (Native o : list)
+ for (Native o: list)
release(o);
}
private static void cleanerStep() {
try {
- CHandle stale = (CHandle) references.poll();
+ CHandle stale = (CHandle)references.poll();
if (stale != null) {
synchronized (map) {
map.remove(stale.p);
log().log(Level.DEBUG, "Native finaliser started");
try {
while (true) {
- CHandle stale = (CHandle) references.remove();
+ CHandle stale = (CHandle)references.remove();
do {
try {
synchronized (map) {
stale.release();
} catch (Throwable ex) {
}
- stale = (CHandle) references.poll();
+ stale = (CHandle)references.poll();
} while (stale != null);
}
} catch (InterruptedException ex) {
}
private static class CHandle extends WeakReference<Native> {
+
protected MemoryAddress p;
final Class<? extends Native> jtype;
CHandle next;
@Override
public boolean equals(Object obj) {
- return (obj instanceof CHandle) && ((CHandle) obj).p == p;
+ return (obj instanceof CHandle) && ((CHandle)obj).p == p;
}
@Override
* @return
*/
public static final int hashCode(long p) {
- return (int) p >>> 4;
+ return (int)p >>> 4;
}
/**
}
public static void debugFlushAll() {
- for (int i=0;i<3;i++) {
+ for (int i = 0; i < 3; i++) {
try {
System.gc();
Thread.sleep(100);
} catch (InterruptedException x) {
}
- CHandle stale = (CHandle) references.poll();
+ CHandle stale = (CHandle)references.poll();
while (stale != null) {
try {
synchronized (map) {
stale.release();
} catch (Throwable ex) {
}
- stale = (CHandle) references.poll();
+ stale = (CHandle)references.poll();
}
}
}
import static au.notzed.zcl.CLLib.*;
import jdk.incubator.foreign.*;
import api.Native;
-import api.Allocator;
import api.Memory;
import api.Callback;
* @param blocking
* @param mem_offset source memory offset in bytes.
* @param size memory transfer size in bytes.
- * @param buffer destination buffer. It must be a native segment.
+ * @param buffer destination buffer. It must be a native segment.
* @param wait
* @param event
* @throws CLException
if (size > buffer.byteSize())
throw new BufferOverflowException();
- try (Allocator frame = Memory.stack()) {
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
EventInfo info = new EventInfo(frame, wait, event);
int res;
res = clEnqueueReadBuffer(addr(), mem.addr(), blocking ? 1 : 0,
mem_offset, size,
- buffer.baseAddress(),
+ buffer.address(),
info.nwait, info.wait, info.event);
if (res != 0)
throw new CLException(res);
ByteBuffer buffer,
CLEventList wait,
CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.ofByteBuffer(buffer)) {
- enqueueReadBuffer(mem, blocking,
- mem_offset, size,
- seg,
- wait, event);
- }
+ MemorySegment seg = MemorySegment.ofByteBuffer(buffer);
+ enqueueReadBuffer(mem, blocking,
+ mem_offset, size,
+ seg,
+ wait, event);
}
/**
* Calls clEnqueueReadBuffer for a byte array.
- *
+ * <p>
* panama note: the buffer must always be copied.
*
* @param mem
byte[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.allocateNative(size)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(size, scope);
enqueueReadBuffer(mem, true, mem_offset, size, seg, wait, event);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).get(buffer, buf_offset, size);
}
/**
* Calls clEnqueueReadBuffer for a short array.
- *
+ * <p>
* Note that mem_offset and size are in terms of the buffer element size.
*
* @param mem
short[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.allocateNative(size * 2)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(size * 2, scope);
enqueueReadBuffer(mem, true, mem_offset * 2, size * 2, seg, wait, event);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asShortBuffer().get(buffer, buf_offset, size);
}
int[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.allocateNative(size * 4)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(size * 4, scope);
enqueueReadBuffer(mem, true, mem_offset * 4, size * 4, seg, wait, event);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asIntBuffer().get(buffer, buf_offset, size);
}
long[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.allocateNative(size * 8)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(size * 8, scope);
enqueueReadBuffer(mem, true, mem_offset * 8, size * 8, seg, wait, event);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asLongBuffer().get(buffer, buf_offset, size);
}
float[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.allocateNative(size * 4)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(size * 4, scope);
enqueueReadBuffer(mem, true, mem_offset * 4, size * 4, seg, wait, event);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asFloatBuffer().get(buffer, buf_offset, size);
}
double[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.allocateNative(size * 8)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(size * 8, scope);
enqueueReadBuffer(mem, true, mem_offset * 8, size * 8, seg, wait, event);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asDoubleBuffer().get(buffer, buf_offset, size);
}
long buffer_row_pitch, long buffer_slice_pitch, long host_row_pitch, long host_slice_pitch,
long limit) {
if (buffer_origin.length != 3
- || host_origin.length != 3
- || region.length != 3)
+ || host_origin.length != 3
+ || region.length != 3)
throw new IllegalArgumentException("origin and region must be 3-dimensional");
long stride = host_row_pitch == 0 ? region[0] : host_row_pitch;
long slice = host_slice_pitch == 0 ? region[1] * stride : host_slice_pitch;
if (host_origin[0] + host_origin[1] * stride + host_origin[2] * slice
- + region[0] + (region[1]-1) * stride + (region[2]-1) * slice
- > limit)
+ + region[0] + (region[1] - 1) * stride + (region[2] - 1) * slice
+ > limit)
throw new BufferOverflowException();
}
buffer_row_pitch, buffer_slice_pitch, host_row_pitch, host_slice_pitch,
buffer.byteSize());
- try (Allocator frame = Memory.stack()) {
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
EventInfo info = new EventInfo(frame, wait, event);
- MemoryAddress cbuffer_origin = toLongV(frame, buffer_origin);
- MemoryAddress chost_origin = toLongV(frame, host_origin);
- MemoryAddress cregion = toLongV(frame, region);
+ MemorySegment cbuffer_origin = toLongV(frame, buffer_origin);
+ MemorySegment chost_origin = toLongV(frame, host_origin);
+ MemorySegment cregion = toLongV(frame, region);
int res;
res = clEnqueueReadBufferRect(
addr(), mem.addr(), blocking ? 1 : 0,
- cbuffer_origin, chost_origin, cregion,
+ cbuffer_origin.address(), chost_origin.address(), cregion.address(),
buffer_row_pitch,
buffer_slice_pitch,
host_row_pitch,
host_slice_pitch,
- buffer.baseAddress(),
+ buffer.address(),
info.nwait, info.wait, info.event);
if (res != 0)
ByteBuffer buffer,
CLEventList waiters, CLEventList events) throws CLException, UnsupportedOperationException {
- try (MemorySegment seg = MemorySegment.ofByteBuffer(buffer)) {
- enqueueReadBufferRect(mem, blocking,
- buffer_origin, host_origin, region,
- buffer_row_pitch, buffer_slice_pitch, host_row_pitch, host_slice_pitch,
- seg,
- waiters, events);
- }
+ MemorySegment seg = MemorySegment.ofByteBuffer(buffer);
+ enqueueReadBufferRect(mem, blocking,
+ buffer_origin, host_origin, region,
+ buffer_row_pitch, buffer_slice_pitch, host_row_pitch, host_slice_pitch,
+ seg,
+ waiters, events);
}
public void enqueueReadBufferRect(CLBuffer mem, boolean blocking,
long slice = region[1] * stride;
long size = slice * region[2];
- try (MemorySegment seg = MemorySegment.allocateNative(size)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(size, scope);
enqueueReadBufferRect(mem, blocking,
buffer_origin, OFFSET_0x0x0, region,
buffer_row_pitch, buffer_slice_pitch, stride, slice,
if (size > buffer.byteSize())
throw new BufferUnderflowException();
- try (Allocator frame = Memory.stack()) {
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
EventInfo info = new EventInfo(frame, wait, event);
int res;
res = clEnqueueWriteBuffer(addr(), mem.addr(), blocking ? 1 : 0,
mem_offset, size,
- buffer.baseAddress(),
+ buffer.address(),
info.nwait, info.wait, info.event);
if (res != 0)
throw new CLException(res);
ByteBuffer buffer,
CLEventList wait,
CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.ofByteBuffer(buffer)) {
- enqueueWriteBuffer(mem, blocking,
- mem_offset, size,
- seg,
- wait, event);
- }
+ MemorySegment seg = MemorySegment.ofByteBuffer(buffer);
+ enqueueWriteBuffer(mem, blocking,
+ mem_offset, size,
+ seg,
+ wait, event);
}
public void enqueueWriteBuffer(CLBuffer mem, boolean blocking,
byte[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.allocateNative(size)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(size, scope);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).put(buffer, buf_offset, size);
enqueueWriteBuffer(mem, true, mem_offset, size, seg, wait, event);
}
short[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.allocateNative(size * 2L)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(size * 2L, scope);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asShortBuffer().put(buffer, buf_offset, size);
enqueueWriteBuffer(mem, true, mem_offset * 2, size * 2L, seg, wait, event);
}
int[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.allocateNative(size * 4L)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(size * 4L, scope);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asIntBuffer().put(buffer, buf_offset, size);
enqueueWriteBuffer(mem, true, mem_offset * 4, size * 4L, seg, wait, event);
}
long[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.allocateNative(size * 8L)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(size * 8L, scope);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asLongBuffer().put(buffer, buf_offset, size);
enqueueWriteBuffer(mem, true, mem_offset * 8, size * 8L, seg, wait, event);
}
float[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.allocateNative(size * 4L)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(size * 4L, scope);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asFloatBuffer().put(buffer, buf_offset, size);
enqueueWriteBuffer(mem, true, mem_offset * 4, size * 4L, seg, wait, event);
}
double[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.allocateNative(size * 8L)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(size * 8L, scope);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asDoubleBuffer().put(buffer, buf_offset, size);
enqueueWriteBuffer(mem, true, mem_offset * 8, size * 8L, seg, wait, event);
}
buffer_row_pitch, buffer_slice_pitch, host_row_pitch, host_slice_pitch,
buffer.byteSize());
- try (Allocator frame = Memory.stack()) {
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
EventInfo info = new EventInfo(frame, wait, event);
- MemoryAddress cbuffer_origin = toLongV(frame, buffer_origin);
- MemoryAddress chost_origin = toLongV(frame, host_origin);
- MemoryAddress cregion = toLongV(frame, region);
+ MemorySegment cbuffer_origin = toLongV(frame, buffer_origin);
+ MemorySegment chost_origin = toLongV(frame, host_origin);
+ MemorySegment cregion = toLongV(frame, region);
int res;
res = clEnqueueWriteBufferRect(
addr(), mem.addr(), blocking ? 1 : 0,
- cbuffer_origin, chost_origin, cregion,
+ cbuffer_origin.address(), chost_origin.address(), cregion.address(),
buffer_row_pitch,
buffer_slice_pitch,
host_row_pitch,
host_slice_pitch,
- buffer.baseAddress(),
+ buffer.address(),
info.nwait, info.wait, info.event);
if (res != 0)
long buffer_row_pitch, long buffer_slice_pitch, long host_row_pitch, long host_slice_pitch,
ByteBuffer buffer,
CLEventList waiters, CLEventList events) throws CLException, UnsupportedOperationException {
- try (MemorySegment seg = MemorySegment.ofByteBuffer(buffer)) {
- enqueueWriteBufferRect(mem, blocking,
- buffer_origin, host_origin, region,
- buffer_row_pitch, buffer_slice_pitch, host_row_pitch, host_slice_pitch,
- seg,
- waiters, events);
- }
+ MemorySegment seg = MemorySegment.ofByteBuffer(buffer);
+ enqueueWriteBufferRect(mem, blocking,
+ buffer_origin, host_origin, region,
+ buffer_row_pitch, buffer_slice_pitch, host_row_pitch, host_slice_pitch,
+ seg,
+ waiters, events);
}
public native void enqueueWriteBufferRect(CLBuffer mem, boolean blocking,
/**
* Calls clEnqueueFillBuffer.
- *
- * This fills the buffer with a pattern. The offset and size
+ * <p>
+ * This fills the buffer with a pattern. The offset and size
* is in multiples of the pattern size.
*
* @param buffer
* @throws CLException
*/
public void enqueueFillBuffer(CLBuffer buffer,
- MemorySegment pattern,
- long offset,
- long size,
- CLEventList wait,
- CLEventList event) throws CLException {
+ MemorySegment pattern,
+ long offset,
+ long size,
+ CLEventList wait,
+ CLEventList event) throws CLException {
requireAPIVersion(CLPlatform.VERSION_1_2);
- try (Allocator frame = Memory.stack()) {
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
EventInfo info = new EventInfo(frame, wait, event);
int res;
res = clEnqueueFillBuffer(
addr(), buffer.addr(),
- pattern.baseAddress(), pattern.byteSize(),
+ pattern.address(), pattern.byteSize(),
offset * pattern.byteSize(),
size * pattern.byteSize(),
info.nwait, info.wait, info.event);
throw new RuntimeException(t);
}
}
+
public void enqueueFillBuffer(CLBuffer buffer,
ByteBuffer pattern,
long offset,
long size,
CLEventList wait,
CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.ofByteBuffer(pattern)) {
- enqueueFillBuffer(buffer,
- pattern,
- offset, size,
- wait, event);
- }
+ MemorySegment seg = MemorySegment.ofByteBuffer(pattern);
+ enqueueFillBuffer(buffer,
+ pattern,
+ offset, size,
+ wait, event);
}
/**
* @throws CLException
*/
public void enqueueFillBuffer(CLBuffer buffer,
- byte[] pattern,
- long offset,
- long size,
- CLEventList wait,
- CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.allocateNative(pattern.length)) {
- MemoryAddress add = seg.baseAddress();
-
- for (int i=0;i<pattern.length;i++)
+ byte[] pattern,
+ long offset,
+ long size,
+ CLEventList wait,
+ CLEventList event) throws CLException {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(pattern.length, scope);
+ MemoryAddress add = seg.address();
+
+ for (int i = 0; i < pattern.length; i++)
setByte(add, i, pattern[i]);
enqueueFillBuffer(buffer, seg, offset, size, wait, event);
* @throws CLException
*/
public void enqueueFillBuffer(CLBuffer buffer,
- short[] pattern,
- long offset,
- long size,
- CLEventList wait,
- CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.allocateNative(pattern.length * 2)) {
- MemoryAddress add = seg.baseAddress();
-
- for (int i=0;i<pattern.length;i++)
+ short[] pattern,
+ long offset,
+ long size,
+ CLEventList wait,
+ CLEventList event) throws CLException {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(pattern.length * 2, scope);
+ MemoryAddress add = seg.address();
+
+ for (int i = 0; i < pattern.length; i++)
setShort(add, i, pattern[i]);
enqueueFillBuffer(buffer, seg, offset * 2, size * 2, wait, event);
* @throws CLException
*/
public void enqueueFillBuffer(CLBuffer buffer,
- int[] pattern,
- long offset,
- long size,
- CLEventList wait,
- CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.allocateNative(pattern.length * 4)) {
- MemoryAddress add = seg.baseAddress();
-
- for (int i=0;i<pattern.length;i++)
+ int[] pattern,
+ long offset,
+ long size,
+ CLEventList wait,
+ CLEventList event) throws CLException {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(pattern.length * 4, scope);
+ MemoryAddress add = seg.address();
+
+ for (int i = 0; i < pattern.length; i++)
setInt(add, i, pattern[i]);
enqueueFillBuffer(buffer, seg, offset * 4, size * 4, wait, event);
* @throws CLException
*/
public void enqueueFillBuffer(CLBuffer buffer,
- float[] pattern,
- long offset,
- long size,
- CLEventList wait,
- CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.allocateNative(pattern.length * 4)) {
- MemoryAddress add = seg.baseAddress();
-
- for (int i=0;i<pattern.length;i++)
+ float[] pattern,
+ long offset,
+ long size,
+ CLEventList wait,
+ CLEventList event) throws CLException {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(pattern.length * 4, scope);
+ MemoryAddress add = seg.address();
+
+ for (int i = 0; i < pattern.length; i++)
setFloat(add, i, pattern[i]);
enqueueFillBuffer(buffer, seg, offset * 4, size * 4, wait, event);
CLEventList wait,
CLEventList event) throws CLException {
- try (Allocator frame = Memory.stack()) {
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
EventInfo info = new EventInfo(frame, wait, event);
int res;
MethodHandle op) throws CLException {
if (origin.length != 3
- || region.length != 3)
+ || region.length != 3)
throw new IllegalArgumentException("origin and region must be 3-dimensional");
long stride = row_pitch == 0 ? region[0] * image.getElementSize() : row_pitch;
if (buffer.byteSize() < slice * region[2])
throw new BufferOverflowException();
- try (Allocator frame = Memory.stack()) {
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
EventInfo info = new EventInfo(frame, wait, event);
- MemoryAddress corigin = toLongV(frame, origin);
- MemoryAddress cregion = toLongV(frame, region);
+ MemorySegment corigin = toLongV(frame, origin);
+ MemorySegment cregion = toLongV(frame, region);
int res;
res = (int)op.invokeExact(
addr(), image.addr(), blocking ? 1 : 0,
- corigin, cregion,
+ corigin.address(), cregion.address(),
row_pitch, slice_pitch,
- buffer.baseAddress(),
+ buffer.address(),
info.nwait, info.wait, info.event);
if (res != 0)
}
private interface CopyAppend {
+
void apply(int doff, int size);
default void copy(int w, int h, int d, int stride, int slice) {
- for (int z=0;z<d;z++) {
- for (int y=0;y<h;y++) {
+ for (int z = 0; z < d; z++) {
+ for (int y = 0; y < h; y++) {
apply(y * stride + z * slice, w);
}
}
long xstride = region[0] << dshift;
long xslice = xstride * region[1];
- try (MemorySegment seg = MemorySegment.allocateNative(alloc)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(alloc, scope);
if (!readMode)
- writer.apply(seg).copy((int)region[0], (int)region[1], (int)region[2],(int)stride, (int)slice);
+ writer.apply(seg).copy((int)region[0], (int)region[1], (int)region[2], (int)stride, (int)slice);
doImageBuffer(image, blocking, origin, region, xstride, 0, seg, wait, event, op);
if (readMode)
- writer.apply(seg).copy((int)region[0], (int)region[1], (int)region[2],(int)stride, (int)slice);
+ writer.apply(seg).copy((int)region[0], (int)region[1], (int)region[2], (int)stride, (int)slice);
} catch (CLException | RuntimeException | Error t) {
throw t;
} catch (Throwable t) {
ByteBuffer buffer,
CLEventList wait,
CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.ofByteBuffer(buffer)) {
- enqueueReadImage(image, blocking,
- origin, region,
- row_pitch, slice_pitch,
- seg,
- wait, event);
- }
+ MemorySegment seg = MemorySegment.ofByteBuffer(buffer);
+ enqueueReadImage(image, blocking,
+ origin, region,
+ row_pitch, slice_pitch,
+ seg,
+ wait, event);
}
/**
ByteBuffer buffer,
CLEventList wait,
CLEventList event) throws CLException {
- try (MemorySegment seg = MemorySegment.ofByteBuffer(buffer)) {
- enqueueWriteImage(image, blocking,
- origin, region,
- row_pitch, slice_pitch,
- seg,
- wait, event);
- }
+ MemorySegment seg = MemorySegment.ofByteBuffer(buffer);
+ enqueueWriteImage(image, blocking,
+ origin, region,
+ row_pitch, slice_pitch,
+ seg,
+ wait, event);
}
public void enqueueWriteImage(CLImage image, boolean blocking,
CLEventList events) throws CLException;
static class MapData {
+
MemoryAddress raw;
ByteBuffer buffer;
}
public ByteBuffer enqueueMapBuffer(CLBuffer buffer, boolean blocking,
- long flags,
- long offset,
- long size,
- CLEventList wait,
- CLEventList event) throws CLException {
- try (Allocator frame = Memory.stack()) {
+ long flags,
+ long offset,
+ long size,
+ CLEventList wait,
+ CLEventList event) throws CLException {
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
EventInfo info = new EventInfo(frame, wait, event);
- MemoryAddress cres = frame.alloca(8);
+ MemorySegment pres = MemorySegment.allocateNative(CLinker.C_INT, frame);
MemoryAddress cmap;
int res;
cmap = clEnqueueMapBuffer(addr(), buffer.addr(), blocking ? 1 : 0,
- flags, offset, size,
- info.nwait, info.wait, info.event,
- cres);
- res = getInt(cres);
+ flags, offset, size,
+ info.nwait, info.wait, info.event,
+ pres.address());
+ res = MemoryAccess.getInt(pres);
if (res != 0)
throw new CLException(res);
/* There is some scope for improvement in this shittastic interface */
public ByteBuffer enqueueMapImage(CLImage image, boolean blocking,
- long flags,
- long[] origin,
- long[] region,
- long[] image_row_pitch,
- long[] image_slice_pitch,
- CLEventList wait,
- CLEventList event) throws CLException {
+ long flags,
+ long[] origin,
+ long[] region,
+ long[] image_row_pitch,
+ long[] image_slice_pitch,
+ CLEventList wait,
+ CLEventList event) throws CLException {
if (origin.length != 3 || region.length != 3)
throw new IllegalArgumentException("origin and region must be 3-dimensional");
if (image_row_pitch.length < 1)
throw new IllegalArgumentException("image_row_pitch must contain 1 element");
// image_slice_pitch may be null for 2D images, checking this isn't worth it
- try (Allocator frame = Memory.stack()) {
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
EventInfo info = new EventInfo(frame, wait, event);
- MemoryAddress corigin = toLongV(frame, origin);
- MemoryAddress cregion = toLongV(frame, region);
- MemoryAddress cstride = frame.alloca(8);
- MemoryAddress cslice = frame.alloca(8);
- MemoryAddress cres = frame.alloca(8);
+ MemorySegment corigin = toLongV(frame, origin);
+ MemorySegment cregion = toLongV(frame, region);
+ MemorySegment cstride = MemorySegment.allocateNative(CLinker.C_LONG, frame);
+ MemorySegment cslice = MemorySegment.allocateNative(CLinker.C_LONG, frame);
+ MemorySegment pres = MemorySegment.allocateNative(CLinker.C_LONG, frame);
MemoryAddress cmap;
long stride, slice;
int res;
cmap = clEnqueueMapImage(
addr(), image.addr(), blocking ? 1 : 0,
flags,
- corigin, cregion,
- cstride, cslice,
+ corigin.address(), cregion.address(),
+ cstride.address(), cslice.address(),
info.nwait, info.wait, info.event,
- cres);
- res = getInt(cres);
+ pres.address());
+ res = MemoryAccess.getInt(pres);
if (res != 0)
throw new CLException(res);
- stride = getLong(cstride);
- slice = getLong(cslice);
+ stride = MemoryAccess.getLong(cstride);
+ slice = MemoryAccess.getLong(cslice);
image_row_pitch[0] = stride;
if (image_slice_pitch != null)
// These look weird but that's what the docs say
long size = slice == 0
- ? stride * region[1] + region[0] // 2D
- : slice * region[2] + stride * region[1] + region[0]; // 3D
+ ? stride * region[1] + region[0] // 2D
+ : slice * region[2] + stride * region[1] + region[0]; // 3D
return Memory.ofNative(cmap, size).asByteBuffer();
} catch (CLException | RuntimeException | Error t) {
}
public void enqueueUnmapMemObject(CLMemory mem, ByteBuffer mapped,
- CLEventList wait,
- CLEventList event) throws CLException {
- MemoryAddress cmap = MemorySegment.ofByteBuffer(mapped).baseAddress();
+ CLEventList wait,
+ CLEventList event) throws CLException {
+ MemoryAddress cmap = MemorySegment.ofByteBuffer(mapped).address();
- try (Allocator frame = Memory.stack()) {
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
EventInfo info = new EventInfo(frame, wait, event);
int res;
CLEventList waiters,
CLEventList events) throws CLException;
- private MemoryAddress wsToLongV(Allocator frame, long[] ws, int dim) {
+ private MemorySegment wsToLongV(ResourceScope frame, long[] ws, int dim) {
if (ws != null) {
if (ws.length < dim)
throw new IllegalArgumentException();
return toLongV(frame, ws);
} else {
- return MemoryAddress.NULL;
+ // or null??
+ return MemorySegment.globalNativeSegment();
}
}
* @throws CLException
*/
public void enqueueNDRangeKernel(CLKernel kernel,
- int work_dim,
- long[] global_offset,
- long[] global_size,
- long[] local_size,
- CLEventList waiters,
- CLEventList events) throws CLException {
- try (Allocator frame = Memory.stack()) {
- MemoryAddress gwo = wsToLongV(frame, global_offset, work_dim);
- MemoryAddress gws = wsToLongV(frame, global_size, work_dim);
- MemoryAddress lws = wsToLongV(frame, local_size, work_dim);
+ int work_dim,
+ long[] global_offset,
+ long[] global_size,
+ long[] local_size,
+ CLEventList waiters,
+ CLEventList events) throws CLException {
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
+ MemorySegment gwo = wsToLongV(frame, global_offset, work_dim);
+ MemorySegment gws = wsToLongV(frame, global_size, work_dim);
+ MemorySegment lws = wsToLongV(frame, local_size, work_dim);
EventInfo info = new EventInfo(frame, waiters, events);
int res;
res = clEnqueueNDRangeKernel(addr(), kernel.addr(),
- work_dim, gwo, gws, lws,
- info.nwait, info.wait, info.event);
+ work_dim, gwo.address(), gws.address(), lws.address(),
+ info.nwait, info.wait, info.event);
if (res != 0)
throw new CLException(res);
* x-deprecated as of OpenCL 2.0
*/
public void enqueueTask(CLKernel kernel,
- CLEventList waiters,
- CLEventList events) throws CLException {
- try (Allocator frame = Memory.stack()) {
+ CLEventList waiters,
+ CLEventList events) throws CLException {
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
EventInfo info = new EventInfo(frame, waiters, events);
int res;
res = clEnqueueTask(addr(), kernel.addr(),
- info.nwait, info.wait, info.event);
+ info.nwait, info.wait, info.event);
if (res != 0)
throw new CLException(res);
Object... args) throws CLException {
// This basically just passes the memory objects to opencl, the rest are handled by the lambda.
// This means args and mem_list are the same value
- try (Allocator frame = Memory.stack()) {
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
EventInfo info = new EventInfo(frame, waiters, events);
- MemoryAddress memstage = frame.alloca(8 * args.length);
- MemoryAddress memptrs = frame.alloca(8 * args.length);
+ MemorySegment memstage = Native.allocAddrV(frame, args.length);
+ MemorySegment memptrs = Native.allocAddrV(frame, args.length);
int nmem = 0;
int res;
for (Object a: args) {
if (a instanceof CLMemory) {
- setAddr(memstage, nmem, ((CLMemory)a).addr());
- setAddr(memptrs, nmem, memstage.addOffset(nmem * 8));
+ MemoryAccess.setAddressAtIndex(memstage, nmem, ((CLMemory)a).addr());
+ MemoryAccess.setAddressAtIndex(memptrs, nmem, memstage.asSlice(nmem * 8));
nmem++;
}
}
Callback<CLNativeKernel> call = Native.resolve(
Call_pv_v.stub((MemoryAddress memargs) -> {
- int xmem = 0;
+ int xmem = 0;
- for (int i=0;i<args.length;i++) {
- if (args[i] instanceof CLMemory) {
- MemoryAddress mem = getAddr(memargs, xmem);
- long size = ((CLMemory)args[i]).getSize();
+ for (int i = 0; i < args.length; i++) {
+ if (args[i] instanceof CLMemory) {
+ MemoryAddress mem = getAddr(memargs, xmem);
+ long size = ((CLMemory)args[i]).getSize();
- save[i] = Memory.ofNative(mem, size).asByteBuffer().order(ByteOrder.nativeOrder());
- xmem++;
- }
+ save[i] = Memory.ofNative(mem, size).asByteBuffer().order(ByteOrder.nativeOrder());
+ xmem++;
}
+ }
- kernel.invoke(save);
- }),
+ kernel.invoke(save);
+ }),
(p) -> new Callback<>(p, kernel));
- res = clEnqueueNativeKernel(addr(), call.addr(), memstage, nmem * 8, nmem, memstage, memptrs, info.nwait, info.wait, info.event);
+ res = clEnqueueNativeKernel(addr(), call.addr(), memstage.address(), nmem * 8, nmem, memstage.address(), memptrs.address(), info.nwait, info.wait, info.event);
if (res != 0)
throw new CLException(res);
public void enqueueMarkerWithWaitList(
CLEventList waiters,
CLEventList events) throws CLException {
- try (Allocator frame = Memory.stack()) {
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
EventInfo info = new EventInfo(frame, waiters, events);
if (haveAPIVersion(CLPlatform.VERSION_1_2)) {
clEnqueueMarkerWithWaitList(addr(), info.nwait, info.wait, info.event);
* @param events
* @throws CLException
*/
- public void enqueueBarrierWithWaitList(
+ public void enqueueBarrierWithWaitList(
CLEventList waiters,
CLEventList events) throws CLException {
- try (Allocator frame = Memory.stack()) {
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
EventInfo info = new EventInfo(frame, waiters, events);
if (haveAPIVersion(CLPlatform.VERSION_1_2)) {
clEnqueueBarrierWithWaitList(addr(), info.nwait, info.wait, info.event);
}
/* ********************************************************************** */
-
protected GLext getGLext() {
return getObjectPlatform()
.getExtension(CLPlatform.cl_gl_ext, (p)
CLEventList wait,
CLEventList event) {
GLext gl = getGLext();
- try (Allocator frame = Memory.stack()) {
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
EventInfo info = new EventInfo(frame, wait, event);
- MemoryAddress cmem_objects = Native.toAddrV(frame, mem_objects);
+ MemorySegment cmem_objects = Native.toAddrV(frame, mem_objects);
int res;
- res = gl.clEnqueueAcquireGLObjects(addr(), mem_objects.length, cmem_objects,
+ res = gl.clEnqueueAcquireGLObjects(addr(), mem_objects.length, cmem_objects.address(),
info.nwait, info.wait, info.event);
if (res != 0)
throw new CLRuntimeException(res);
CLEventList wait,
CLEventList event) {
GLext gl = getGLext();
- try (Allocator frame = Memory.stack()) {
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
EventInfo info = new EventInfo(frame, wait, event);
- MemoryAddress cmem_objects = Native.toAddrV(frame, mem_objects);
+ MemorySegment cmem_objects = Native.toAddrV(frame, mem_objects);
int res;
- res = gl.clEnqueueReleaseGLObjects(addr(), mem_objects.length, cmem_objects,
+ res = gl.clEnqueueReleaseGLObjects(addr(), mem_objects.length, cmem_objects.address(),
info.nwait, info.wait, info.event);
if (res != 0)
throw new CLRuntimeException(res);
}
/* ********************************************************************** */
-
/**
* Invoke task.queue for this queue with no event lists.
*
@Override
public String toString() {
StringBuilder sb = new StringBuilder("CLContext[");
- for (CLDevice d : getDevices()) {
+ for (CLDevice d: getDevices()) {
sb.append(d);
sb.append(' ');
}
* @throws CLRuntimeException
*/
public static CLContext createContext(CLContextProperty[] properties, CLDevice[] devices, CLContextNotify notify) throws CLRuntimeException {
- try (Allocator frame = Memory.stack()) {
- MemoryAddress pprops = CLProperty.toNative(frame, properties);
- MemoryAddress pdevs = frame.alloca(devices.length * 8);
- MemoryAddress pres = frame.alloca(8);
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
+ MemorySegment pprops = CLProperty.toNative(frame, properties);
+ MemorySegment pdevs = MemorySegment.allocateNative(devices.length * CLinker.C_POINTER.byteSize(), CLinker.C_POINTER.byteAlignment(), frame);
+ MemorySegment pres = MemorySegment.allocateNative(CLinker.C_INT, frame);
Callback<CLContextNotify> call = CLContextNotify.call(notify);
MemoryAddress cl;
int res;
- for (int i=0;i<devices.length;i++)
- setAddr(pdevs, i, devices[i].addr());
+ for (int i = 0; i < devices.length; i++)
+ MemoryAccess.setAddressAtIndex(pdevs, i, devices[i].addr());
- cl = clCreateContext(pprops, devices.length, pdevs, call.addr(), MemoryAddress.NULL, pres);
- res = getInt(pres);
+ cl = clCreateContext(pprops.address(), devices.length, pdevs.address(), call.addr(), MemoryAddress.NULL, pres.address());
+ res = MemoryAccess.getInt(pres);
if (res != 0)
throw new CLRuntimeException(res);
* Directly lookup platform for a context where we have no other context to find it
*/
static CLPlatform findPlatform(MemoryAddress ccl) {
- try (Allocator a = Memory.stack()) {
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
MemorySegment devices = getInfoAny(ccl, CL_CONTEXT_DEVICES, clGetContextInfo, a);
- MemoryAddress dev0 = getAddr(devices.baseAddress());
- MemoryAddress plat = getAddr(getInfo(dev0, CL_DEVICE_PLATFORM, clGetDeviceInfo, a, 8));
+ MemoryAddress dev0 = MemoryAccess.getAddress(devices);
+ MemoryAddress plat = MemoryAccess.getAddress(getInfo(dev0, CL_DEVICE_PLATFORM, clGetDeviceInfo, a, 8));
return Native.resolve(plat, CLPlatform::new);
} catch (RuntimeException | Error t) {
* @throws CLRuntimeException
*/
public static CLContext createContextFromType(CLContextProperty[] properties, long device_type, CLContextNotify notify) throws CLRuntimeException {
- try (Allocator frame = Memory.stack()) {
- MemoryAddress pprops = CLProperty.toNative(frame, properties);
- MemoryAddress pres = frame.alloca(8);
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
+ MemorySegment pprops = CLProperty.toNative(frame, properties);
+ MemorySegment pres = MemorySegment.allocateNative(CLinker.C_INT, frame);
MemoryAddress cl;
Callback<CLContextNotify> call = CLContextNotify.call(notify);
int res;
- cl = clCreateContextFromType(pprops, device_type, call.addr(), MemoryAddress.NULL, pres);
- res = getInt(pres);
+ cl = clCreateContextFromType(pprops.address(), device_type, call.addr(), MemoryAddress.NULL, pres.address());
+ res = MemoryAccess.getInt(pres);
if (res != 0)
throw new CLRuntimeException(res);
* deprecated as of OpenCL 2.0
*/
public CLCommandQueue createCommandQueue(CLDevice dev, long properties) throws CLRuntimeException {
- try (Allocator frame = Memory.stack()) {
- MemoryAddress pres = frame.alloca(8);
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
+ MemorySegment pres = MemorySegment.allocateNative(CLinker.C_INT, frame);
MemoryAddress q;
int res;
- q = clCreateCommandQueue(addr(), dev.addr(), properties, pres);
- res = getInt(pres);
+ q = clCreateCommandQueue(addr(), dev.addr(), properties, pres.address());
+ res = MemoryAccess.getInt(pres);
if (res != 0)
throw new CLRuntimeException(res);
// Fallback if opencl2 not supported?
requireAPIVersion(CLPlatform.VERSION_2_0);
- try (Allocator frame = Memory.stack()) {
- MemoryAddress pprops = CLProperty.toNative(frame, properties);
- MemoryAddress pres = frame.alloca(8);
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
+ MemorySegment pprops = CLProperty.toNative(frame, properties);
+ MemorySegment pres = MemorySegment.allocateNative(CLinker.C_INT, frame);
MemoryAddress q;
int res;
- q = clCreateCommandQueueWithProperties(addr(), dev.addr(), pprops, pres);
- res = getInt(pres);
+ q = clCreateCommandQueueWithProperties(addr(), dev.addr(), pprops.address(), pres.address());
+ res = MemoryAccess.getInt(pres);
if (res != 0)
throw new CLRuntimeException(res);
}
/* ********************************************************************** */
-
/**
* Calls clCreateBuffer with an empty host pointer.
*
/**
* Calls clCreateBuffer.
- *
+ * <p>
* If flags contains CL_MEM_USE_HOST_PTR then hostp must not
- * be null. Currently in this case the CLBuffer must be
+ * be null. Currently in this case the CLBuffer must be
* explicitly released() on the caller thread.
*
* @param flags CL_MEM_* flags.
if (hostseg != null && hostseg.byteSize() < size)
throw new CLRuntimeException(CL_INVALID_HOST_PTR);
- try (Allocator frame = Memory.stack()) {
- MemoryAddress pres = frame.alloca(8);
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
+ MemorySegment pres = MemorySegment.allocateNative(CLinker.C_INT, frame);
MemoryAddress pbuffer;
int res;
- pbuffer = clCreateBuffer(addr(), flags, size, addr(hostseg), pres);
- res = getInt(pres);
-
- if (res != 0)
+ pbuffer = clCreateBuffer(addr(), flags, size, addr(hostseg), pres.address());
+ if ((res = MemoryAccess.getInt(pres)) != 0)
throw new CLRuntimeException(res);
if (hostseg != null && (flags & CL_MEM_USE_HOST_PTR) != 0)
/**
* Calls clCreateBuffer.
- *
+ * <p>
* If flags contains CL_MEM_USE_HOST_PTR then hostp must not
- * be null. Currently the CLBuffer must be explicitly
+ * be null. Currently the CLBuffer must be explicitly
* released() on the caller thread.
*
* @param flags CL_MEM_* flags.
try {
CLBuffer buffer = createBuffer(flags, size, hostseg);
- if ((flags & CL_MEM_USE_HOST_PTR) != 0)
- hostseg = null;
-
+ //if ((flags & CL_MEM_USE_HOST_PTR) != 0)
+ // hostseg = null;
return buffer;
} finally {
- if (hostseg != null)
- hostseg.close();
+ //if (hostseg != null)
+ // hostseg.close();
}
} else {
return createBuffer(flags, size, (MemorySegment)null);
throw new NullPointerException();
// Must copy.
- try (MemorySegment mem = MemorySegment.allocateNative(hostp.length, 16)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment mem = MemorySegment.allocateNative(hostp.length, 16, scope);
mem.asByteBuffer().order(ByteOrder.nativeOrder()).put(hostp);
return createBuffer(flags, hostp.length, mem);
}
}
//native public CLBuffer createBuffer(long flags, short[] hostp) throws CLRuntimeException;
-
//native public CLBuffer createBuffer(long flags, int[] hostp) throws CLRuntimeException;
-
public CLBuffer createBuffer(long flags, float[] hostp) throws CLRuntimeException {
if ((flags & CL_MEM_USE_HOST_PTR) != 0)
throw new CLRuntimeException(CL_INVALID_VALUE);
throw new NullPointerException();
// Must copy.
- try (MemorySegment mem = MemorySegment.allocateNative(hostp.length, 16)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment mem = MemorySegment.allocateNative(hostp.length, 16, scope);
mem.asByteBuffer().order(ByteOrder.nativeOrder()).asFloatBuffer().put(hostp);
return createBuffer(flags, hostp.length * 4, mem);
}
}
//native public CLBuffer createBuffer(long flags, double[] hostp) throws CLRuntimeException;
-
// FIXME: clCreateSubBuffer
/* ********************************************************************** */
-
public CLImage createImage(long flags, CLImageFormat fmt, CLImageDesc desc) throws CLRuntimeException, UnsupportedOperationException {
- return createImage(flags, fmt, desc, (MemorySegment) null);
+ return createImage(flags, fmt, desc, (MemorySegment)null);
}
/**
int res;
// FIXME: perform range checks
-
if (haveAPIVersion(CLPlatform.VERSION_1_2)) {
MemoryAddress cdesc = CLImageDesc.toNative(frame, desc);
break;
case CL_MEM_OBJECT_IMAGE3D:
ci = clCreateImage3D(addr(), flags, cfmt, desc.imageWidth, desc.imageHeight, desc.imageDepth,
- desc.imageRowPitch, desc.imageSlicePitch, addr(hostseg), cres);
+ desc.imageRowPitch, desc.imageSlicePitch, addr(hostseg), cres);
break;
default:
throw new UnsupportedOperationException("Requires OpenCL 1.2");
try {
CLImage image = createImage(flags, fmt, desc, hostseg);
- if ((flags & CL_MEM_USE_HOST_PTR) != 0)
- hostseg = null;
-
+ //if ((flags & CL_MEM_USE_HOST_PTR) != 0)
+ // hostseg = null;
return image;
} finally {
- if (hostseg != null)
- hostseg.close();
+ //if (hostseg != null)
+ // hostseg.close();
}
} else {
return createImage(flags, fmt, desc, (MemorySegment)null);
throw new NullPointerException();
// Must copy.
- try (MemorySegment mem = MemorySegment.allocateNative(hostp.length, 16)) {
- mem.asByteBuffer().order(ByteOrder.nativeOrder()).put(hostp);
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment mem = MemorySegment.allocateNative(hostp.length, 16, scope);
+ mem.copyFrom(MemorySegment.ofArray(hostp));
return createImage(flags, fmt, desc, mem);
}
}
//native public CLImage createImage(long flags, CLImageFormat fmt, CLImageDesc desc, short[] hostp) throws CLRuntimeException, UnsupportedOperationException;
-
//native public CLImage createImage(long flags, CLImageFormat fmt, CLImageDesc desc, int[] hostp) throws CLRuntimeException, UnsupportedOperationException;
-
public CLImage createImage(long flags, CLImageFormat fmt, CLImageDesc desc, float[] hostp) throws CLRuntimeException, UnsupportedOperationException {
if ((flags & CL_MEM_USE_HOST_PTR) != 0)
throw new CLRuntimeException(CL_INVALID_VALUE);
throw new NullPointerException();
// Must copy.
- try (MemorySegment mem = MemorySegment.allocateNative(hostp.length * 4, 16)) {
- mem.asByteBuffer().order(ByteOrder.nativeOrder()).asFloatBuffer().put(hostp);
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment mem = MemorySegment.allocateNative(hostp.length * 4, 16, scope);
+ mem.copyFrom(MemorySegment.ofArray(hostp));
return createImage(flags, fmt, desc, mem);
}
}
public CLPipe createPipe(long flags, int packetSize, int maxPackets, CLPipeProperty[] properties) throws CLRuntimeException, UnsupportedOperationException {
requireAPIVersion(CLPlatform.VERSION_2_0);
- try (Allocator frame = Memory.stack()) {
- MemoryAddress cprops = CLProperty.toNative(frame, properties);
- MemoryAddress cres = frame.alloca(8);
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
+ MemorySegment pprops = CLProperty.toNative(frame, properties);
+ MemorySegment pres = MemorySegment.allocateNative(CLinker.C_INT, frame);
int res;
MemoryAddress cp;
- cp = clCreatePipe(addr(), flags, packetSize, maxPackets, cprops, cres);
- res = getInt(cres);
- if (res != 0)
+ cp = clCreatePipe(addr(), flags, packetSize, maxPackets, pprops.address(), pres.address());
+ if ((res = MemoryAccess.getInt(pres)) != 0)
throw new CLRuntimeException(res);
return resolve(cp, (x) -> new CLPipe(x, getObjectPlatform()));
res = clGetSupportedImageFormats(addr(), flags, type, num, list, cnum);
CLImageFormat[] out = new CLImageFormat[num];
- for (int i=0;i<out.length;i++)
+ for (int i = 0; i < out.length; i++)
out[i] = CLImageFormat.fromNative(getAddr(list, i));
return out;
/**
* Calls clCreateSamplerWithProperties
*
- * @param props list of propertyes
+ * @param properties list of propertyes
* @return
* @throws CLRuntimeException
* @since OpenCL 2.0
*/
- public CLSampler createSampler(CLSamplerProperty[] props) throws CLRuntimeException {
+ public CLSampler createSampler(CLSamplerProperty[] properties) throws CLRuntimeException {
// Fallback if opencl2 not supported?
requireAPIVersion(CLPlatform.VERSION_2_0);
- try (Allocator frame = Memory.stack()) {
- MemoryAddress cres = frame.alloca(8);
- MemoryAddress cprops = CLProperty.toNative(frame, props);
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
+ MemorySegment pprops = CLProperty.toNative(frame, properties);
+ MemorySegment pres = MemorySegment.allocateNative(CLinker.C_INT, frame);
int res;
MemoryAddress cs;
- cs = clCreateSamplerWithProperties(addr(), cprops, cres);
- res = getInt(cres);
- if (res != 0)
+ cs = clCreateSamplerWithProperties(addr(), pprops.address(), pres.address());
+ if ((res = MemoryAccess.getInt(pres)) != 0)
throw new CLRuntimeException(res);
return resolve(cs, (x) -> new CLSampler(x, getObjectPlatform()));
static long length(byte[][] strings) {
long len = 0;
- for (int i=0;i<strings.length;i++)
+ for (int i = 0; i < strings.length; i++)
len += strings[i].length;
return len;
}
- static void copy(MemoryAddress addr, byte[][]list) {
- for (int i=0,k=0;i<list.length;i++) {
- byte[] row = list[i];
-
- for (int j=0;j<row.length;j++,k++)
- setByte(addr, k, row[j]);
+ static void copy(MemorySegment addr, byte[][] list) {
+ for (int i = 0, k = 0; i < list.length; i++) {
+ addr.asSlice(k).copyFrom(MemorySegment.ofArray(list[i]));
+ k += list[i].length;
}
}
- static void copy(MemoryAddress addr, byte[]row) {
- for (int j=0;j<row.length;j++)
- setByte(addr, j, row[j]);
+ static void copy(MemorySegment addr, byte[] row) {
+ addr.copyFrom(MemorySegment.ofArray(row));
}
/**
*/
public CLProgram createProgramWithSource(byte[]... strings) throws CLException {
long size = length(strings);
- try (Allocator frame = Memory.stack();
- MemorySegment buffer = MemorySegment.allocateNative(size)) {
- MemoryAddress cstring = frame.alloca(8);
- MemoryAddress clength = frame.alloca(8);
- MemoryAddress cret = frame.alloca(8);
- MemoryAddress base = buffer.baseAddress();
+
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
+ MemorySegment buffer = MemorySegment.allocateNative(size, frame);
+ MemorySegment cstring = MemorySegment.allocateNative(CLinker.C_POINTER, frame);
+ MemorySegment clength = MemorySegment.allocateNative(CLinker.C_LONG, frame);
+ MemorySegment pres = MemorySegment.allocateNative(CLinker.C_INT, frame);
MemoryAddress cp;
int res;
- copy(base, strings);
- setAddr(cstring, base);
- setLong(clength, size);
+ copy(buffer, strings);
+ MemoryAccess.setAddress(cstring, buffer.address());
+ MemoryAccess.setLong(clength, size);
- cp = clCreateProgramWithSource(addr(), 1, cstring, clength, cret);
-
- res = getInt(cret);
- if (res != 0)
+ cp = clCreateProgramWithSource(addr(), 1, cstring.address(), clength.address(), pres.address());
+ if ((res = MemoryAccess.getInt(pres)) != 0)
throw new CLException(res);
return resolve(cp, (x) -> new CLProgram(x, getObjectPlatform()));
public CLProgram createProgramWithIL(byte[] il) throws CLException {
requireAPIVersion(CLPlatform.VERSION_2_1);
- try (Allocator frame = Memory.stack();
- MemorySegment buffer = MemorySegment.allocateNative(il.length, 1)) {
- MemoryAddress cret = frame.alloca(8);
- MemoryAddress base = buffer.baseAddress();
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
+ MemorySegment buffer = MemorySegment.allocateNative(il.length, 1, frame);
+ MemorySegment pres = MemorySegment.allocateNative(CLinker.C_INT, frame);
MemoryAddress cp;
int res;
- copy(base, il);
-
- cp = clCreateProgramWithIL(addr(), base, il.length, cret);
+ copy(buffer, il);
- res = getInt(cret);
- if (res != 0)
+ cp = clCreateProgramWithIL(addr(), buffer.address(), il.length, pres.address());
+ if ((res = MemoryAccess.getInt(pres)) != 0)
throw new CLException(res);
return resolve(cp, (x) -> new CLProgram(x, getObjectPlatform()));
*/
public CLProgram createProgramWithBinary(CLDevice[] devices, byte[][] binaries, int[] status) throws CLException {
long size = length(binaries);
- try (Allocator frame = Memory.stack();
- MemorySegment buffer = MemorySegment.allocateNative(size, 1)) {
- MemoryAddress cdevs = toAddrV(frame, devices);
- MemoryAddress barray = frame.alloca(8 * binaries.length);
- MemoryAddress larray = frame.alloca(8 * binaries.length);
- MemoryAddress cstatus = status != null ? frame.alloca(4 * devices.length) : MemoryAddress.NULL;
- MemoryAddress cret = frame.alloca(8);
- MemoryAddress base = buffer.baseAddress();
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
+ MemorySegment buffer = MemorySegment.allocateNative(size, 1, frame);
+ MemorySegment cdevs = toAddrV(frame, devices);
+ MemorySegment barray = Native.allocAddrV(frame, binaries.length);
+ MemorySegment larray = Native.allocV(frame, CLinker.C_LONG, binaries.length);
+ MemorySegment cstatus = status != null ? MemorySegment.allocateNative(4 * status.length, 4, frame) : MemorySegment.globalNativeSegment();
+ MemorySegment pres = MemorySegment.allocateNative(CLinker.C_INT, frame);
MemoryAddress cp;
int res;
- for (int i=0, o=0;i<binaries.length;i++) {
- MemoryAddress addr = base.addOffset(o);
+ for (int i = 0, o = 0; i < binaries.length; i++) {
+ MemorySegment addr = buffer.asSlice(o);
copy(addr, binaries[i]);
- setAddr(barray, i, addr);
+ MemoryAccess.setAddressAtIndex(barray, i, addr);
+ MemoryAccess.setLongAtIndex(larray, i, binaries[i].length);
- setLong(larray, i, binaries[i].length);
o += binaries[i].length;
}
- cp = clCreateProgramWithBinary(addr(), devices.length, cdevs, larray, barray, cstatus, cret);
-
- res = getInt(cret);
- if (res != 0)
+ cp = clCreateProgramWithBinary(addr(), devices.length, cdevs.address(), larray.address(), barray.address(), cstatus.address(), pres.address());
+ if ((res = MemoryAccess.getInt(pres)) != 0)
throw new CLException(res);
if (status != null) {
- for (int i=0;i<status.length;i++)
- status[i] = getInt(cstatus, i);
+ for (int i = 0; i < status.length; i++)
+ status[i] = MemoryAccess.getIntAtIndex(cstatus, i);
}
return resolve(cp, (x) -> new CLProgram(x, getObjectPlatform()));
public CLEvent createUserEvent() throws CLException {
requireAPIVersion(CLPlatform.VERSION_1_1);
- try (Allocator frame = Memory.stack()) {
- MemoryAddress cret = frame.alloca(8);
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
+ MemorySegment pres = MemorySegment.allocateNative(CLinker.C_INT, frame);
MemoryAddress ce;
int res;
- ce = clCreateUserEvent(addr(), cret);
-
- res = getInt(cret);
- if (res != 0)
+ ce = clCreateUserEvent(addr(), pres.address());
+ if ((res = MemoryAccess.getInt(pres)) != 0)
throw new CLException(res);
return resolve(ce, (x) -> new CLEvent(x, getObjectPlatform()));
}
/* ********************************************************************** */
-
protected GLext getGLext() {
return getObjectPlatform()
.getExtension(CLPlatform.cl_gl_ext, (p)
/* **********************************************************************
* cl_khr_sharing extension
*/
-
public static CLContextProperty GL_CONTEXT_KHR(long id) {
return new CLContextProperty.TagValue(CL_GL_CONTEXT_KHR, id);
}
CLContextProperty[] properties
int ctype,
int param_name) throws CLRuntimeException;
- */
- /*
+ */
+ /*
public CLDevice getCurrendDeviceForGLConextKHR(CLContextProperty[] properties) throws CLRuntimeException {
return getGLContextInfoKHRAny(properties, CLObject.CTYPE_DEVICE, CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR);
}
public CLDevice[] getDevicesForGLConextKHR(CLContextProperty[] properties) throws CLRuntimeException {
return getGLContextInfoKHRAnyV(properties, CLObject.CTYPE_DEVICE, CL_DEVICES_FOR_GL_CONTEXT_KHR);
}*/
-
-
- /* **********************************************************************
+ /* **********************************************************************
* cl_khr_gl_event extension
*/
-
/**
* @since cl_khr_gl_event extension
*/
package au.notzed.zcl;
import api.Native;
-import jdk.incubator.foreign.MemoryAddress;
+import jdk.incubator.foreign.MemoryAccess;
+import jdk.incubator.foreign.MemorySegment;
/**
* Properties for CLDevice sub-device creation.
}
@Override
- public int toInt(MemoryAddress dst, int o) {
- Native.setInt(dst, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS);
+ public int toInt(MemorySegment dst, int o) {
+ MemoryAccess.setIntAtIndex(dst, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS);
for (int c : counts)
- Native.setInt(dst, o++, c);
- Native.setInt(dst, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS_LIST_END);
+ MemoryAccess.setIntAtIndex(dst, o++, c);
+ MemoryAccess.setIntAtIndex(dst, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS_LIST_END);
return o;
}
@Override
- public int toLong(MemoryAddress dst, int o) {
- Native.setLong(dst, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS);
+ public int toLong(MemorySegment dst, int o) {
+ MemoryAccess.setLongAtIndex(dst, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS);
for (int c : counts)
- Native.setLong(dst, o++, c);
- Native.setLong(dst, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS_LIST_END);
+ MemoryAccess.setLongAtIndex(dst, o++, c);
+ MemoryAccess.setLongAtIndex(dst, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS_LIST_END);
return o;
}
}
* This goes via the context - every one must have a context?
*/
static CLPlatform findPlatform(MemoryAddress cevent) {
- try (Allocator a = Memory.stack()) {
- MemoryAddress ccl = getAddr(getInfo(cevent, CL_EVENT_CONTEXT, clGetEventInfo, a, 8));
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
+ MemoryAddress ccl = MemoryAccess.getAddress(getInfo(cevent, CL_EVENT_CONTEXT, clGetEventInfo, a, 8));
return CLContext.findPlatform(ccl);
} catch (RuntimeException | Error t) {
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemorySegment;
-import api.Memory;
-import api.Allocator;
import api.Native;
import static au.notzed.zcl.CLLib.*;
+import jdk.incubator.foreign.MemoryAccess;
+import jdk.incubator.foreign.ResourceScope;
/**
* Manages a list of cl_events.
*/
public final class CLEventList implements AutoCloseable {
+ // FIXME: pass scope in instead of being autocloseable
+ final ResourceScope scope = ResourceScope.newConfinedScope();
/**
* Raw event values.
*/
- final MemoryAddress cevents;
+ final MemorySegment cevents;
/**
* Event references?
*/
public CLEventList(int capacity) {
this.jevents = new CLEvent[capacity];
- this.cevents = MemorySegment.allocateNative(8 * capacity, 8).baseAddress();
+ this.cevents = MemorySegment.allocateNative(8 * capacity, 8, scope);
}
/**
if (ev == null) {
try {
- clReleaseEvent(Native.getAddr(cevents, i));
+ clReleaseEvent(MemoryAccess.getAddressAtIndex(cevents, i));
} catch (Throwable t) {
}
} else {
ev.release();
jevents[i] = null;
}
- Native.setAddr(cevents, i, MemoryAddress.NULL);
+ MemoryAccess.setAddressAtIndex(cevents, i, MemoryAddress.NULL);
}
index = 0;
}
*/
public void release() {
reset();
- cevents.segment().close();
+ scope.close();
}
@Override
* This is used internally by CLCommandQueue.EventInfo to write directly to the event list.
*/
public MemoryAddress slots() {
- return cevents;
+ return cevents.address();
}
/**
*/
public MemoryAddress currentSlot() {
if (index < jevents.length) {
- MemoryAddress addr = cevents.addOffset(index * 8);
+ MemorySegment addr = cevents.asSlice(index * 8);
// This should already be null, but this performs a range check
- Native.setAddr(addr, MemoryAddress.NULL);
- return addr;
+ MemoryAccess.setAddress(addr, MemoryAddress.NULL);
+ return addr.address();
} else
throw new ArrayIndexOutOfBoundsException();
}
if (i < index) {
CLEvent ev = jevents[i];
if (ev == null)
- jevents[i] = ev = Native.resolve(Native.getAddr(cevents, i), CLEvent::new);
+ jevents[i] = ev = Native.resolve(MemoryAccess.getAddressAtIndex(cevents, i), CLEvent::new);
return ev;
} else
throw new ArrayIndexOutOfBoundsException();
* @param event
*/
public void add(CLEvent event) {
- Native.setAddr(cevents, index, event.addr());
+ MemoryAccess.setAddressAtIndex(cevents, index, event.addr());
jevents[index++] = event;
}
public void waitForEvents() throws CLException {
if (size() > 0) {
try {
- int res = clWaitForEvents(size(), cevents);
+ int res = clWaitForEvents(size(), cevents.address());
if (res != 0)
throw new CLException(res);
} catch (CLException | RuntimeException | Error t) {
}
static CLImageFormat fromNative(MemorySegment addr) {
- return fromNative(addr.baseAddress());
+ return fromNative(addr.address());
}
static MemoryLayout layout() { return Native.parseStruct("[u32(image_channel_order)u32(image_channel_data_type)]"); }
return null;
- try (Allocator a = Memory.stack()) {
- MemoryAddress addr = getInfo(p, CL_MEM_TYPE, clGetMemObjectInfo, a, 4);
- int type = getInt(addr);
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
+ MemorySegment addr = getInfo(p, CL_MEM_TYPE, clGetMemObjectInfo, a, 4);
+ int type = MemoryAccess.getInt(addr);
switch (type) {
case CL_MEM_OBJECT_BUFFER:
*/
@Override
public void release() {
- if (seg != null) {
- seg.close();
- seg = null;
- }
+ // ??
+ //if (seg != null) {
+ // seg.close();
+ // seg = null;
+ //}
super.release();
}
* @since cl_khr_gl_sharing
*/
public int getGLTextureInfoInt(int param) {
- try (Allocator frame = Memory.stack()) {
- return Native.getInt(getInfo(addr(), CL_GL_TEXTURE_TARGET, getGLext().clGetGLTextureInfo, frame, 4));
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
+ return MemoryAccess.getInt(getInfo(addr(), CL_GL_TEXTURE_TARGET, getGLext().clGetGLTextureInfo, frame, 4));
}
}
import java.util.function.BiFunction;
import jdk.incubator.foreign.*;
import api.Native;
-import api.Allocator;
-import api.Memory;
/**
* Base class for all OpenCL types which can be represented as objects.
/**
* Cache the platform this object belongs to, for api checking.
- * Classes can use getObjectPlatform to look it up. If
+ * Classes can use getObjectPlatform to look it up. If
* they need to support null platform at createion (e.g. clevent)
* they must override it.
*/
/**
* Instantiate a new CLObject.
*
- * @param p address of object. May be MemoryAddress.NULL if required.
+ * @param p address of object. May be MemoryAddress.NULL if required.
* @param platform This must not be null.
*/
protected CLObject(MemoryAddress p, CLPlatform platform) {
}
// some are static for access before object instantiation
-
// new 5-param version
- static MemoryAddress getInfo(MemoryAddress self, int id, MethodHandle getInfo, Allocator frame, long size) throws CLRuntimeException {
+ static MemorySegment getInfo(MemoryAddress self, int id, MethodHandle getInfo, ResourceScope frame, long size) throws CLRuntimeException {
try {
- MemoryAddress addr = frame.alloca(size);
+ MemorySegment addr = MemorySegment.allocateNative(size, 8, frame);
int res;
- res = (int)getInfo.invokeExact(self, id, size, addr, MemoryAddress.NULL);
+ res = (int)getInfo.invokeExact(self, id, size, addr.address(), MemoryAddress.NULL);
if (res != 0)
throw new CLRuntimeException(res);
}
protected int getInfoInt(int id, MethodHandle getInfo) {
- try (Allocator a = Memory.stack()) {
- return getInt(getInfo(addr(), id, getInfo, a, 4));
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
+ return MemoryAccess.getInt(getInfo(addr(), id, getInfo, a, 4));
}
}
protected long getInfoLong(int id, MethodHandle getInfo) {
- try (Allocator a = Memory.stack()) {
- return getLong(getInfo(addr(), id, getInfo, a, 8));
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
+ return MemoryAccess.getLong(getInfo(addr(), id, getInfo, a, 8));
}
}
}
protected <T extends CLObject> T getInfoAny(int id, MethodHandle getInfo, Function<MemoryAddress, T> create) {
- try (Allocator a = Memory.stack()) {
- return Native.resolve(getAddr(getInfo(addr(), id, getInfo, a, 8)), create);
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
+ return Native.resolve(MemoryAccess.getAddress(getInfo(addr(), id, getInfo, a, 8)), create);
}
}
protected <T> T getInfoJava(int id, MethodHandle getInfo, Function<MemoryAddress, T> create) {
- try (Allocator a = Memory.stack()) {
- return create.apply(getAddr(getInfo(addr(), id, getInfo, a, 8)));
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
+ return create.apply(MemoryAccess.getAddress(getInfo(addr(), id, getInfo, a, 8)));
}
}
// new 5-param version for get any
- static MemorySegment getInfoAny(MemoryAddress addr, int id, MethodHandle getInfo, Allocator a) throws CLRuntimeException {
+ static MemorySegment getInfoAny(MemoryAddress addr, int id, MethodHandle getInfo, ResourceScope a) throws CLRuntimeException {
try {
- MemoryAddress sizep = a.alloca(8);
+ MemorySegment sizep = MemorySegment.allocateNative(8, 8, a);
MemorySegment valp;
long size;
int res;
- res = (int)getInfo.invokeExact(addr, id, 0L, MemoryAddress.NULL, sizep);
+ res = (int)getInfo.invokeExact(addr, id, 0L, MemoryAddress.NULL, sizep.address());
- size = getLong(sizep);
- valp = a.allocs(size);
+ size = MemoryAccess.getLong(sizep);
+ valp = MemorySegment.allocateNative(size, a);
- res = (int)getInfo.invokeExact(addr, id, size, valp.baseAddress(), sizep);
+ res = (int)getInfo.invokeExact(addr, id, size, valp.address(), sizep.address());
if (res != 0)
throw new CLRuntimeException(res);
}
protected byte[] getInfoByteV(int id, MethodHandle getInfo) {
- try (Allocator a = Memory.stack()) {
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
return getInfoAny(addr(), id, getInfo, a).toByteArray();
}
}
protected String getInfoString(int id, MethodHandle getInfo) {
- try (Allocator a = Memory.stack()) {
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
return infoToString(getInfoAny(addr(), id, getInfo, a));
}
}
protected <T extends CLProperty> T[] getInfoPropertyV(int id, MethodHandle getInfo, BiFunction<Long, Long, T> create, IntFunction<T[]> createArray) {
- try (Allocator a = Memory.stack()) {
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
return CLProperty.fromNative(getInfoAny(addr(), id, getInfo, a), create, createArray);
}
}
protected long[] getInfoLongV(int id, MethodHandle getInfo) {
- try (Allocator a = Memory.stack()) {
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
return Native.toLongV(getInfoAny(addr(), id, getInfo, a));
}
}
protected <T extends CLObject> T[] getInfoAnyV(int id, MethodHandle getInfo, Function<MemoryAddress, T> create, IntFunction<T[]> createArray) {
- try (Allocator a = Memory.stack()) {
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
return Native.toObjectV(getInfoAny(addr(), id, getInfo, a), create, createArray);
} catch (RuntimeException | Error t) {
throw t;
}
// new 6-param version
- protected <T extends CLObject> MemoryAddress getInfo(T ctx, int id, MethodHandle getInfo, Allocator a, long size) throws CLRuntimeException {
+ protected <T extends CLObject> MemorySegment getInfo(T ctx, int id, MethodHandle getInfo, ResourceScope a, long size) throws CLRuntimeException {
try {
- MemoryAddress addr = a.alloca(size);
+ // TODO: use ... ValueLayout?
+ MemorySegment addr = MemorySegment.allocateNative(size, 16, a);
int res;
- res = (int)getInfo.invokeExact(addr(), ctx.addr(), id, size, addr, MemoryAddress.NULL);
+ res = (int)getInfo.invokeExact(addr(), ctx.addr(), id, size, addr.address(), MemoryAddress.NULL);
if (res != 0)
throw new CLRuntimeException(res);
}
protected <T extends CLObject> int getInfoInt(T ctx, int id, MethodHandle getInfo) {
- try (Allocator a = Memory.stack()) {
- return getInt(getInfo(ctx, id, getInfo, a, 4));
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
+ return MemoryAccess.getInt(getInfo(ctx, id, getInfo, a, 4));
}
}
protected <T extends CLObject> long getInfoLong(T ctx, int id, MethodHandle getInfo) {
- try (Allocator a = Memory.stack()) {
- return getLong(getInfo(ctx, id, getInfo, a, 8));
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
+ return MemoryAccess.getLong(getInfo(ctx, id, getInfo, a, 8));
}
}
}
// new 6-param get-any
- protected <T extends CLObject> MemorySegment getInfoAny(T ctx, int id, MethodHandle getInfo, Allocator a) throws CLRuntimeException {
+ // TODO: pass type descriptor for alignment
+ protected <T extends CLObject> MemorySegment getInfoAny(T ctx, int id, MethodHandle getInfo, ResourceScope a) throws CLRuntimeException {
try {
- MemoryAddress sizep = a.alloca(8);
+ MemorySegment sizep = MemorySegment.allocateNative(CLinker.C_LONG, a);
MemorySegment valp;
long size;
int res;
- res = (int)getInfo.invokeExact(addr(), ctx.addr(), id, 0L, MemoryAddress.NULL, sizep);
+ res = (int)getInfo.invokeExact(addr(), ctx.addr(), id, 0L, MemoryAddress.NULL, sizep.address());
- size = getLong(sizep);
- valp = a.allocs(size);
+ size = MemoryAccess.getLong(sizep);
+ valp = MemorySegment.allocateNative(size, 16, a);
- res = (int)getInfo.invokeExact(addr(), ctx.addr(), id, size, valp.baseAddress(), sizep);
+ res = (int)getInfo.invokeExact(addr(), ctx.addr(), id, size, valp.address(), sizep.address());
if (res != 0)
throw new CLRuntimeException(res);
}
protected <T extends CLObject> byte[] getInfoByteV(T ctx, int id, MethodHandle getInfo) {
- try (Allocator a = Memory.stack()) {
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
MemorySegment seg = getInfoAny(ctx, id, getInfo, a);
return seg.toByteArray();
}
// clGet*Info includes terminating 0
static String infoToString(MemorySegment seg) {
- if (false) {
- MemoryAddress valp = seg.baseAddress();
+ if (true) {
+ return CLinker.toJavaString(seg);
+ } else if (false) {
+ MemoryAddress valp = seg.address();
byte[] val = new byte[(int)(seg.byteSize() - 1)];
for (int i = 0; i < val.length; i++)
}
protected <T extends CLObject> String getInfoString(T ctx, int id, MethodHandle getInfo) {
- try (Allocator a = Memory.stack()) {
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
MemorySegment seg = getInfoAny(ctx, id, getInfo, a);
return infoToString(seg);
}
}
protected <T extends CLObject> long[] getInfoLongV(T ctx, int id, MethodHandle getInfo) {
- try (Allocator a = Memory.stack()) {
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
MemorySegment valp = getInfoAny(ctx, id, getInfo, a);
- MemoryAddress val = valp.baseAddress();
int len = (int)(valp.byteSize() >>> 3);
long[] list = new long[len];
- for (int i = 0; i < list.length; i++)
- list[i] = getLong(val, i);
+ MemorySegment.ofArray(list).copyFrom(valp);
return list;
} catch (RuntimeException | Error t) {
}
// indexed version
- protected MemoryAddress getInfo(int index, int id, MethodHandle getInfo, Allocator a, long size) throws CLRuntimeException {
+ protected MemorySegment getInfo(int index, int id, MethodHandle getInfo, ResourceScope a, long size) throws CLRuntimeException {
try {
- MemoryAddress addr = a.alloca(size);
+ // TODO: type for alignment
+ MemorySegment addr = MemorySegment.allocateNative(size, 16, a);
int res = (int)getInfo.invokeExact(addr(), index, id, size, addr, MemoryAddress.NULL);
if (res != 0)
}
protected int getInfoInt(int index, int id, MethodHandle getInfo) {
- try (Allocator a = Memory.stack()) {
- return getInt(getInfo(index, id, getInfo, a, 4));
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
+ return MemoryAccess.getInt(getInfo(index, id, getInfo, a, 4));
}
}
protected long getInfoLong(int index, int id, MethodHandle getInfo) {
- try (Allocator a = Memory.stack()) {
- return getLong(getInfo(index, id, getInfo, a, 8));
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
+ return MemoryAccess.getLong(getInfo(index, id, getInfo, a, 8));
}
}
- protected MemorySegment getInfoAny(int index, int id, MethodHandle getInfo, Allocator a) throws CLRuntimeException {
+ protected MemorySegment getInfoAny(int index, int id, MethodHandle getInfo, ResourceScope a) throws CLRuntimeException {
try {
- MemoryAddress sizep = a.alloca(8);
+ MemorySegment sizep = MemorySegment.allocateNative(CLinker.C_LONG, a);
MemorySegment valp;
long size;
int res;
- res = (int)getInfo.invokeExact(addr(), index, id, 0L, MemoryAddress.NULL, sizep);
+ res = (int)getInfo.invokeExact(addr(), index, id, 0L, MemoryAddress.NULL, sizep.address());
- size = getLong(sizep);
- valp = a.allocs(size);
+ size = MemoryAccess.getLong(sizep);
+ valp = MemorySegment.allocateNative(size, a);
- res = (int)getInfo.invokeExact(addr(), index, id, size, valp.baseAddress(), sizep);
+ res = (int)getInfo.invokeExact(addr(), index, id, size, valp.address(), sizep.address());
if (res != 0)
throw new CLRuntimeException(res);
}
protected String getInfoString(int index, int id, MethodHandle getInfo) {
- try (Allocator a = Memory.stack()) {
+ try (ResourceScope a = ResourceScope.newConfinedScope()) {
MemorySegment valp = getInfoAny(index, id, getInfo, a);
return infoToString(valp);
}
* @throws CLRuntimeException
*/
public static CLPlatform[] getPlatforms() /*throws CLRuntimeException*/ {
- try (Allocator frame = Memory.stack()) {
- MemoryAddress lenp = frame.alloca(8);
- MemoryAddress list;
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment lenp = MemorySegment.allocateNative(CLinker.C_INT, scope);
+ MemorySegment list;
int len;
int res;
- res = (int)clGetPlatformIDs.invokeExact(0, MemoryAddress.NULL, lenp);
+ res = (int)clGetPlatformIDs.invokeExact(0, MemoryAddress.NULL, lenp.address());
if (res != 0)
throw new CLRuntimeException(res);
- len = Native.getInt(lenp);
- list = frame.alloca(8 * len);
+ len = MemoryAccess.getInt(lenp);
+ list = MemorySegment.allocateNative(MemoryLayout.sequenceLayout(len, CLinker.C_POINTER), scope);
- res = (int)clGetPlatformIDs.invokeExact(len, list, lenp);
+ res = (int)clGetPlatformIDs.invokeExact(len, list.address(), lenp.address());
+ if (res != 0)
+ throw new CLRuntimeException(res);
return toObjectV(list, new CLPlatform[len], CLPlatform::new);
} catch (RuntimeException | Error t) {
* @return List of matching devices in this platform.
* @throws CLRuntimeException
*/
- public CLDevice []getDevices(long type) /*throws CLRuntimeException*/ {
- try (Allocator frame = Memory.stack()) {
- MemoryAddress lenp = frame.alloca(8);
- MemoryAddress list;
+ public CLDevice[] getDevices(long type) /*throws CLRuntimeException*/ {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment lenp = MemorySegment.allocateNative(CLinker.C_INT, scope);
+ MemorySegment list;
int res, len;
- res = (int)clGetDeviceIDs.invokeExact(addr(), type, 0, MemoryAddress.NULL, lenp);
+ res = (int)clGetDeviceIDs.invokeExact(addr(), type, 0, MemoryAddress.NULL, lenp.address());
if (res == CL_DEVICE_NOT_FOUND)
return new CLDevice[0];
else if (res != 0)
throw new CLRuntimeException(res);
- len = Native.getInt(lenp);
- list = frame.alloca(len * 8);
+ len = MemoryAccess.getInt(lenp);
+ list = MemorySegment.allocateNative(MemoryLayout.sequenceLayout(len, CLinker.C_POINTER), scope);
- res = (int)clGetDeviceIDs.invokeExact(addr(), type, len, list, lenp);
+ res = (int)clGetDeviceIDs.invokeExact(addr(), type, len, list.address(), lenp.address());
+ if (res != 0)
+ throw new CLRuntimeException(res);
return toObjectV(list, new CLDevice[len], (d) -> new CLDevice(d, this));
} catch (RuntimeException | Error t) {
CLDevice best = null;
double bestScore = 0;
- for (CLPlatform p : platforms) {
+ for (CLPlatform p: platforms) {
CLDevice[] devs = p.getDevices(type);
- for (CLDevice d : devs) {
+ for (CLDevice d: devs) {
double score = scoreFunc.applyAsDouble(d);
if (best == null || score > bestScore) {
/**
* Calls clGetExtensionFunctionAddressForPlatform.If not available then
* it falls back to clGetExtensionFunctionAddress.
+ *
* @param name extension function name
* @return MemoryAddress of function entry point, or MemoryAddress.NULL.
*/
/**
* Get the platform api versiom.
- *
+ * <p>
* This may be compared to the version constants
* {@link #VERSION_1_0}, {@link #VERSION_1_1}, and so on.
*
/**
* Retrieve an extension by extension id.
- *
+ * <p>
* The extension id must be one of the supported extensions.
* This is called internally by the class/method to which the
- * extension applies. It should check the published list of
+ * extension applies. It should check the published list of
* extensions for validity and throw
* UnsupportedOperationException if it isn't.
- *
+ * <p>
* TODO: pass the class, constructor.invoke? id = getfield()?
*/
@SuppressWarnings("unchecked")
- <T> T getExtension(int id, Function<CLPlatform,T> create) {
+ <T> T getExtension(int id, Function<CLPlatform, T> create) {
synchronized (extension) {
T x = (T)extension[id];
return x;
}
}
+
+ public static void main(String[] args) {
+
+ for (CLPlatform p: getPlatforms()) {
+ System.out.println(p.getName());
+ p.getDevices(CL_DEVICE_TYPE_ALL);
+ }
+ }
}
import jdk.incubator.foreign.*;
import api.Native;
import api.Memory;
-import api.Allocator;
import api.Callback;
import java.lang.invoke.MethodHandle;
import java.util.stream.LongStream;
* @throws CLException
*/
public void buildProgram(CLDevice[] devices, String options, CLNotify<CLProgram> notify) throws CLException {
- try (Allocator frame = Memory.stack();
+ try(ResourceScope frame = ResourceScope.newConfinedScope();
Callback<CLNotify<CLProgram>> call = CLNotify.call(notify, (x) -> new CLProgram(x, getObjectPlatform()))) {
- MemoryAddress pdevs = toAddrV(frame, devices);
- MemoryAddress poptions = toByteV(frame, options);
+ MemorySegment pdevs = toAddrV(frame, devices);
+ MemorySegment poptions = options != null ? CLinker.toCString(options, frame) : MemorySegment.globalNativeSegment();
int res;
- res = clBuildProgram(addr(), devices.length, pdevs, poptions, call.addr(), MemoryAddress.NULL);
+ res = clBuildProgram(addr(), devices.length, pdevs.address(), poptions.address(), call.addr(), MemoryAddress.NULL);
if (res != 0)
throw new CLException(res);
} catch (CLException | RuntimeException | Error t) {
public void buildProgram(CLDevice[] devices, String options) throws CLException {
CLNotify<CLProgram> notify = (CLProgram source) -> {
- for (CLDevice d : devices) {
+ for (CLDevice d: devices) {
int status = source.getBuildStatus(d);
if (status != 0) {
System.err.printf("Build status: %d\n", status);
throw new IllegalArgumentException();
}
- try (Allocator frame = Memory.stack();
+ try(ResourceScope frame = ResourceScope.newConfinedScope();
Callback<CLNotify<CLProgram>> call = CLNotify.call(notify, (x) -> new CLProgram(x, getObjectPlatform()))) {
- MemoryAddress cdevs = toAddrV(frame, devices);
- MemoryAddress coptions = toByteV(frame, options);
- MemoryAddress cheaders = toAddrV(frame, headers);
- MemoryAddress cnames = Native.toAddrV(frame, header_names);
- MemoryAddress cres = frame.alloca(8);
+ MemorySegment cdevs = toAddrV(frame, devices);
+ MemorySegment poptions = CLinker.toCString(options, frame);
+ MemorySegment cheaders = toAddrV(frame, headers);
+ MemorySegment cnames = Native.toCStringV(header_names, frame);
int res;
- MemoryAddress ck;
- res = clCompileProgram(addr(), devices.length, cdevs, coptions, nheaders, cheaders, cnames, call.addr(), MemoryAddress.NULL);
+ res = clCompileProgram(addr(), devices.length, cdevs.address(), poptions.address(), nheaders, cheaders.address(), cnames.address(), call.addr(), MemoryAddress.NULL);
if (res != 0)
throw new CLException(res);
} catch (CLException | RuntimeException | Error t) {
* @throws CLRuntimeException
*/
public CLKernel createKernel(String name) throws CLException {
- try (Allocator frame = Memory.stack()) {
- MemoryAddress pres = frame.alloca(8);
- MemoryAddress pname = toByteV(frame, name);
+ try(ResourceScope frame = ResourceScope.newConfinedScope()) {
+ MemorySegment pres = MemorySegment.allocateNative(CLinker.C_INT, frame);
+ MemorySegment pname = CLinker.toCString(name, frame);
int res;
MemoryAddress ck;
- ck = clCreateKernel(addr(), pname, pres);
- res = getInt(pres);
- if (res != 0)
+ ck = clCreateKernel(addr(), pname.address(), pres.address());
+ if ((res = MemoryAccess.getInt(pres)) != 0)
throw new CLException(res);
return resolve(ck, (x) -> new CLKernel(x, getObjectPlatform()));
} catch (CLException | RuntimeException | Error t) {
public CLKernel[] createKernelsInProgram() throws CLRuntimeException {
int size = getNumKernels();
- try (api.Allocator a = api.Memory.stack()) {
- MemoryAddress csize = a.alloca(8);
- MemoryAddress ckern = a.alloca(8 * size);
+ try(ResourceScope frame = ResourceScope.newConfinedScope()) {
+ MemorySegment csize = MemorySegment.allocateNative(CLinker.C_INT, frame);
+ MemorySegment ckern = Native.allocAddrV(frame, size);
int res;
- res = clCreateKernelsInProgram(addr(), size, ckern, csize);
+ res = clCreateKernelsInProgram(addr(), size, ckern.address(), csize.address());
if (res != 0)
throw new CLRuntimeException();
- size = getInt(csize);
+ size = MemoryAccess.getInt(csize);
return Native.toObjectV(ckern, new CLKernel[size], (x) -> new CLKernel(x, getObjectPlatform()));
} catch (RuntimeException | Error t) {
throw t;
/* This is unnecessary since getBinaries() is the only thing that needs it
public long[] getBinarySizes() { }
- */
-
+ */
public byte[][] getBinaries() {
long[] sizes = getInfoSizeTA(CL_PROGRAM_BINARY_SIZES);
long size = LongStream.of(sizes).sum();
- try (api.Allocator a = api.Memory.stack();
- MemorySegment seg = MemorySegment.allocateNative(size)) {
- MemoryAddress data = seg.baseAddress();
- MemoryAddress cptrs = a.alloca(sizes.length * 8);
+ try (ResourceScope frame = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(size, frame);
+ MemoryAddress data = seg.address();
+ MemorySegment cptrs = Native.allocAddrV(frame, sizes.length);
long off = 0;
int res;
- for (int i=0;i<sizes.length;i++) {
- setAddr(cptrs, i, data.addOffset(off));
+ for (int i = 0; i < sizes.length; i++) {
+ MemoryAccess.setAddressAtIndex(cptrs, i, data.addOffset(off));
off += sizes[i];
}
- res = clGetProgramInfo(addr(), CL_PROGRAM_BINARIES, sizes.length * 8L, cptrs, MemoryAddress.NULL);
+ res = clGetProgramInfo(addr(), CL_PROGRAM_BINARIES, sizes.length * CLinker.C_POINTER.byteSize(), cptrs.address(), MemoryAddress.NULL);
if (res != 0)
throw new CLRuntimeException(res);
byte[][] out = new byte[sizes.length][];
off = 0;
- for (int i=0;i<sizes.length;i++) {
+ for (int i = 0; i < sizes.length; i++) {
out[i] = seg.asSlice(off, sizes[i]).toByteArray();
off += sizes[i];
}
*/
package au.notzed.zcl;
-import api.Native;
import static au.notzed.zcl.CL.*;
-import api.Allocator;
-import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemorySegment;
import java.util.function.BiFunction;
import java.util.function.IntFunction;
import java.util.ArrayList;
+import jdk.incubator.foreign.MemoryAccess;
+import jdk.incubator.foreign.ResourceScope;
/**
* All property arrays implement this interface.
* @param o offset
* @return updated o = (o + getSize())
*/
- int toInt(MemoryAddress dst, int o);
+ int toInt(MemorySegment dst, int o);
/**
* Encode this property as a long poke.
* @param o offset
* @return updated o = (o + getSize()))
*/
- int toLong(MemoryAddress dst, int o);
+ int toLong(MemorySegment dst, int o);
/**
* A simple tag/value property type.
}
@Override
- public int toInt(MemoryAddress dst, int o) {
- Native.setInt(dst, o++, (int)tag);
- Native.setInt(dst, o++, (int)value);
+ public int toInt(MemorySegment dst, int o) {
+ MemoryAccess.setIntAtIndex(dst, o++, (int)tag);
+ MemoryAccess.setIntAtIndex(dst, o++, (int)value);
return o;
}
@Override
- public int toLong(MemoryAddress dst, int o) {
- Native.setLong(dst, o++, tag);
- Native.setLong(dst, o++, value);
+ public int toLong(MemorySegment dst, int o) {
+ MemoryAccess.setLongAtIndex(dst, o++, tag);
+ MemoryAccess.setLongAtIndex(dst, o++, value);
return o;
}
return size + 1;
}
- static <T extends CLProperty> MemoryAddress toNative(Allocator frame, T[] properties) {
+ static <T extends CLProperty> MemorySegment toNative(ResourceScope frame, T[] properties) {
if (properties != null && properties.length > 0) {
- MemoryAddress addr = frame.alloca(getSize(properties) * 8);
+ MemorySegment addr = MemorySegment.allocateNative(getSize(properties) * 8, 8, frame);
int i = 0;
for (CLProperty p: properties)
i = p.toLong(addr, i);
- Native.setLong(addr, i, 0L);
+ MemoryAccess.setLongAtIndex(addr, i, 0L);
return addr;
} else {
- MemoryAddress addr = frame.alloca(8);
- Native.setLong(addr, 0L);
+ MemorySegment addr = MemorySegment.allocateNative(8, 8, frame);
+ MemoryAccess.setLong(addr, 0L);
return addr;
-
- //return MemoryAddress.NULL;
}
}
static <T extends CLProperty> T[] fromNative(MemorySegment seg, BiFunction<Long,Long,T> create, IntFunction<T[]> createArray) {
- MemoryAddress add = seg.baseAddress();
ArrayList<T> list = new ArrayList<>();
long tag;
- for (int i = 0;(tag = Native.getLong(add, i++)) != 0;) {
+ for (int i = 0;(tag = MemoryAccess.getLongAtIndex(seg, i++)) != 0;) {
// HACK: this needs some other mechanism, just hardcode each for now
if (tag == CL_DEVICE_PARTITION_BY_COUNTS) {
throw new UnsupportedOperationException();
} else {
- long value = Native.getLong(add, i++);
+ long value = MemoryAccess.getLongAtIndex(seg, i++);
list.add(create.apply(tag, value));
}
package au.notzed.zcl;
import jdk.incubator.foreign.*;
-import au.notzed.zcl.CLEventList;
-import api.Allocator;
/**
* Simplify wait/event handling.
public final MemoryAddress wait;
public final MemoryAddress event;
- public EventInfo(Allocator frame, CLEventList waiters, CLEventList events) {
+ // TODO: frame isn't used, maybe it should be removed? if so check callers where scope isn't doing anyting
+ public EventInfo(ResourceScope frame, CLEventList waiters, CLEventList events) {
nwait = waiters != null ? waiters.size() : 0;
wait = nwait > 0 ? waiters.slots() : MemoryAddress.NULL;
event = events != null ? events.currentSlot() : MemoryAddress.NULL;
* Implements a java-friendly binding to OpenCL.
*/
module notzed.zcl {
- requires transitive jdk.incubator.foreign;
+ requires jdk.incubator.foreign;
exports au.notzed.zcl;
if (@libs) {
# static function handle init
print $dst "\tstatic {\n";
- print $dst "\t\tLibraryLookup[] ctx = Native.loadLibraries(libraries);\n";
+ print $dst "\t\tNative.loadLibraries(libraries);\n";
+ print $dst "\t\tSymbolLookup ctx = SymbolLookup.loaderLookup();\n";
} else {
# address factory handle init
print $dst "\tpublic $class{name}(Function<String,MemoryAddress> ctx) {\n";
-
package au.notzed.zcl;
import org.junit.*;
*/
public class CLBufferTest {
+
boolean haveCL() {
CLPlatform[] list = CLPlatform.getPlatforms();
return list != null && list.length > 0;
org.junit.Assume.assumeTrue(haveCL());
plat = CLPlatform.getPlatforms()[0];
- devs = new CLDevice[] { plat.getDevices(CL_DEVICE_TYPE_ALL)[0] };
+ devs = new CLDevice[]{plat.getDevices(CL_DEVICE_TYPE_ALL)[0]};
cl = CLContext.createContext(null, devs);
q = cl.createCommandQueue(devs[0], 0);
}
+
@After
public void shutdown() {
CLObject.release(q, cl);
long[] flags = {
CL_MEM_READ_WRITE,
CL_MEM_WRITE_ONLY,
- CL_MEM_READ_ONLY,
- };
+ CL_MEM_READ_ONLY,};
for (long flag: flags) {
CLBuffer b = cl.createBuffer(flag, 1024);
byte[] filla = "opencl ftw!".getBytes();
static void fillSegment(MemorySegment seg, byte[] seq) {
- MemoryAddress add = seg.baseAddress();
- for (long i=0;i<seg.byteSize();i++) {
- Native.setByte(add, i, seq[(int)(i % seq.length)]);
+ for (long i = 0; i < seg.byteSize(); i++) {
+ MemoryAccess.setByteAtOffset(seg, i, seq[(int)(i % seq.length)]);
}
}
static boolean equalSegment(MemorySegment a, MemorySegment b) {
boolean same = true;
- MemoryAddress c = a.baseAddress();
- MemoryAddress d = b.baseAddress();
- for (long i=0;same && i<a.byteSize();i++) {
- same &= Native.getByte(c, i) == Native.getByte(d, i);
+ for (long i = 0; same && i < a.byteSize(); i++) {
+ same &= MemoryAccess.getByteAtOffset(a, i) == MemoryAccess.getByteAtOffset(b, i);
}
return same;
}
static boolean equal(ByteBuffer a, MemorySegment b) {
boolean same = true;
- MemoryAddress d = b.baseAddress();
+ MemoryAddress d = b.address();
- for (long i=0;same && i<a.capacity();i++) {
+ for (long i = 0; same && i < a.capacity(); i++) {
same &= a.get((int)i) == Native.getByte(d, i);
}
return same;
public void testCreate4() throws Exception {
System.out.println("createBuffer COPY_HOST_PTR");
- try (MemorySegment sega = MemorySegment.allocateNative(1024, 8);
- MemorySegment segb = MemorySegment.allocateNative(1024, 8)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment sega = MemorySegment.allocateNative(1024, 8, scope);
+ MemorySegment segb = MemorySegment.allocateNative(1024, 8, scope);
fillSegment(sega, filla);
CLBuffer b = cl.createBuffer(CL_MEM_COPY_HOST_PTR, 1024, sega);
assertEquals(CLRuntimeException.class, x.getClass());
assertEquals(CL_INVALID_HOST_PTR, ((CLRuntimeException)x).err);
- try (MemorySegment seg = MemorySegment.allocateNative(8, 8)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(8, 8, scope);
x = null;
cl.createBuffer(0, 8, seg);
} catch (Throwable t) {
assertEquals(CLRuntimeException.class, x.getClass());
assertEquals(CL_INVALID_HOST_PTR, ((CLRuntimeException)x).err);
- try (MemorySegment seg = MemorySegment.allocateNative(8, 8)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(8, 8, scope);
x = null;
cl.createBuffer(0, seg);
} catch (Throwable t) {
Throwable x;
System.out.println("createBuffer sized segment");
- try (MemorySegment seg = MemorySegment.allocateNative(720, 8)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(720, 8, scope);
CLBuffer b = cl.createBuffer(CL_MEM_COPY_HOST_PTR, seg);
assertEquals(seg.byteSize(), b.getSize());
@Test
public void testMap1() throws Exception {
System.out.println("mapBuffer");
- try (MemorySegment sega = MemorySegment.allocateNative(1024, 8)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment sega = MemorySegment.allocateNative(1024, 8, scope);
fillSegment(sega, filla);
CLBuffer b = cl.createBuffer(CL_MEM_COPY_HOST_PTR, 1024, sega);
@Test
public void testMap2() throws Exception {
System.out.println("mapBuffer double unmap");
- try (MemorySegment sega = MemorySegment.allocateNative(1024, 8)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment sega = MemorySegment.allocateNative(1024, 8, scope);
fillSegment(sega, filla);
CLBuffer b = cl.createBuffer(CL_MEM_COPY_HOST_PTR, 1024, sega);
public void testFill1() throws Exception {
org.junit.Assume.assumeTrue(plat.getAPIVersion() >= CLPlatform.VERSION_1_2);
- byte[] data = new byte[] { 1, 2, 3, 4 };
+ byte[] data = new byte[]{1, 2, 3, 4};
- try (MemorySegment seg = MemorySegment.allocateNative(1024, 8)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(1024, 8, scope);
CLBuffer b = cl.createBuffer(0, 1024);
q.enqueueFillBuffer(b, data, 0, 256, null, null);
q.enqueueReadBuffer(b, true, 0, seg.byteSize(), seg, null, null);
boolean same = true;
- MemoryAddress add = seg.baseAddress();
- for (int i=0;same && i<1024;i++) {
+ MemoryAddress add = seg.address();
+ for (int i = 0; same && i < 1024; i++) {
same = Native.getByte(add, i) == data[i % data.length];
}
-
package au.notzed.zcl;
import org.junit.*;
CLEvent and CLEventList tests
*/
public class CLEventTest {
+
boolean haveCL() {
CLPlatform[] list = CLPlatform.getPlatforms();
return list != null && list.length > 0;
org.junit.Assume.assumeTrue(haveCL());
plat = CLPlatform.getPlatforms()[0];
- devs = new CLDevice[] { plat.getDevices(CL_DEVICE_TYPE_ALL)[0] };
+ devs = new CLDevice[]{plat.getDevices(CL_DEVICE_TYPE_ALL)[0]};
cl = CLContext.createContext(null, devs);
q = cl.createCommandQueue(devs[0], 0);
}
+
@After
public void shutdown() {
q.release();
* I think this should pass but it times out on Mesa 19.
*/
@Ignore
- @Test(timeout=1000)
+ @Test(timeout = 1000)
public void testUserWait() throws Exception {
org.junit.Assume.assumeTrue(plat.getAPIVersion() >= CLPlatform.VERSION_1_1);
System.out.println("UserEvent wait");
CLEvent ev = cl.createUserEvent();
CLBuffer mem = cl.createBuffer(0, 64);
- try (MemorySegment seg = MemorySegment.allocateNative(64, 8)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope();
CLEventList list = new CLEventList(1);
- CLEventList wait = new CLEventList(1);
+ CLEventList wait = new CLEventList(1)) {
+ MemorySegment seg = MemorySegment.allocateNative(64, 8, scope);
wait.add(ev);
assertEquals(ev, wait.get(0));
System.out.println("event callback, state");
CLBuffer mem = cl.createBuffer(0, 64);
- try (MemorySegment seg = MemorySegment.allocateNative(64, 8)) {
- CLEventList list = new CLEventList(1);
+ try (ResourceScope scope = ResourceScope.newConfinedScope();
+ CLEventList list = new CLEventList(1)) {
+ MemorySegment seg = MemorySegment.allocateNative(64, 8, scope);
CLEvent ev;
q.enqueueReadBuffer(mem, true, 0, 64, seg, null, list);
assertEquals(CL_COMMAND_READ_BUFFER, ev.getCommandType());
ev.setEventCallback(CL_SUBMITTED, (e, s) -> {
- assertTrue(s <= CL_SUBMITTED);
- count[0] += 1;
- });
+ assertTrue(s <= CL_SUBMITTED);
+ count[0] += 1;
+ });
ev.setEventCallback(CL_RUNNING, (e, s) -> {
- assertTrue(s <= CL_RUNNING);
- count[0] += 1;
- });
+ assertTrue(s <= CL_RUNNING);
+ count[0] += 1;
+ });
ev.setEventCallback(CL_COMPLETE, (e, s) -> {
- assertTrue(s <= CL_COMPLETE);
- count[0] += 1;
- });
+ assertTrue(s <= CL_COMPLETE);
+ count[0] += 1;
+ });
q.finish();
assertEquals(IllegalStateException.class, x.getClass());
CLBuffer mem = cl.createBuffer(0, 64);
- try (MemorySegment seg = MemorySegment.allocateNative(64, 8)) {
+ try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ MemorySegment seg = MemorySegment.allocateNative(64, 8, scope);
x = null;
q.enqueueReadBuffer(mem, false, 0, 64, seg, null, list);
} catch (Throwable t) {
public void testMulti() throws Exception {
System.out.println("multi");
CLBuffer mem = cl.createBuffer(0, 64);
- try (MemorySegment seg = MemorySegment.allocateNative(64, 8);
+ try (ResourceScope scope = ResourceScope.newConfinedScope();
CLEventList list = new CLEventList(2);
CLEventList last = new CLEventList(1)) {
+ MemorySegment seg = MemorySegment.allocateNative(64, 8, scope);
q.enqueueReadBuffer(mem, false, 0, 64, seg, null, list);
assertEquals(1, list.size());
}
}
- @Test(timeout=1000)
+ @Test(timeout = 1000)
public void testMultiWait() throws Exception {
System.out.println("multi wait");
CLBuffer mem = cl.createBuffer(0, 64);
- try (MemorySegment seg = MemorySegment.allocateNative(64, 8);
+ try (ResourceScope scope = ResourceScope.newConfinedScope();
CLEventList list = new CLEventList(3)) {
+ MemorySegment seg = MemorySegment.allocateNative(64, 8, scope);
q.enqueueReadBuffer(mem, false, 0, 64, seg, null, list);
assertEquals(1, list.size());