From 3f38900c33425e198f989714b4df643c1d1a4114 Mon Sep 17 00:00:00 2001 From: raver119 Date: Wed, 20 Nov 2019 07:43:17 +0300 Subject: [PATCH] J9+ -> J8 ByteBuffer fix (#59) Signed-off-by: raver119 --- .../nd4j/linalg/compression/CompressionDescriptor.java | 3 ++- .../src/main/java/org/nd4j/linalg/factory/Nd4j.java | 3 ++- .../src/main/java/org/nd4j/serde/binary/BinarySerde.java | 9 +++++---- .../org/nd4j/nativeblas/BaseNativeNDArrayFactory.java | 9 +++++---- .../java/org/nd4j/aeron/ipc/AeronNDArrayPublisher.java | 3 ++- .../src/main/java/org/nd4j/aeron/ipc/NDArrayMessage.java | 5 +++-- 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/compression/CompressionDescriptor.java b/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/compression/CompressionDescriptor.java index 89a1ac4a3..42f9153eb 100644 --- a/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/compression/CompressionDescriptor.java +++ b/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/compression/CompressionDescriptor.java @@ -21,6 +21,7 @@ import org.nd4j.linalg.api.buffer.DataBuffer; import org.nd4j.linalg.api.buffer.DataType; import java.io.Serializable; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -142,7 +143,7 @@ public class CompressionDescriptor implements Cloneable, Serializable { directAlloc.putLong(numberOfElements); directAlloc.putLong(originalElementSize); directAlloc.putInt(originalDataType.ordinal()); - directAlloc.rewind(); + ((Buffer) directAlloc).rewind(); return directAlloc; } diff --git a/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/factory/Nd4j.java b/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/factory/Nd4j.java index c95dc5ef2..5e62dd198 100644 --- a/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/factory/Nd4j.java +++ b/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/factory/Nd4j.java @@ -93,6 +93,7 @@ import org.nd4j.versioncheck.VersionCheck; import java.io.*; import java.lang.reflect.Constructor; import java.math.BigDecimal; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.WritableByteChannel; @@ -5681,7 +5682,7 @@ public class Nd4j { public static INDArray createNpyFromByteArray(@NonNull byte[] input) { ByteBuffer byteBuffer = ByteBuffer.allocateDirect(input.length); byteBuffer.put(input); - byteBuffer.rewind(); + ((Buffer) byteBuffer).rewind(); Pointer pointer = new Pointer(byteBuffer); return createFromNpyPointer(pointer); } diff --git a/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/serde/binary/BinarySerde.java b/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/serde/binary/BinarySerde.java index 60bb0378b..ecc4cfe18 100644 --- a/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/serde/binary/BinarySerde.java +++ b/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/serde/binary/BinarySerde.java @@ -31,6 +31,7 @@ import org.nd4j.linalg.factory.Nd4j; import org.nd4j.linalg.primitives.Pair; import java.io.*; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.channels.Channels; @@ -215,7 +216,7 @@ public class BinarySerde { allocated.put(shapeBuffer); allocated.put(buffer); if (rewind) - allocated.rewind(); + ((Buffer) allocated).rewind(); } /** @@ -247,7 +248,7 @@ public class BinarySerde { //finally put the data allocated.put(buffer); if (rewind) - allocated.rewind(); + ((Buffer) allocated).rewind(); } @@ -314,7 +315,7 @@ public class BinarySerde { ByteBuffer byteBuffer = buffer.order(ByteOrder.nativeOrder()); - buffer.position(0); + ((Buffer) buffer).position(0); int rank = byteBuffer.getInt(); val result = new long[Shape.shapeInfoLength(rank)]; @@ -324,7 +325,7 @@ public class BinarySerde { // skipping two next values (dtype and rank again) // please , that this time rank has dtype of LONG, so takes 8 bytes. - byteBuffer.position(16); + ((Buffer) byteBuffer).position(16); // filling shape information for (int e = 1; e < Shape.shapeInfoLength(rank); e++) { diff --git a/nd4j/nd4j-backends/nd4j-api-parent/nd4j-native-api/src/main/java/org/nd4j/nativeblas/BaseNativeNDArrayFactory.java b/nd4j/nd4j-backends/nd4j-api-parent/nd4j-native-api/src/main/java/org/nd4j/nativeblas/BaseNativeNDArrayFactory.java index 153183f57..f94ead011 100644 --- a/nd4j/nd4j-backends/nd4j-api-parent/nd4j-native-api/src/main/java/org/nd4j/nativeblas/BaseNativeNDArrayFactory.java +++ b/nd4j/nd4j-backends/nd4j-api-parent/nd4j-native-api/src/main/java/org/nd4j/nativeblas/BaseNativeNDArrayFactory.java @@ -36,6 +36,7 @@ import org.nd4j.linalg.util.ArrayUtil; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.Charset; @@ -492,8 +493,8 @@ public abstract class BaseNativeNDArrayFactory extends BaseNDArrayFactory { byte[] pathBytes = file.getAbsolutePath().getBytes(Charset.forName("UTF-8")); ByteBuffer directBuffer = ByteBuffer.allocateDirect(pathBytes.length).order(ByteOrder.nativeOrder()); directBuffer.put(pathBytes); - directBuffer.rewind(); - directBuffer.position(0); + ((Buffer) directBuffer).rewind(); + ((Buffer) directBuffer).position(0); Pointer pointer = nativeOps.numpyFromFile(new BytePointer(directBuffer)); INDArray result = createFromNpyPointer(pointer); @@ -672,8 +673,8 @@ public abstract class BaseNativeNDArrayFactory extends BaseNDArrayFactory { byte[] pathBytes = file.getAbsolutePath().getBytes(Charset.forName("UTF-8")); ByteBuffer directBuffer = ByteBuffer.allocateDirect(pathBytes.length).order(ByteOrder.nativeOrder()); directBuffer.put(pathBytes); - directBuffer.rewind(); - directBuffer.position(0); + ((Buffer) directBuffer).rewind(); + ((Buffer) directBuffer).position(0); Pointer pointer = nativeOps.mapFromNpzFile(new BytePointer(directBuffer)); int n = nativeOps.getNumNpyArraysInMap(pointer); HashMap map = new HashMap<>(); diff --git a/nd4j/nd4j-serde/nd4j-aeron/src/main/java/org/nd4j/aeron/ipc/AeronNDArrayPublisher.java b/nd4j/nd4j-serde/nd4j-aeron/src/main/java/org/nd4j/aeron/ipc/AeronNDArrayPublisher.java index 53cc03e70..6aaeae42d 100644 --- a/nd4j/nd4j-serde/nd4j-aeron/src/main/java/org/nd4j/aeron/ipc/AeronNDArrayPublisher.java +++ b/nd4j/nd4j-serde/nd4j-aeron/src/main/java/org/nd4j/aeron/ipc/AeronNDArrayPublisher.java @@ -32,6 +32,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.Closeable; +import java.nio.Buffer; import java.nio.ByteBuffer; /** @@ -129,7 +130,7 @@ public class AeronNDArrayPublisher implements AutoCloseable { NDArrayMessageChunk[] chunks = NDArrayMessage.chunks(message, publication.maxMessageLength() / 128); for (int i = 0; i < chunks.length; i++) { ByteBuffer sendBuff = NDArrayMessageChunk.toBuffer(chunks[i]); - sendBuff.rewind(); + ((Buffer) sendBuff).rewind(); DirectBuffer buffer = new UnsafeBuffer(sendBuff); sendBuffer(buffer); } diff --git a/nd4j/nd4j-serde/nd4j-aeron/src/main/java/org/nd4j/aeron/ipc/NDArrayMessage.java b/nd4j/nd4j-serde/nd4j-aeron/src/main/java/org/nd4j/aeron/ipc/NDArrayMessage.java index 7a0612982..469bf805b 100644 --- a/nd4j/nd4j-serde/nd4j-aeron/src/main/java/org/nd4j/aeron/ipc/NDArrayMessage.java +++ b/nd4j/nd4j-serde/nd4j-aeron/src/main/java/org/nd4j/aeron/ipc/NDArrayMessage.java @@ -28,6 +28,7 @@ import org.nd4j.linalg.api.ndarray.INDArray; import org.nd4j.linalg.factory.Nd4j; import java.io.Serializable; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.time.Instant; @@ -229,7 +230,7 @@ public class NDArrayMessage implements Serializable { for (int i = 0; i < chunks.length; i++) { ByteBuffer curr = chunks[i].getData(); if (curr.capacity() > chunks[0].getChunkSize()) { - curr.position(0).limit(chunks[0].getChunkSize()); + ((Buffer) curr).position(0).limit(chunks[0].getChunkSize()); curr = curr.slice(); } all.put(curr); @@ -311,7 +312,7 @@ public class NDArrayMessage implements Serializable { //rewind the buffer before putting it in to the unsafe buffer //note that we set rewind to false in the do byte buffer put methods - byteBuffer.rewind(); + ((Buffer) byteBuffer).rewind(); return new UnsafeBuffer(byteBuffer); }