From fa98b8329519a033330b91719f38b08729d04b18 Mon Sep 17 00:00:00 2001 From: Robert Altena Date: Fri, 26 Jul 2019 11:28:51 +0900 Subject: [PATCH] remove duplicate code in createBufferDetached. (#83) Signed-off-by: Robert Altena --- .../java/org/nd4j/linalg/factory/Nd4j.java | 62 +++++-------------- 1 file changed, 16 insertions(+), 46 deletions(-) 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 260a0198e..82dd64337 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 @@ -1389,51 +1389,6 @@ public class Nd4j { return Nd4j.getMemoryManager().getCurrentWorkspace() == null ? DATA_BUFFER_FACTORY_INSTANCE.createFloat(length, true) : DATA_BUFFER_FACTORY_INSTANCE.createFloat(length, true, Nd4j.getMemoryManager().getCurrentWorkspace()); } - /** - * Create a buffer equal of length prod(shape). The buffer is 'detached': Not in any memory workspace even if a - * workspace is currently open. - * - * @param shape the shape of the buffer to create - * @param type the opType to create - * @return - */ - public static DataBuffer createBufferDetached(int[] shape, DataType type) { - long length = ArrayUtil.prodLong(shape); - switch (type){ - case DOUBLE: - return DATA_BUFFER_FACTORY_INSTANCE.createDouble(length); - case FLOAT: - return DATA_BUFFER_FACTORY_INSTANCE.createFloat(length); - case HALF: - return DATA_BUFFER_FACTORY_INSTANCE.createHalf(length); - case BFLOAT16: - return DATA_BUFFER_FACTORY_INSTANCE.createBFloat16(length); - case UINT64: - return DATA_BUFFER_FACTORY_INSTANCE.createULong(length); - case LONG: - return DATA_BUFFER_FACTORY_INSTANCE.createLong(length); - case UINT32: - return DATA_BUFFER_FACTORY_INSTANCE.createUInt(length); - case INT: - return DATA_BUFFER_FACTORY_INSTANCE.createInt(length); - case UINT16: - return DATA_BUFFER_FACTORY_INSTANCE.createUShort(length); - case SHORT: - return DATA_BUFFER_FACTORY_INSTANCE.createShort(length); - case UBYTE: - return DATA_BUFFER_FACTORY_INSTANCE.createUByte(length); - case BYTE: - return DATA_BUFFER_FACTORY_INSTANCE.createByte(length); - case BOOL: - return DATA_BUFFER_FACTORY_INSTANCE.createBool(length); - case UTF8: - case COMPRESSED: - case UNKNOWN: - default: - throw new UnsupportedOperationException("Cannot create type: " + type); - } - } - /** * See {@link #createBuffer(int[], DataType)} */ @@ -1475,12 +1430,27 @@ public class Nd4j { } } + /** + * Create a buffer equal of length prod(shape). The buffer is 'detached': Not in any memory workspace even if a + * workspace is currently open. + * + * @param shape the shape of the buffer to create + * @param type the opType to create + * @return + */ + public static DataBuffer createBufferDetached(int[] shape, DataType type) { + return createBufferDetachedImpl( ArrayUtil.prodLong(shape), type); + } /** * See {@link #createBufferDetached(int[], DataType)} */ public static DataBuffer createBufferDetached(long[] shape, DataType type) { - long length = ArrayUtil.prodLong(shape); + return createBufferDetachedImpl( ArrayUtil.prodLong(shape), type); + } + + // used by createBufferDetached(long[] DataType) and createBufferDetached(int[] , DataType) + private static DataBuffer createBufferDetachedImpl(long length, DataType type){ switch (type){ case DOUBLE: