From 45a1a14bf7eaabc256e5ed61ba3bb6413e176044 Mon Sep 17 00:00:00 2001 From: carlosuc3m <100329787@alumnos.uc3m.es> Date: Fri, 20 Dec 2024 13:02:21 +0100 Subject: [PATCH] correct tricky bug caused by discrete shared memory pages in mac and win --- .../tensor/shm/SharedMemoryArrayLinux.java | 11 +++++++---- .../tensor/shm/SharedMemoryArrayMacOS.java | 11 +++++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/bioimage/modelrunner/tensor/shm/SharedMemoryArrayLinux.java b/src/main/java/io/bioimage/modelrunner/tensor/shm/SharedMemoryArrayLinux.java index ffbfbd89..09c8ed0f 100644 --- a/src/main/java/io/bioimage/modelrunner/tensor/shm/SharedMemoryArrayLinux.java +++ b/src/main/java/io/bioimage/modelrunner/tensor/shm/SharedMemoryArrayLinux.java @@ -897,12 +897,15 @@ public ByteBuffer getDataBuffer() { */ public ByteBuffer getDataBufferNoHeader() { int offset = 0; + long totSize = this.size; + for (long l : this.originalDims) + totSize *= l; if (this.isNumpyFormat()) { - long flatSize = 1; - for (long l : this.originalDims) flatSize *= l; - offset = (int) (this.size - DecodeNumpy.DATA_TYPES_MAP.get(this.originalDataType) * flatSize); + long npSize = DecodeNumpy.calculateNpyStyleByteArrayLength(originalDims, Cast.unchecked(CommonUtils.getImgLib2DataType(originalDataType)), this.isFortran); + offset = (int) (npSize - DecodeNumpy.DATA_TYPES_MAP.get(this.originalDataType) * totSize); + totSize = npSize; } - return pSharedMemory.getByteBuffer(offset, this.size - offset); + return pSharedMemory.getByteBuffer(offset, totSize - offset); } private static & NativeType> diff --git a/src/main/java/io/bioimage/modelrunner/tensor/shm/SharedMemoryArrayMacOS.java b/src/main/java/io/bioimage/modelrunner/tensor/shm/SharedMemoryArrayMacOS.java index ea63242b..f40a77c4 100644 --- a/src/main/java/io/bioimage/modelrunner/tensor/shm/SharedMemoryArrayMacOS.java +++ b/src/main/java/io/bioimage/modelrunner/tensor/shm/SharedMemoryArrayMacOS.java @@ -837,12 +837,15 @@ public ByteBuffer getDataBuffer() { */ public ByteBuffer getDataBufferNoHeader() { int offset = 0; + long totSize = this.size; + for (long l : this.originalDims) + totSize *= l; if (this.isNumpyFormat()) { - long flatSize = 1; - for (long l : this.originalDims) flatSize *= l; - offset = (int) (this.size - DecodeNumpy.DATA_TYPES_MAP.get(this.originalDataType) * flatSize); + long npSize = DecodeNumpy.calculateNpyStyleByteArrayLength(originalDims, Cast.unchecked(CommonUtils.getImgLib2DataType(originalDataType)), this.isFortran); + offset = (int) (npSize - DecodeNumpy.DATA_TYPES_MAP.get(this.originalDataType) * totSize); + totSize = npSize; } - return pSharedMemory.getByteBuffer(offset, this.size - offset); + return pSharedMemory.getByteBuffer(offset, totSize - offset); } private static & NativeType>