From 699828ce9c38458db870cec9517beb21bb3b2b49 Mon Sep 17 00:00:00 2001 From: Juan Fumero Date: Fri, 2 Feb 2024 11:00:53 +0100 Subject: [PATCH] [fix] MatrixVectorFloat4 set/get accesses fixed --- .../api/types/collections/VectorFloat4.java | 4 +- .../tornado/api/types/images/ImageByte3.java | 6 +- .../tornado/api/types/images/ImageByte4.java | 6 +- .../tornado/api/types/images/ImageFloat.java | 6 +- .../tornado/api/types/images/ImageFloat3.java | 6 +- .../tornado/api/types/images/ImageFloat4.java | 6 +- .../tornado/api/types/images/ImageFloat8.java | 6 +- .../api/types/matrix/Matrix2DDouble.java | 15 ++-- .../api/types/matrix/Matrix2DFloat.java | 11 ++- .../api/types/matrix/Matrix2DFloat4.java | 2 +- .../tornado/api/types/matrix/Matrix2DInt.java | 26 ++----- .../api/types/matrix/Matrix3DFloat.java | 2 +- .../api/types/matrix/Matrix4x4Float.java | 6 +- .../api/types/utils/StorageFormats.java | 9 ++- .../api/types/volumes/VolumeShort2.java | 8 +- .../compiler/plugins/OCLVectorPlugins.java | 1 - .../examples/compute/MatrixVector.java | 7 +- .../tornado/examples/vectors/DFTVector.java | 6 +- .../examples/vectors/MatrixVector.java | 18 ++--- .../examples/vectors/VectorAddTest.java | 6 +- .../unittests/compute/ComputeTests.java | 78 ++++++++++++++++--- 21 files changed, 136 insertions(+), 99 deletions(-) diff --git a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/collections/VectorFloat4.java b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/collections/VectorFloat4.java index adee9acfce..6defd374f8 100644 --- a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/collections/VectorFloat4.java +++ b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/collections/VectorFloat4.java @@ -30,7 +30,7 @@ public final class VectorFloat4 implements TornadoCollectionInterface { /** * backing array. */ - protected final ByteArray storage; + private final ByteArray storage; /** * Number of rows. */ - protected final int Y; + private final int Y; /** * Number of columns. */ - protected final int X; + private final int X; /** * number of elements in the storage. */ diff --git a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageByte4.java b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageByte4.java index bdaa3bc849..759856f0d6 100644 --- a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageByte4.java +++ b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageByte4.java @@ -33,15 +33,15 @@ public final class ImageByte4 implements TornadoImagesInterface { /** * backing array. */ - protected final ByteArray storage; + private final ByteArray storage; /** * Number of rows. */ - protected final int Y; + private final int Y; /** * Number of columns. */ - protected final int X; + private final int X; /** * number of elements in the storage. */ diff --git a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageFloat.java b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageFloat.java index d4eb620657..c04edf493a 100644 --- a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageFloat.java +++ b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageFloat.java @@ -31,15 +31,15 @@ public final class ImageFloat implements TornadoImagesInterface { /** * backing array. */ - protected final FloatArray storage; + private final FloatArray storage; /** * Number of rows. */ - protected final int Y; + private final int Y; /** * Number of columns. */ - protected final int X; + private final int X; /** * number of elements in the storage. */ diff --git a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageFloat3.java b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageFloat3.java index e2b8a87a2e..9a1f35b007 100644 --- a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageFloat3.java +++ b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageFloat3.java @@ -32,15 +32,15 @@ public final class ImageFloat3 implements TornadoImagesInterface { /** * backing array. */ - protected final FloatArray storage; + private final FloatArray storage; /** * Number of rows. */ - protected final int Y; + private final int Y; /** * Number of columns. */ - protected final int X; + private final int X; /** * number of elements in the storage. */ diff --git a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageFloat4.java b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageFloat4.java index 140ab21109..cb2407e0a5 100644 --- a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageFloat4.java +++ b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageFloat4.java @@ -32,15 +32,15 @@ public final class ImageFloat4 implements TornadoImagesInterface { /** * backing array. */ - protected final FloatArray storage; + private final FloatArray storage; /** * Number of rows. */ - protected final int Y; + private final int Y; /** * Number of columns. */ - protected final int X; + private final int X; /** * number of elements in the storage. */ diff --git a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageFloat8.java b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageFloat8.java index f4b5b29d8e..1226f97611 100644 --- a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageFloat8.java +++ b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/images/ImageFloat8.java @@ -32,15 +32,15 @@ public final class ImageFloat8 implements TornadoImagesInterface { /** * backing array. */ - protected final FloatArray storage; + private final FloatArray storage; /** * Number of rows. */ - protected final int Y; + private final int Y; /** * Number of columns. */ - protected final int X; + private final int X; /** * number of elements in the storage. */ diff --git a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix2DDouble.java b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix2DDouble.java index cd15594d76..36d343f953 100644 --- a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix2DDouble.java +++ b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix2DDouble.java @@ -30,7 +30,7 @@ public final class Matrix2DDouble extends Matrix2DType implements TornadoMatrixI /** * backing array. */ - protected final DoubleArray storage; + private final DoubleArray storage; /** * number of elements in the storage. @@ -103,23 +103,22 @@ public void set(int i, int j, double value) { } public VectorDouble row(int row) { - int index = toRowMajor(row, 0, COLUMNS); - int from = index; - int to = getFinalIndexOfRange(index); - int size = to - from; + int baseIndex = toRowMajor(row, 0, COLUMNS); + int to = getFinalIndexOfRange(baseIndex); + int size = to - baseIndex; DoubleArray f = new DoubleArray(size); int j = 0; - for (int i = from; i < to; i++, j++) { + for (int i = baseIndex; i < to; i++, j++) { f.set(j, storage.get(i)); } return new VectorDouble(COLUMNS, f); } public VectorDouble column(int col) { - int index = StorageFormats.toRowMajor(0, col, COLUMNS); + int baseIndex = StorageFormats.toRowMajor(0, col, COLUMNS); final VectorDouble v = new VectorDouble(ROWS); for (int i = 0; i < ROWS; i++) { - v.set(i, storage.get(index + (i * COLUMNS))); + v.set(i, storage.get(baseIndex + (i * COLUMNS))); } return v; } diff --git a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix2DFloat.java b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix2DFloat.java index 00fc109f2d..fe43c4e747 100644 --- a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix2DFloat.java +++ b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix2DFloat.java @@ -31,7 +31,7 @@ public final class Matrix2DFloat extends Matrix2DType implements TornadoMatrixIn /** * backing array. */ - protected final FloatArray storage; + private final FloatArray storage; /** * number of elements in the storage. @@ -110,13 +110,12 @@ public void set(int i, int j, float value) { } public VectorFloat row(int row) { - int index = toRowMajor(row, 0, COLUMNS); - int from = index; - int to = getFinalIndexOfRange(index); - int size = to - from; + int baseIndex = toRowMajor(row, 0, COLUMNS); + int to = getFinalIndexOfRange(baseIndex); + int size = to - baseIndex; FloatArray f = new FloatArray(size); int j = 0; - for (int i = from; i < to; i++) { + for (int i = baseIndex; i < to; i++) { f.set(j, storage.get(i)); j++; } diff --git a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix2DFloat4.java b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix2DFloat4.java index 7a300a11ea..1d4c4986c2 100644 --- a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix2DFloat4.java +++ b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix2DFloat4.java @@ -37,7 +37,7 @@ public final class Matrix2DFloat4 extends Matrix2DType implements TornadoMatrixI /** * backing array. */ - protected final FloatArray storage; + private final FloatArray storage; /** * number of elements in the storage. */ diff --git a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix2DInt.java b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix2DInt.java index 0e3c01bc4e..7a72be0b9d 100644 --- a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix2DInt.java +++ b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix2DInt.java @@ -27,10 +27,11 @@ import uk.ac.manchester.tornado.api.types.utils.StorageFormats; public final class Matrix2DInt extends Matrix2DType implements TornadoMatrixInterface { + /** * backing array. */ - protected final IntArray storage; + private final IntArray storage; /** * number of elements in the storage. @@ -109,13 +110,12 @@ public void set(int i, int j, int value) { } public VectorInt row(int row) { - int index = toRowMajor(row, 0, COLUMNS); - int from = index; - int to = getFinalIndexOfRange(index); - int size = to - from; + int baseIndex = toRowMajor(row, 0, COLUMNS); + int to = getFinalIndexOfRange(baseIndex); + int size = to - baseIndex; IntArray f = new IntArray(size); int j = 0; - for (int i = from; i < to; i++) { + for (int i = baseIndex; i < to; i++) { f.set(j, storage.get(i)); j++; } @@ -157,20 +157,6 @@ public void multiply(Matrix2DInt a, Matrix2DInt b) { } } - public void tmultiply(Matrix2DInt a, Matrix2DInt b) { - System.out.printf("tmult: M=%d (expect %d)\n", getNumRows(), a.getNumRows()); - System.out.printf("tmult: N=%d (expect %d)\n", getNumColumns(), b.getNumRows()); - for (int row = 0; row < getNumRows(); row++) { - for (int col = 0; col < b.getNumRows(); col++) { - int sum = 0; - for (int k = 0; k < b.getNumColumns(); k++) { - sum += a.get(row, k) * b.get(col, k); - } - set(row, col, sum); - } - } - } - public Matrix2DInt duplicate() { Matrix2DInt matrix = new Matrix2DInt(ROWS, COLUMNS); matrix.set(this); diff --git a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix3DFloat.java b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix3DFloat.java index 33d0e64aa4..ffe52c2a54 100644 --- a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix3DFloat.java +++ b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix3DFloat.java @@ -28,7 +28,7 @@ public final class Matrix3DFloat extends Matrix3DType implements TornadoMatrixIn /** * backing array. */ - protected final FloatArray storage; + private final FloatArray storage; /** * number of elements in the storage. diff --git a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix4x4Float.java b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix4x4Float.java index f5a974580c..79d748a8c3 100644 --- a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix4x4Float.java +++ b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/matrix/Matrix4x4Float.java @@ -31,11 +31,11 @@ public final class Matrix4x4Float implements TornadoMatrixInterface /** * Number of rows. */ - protected static final int ROWS = 4; + private static final int ROWS = 4; /** * Number of columns. */ - protected static final int COLUMNS = 4; + private static final int COLUMNS = 4; /** * number of elements in the storage. */ @@ -43,7 +43,7 @@ public final class Matrix4x4Float implements TornadoMatrixInterface /** * backing array. */ - protected final FloatArray storage; + private final FloatArray storage; public Matrix4x4Float() { this(new FloatArray(NUM_ELEMENTS)); diff --git a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/utils/StorageFormats.java b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/utils/StorageFormats.java index a5bf72b60c..e8689364d3 100644 --- a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/utils/StorageFormats.java +++ b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/utils/StorageFormats.java @@ -38,6 +38,7 @@ private StorageFormats() { * length of a column * @return int */ + @Deprecated public static int toColumnMajor(int i, int j, int ld) { return (j * ld) + i; } @@ -49,16 +50,16 @@ public static int toColumnMajor(int i, int j, int ld) { * row index * @param j * column index - * @param yMax + * @param numColumns * length of a row * @return int */ - public static int toRowMajor(int i, int j, int yMax) { - return (i * yMax) + j; + public static int toRowMajor(int i, int j, int numColumns) { + return (i * numColumns) + j; } public static int toRowMajorVector(int i, int j, int numColumns, int vectorElements) { - return (i * numColumns * vectorElements) + j; + return ((i * numColumns) + j) * vectorElements; } public static int toRowMajor3D(int i, int j, int k, int zMax, int yMax) { diff --git a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/volumes/VolumeShort2.java b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/volumes/VolumeShort2.java index be8f49de17..4d821f21cc 100644 --- a/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/volumes/VolumeShort2.java +++ b/tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/volumes/VolumeShort2.java @@ -30,19 +30,19 @@ public final class VolumeShort2 implements TornadoVolumesInterface /** * backing array. */ - protected final ShortArray storage; + private final ShortArray storage; /** * Size in Y dimension. */ - protected final int Y; + private final int Y; /** * Size in X dimension. */ - protected final int X; + private final int X; /** * Size in Y dimension. */ - protected final int Z; + private final int Z; /** * number of elements in the storage. */ diff --git a/tornado-drivers/opencl/src/main/java/uk/ac/manchester/tornado/drivers/opencl/graal/compiler/plugins/OCLVectorPlugins.java b/tornado-drivers/opencl/src/main/java/uk/ac/manchester/tornado/drivers/opencl/graal/compiler/plugins/OCLVectorPlugins.java index 2984d3454d..873f0d17fd 100644 --- a/tornado-drivers/opencl/src/main/java/uk/ac/manchester/tornado/drivers/opencl/graal/compiler/plugins/OCLVectorPlugins.java +++ b/tornado-drivers/opencl/src/main/java/uk/ac/manchester/tornado/drivers/opencl/graal/compiler/plugins/OCLVectorPlugins.java @@ -365,7 +365,6 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec return true; } }); - } static void registerParameterPlugins(Plugins plugins) { diff --git a/tornado-examples/src/main/java/uk/ac/manchester/tornado/examples/compute/MatrixVector.java b/tornado-examples/src/main/java/uk/ac/manchester/tornado/examples/compute/MatrixVector.java index a4363e5b5f..91189d8796 100644 --- a/tornado-examples/src/main/java/uk/ac/manchester/tornado/examples/compute/MatrixVector.java +++ b/tornado-examples/src/main/java/uk/ac/manchester/tornado/examples/compute/MatrixVector.java @@ -61,10 +61,10 @@ public class MatrixVector { public static final int MAX_ITERATIONS = 100; private static void computeMatrixVector(Matrix2DFloat matrix, VectorFloat vector, VectorFloat output) { - for (@Parallel int i = 0; i < vector.size(); i++) { + for (@Parallel int i = 0; i < matrix.getNumRows(); i++) { float sum = 0.0f; for (int j = 0; j < matrix.getNumColumns(); j++) { - sum += vector.get(i) * matrix.get(i, i); + sum += matrix.get(i, j) * vector.get(j); } output.set(i, sum); } @@ -77,7 +77,8 @@ public static void main(String[] args) { try { size = Integer.parseInt(args[0]); } catch (NumberFormatException numberFormatException) { - size = 8192; + numberFormatException.printStackTrace(); + throw new NullPointerException(); } } diff --git a/tornado-examples/src/main/java/uk/ac/manchester/tornado/examples/vectors/DFTVector.java b/tornado-examples/src/main/java/uk/ac/manchester/tornado/examples/vectors/DFTVector.java index 508291d17b..2cfaa79cae 100644 --- a/tornado-examples/src/main/java/uk/ac/manchester/tornado/examples/vectors/DFTVector.java +++ b/tornado-examples/src/main/java/uk/ac/manchester/tornado/examples/vectors/DFTVector.java @@ -88,7 +88,7 @@ public static void computeDFTVector2(VectorFloat2 inreal, VectorFloat2 inimag, V Float2 sumReal = new Float2(0, 0); Float2 simImag = new Float2(0, 0); for (int t = 0; t < n; t++) { // For each input element - float angle = (float) ((2 * TornadoMath.floatPI() * t * k) / n); + float angle = (2 * TornadoMath.floatPI() * t * k) / n; Float2 partA = Float2.mult(inreal.get(t), TornadoMath.cos(angle)); Float2 partB = Float2.mult(inimag.get(t), TornadoMath.sin(angle)); @@ -138,7 +138,7 @@ public static void computeDFTVector8(VectorFloat8 inreal, VectorFloat8 inimag, V Float8 sumReal = new Float8(0, 0, 0, 0, 0, 0, 0, 0); Float8 simImag = new Float8(0, 0, 0, 0, 0, 0, 0, 0); for (int t = 0; t < n; t++) { // For each input element - float angle = (float) ((2 * TornadoMath.floatPI() * t * k) / n); + float angle = (2 * TornadoMath.floatPI() * t * k) / n; Float8 partA = Float8.mult(inreal.get(t), TornadoMath.cos(angle)); Float8 partB = Float8.mult(inimag.get(t), TornadoMath.sin(angle)); @@ -164,7 +164,7 @@ public static void computeDFTVector16(VectorFloat16 inreal, VectorFloat16 inimag Float16 sumImag = new Float16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); for (int t = 0; t < n; t++) { // For each input element - float angle = (float) ((2 * TornadoMath.floatPI() * t * k) / n); + float angle = (2 * TornadoMath.floatPI() * t * k) / n; Float16 partA = Float16.mult(inreal.get(t), TornadoMath.cos(angle)); Float16 partB = Float16.mult(inimag.get(t), TornadoMath.sin(angle)); diff --git a/tornado-examples/src/main/java/uk/ac/manchester/tornado/examples/vectors/MatrixVector.java b/tornado-examples/src/main/java/uk/ac/manchester/tornado/examples/vectors/MatrixVector.java index 13e282bf15..ebe3cbf0b5 100644 --- a/tornado-examples/src/main/java/uk/ac/manchester/tornado/examples/vectors/MatrixVector.java +++ b/tornado-examples/src/main/java/uk/ac/manchester/tornado/examples/vectors/MatrixVector.java @@ -63,20 +63,20 @@ public class MatrixVector { public static final int ITERATIONS = 100; private static void compute(Matrix2DFloat matrix, VectorFloat vector, VectorFloat output) { - for (@Parallel int i = 0; i < vector.size(); i++) { + for (@Parallel int i = 0; i < matrix.getNumRows(); i++) { float sum = 0.0f; for (int j = 0; j < matrix.getNumColumns(); j++) { - sum += vector.get(i) * matrix.get(i, i); + sum += matrix.get(i, j) * vector.get(j); } output.set(i, sum); } } - private static void computeWithVectors(Matrix2DFloat4 matrix, VectorFloat4 vector, VectorFloat4 output) { - for (@Parallel int i = 0; i < vector.getLength(); i++) { - Float4 sum = new Float4(0, 0, 0, 0); + private static void computeWithVectors(Matrix2DFloat4 matrix, VectorFloat4 vector, VectorFloat output) { + for (@Parallel int i = 0; i < matrix.getNumRows(); i++) { + float sum = 0; for (int j = 0; j < matrix.getNumColumns(); j++) { - sum = Float4.add(sum, Float4.mult(vector.get(i), matrix.get(i, i))); + sum += Float4.sum(Float4.mult(matrix.get(i, j), vector.get(j))); } output.set(i, sum); } @@ -90,7 +90,7 @@ private static void runWithVectorTypes(int size, TornadoDevice device) { VectorFloat4 vectorFloat = new VectorFloat4(size); // Output - VectorFloat4 result = new VectorFloat4(size); + VectorFloat result = new VectorFloat(size); Random r = new Random(); final int s = size; @@ -186,7 +186,7 @@ private static void computeWithStreams(final int size, Matrix2DFloat matrix, Vec IntStream.range(0, size).parallel().forEach(i -> { float sum = 0.0f; for (int j = 0; j < matrix.getNumColumns(); j++) { - sum += vector.get(i) * matrix.get(i, i); + sum += matrix.get(i, j) * vector.get(j); } output.set(i, sum); }); @@ -246,7 +246,7 @@ public static void main(String[] args) { } } - TornadoDevice device = TornadoExecutionPlan.getDevice(0, 2); + TornadoDevice device = TornadoExecutionPlan.getDevice(0, 0); if (version.startsWith("vector")) { runWithVectorTypes(size, device); diff --git a/tornado-examples/src/main/java/uk/ac/manchester/tornado/examples/vectors/VectorAddTest.java b/tornado-examples/src/main/java/uk/ac/manchester/tornado/examples/vectors/VectorAddTest.java index ea1bd7f692..08dd80e6e4 100644 --- a/tornado-examples/src/main/java/uk/ac/manchester/tornado/examples/vectors/VectorAddTest.java +++ b/tornado-examples/src/main/java/uk/ac/manchester/tornado/examples/vectors/VectorAddTest.java @@ -243,9 +243,7 @@ private static void runWithoutVectorTypes(int size, TornadoDevice device) { } private static void computeWithStreams(final int size, FloatArray a, FloatArray b, FloatArray results) { - IntStream.range(0, size).parallel().forEach(i -> { - results.set(i, a.get(i) + b.get(i)); - }); + IntStream.range(0, size).parallel().forEach(i -> results.set(i, a.get(i) + b.get(i))); } private static void runWithJavaStreams(int size) { @@ -295,7 +293,7 @@ public static void main(String[] args) { } } - TornadoDevice device = TornadoExecutionPlan.getDevice(0, 2); + TornadoDevice device = TornadoExecutionPlan.getDevice(0, 0); if (version.startsWith("vector4")) { runWithVectorTypes4(size, device); diff --git a/tornado-unittests/src/main/java/uk/ac/manchester/tornado/unittests/compute/ComputeTests.java b/tornado-unittests/src/main/java/uk/ac/manchester/tornado/unittests/compute/ComputeTests.java index 0bb833225e..e0744d8358 100644 --- a/tornado-unittests/src/main/java/uk/ac/manchester/tornado/unittests/compute/ComputeTests.java +++ b/tornado-unittests/src/main/java/uk/ac/manchester/tornado/unittests/compute/ComputeTests.java @@ -37,6 +37,7 @@ import uk.ac.manchester.tornado.api.WorkerGrid; import uk.ac.manchester.tornado.api.WorkerGrid1D; import uk.ac.manchester.tornado.api.annotations.Parallel; +import uk.ac.manchester.tornado.api.types.matrix.Matrix2DFloat4; import uk.ac.manchester.tornado.api.types.vectors.Byte3; import uk.ac.manchester.tornado.api.types.vectors.Float3; import uk.ac.manchester.tornado.api.types.vectors.Float4; @@ -396,10 +397,20 @@ private static BufferedImage writeFile(IntArray output, int size) { } private static void computeMatrixVector(Matrix2DFloat matrix, VectorFloat vector, VectorFloat output) { - for (@Parallel int i = 0; i < vector.size(); i++) { + for (@Parallel int i = 0; i < matrix.getNumRows(); i++) { float sum = 0.0f; for (int j = 0; j < matrix.getNumColumns(); j++) { - sum += vector.get(i) * matrix.get(i, i); + sum += matrix.get(i, j) * vector.get(j); + } + output.set(i, sum); + } + } + + private static void computeMatrixVectorFloat4(Matrix2DFloat4 matrix, VectorFloat4 vector, VectorFloat output) { + for (@Parallel int i = 0; i < matrix.getNumRows(); i++) { + float sum = 0; + for (int j = 0; j < matrix.getNumColumns(); j++) { + sum += Float4.sum(Float4.mult(matrix.get(i, j), vector.get(j))); } output.set(i, sum); } @@ -848,28 +859,20 @@ public void testJuliaSets() { public void matrixVector() { int size = 4096; - // Create a matrix of M rows and N columns (MxN) Matrix2DFloat matrix2DFloat = new Matrix2DFloat(size, size); - - // Vector must be of size N VectorFloat vectorFloat = new VectorFloat(size); - - // Output VectorFloat result = new VectorFloat(size); - VectorFloat resultSeq = new VectorFloat(size); Random r = new Random(); - final int s = size; - // Init Data IntStream.range(0, size).forEach(idx -> vectorFloat.set(idx, r.nextFloat())); IntStream.range(0, size).forEach(idx -> IntStream.range(0, s).forEach(jdx -> { matrix2DFloat.set(idx, jdx, r.nextFloat()); })); - TaskGraph taskGraph = new TaskGraph("la") // + TaskGraph taskGraph = new TaskGraph("graph") // .transferToDevice(DataTransferMode.EVERY_EXECUTION, matrix2DFloat, vectorFloat) // .task("mv", ComputeTests::computeMatrixVector, matrix2DFloat, vectorFloat, result) // .transferToHost(DataTransferMode.EVERY_EXECUTION, result); @@ -880,7 +883,58 @@ public void matrixVector() { computeMatrixVector(matrix2DFloat, vectorFloat, resultSeq); for (int i = 0; i < vectorFloat.size(); i++) { - assertEquals(resultSeq.get(i), resultSeq.get(i), 0.1); + assertEquals(resultSeq.get(i), result.get(i), 0.01f); + } + } + + @Test + public void matrixVectorFloat4() { + int M = 2048; + int N = 4096; + + Matrix2DFloat4 matrix2DFloat = new Matrix2DFloat4(M, N); + VectorFloat4 vectorFloat = new VectorFloat4(N); + VectorFloat result = new VectorFloat(M); + + Matrix2DFloat inputA = new Matrix2DFloat(M, N * 4); + VectorFloat inputB = new VectorFloat(N * 4); + VectorFloat resultSeq = new VectorFloat(M); + + Random r = new Random(11); + // Init Data + for (int i = 0; i < vectorFloat.getLength(); i++) { + Float4 f = new Float4(0, 1, 2, 3); + int indexI = i * 4; + inputB.set(indexI, f.getX()); + inputB.set(indexI + 1, f.getY()); + inputB.set(indexI + 2, f.getZ()); + inputB.set(indexI + 3, f.getW()); + vectorFloat.set(i, f); + } + for (int i = 0; i < matrix2DFloat.getNumRows(); i++) { + for (int j = 0; j < matrix2DFloat.getNumColumns(); j++) { + Float4 f = new Float4(0, 1, 2, 3); + matrix2DFloat.set(i, j, f); + int indexJ = j * 4; + inputA.set(i, indexJ, f.getX()); + inputA.set(i, indexJ + 1, f.getY()); + inputA.set(i, indexJ + 2, f.getZ()); + inputA.set(i, indexJ + 3, f.getW()); + } + } + + TaskGraph taskGraph = new TaskGraph("graph") // + .transferToDevice(DataTransferMode.EVERY_EXECUTION, matrix2DFloat, vectorFloat) // + .task("mv", ComputeTests::computeMatrixVectorFloat4, matrix2DFloat, vectorFloat, result) // + .transferToHost(DataTransferMode.EVERY_EXECUTION, result); + + ImmutableTaskGraph immutableTaskGraph = taskGraph.snapshot(); + TornadoExecutionPlan executionPlan = new TornadoExecutionPlan(immutableTaskGraph); + executionPlan.execute(); + + computeMatrixVector(inputA, inputB, resultSeq); + for (int i = 0; i < result.getLength(); i++) { + assertEquals(resultSeq.get(i), result.get(i), 0.01f); } } // CHECKSTYLE:ON