Skip to content

Commit

Permalink
Make shm the third, not first, NDArray attribute
Browse files Browse the repository at this point in the history
So that it matches the Python implementation of Appose.
The reason it's third in Python is so that it can have the default
parameter value of None, to provide feature parity with the Java
version's two-argument NDAray constructor that creates the SharedMemory.
  • Loading branch information
ctrueden committed Jul 15, 2024
1 parent 77fd98f commit c1a1194
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
31 changes: 16 additions & 15 deletions src/main/java/org/apposed/appose/NDArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class NDArray implements AutoCloseable {
/**
* shared memory containing the flattened array data.
*/
private final SharedMemory sharedMemory;
private final SharedMemory shm;

/**
* data type of the array elements.
Expand All @@ -56,14 +56,15 @@ public class NDArray implements AutoCloseable {
private final Shape shape;

/**
* Constructs an {@code NDArray} with the specified {@code SharedMemory}.
* Constructs an {@code NDArray} with the specified data type, shape,
* and {@code SharedMemory}.
*
* @param sharedMemory the flattened array data.
* @param dType element data type
* @param shape array shape
* @param shm the flattened array data.
*/
public NDArray(final SharedMemory sharedMemory, final DType dType, final Shape shape) {
this.sharedMemory = sharedMemory;
public NDArray(final DType dType, final Shape shape, final SharedMemory shm) {
this.shm = shm;
this.dType = dType;
this.shape = shape;
}
Expand All @@ -76,8 +77,8 @@ public NDArray(final SharedMemory sharedMemory, final DType dType, final Shape s
* @param shape array shape
*/
public NDArray(final DType dType, final Shape shape) {
this(SharedMemory.create(null,
safeInt(shape.numElements() * dType.bytesPerElement())), dType, shape);
this(dType, shape, SharedMemory.create(null,
safeInt(shape.numElements() * dType.bytesPerElement())));
}

/**
Expand All @@ -98,7 +99,7 @@ public Shape shape() {
* @return The shared memory block containing the array data.
*/
public SharedMemory shm() {
return sharedMemory;
return shm;
}

/**
Expand All @@ -108,24 +109,24 @@ public SharedMemory shm() {
*/
public ByteBuffer buffer() {
final long length = shape.numElements() * dType.bytesPerElement();
return sharedMemory.pointer().getByteBuffer(0, length);
return shm.pointer().getByteBuffer(0, length);
}

/**
* Release resources ({@code SharedMemory}) associated with this {@code NDArray}.
*/
@Override
public void close() {
sharedMemory.close();
shm.close();
}

@Override
public String toString() {
return "NDArray{" +
"sharedMemory=" + sharedMemory +
", dType=" + dType +
", shape=" + shape +
'}';
return "NDArray(" +
"dType=" + dType +
", shape=" + shape +
", shm=" + shm +
")";
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apposed/appose/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ private static Object processValue(Object value) {
final int size = (int) map.get("size");
return SharedMemory.attach(name, size);
case "ndarray":
final SharedMemory shm = (SharedMemory) map.get("shm");
final NDArray.DType dType = toDType((String) map.get("dtype"));
final NDArray.Shape shape = toShape((List<Integer>) map.get("shape"));
return new NDArray(shm, dType, shape);
final SharedMemory shm = (SharedMemory) map.get("shm");
return new NDArray(dType, shape, shm);
default:
System.err.println("unknown appose_type \"" + appose_type + "\"");
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/apposed/appose/NDArrayExampleGroovy.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public static void main(String[] args) throws Exception {
// create a FLOAT32 NDArray with shape (4,3,2) in F_ORDER
// respectively (2,3,4) in C_ORDER
final NDArray.DType dType = NDArray.DType.FLOAT32;
final NDArray ndArray = new NDArray(dType, new NDArray.Shape(F_ORDER, 4, 3, 2));
final NDArray.Shape shape = new NDArray.Shape(F_ORDER, 4, 3, 2);
final NDArray ndArray = new NDArray(dType, shape);

// fill with values 0..23 in flat iteration order
final FloatBuffer buf = ndArray.buffer().asFloatBuffer();
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/apposed/appose/NDArrayExamplePython.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public static void main(String[] args) throws Exception {
// create a FLOAT32 NDArray with shape (4,3,2) in F_ORDER
// respectively (2,3,4) in C_ORDER
final NDArray.DType dType = NDArray.DType.FLOAT32;
final NDArray ndArray = new NDArray(dType, new NDArray.Shape(F_ORDER, 4, 3, 2));
final NDArray.Shape = new NDArray.Shape(F_ORDER, 4, 3, 2);
final NDArray ndArray = new NDArray(dType, shape);

// fill with values 0..23 in flat iteration order
final FloatBuffer buf = ndArray.buffer().asFloatBuffer();
Expand Down

0 comments on commit c1a1194

Please sign in to comment.