diff --git a/src/aeminium/gpu/buffers/BufferHelper.java b/src/aeminium/gpu/buffers/BufferHelper.java index 32c76ec..28370a3 100644 --- a/src/aeminium/gpu/buffers/BufferHelper.java +++ b/src/aeminium/gpu/buffers/BufferHelper.java @@ -2,6 +2,7 @@ import java.util.HashMap; +import aeminium.gpu.collections.lazyness.LazyGPUList; import aeminium.gpu.collections.lists.BooleanList; import aeminium.gpu.collections.lists.PList; import aeminium.gpu.collections.properties.evaluation.LazyCollection; @@ -75,6 +76,9 @@ public static CLBuffer createInputBufferFor(CLContext context, public static CLBuffer createInputOutputBufferFor(CLContext context, PList list) { + if (list instanceof LazyGPUList) { + return ((LazyGPUList) list).getCLBuffer(); + } IBufferFactory f = getFactory(list); return f.createInputOutputBufferFor(context, list); } @@ -105,6 +109,7 @@ public static CLBuffer createOutputBufferFor(CLContext context, public static PList extractFromBuffer(CLBuffer outbuffer, CLQueue q, CLEvent ev, int size, PList list) { + System.out.println("T: " + list.getType()); return extractFromBuffer(outbuffer, q, ev, list.getType() .getSimpleName(), size); } diff --git a/src/aeminium/gpu/collections/lazyness/LazyEvaluator.java b/src/aeminium/gpu/collections/lazyness/LazyEvaluator.java index fffc140..105ae10 100644 --- a/src/aeminium/gpu/collections/lazyness/LazyEvaluator.java +++ b/src/aeminium/gpu/collections/lazyness/LazyEvaluator.java @@ -1,5 +1,7 @@ package aeminium.gpu.collections.lazyness; +import com.nativelibs4java.opencl.CLBuffer; + import aeminium.gpu.collections.lists.PList; import aeminium.gpu.operations.Map; import aeminium.gpu.operations.Reduce; @@ -19,4 +21,6 @@ public interface LazyEvaluator { public T mergeWithReducer(Reduce reduceOp); + public CLBuffer getCLBuffer(); + } diff --git a/src/aeminium/gpu/collections/lazyness/LazyGPUList.java b/src/aeminium/gpu/collections/lazyness/LazyGPUList.java new file mode 100644 index 0000000..d0c87e8 --- /dev/null +++ b/src/aeminium/gpu/collections/lazyness/LazyGPUList.java @@ -0,0 +1,103 @@ +package aeminium.gpu.collections.lazyness; + +import aeminium.gpu.buffers.BufferHelper; +import aeminium.gpu.collections.factories.CollectionFactory; +import aeminium.gpu.collections.lists.AbstractList; +import aeminium.gpu.collections.lists.PList; +import aeminium.gpu.devices.GPUDevice; + +import com.nativelibs4java.opencl.CLBuffer; +import com.nativelibs4java.opencl.CLEvent; + +public class LazyGPUList extends AbstractList implements PList { + + private PList cpuList = null; + private String type; + + private CLBuffer buffer; + + private CLEvent event; + private GPUDevice device; + + + public LazyGPUList(CLBuffer buffer, String type, int size, GPUDevice dev, CLEvent e) { + super(); + this.size = size; + this.buffer = buffer; + this.type = type; + this.event = e; + this.device = dev; + } + + public boolean hasBeenEvaluated() { + return cpuList != null; + } + + @SuppressWarnings("unchecked") + public PList evaluate() { + if (cpuList == null) { + cpuList = (PList) BufferHelper.extractFromBuffer(buffer, device.getQueue(), event, type, size); + } + return cpuList; + } + + @Override + public int size() { + return (cpuList != null ? cpuList.size() : size); + } + + @Override + public void add(int index, T e) { + evaluate(); + cpuList.add(index, e); + } + + @Override + public void remove(T o) { + evaluate(); + cpuList.remove(o); + } + + @Override + public T get(int index) { + evaluate(); + return cpuList.get(index); + } + + @Override + public void set(int index, T e) { + evaluate(); + cpuList.set(index, e); + } + + @Override + public T remove(int index) { + evaluate(); + return cpuList.remove(index); + } + + @SuppressWarnings("unchecked") + @Override + public void clear() { + size = 0; + cpuList = (PList) CollectionFactory.listFromType(getType() + .getSimpleName().toString()); + } + + @Override + public PList subList(int fromIndex, int toIndex) { + evaluate(); + return cpuList.subList(fromIndex, toIndex); + } + + @Override + public Class getType() { + return BufferHelper.getClassOf(type); + + } + + public CLBuffer getCLBuffer() { + return buffer; + } + +} diff --git a/src/aeminium/gpu/collections/lazyness/LazyPList.java b/src/aeminium/gpu/collections/lazyness/LazyPList.java deleted file mode 100644 index 30ddf9c..0000000 --- a/src/aeminium/gpu/collections/lazyness/LazyPList.java +++ /dev/null @@ -1,124 +0,0 @@ -package aeminium.gpu.collections.lazyness; - -import aeminium.gpu.collections.factories.CollectionFactory; -import aeminium.gpu.collections.lists.AbstractList; -import aeminium.gpu.collections.lists.PList; -import aeminium.gpu.operations.Map; -import aeminium.gpu.operations.Reduce; -import aeminium.gpu.operations.functions.LambdaMapper; -import aeminium.gpu.operations.functions.LambdaReducerWithSeed; - -public class LazyPList extends AbstractList implements PList { - - private boolean evaluated = false; - private PList actual; - private LazyEvaluator evaluator; - private int lazynessLevel = 1; - - public LazyPList(LazyEvaluator eval, int size) { - super(); - this.size = size; - this.evaluator = eval; - } - - @SuppressWarnings("unchecked") - public PList evaluate() { - if (!evaluated) { - actual = (PList) evaluator.evaluate(); - evaluated = true; - } - return actual; - } - - @Override - public int size() { - if (evaluated) { - return actual.size(); - } else { - return size; - } - } - - @Override - public void add(int index, T e) { - evaluate(); - actual.add(index, e); - } - - @Override - public void remove(T o) { - evaluate(); - actual.remove(o); - } - - @Override - public T get(int index) { - evaluate(); - return actual.get(index); - } - - @Override - public void set(int index, T e) { - evaluate(); - actual.set(index, e); - } - - @Override - public T remove(int index) { - evaluate(); - return actual.remove(index); - } - - @SuppressWarnings("unchecked") - @Override - public void clear() { - size = 0; - evaluated = true; - actual = (PList) CollectionFactory.listFromType(getType() - .getSimpleName().toString()); - } - - @Override - public PList subList(int fromIndex, int toIndex) { - evaluate(); - return actual.subList(fromIndex, toIndex); - } - - @Override - public Class getType() { - return evaluator.getType(); - } - - @Override - public PList map(LambdaMapper mapFun) { - if (!evaluated && evaluator.canMergeWithMap(mapFun)) { - Map m = new Map(mapFun, this, this.getDevice()); - LazyPList r = (LazyPList) evaluator.mergeWithMap(m); - r.setLazynessLevel(lazynessLevel + 1); - return r; - } else { - evaluate(); - return actual.map(mapFun); - } - } - - @Override - public T reduce(LambdaReducerWithSeed reducer) { - if (!evaluated && evaluator.canMergeWithReduce(reducer)) { - Reduce m = new Reduce(reducer, this, this.getDevice()); - return evaluator.mergeWithReducer(m); - } else { - evaluate(); - return actual.reduce(reducer); - } - } - - public int getLazynessLevel() { - return lazynessLevel; - } - - public void setLazynessLevel(int lazynessLevel) { - this.lazynessLevel = lazynessLevel; - } - -} diff --git a/src/aeminium/gpu/executables/GenericProgram.java b/src/aeminium/gpu/executables/GenericProgram.java index 6ef3d27..52ba27e 100644 --- a/src/aeminium/gpu/executables/GenericProgram.java +++ b/src/aeminium/gpu/executables/GenericProgram.java @@ -97,9 +97,9 @@ public void release() { if (program != null) { program.release(); } - if (kernelCompletion != null) { + /*if (kernelCompletion != null) { kernelCompletion.release(); - } + }*/ if (kernel != null) { kernel.release(); } diff --git a/src/aeminium/gpu/operations/Map.java b/src/aeminium/gpu/operations/Map.java index f39c46d..9d5634d 100644 --- a/src/aeminium/gpu/operations/Map.java +++ b/src/aeminium/gpu/operations/Map.java @@ -2,8 +2,7 @@ import aeminium.gpu.buffers.BufferHelper; import aeminium.gpu.collections.factories.CollectionFactory; -import aeminium.gpu.collections.lazyness.LazyEvaluator; -import aeminium.gpu.collections.lazyness.LazyPList; +import aeminium.gpu.collections.lazyness.LazyGPUList; import aeminium.gpu.collections.lazyness.Range; import aeminium.gpu.collections.lists.PList; import aeminium.gpu.devices.GPUDevice; @@ -11,10 +10,7 @@ import aeminium.gpu.executables.Program; import aeminium.gpu.operations.deciders.OpenCLDecider; import aeminium.gpu.operations.functions.LambdaMapper; -import aeminium.gpu.operations.functions.LambdaReducerWithSeed; import aeminium.gpu.operations.generator.MapCodeGen; -import aeminium.gpu.operations.mergers.MapToMapMerger; -import aeminium.gpu.operations.mergers.MapToReduceMerger; import com.nativelibs4java.opencl.CLBuffer; import com.nativelibs4java.opencl.CLContext; @@ -99,65 +95,27 @@ public void execute(CLContext ctx, CLQueue q) { } } - @SuppressWarnings("unchecked") + // @SuppressWarnings("unchecked") @Override public void retrieveResults(CLContext ctx, CLQueue q) { - output = (PList) BufferHelper.extractFromBuffer(outbuffer, q, - kernelCompletion, getOutputType(), input.size()); + // output = (PList) BufferHelper.extractFromBuffer(outbuffer, q, + // kernelCompletion, getOutputType(), input.size()); } @Override public void release() { super.release(); this.inbuffer.release(); - this.outbuffer.release(); } // Output public PList getOutput() { - final Map innerMap = this; - - // Lazy return - LazyEvaluator operation = new LazyEvaluator() { - - @Override - public PList evaluate() { - innerMap.execute(); - return output; - } - - @Override - public Class getType() { - return BufferHelper.getClassOf(getOutputType()); - } - - @Override - public boolean canMergeWithMap(LambdaMapper mapFun) { - return true; - } - - @Override - public PList mergeWithMap(Map mapOp) { - MapToMapMerger merger = new MapToMapMerger( - innerMap, mapOp, input); - return merger.getOutput(); - } - - @Override - public boolean canMergeWithReduce(LambdaReducerWithSeed reduceFun) { - return true; - } - - @Override - public O mergeWithReducer(Reduce reduceOp) { - MapToReduceMerger merger = new MapToReduceMerger( - innerMap, reduceOp, input); - return merger.getOutput(); - } - - }; - return new LazyPList(operation, input.size()); + this.execute(); + if (output != null) + return output; + else + return new LazyGPUList(outbuffer, getOutputType(), input.size(), device, kernelCompletion); } // Utils diff --git a/src/aeminium/gpu/operations/mergers/MapToMapMerger.java b/src/aeminium/gpu/operations/mergers/MapToMapMerger.java deleted file mode 100644 index f8a0322..0000000 --- a/src/aeminium/gpu/operations/mergers/MapToMapMerger.java +++ /dev/null @@ -1,94 +0,0 @@ -package aeminium.gpu.operations.mergers; - -import aeminium.gpu.collections.lazyness.LazyEvaluator; -import aeminium.gpu.collections.lazyness.LazyPList; -import aeminium.gpu.collections.lists.PList; -import aeminium.gpu.operations.Map; -import aeminium.gpu.operations.Reduce; -import aeminium.gpu.operations.functions.LambdaMapper; -import aeminium.gpu.operations.functions.LambdaReducerWithSeed; - -public class MapToMapMerger { - - private Map first; - private Map second; - private PList current; - - public MapToMapMerger(Map f, Map s, PList c) { - first = f; - second = s; - current = c; - } - - public PList getOutput() { - LazyEvaluator eval = new LazyEvaluator() { - - private Map fakeMap() { - LambdaMapper fakeLambda = new LambdaMapper() { - - @Override - public O map(I o) { - return second.getMapFun().map(first.getMapFun().map(o)); - } - - @Override - public String getSource() { - return String.format("return %s(%s(input));", - second.getMapOpenCLName(), - first.getMapOpenCLName()); - } - }; - - StringBuilder extraCode = new StringBuilder(); - extraCode.append(first.getOtherSources()); - extraCode.append(second.getOtherSources()); - extraCode.append(first.getMapOpenCLSource()); - extraCode.append(second.getMapOpenCLSource()); - Map op = new Map(fakeLambda, current, - extraCode.toString(), first.getDevice()) { - public String getOutputType() { - return second.getOutputType(); - } - }; - return op; - } - - @Override - public PList evaluate() { - Map op = fakeMap(); - return op.getOutput(); - } - - @Override - public Class getType() { - return current.getType(); - } - - @Override - public boolean canMergeWithMap(LambdaMapper mapFun) { - return true; - } - - @Override - public PList mergeWithMap(Map mapOp) { - MapToMapMerger merger = new MapToMapMerger( - fakeMap(), mapOp, current); - return merger.getOutput(); - } - - @Override - public boolean canMergeWithReduce(LambdaReducerWithSeed reduceFun) { - return true; - } - - @Override - public O mergeWithReducer(Reduce reduceOp) { - MapToReduceMerger merger = new MapToReduceMerger( - fakeMap(), reduceOp, current); - return merger.getOutput(); - } - }; - return new LazyPList(eval, first.getOutputSize()); - } - -} diff --git a/src/aeminium/gpu/operations/mergers/MapToReduceMerger.java b/src/aeminium/gpu/operations/mergers/MapToReduceMerger.java deleted file mode 100644 index 7054b63..0000000 --- a/src/aeminium/gpu/operations/mergers/MapToReduceMerger.java +++ /dev/null @@ -1,66 +0,0 @@ -package aeminium.gpu.operations.mergers; - -import aeminium.gpu.collections.lazyness.LazyEvaluator; -import aeminium.gpu.collections.lists.PList; -import aeminium.gpu.operations.Map; -import aeminium.gpu.operations.MapReduce; -import aeminium.gpu.operations.Reduce; -import aeminium.gpu.operations.functions.LambdaMapper; -import aeminium.gpu.operations.functions.LambdaReducerWithSeed; - -public class MapToReduceMerger { - - private Map first; - private Reduce second; - private PList current; - - public MapToReduceMerger(Map f, Reduce s, PList c) { - first = f; - second = s; - current = c; - } - - @SuppressWarnings("unchecked") - public O getOutput() { - LazyEvaluator eval = new LazyEvaluator() { - - @Override - public O evaluate() { - StringBuilder extraCode = new StringBuilder(); - extraCode.append(first.getOtherSources()); - extraCode.append(second.getOtherSources()); - MapReduce op = new MapReduce(first.getMapFun(), - second.getReduceFun(), current, extraCode.toString(), - first.getDevice()); - return op.getOutput(); - } - - @Override - public Class getType() { - return current.getType(); - } - - @Override - public boolean canMergeWithMap(LambdaMapper mapFun) { - return false; - } - - @Override - public PList mergeWithMap(Map mapOp) { - return null; - } - - @Override - public boolean canMergeWithReduce(LambdaReducerWithSeed reduceFun) { - return false; - } - - @Override - public O mergeWithReducer(Reduce reduceOp) { - return null; - } - - }; - return (O) eval.evaluate(); - } -} diff --git a/src/aeminium/gpu/tests/operations/TestMatrix.java b/src/aeminium/gpu/tests/operations/TestMatrix.java new file mode 100644 index 0000000..942587c --- /dev/null +++ b/src/aeminium/gpu/tests/operations/TestMatrix.java @@ -0,0 +1,87 @@ +package aeminium.gpu.tests.operations; + +import aeminium.gpu.collections.lists.PList; +import aeminium.gpu.collections.matrices.IntMatrix; +import aeminium.gpu.collections.matrices.PMatrix; +import aeminium.gpu.operations.functions.LambdaMapper; +import aeminium.gpu.operations.functions.LambdaReducerWithSeed; +import junit.framework.TestCase; + +public class TestMatrix extends TestCase { + + private static int TEST_SIZE = 100; + private static int TEST_SIZE2 = 200; + + public void testMapMatrix() { + PMatrix m = new IntMatrix(TEST_SIZE, TEST_SIZE2); + + for (int i = 0; i < TEST_SIZE; i++) { + for (int j = 0; j < TEST_SIZE2; j++) { + m.set(i, j, 0); + } + } + + PMatrix m2 = m.map(new LambdaMapper() { + + @Override + public Integer map(Integer input) { + return input + 1; + } + + public String getSource() { + return "return input+1;"; + } + }); + + for (int i = 0; i < TEST_SIZE; i++) { + for (int j = 0; j < TEST_SIZE2; j++) { + assertEquals(1, m.get(i, j).intValue()); + } + } + + int i = m.reduce(new LambdaReducerWithSeed() { + + public String getSource() { + return "return reduce_input_first + reduce_input_second;"; + } + + @Override + public Integer combine(Integer input, Integer other) { + return input + other; + } + + @Override + public Integer getSeed() { + return 0; + } + }); + + //assertEquals(TEST_SIZE * TEST_SIZE2, i); + + + PList li = m2.reduceLines(new LambdaReducerWithSeed() { + + public String getSource() { + return "return reduce_input_first + reduce_input_second;"; + } + + @Override + public Integer combine(Integer input, Integer other) { + return input + other; + } + + @Override + public Integer getSeed() { + return 0; + } + + }); + + for (int c = 0; c < li.length(); c++) { + assertEquals(TEST_SIZE, li.get(c).intValue()); + } + + + } + +} diff --git a/src/aeminium/gpu/tests/operations/TestRandom.java b/src/aeminium/gpu/tests/operations/TestRandom.java new file mode 100644 index 0000000..59bac23 --- /dev/null +++ b/src/aeminium/gpu/tests/operations/TestRandom.java @@ -0,0 +1,40 @@ +package aeminium.gpu.tests.operations; + +import junit.framework.TestCase; +import aeminium.gpu.collections.lazyness.RandomList; +import aeminium.gpu.collections.lists.PList; +import aeminium.gpu.operations.functions.LambdaMapper; + +public class TestRandom extends TestCase { + + private static int TEST_SIZE = 10000; + + public void testMapRandom() { + + LambdaMapper mapper = new LambdaMapper() { + + @Override + public Integer map(Float input) { + return Math.round(input * 10); + } + + public String getSource() { + return "return round(10 * input);"; + } + + }; + + PList output = new RandomList(TEST_SIZE, 123).map(mapper); + PList output2 = new RandomList(TEST_SIZE, 321).map(mapper); + + + assertEquals(output.size(), TEST_SIZE); + assertEquals(output2.size(), TEST_SIZE); + boolean cond = true; + for (int i = 0; i < TEST_SIZE; i++) { + cond = cond && (output.get(i).intValue() == output2.get(i).intValue()); + } + assertTrue(!cond); + } + +} diff --git a/src/aeminium/gpu/tests/operations/mergers/TestMapToMap.java b/src/aeminium/gpu/tests/operations/mergers/TestMapToMap.java deleted file mode 100644 index d1d811a..0000000 --- a/src/aeminium/gpu/tests/operations/mergers/TestMapToMap.java +++ /dev/null @@ -1,70 +0,0 @@ -package aeminium.gpu.tests.operations.mergers; - -import junit.framework.TestCase; -import aeminium.gpu.collections.lazyness.LazyPList; -import aeminium.gpu.collections.lists.IntList; -import aeminium.gpu.collections.lists.PList; -import aeminium.gpu.operations.functions.LambdaMapper; - -public class TestMapToMap extends TestCase { - - private static int TEST_SIZE = 10; - private static int MERGES = 10; - - public void testMapIntToInt() { - PList example = new IntList(); - for (int i = 0; i < TEST_SIZE; i++) { - example.add(i); - } - PList output = example; - int m = 1; - for (int i = 0; i < MERGES; i++) { - output = output.map(new LambdaMapper() { - - @Override - public Integer map(Integer input) { - return 2 * input; - } - - public String getSource() { - return "return 2 * input;"; - } - - }); - m *= 2; - } - - assertTrue(output instanceof LazyPList); - assertEquals(MERGES, ((LazyPList) output).getLazynessLevel()); - - for (int i = 0; i < TEST_SIZE; i++) { - assertEquals(m * i, output.get(i).intValue()); - } - } - - public void testClearAfterMap() { - PList example = new IntList(); - for (int i = 0; i < TEST_SIZE; i++) { - example.add(1); - } - - PList output = example.map(new LambdaMapper() { - - @Override - public Float map(Integer input) { - fail(); - return 2.0f * input; - } - - @Override - public String getSource() { - return "return 2.0 * input;"; - } - - }); - output.clear(); - output.add(1f); - assertEquals(1, output.size()); - - } -} diff --git a/src/aeminium/gpu/tests/operations/mergers/TestMapToReduce.java b/src/aeminium/gpu/tests/operations/mergers/TestMapToReduce.java deleted file mode 100644 index d45c29b..0000000 --- a/src/aeminium/gpu/tests/operations/mergers/TestMapToReduce.java +++ /dev/null @@ -1,142 +0,0 @@ -package aeminium.gpu.tests.operations.mergers; - -import junit.framework.TestCase; -import aeminium.gpu.collections.lists.IntList; -import aeminium.gpu.collections.lists.PList; -import aeminium.gpu.operations.functions.LambdaMapper; -import aeminium.gpu.operations.functions.LambdaReducerWithSeed; - -public class TestMapToReduce extends TestCase { - - private static int TEST_SIZE = 32; - - public void testMapToReduce() { - PList example = new IntList(); - for (int i = 0; i < TEST_SIZE; i++) { - example.add(1); - } - - Integer output = example.map(new LambdaMapper() { - - @Override - public Integer map(Integer input) { - return 2 * input; - } - - @Override - public String getSource() { - return "return 2 * input;"; - } - - }).reduce(new LambdaReducerWithSeed() { - - @Override - public Integer combine(Integer input, Integer other) { - return input + other; - } - - @Override - public Integer getSeed() { - return 0; - } - - @Override - public String getSource() { - return "return reduce_input_first + reduce_input_second;"; - } - - }); - assertEquals(2 * TEST_SIZE, output.intValue()); - } - - public void testMapToMapToReduce() { - PList example = new IntList(); - for (int i = 0; i < TEST_SIZE; i++) { - example.add(1); - } - - Integer output = example.map(new LambdaMapper() { - - @Override - public Integer map(Integer input) { - return 2 * input; - } - - @Override - public String getSource() { - return "return 2 * input;"; - } - - }).map(new LambdaMapper() { - - @Override - public Integer map(Integer input) { - return 3 * input; - } - - @Override - public String getSource() { - return "return 3 * input;"; - } - - }).reduce(new LambdaReducerWithSeed() { - - @Override - public Integer combine(Integer input, Integer other) { - return input + other; - } - - @Override - public Integer getSeed() { - return 0; - } - - @Override - public String getSource() { - return "return reduce_input_first + reduce_input_second;"; - } - - }); - assertEquals(2 * 3 * TEST_SIZE, output.intValue()); - } - - public void testMapToReduceWithDifferentTypes() { - PList example = new IntList(); - for (int i = 0; i < TEST_SIZE; i++) { - example.add(1); - } - - Float output = example.map(new LambdaMapper() { - - @Override - public Float map(Integer input) { - return 2.0f * input; - } - - @Override - public String getSource() { - return "return 2.0 * input;"; - } - - }).reduce(new LambdaReducerWithSeed() { - - @Override - public Float combine(Float input, Float other) { - return input + other; - } - - @Override - public Float getSeed() { - return 0f; - } - - @Override - public String getSource() { - return "return reduce_input_first + reduce_input_second;"; - } - - }); - assertEquals((float) (2.0 * TEST_SIZE), output.floatValue()); - } - -}