diff --git a/drivers/opencl/src/main/java/uk/ac/manchester/tornado/drivers/opencl/graal/OCLLoweringProvider.java b/drivers/opencl/src/main/java/uk/ac/manchester/tornado/drivers/opencl/graal/OCLLoweringProvider.java index 70d0c97ed9..3061453635 100644 --- a/drivers/opencl/src/main/java/uk/ac/manchester/tornado/drivers/opencl/graal/OCLLoweringProvider.java +++ b/drivers/opencl/src/main/java/uk/ac/manchester/tornado/drivers/opencl/graal/OCLLoweringProvider.java @@ -95,7 +95,6 @@ import uk.ac.manchester.tornado.drivers.opencl.graal.lir.OCLKind; import uk.ac.manchester.tornado.drivers.opencl.graal.lir.OCLWriteAtomicNode; import uk.ac.manchester.tornado.drivers.opencl.graal.lir.OCLWriteAtomicNode.ATOMIC_OPERATION; -import uk.ac.manchester.tornado.drivers.opencl.graal.lir.OCLWriteNode; import uk.ac.manchester.tornado.drivers.opencl.graal.nodes.AtomicAddNode; import uk.ac.manchester.tornado.drivers.opencl.graal.nodes.CastNode; import uk.ac.manchester.tornado.drivers.opencl.graal.nodes.FixedArrayNode; @@ -362,10 +361,6 @@ private void lowerAtomicStoreIndexedNode(StoreAtomicIndexedNode storeIndexed) { graph.replaceFixedWithFixed(storeIndexed, memoryWrite); } - private boolean isSimpleCharOrShort(JavaKind elementKind, ValueNode value) { - return (elementKind == JavaKind.Char && value.getStackKind() != JavaKind.Object) || (elementKind == JavaKind.Short && value.getStackKind() != JavaKind.Object); - } - @Override public void lowerStoreIndexedNode(StoreIndexedNode storeIndexed, LoweringTool tool) { StructuredGraph graph = storeIndexed.graph(); @@ -597,17 +592,15 @@ private AddressNode createArrayAccess(StructuredGraph graph, LoadIndexedNode loa private AbstractWriteNode createMemWriteNode(JavaKind elementKind, ValueNode value, ValueNode array, AddressNode address, StructuredGraph graph, StoreIndexedNode storeIndexed) { AbstractWriteNode memoryWrite; - if (isSimpleCharOrShort(elementKind, value)) { - // XXX: This call is due to an error in Graal when storing a variable of type - // char or short. In future integrations with JVMCI and Graal, this issue is - // completely solved. - memoryWrite = graph.add(new OCLWriteNode(address, NamedLocationIdentity.getArrayLocation(elementKind), value, OnHeapMemoryAccess.BarrierType.NONE, elementKind)); - } else if (isLocalIDNode(storeIndexed) || isPrivateIDNode(storeIndexed)) { + if (isLocalIDNode(storeIndexed) || isPrivateIDNode(storeIndexed)) { address = createArrayLocalAddress(graph, array, storeIndexed.index()); - memoryWrite = graph.add(new WriteNode(address, NamedLocationIdentity.getArrayLocation(elementKind), value, OnHeapMemoryAccess.BarrierType.NONE)); - } else { - memoryWrite = graph.add(new WriteNode(address, NamedLocationIdentity.getArrayLocation(elementKind), value, OnHeapMemoryAccess.BarrierType.NONE)); } + ValueNode storeConvertValue = value; + Stamp valueStamp = value.stamp(NodeView.DEFAULT); + if (!(valueStamp instanceof OCLStamp) || !((OCLStamp) valueStamp).getOCLKind().isVector()) { + storeConvertValue = implicitStoreConvert(graph, elementKind, value); + } + memoryWrite = graph.add(new WriteNode(address, NamedLocationIdentity.getArrayLocation(elementKind), storeConvertValue, OnHeapMemoryAccess.BarrierType.NONE)); return memoryWrite; } } diff --git a/drivers/opencl/src/main/java/uk/ac/manchester/tornado/drivers/opencl/graal/lir/OCLWriteNode.java b/drivers/opencl/src/main/java/uk/ac/manchester/tornado/drivers/opencl/graal/lir/OCLWriteNode.java deleted file mode 100644 index 4555876322..0000000000 --- a/drivers/opencl/src/main/java/uk/ac/manchester/tornado/drivers/opencl/graal/lir/OCLWriteNode.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2020, APT Group, Department of Computer Science, - * School of Engineering, The University of Manchester. All rights reserved. - * Copyright (c) 2018, 2020, APT Group, Department of Computer Science, - * The University of Manchester. All rights reserved. - * Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Authors: Juan Fumero - * - */ -package uk.ac.manchester.tornado.drivers.opencl.graal.lir; - -import org.graalvm.compiler.core.common.LIRKind; -import org.graalvm.compiler.core.common.type.Stamp; -import org.graalvm.compiler.graph.NodeClass; -import org.graalvm.compiler.graph.iterators.NodeIterable; -import org.graalvm.compiler.nodeinfo.NodeInfo; -import org.graalvm.compiler.nodes.FrameState; -import org.graalvm.compiler.nodes.NodeView; -import org.graalvm.compiler.nodes.ValueNode; -import org.graalvm.compiler.nodes.memory.AbstractWriteNode; -import org.graalvm.compiler.nodes.memory.LIRLowerableAccess; -import org.graalvm.compiler.nodes.memory.address.AddressNode; -import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; -import org.graalvm.word.LocationIdentity; - -import jdk.vm.ci.meta.JavaKind; -import uk.ac.manchester.tornado.drivers.opencl.graal.OCLStamp; -import uk.ac.manchester.tornado.runtime.graal.phases.MarkOCLWriteNode; - -@NodeInfo(nameTemplate = "OCLWrite#{p#location/s}") -public class OCLWriteNode extends AbstractWriteNode implements LIRLowerableAccess, MarkOCLWriteNode { - - private JavaKind type; - - public static final NodeClass TYPE = NodeClass.create(OCLWriteNode.class); - - public OCLWriteNode(AddressNode address, LocationIdentity location, ValueNode value, BarrierType barrierType, JavaKind type) { - super(TYPE, address, location, value, barrierType); - assert (type == JavaKind.Char || type == JavaKind.Short); - this.type = type; - } - - @Override - public void generate(NodeLIRBuilderTool gen) { - OCLStamp oclStamp = null; - if (type == JavaKind.Char) { - oclStamp = new OCLStamp(OCLKind.CHAR); - } else if (type == JavaKind.Short) { - oclStamp = new OCLStamp(OCLKind.SHORT); - } - - LIRKind writeKind = gen.getLIRGeneratorTool().getLIRKind(oclStamp); - AddressNode address = super.getAddress(); - gen.getLIRGeneratorTool().getArithmetic().emitStore(writeKind, gen.operand(address), gen.operand(value()), gen.state(this)); - } - - @Override - public boolean canNullCheck() { - return true; - } - - @Override - public Stamp getAccessStamp(NodeView view) { - return value().stamp(view); - } - - @Override - public LocationIdentity getKilledLocationIdentity() { - return getLocationIdentity(); - } -} \ No newline at end of file diff --git a/drivers/ptx/src/main/java/uk/ac/manchester/tornado/drivers/ptx/graal/PTXLoweringProvider.java b/drivers/ptx/src/main/java/uk/ac/manchester/tornado/drivers/ptx/graal/PTXLoweringProvider.java index 4ccad4134f..4a0be8d1e1 100644 --- a/drivers/ptx/src/main/java/uk/ac/manchester/tornado/drivers/ptx/graal/PTXLoweringProvider.java +++ b/drivers/ptx/src/main/java/uk/ac/manchester/tornado/drivers/ptx/graal/PTXLoweringProvider.java @@ -84,7 +84,6 @@ import jdk.vm.ci.meta.ResolvedJavaField; import jdk.vm.ci.meta.ResolvedJavaType; import uk.ac.manchester.tornado.drivers.ptx.graal.lir.PTXKind; -import uk.ac.manchester.tornado.drivers.ptx.graal.lir.PTXWriteNode; import uk.ac.manchester.tornado.drivers.ptx.graal.nodes.CastNode; import uk.ac.manchester.tornado.drivers.ptx.graal.nodes.FixedArrayNode; import uk.ac.manchester.tornado.drivers.ptx.graal.nodes.GlobalThreadIdNode; @@ -501,21 +500,15 @@ private boolean isPrivateIDNode(LoadIndexedNode loadIndexedNode) { private AbstractWriteNode createMemWriteNode(JavaKind elementKind, ValueNode value, ValueNode array, AddressNode address, StructuredGraph graph, StoreIndexedNode storeIndexed) { AbstractWriteNode memoryWrite; - if (isSimpleCharOrShort(elementKind, value)) { - // XXX: This call is due to an error in Graal when storing a variable of type - // char or short. In future integrations with JVMCI and Graal, this issue is - // completely solved. - memoryWrite = graph.add(new PTXWriteNode(address, NamedLocationIdentity.getArrayLocation(elementKind), value, OnHeapMemoryAccess.BarrierType.NONE, elementKind)); - } else if (isLocalIDNode(storeIndexed) || isPrivateIDNode(storeIndexed)) { + if (isLocalIDNode(storeIndexed) || isPrivateIDNode(storeIndexed)) { address = createArrayLocalAddress(graph, array, storeIndexed.index()); - memoryWrite = graph.add(new WriteNode(address, NamedLocationIdentity.getArrayLocation(elementKind), value, OnHeapMemoryAccess.BarrierType.NONE)); - } else { - memoryWrite = graph.add(new WriteNode(address, NamedLocationIdentity.getArrayLocation(elementKind), value, OnHeapMemoryAccess.BarrierType.NONE)); } + ValueNode storeConvertValue = value; + Stamp valueStamp = value.stamp(NodeView.DEFAULT); + if (!(valueStamp instanceof PTXStamp) || !((PTXStamp) valueStamp).getPTXKind().isVector()) { + storeConvertValue = implicitStoreConvert(graph, elementKind, value); + } + memoryWrite = graph.add(new WriteNode(address, NamedLocationIdentity.getArrayLocation(elementKind), storeConvertValue, OnHeapMemoryAccess.BarrierType.NONE)); return memoryWrite; } - - private boolean isSimpleCharOrShort(JavaKind elementKind, ValueNode value) { - return (elementKind == JavaKind.Char && value.getStackKind() != JavaKind.Object) || (elementKind == JavaKind.Short && value.getStackKind() != JavaKind.Object); - } } diff --git a/drivers/ptx/src/main/java/uk/ac/manchester/tornado/drivers/ptx/graal/lir/PTXWriteNode.java b/drivers/ptx/src/main/java/uk/ac/manchester/tornado/drivers/ptx/graal/lir/PTXWriteNode.java deleted file mode 100644 index 6a3567b712..0000000000 --- a/drivers/ptx/src/main/java/uk/ac/manchester/tornado/drivers/ptx/graal/lir/PTXWriteNode.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (c) 2020, APT Group, Department of Computer Science, - * School of Engineering, The University of Manchester. All rights reserved. - * Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - */ -package uk.ac.manchester.tornado.drivers.ptx.graal.lir; - -import jdk.vm.ci.meta.JavaKind; -import org.graalvm.compiler.core.common.LIRKind; -import org.graalvm.compiler.core.common.type.Stamp; -import org.graalvm.compiler.graph.NodeClass; -import org.graalvm.compiler.nodeinfo.NodeInfo; -import org.graalvm.compiler.nodes.NodeView; -import org.graalvm.compiler.nodes.ValueNode; -import org.graalvm.compiler.nodes.memory.AbstractWriteNode; -import org.graalvm.compiler.nodes.memory.LIRLowerableAccess; -import org.graalvm.compiler.nodes.memory.address.AddressNode; -import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; -import org.graalvm.word.LocationIdentity; -import uk.ac.manchester.tornado.drivers.ptx.graal.PTXStamp; -import uk.ac.manchester.tornado.runtime.graal.phases.MarkOCLWriteNode; - -@NodeInfo(nameTemplate = "PTXWrite#{p#location/s}") -public class PTXWriteNode extends AbstractWriteNode implements LIRLowerableAccess, MarkOCLWriteNode { - - private JavaKind type; - - public static final NodeClass TYPE = NodeClass.create(PTXWriteNode.class); - - public PTXWriteNode(AddressNode address, LocationIdentity location, ValueNode value, BarrierType barrierType, JavaKind type) { - super(TYPE, address, location, value, barrierType); - assert (type == JavaKind.Char || type == JavaKind.Short); - this.type = type; - } - - protected PTXWriteNode(NodeClass c, AddressNode address, LocationIdentity location, ValueNode value, BarrierType barrierType) { - super(c, address, location, value, barrierType); - } - - @Override - public void generate(NodeLIRBuilderTool gen) { - PTXStamp oclStamp = null; - if (type == JavaKind.Char) { - oclStamp = new PTXStamp(PTXKind.U16); - } else if (type == JavaKind.Short) { - oclStamp = new PTXStamp(PTXKind.S16); - } - - LIRKind writeKind = gen.getLIRGeneratorTool().getLIRKind(oclStamp); - AddressNode address = super.getAddress(); - gen.getLIRGeneratorTool().getArithmetic().emitStore(writeKind, gen.operand(address), gen.operand(value()), gen.state(this)); - } - - @Override - public boolean canNullCheck() { - return true; - } - - @Override - public Stamp getAccessStamp(NodeView view) { - return value().stamp(view); - } - - @Override - public LocationIdentity getKilledLocationIdentity() { - return getLocationIdentity(); - } -} \ No newline at end of file diff --git a/unittests/src/main/java/uk/ac/manchester/tornado/unittests/arrays/TestArrays.java b/unittests/src/main/java/uk/ac/manchester/tornado/unittests/arrays/TestArrays.java index e49fc5e85f..e1c8239992 100644 --- a/unittests/src/main/java/uk/ac/manchester/tornado/unittests/arrays/TestArrays.java +++ b/unittests/src/main/java/uk/ac/manchester/tornado/unittests/arrays/TestArrays.java @@ -74,12 +74,24 @@ public static void vectorChars(char[] a, char[] b, char[] c) { } } + public static void vectorAddByte(byte[] a, byte[] b, byte[] c) { + for (@Parallel int i = 0; i < c.length; i++) { + c[i] = (byte) (a[i] + b[i]); + } + } + public static void addChars(char[] a, int[] b) { for (@Parallel int i = 0; i < a.length; i++) { a[i] += b[i]; } } + public static void initializeSequentialByte(byte[] a) { + for (int i = 0; i < a.length; i++) { + a[i] = 21; + } + } + public static void initializeSequential(int[] a) { for (int i = 0; i < a.length; i++) { a[i] = 1; @@ -120,6 +132,23 @@ public void testWarmUp() { } } + @Test + public void testInitByteArray() { + final int N = 128; + byte[] data = new byte[N]; + + TaskSchedule s0 = new TaskSchedule("s0"); + assertNotNull(s0); + + s0.task("t0", TestArrays::initializeSequentialByte, data); + s0.streamOut(data).warmup(); + s0.execute(); + + for (int i = 0; i < N; i++) { + assertEquals(21, data[i]); + } + } + @Test public void testInitNotParallel() { final int N = 128; @@ -329,6 +358,31 @@ public void testVectorChars() { } } + @Test + public void testVectorBytes() { + final int numElements = 4096; + byte[] a = new byte[numElements]; + byte[] b = new byte[numElements]; + byte[] c = new byte[numElements]; + + IntStream.range(0, numElements).parallel().forEach(idx -> { + a[idx] = 10; + b[idx] = 11; + }); + + //@formatter:off + new TaskSchedule("s0") + .streamIn(a, b) + .task("t0", TestArrays::vectorAddByte, a, b, c) + .streamOut(c) + .execute(); + //@formatter:on + + for (byte value : c) { + assertEquals(21, value); + } + } + /** * Inspired by the CUDA Hello World from Computer Graphics: *