diff options
-rw-r--r-- | lib/junit_4/junit-4.5-api.zip | bin | 184067 -> 0 bytes | |||
-rw-r--r-- | lib/junit_4/junit-4.5-src.jar | bin | 109073 -> 0 bytes | |||
-rw-r--r-- | lib/junit_4/junit-4.5.jar | bin | 198945 -> 0 bytes | |||
-rw-r--r-- | lib/junit_4/junit-4.8.1.jar | bin | 0 -> 237047 bytes | |||
-rw-r--r-- | nbproject/project.properties | 3 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLBuildConfiguration.java | 33 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLProgram.java | 7 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLProgramBuilder.java | 128 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLProgramConfiguration.java | 9 | ||||
-rw-r--r-- | test/com/mbien/opencl/CLProgramTest.java | 40 |
10 files changed, 176 insertions, 44 deletions
diff --git a/lib/junit_4/junit-4.5-api.zip b/lib/junit_4/junit-4.5-api.zip Binary files differdeleted file mode 100644 index 5748c444..00000000 --- a/lib/junit_4/junit-4.5-api.zip +++ /dev/null diff --git a/lib/junit_4/junit-4.5-src.jar b/lib/junit_4/junit-4.5-src.jar Binary files differdeleted file mode 100644 index 14fa2ea5..00000000 --- a/lib/junit_4/junit-4.5-src.jar +++ /dev/null diff --git a/lib/junit_4/junit-4.5.jar b/lib/junit_4/junit-4.5.jar Binary files differdeleted file mode 100644 index 73392162..00000000 --- a/lib/junit_4/junit-4.5.jar +++ /dev/null diff --git a/lib/junit_4/junit-4.8.1.jar b/lib/junit_4/junit-4.8.1.jar Binary files differnew file mode 100644 index 00000000..524cd65c --- /dev/null +++ b/lib/junit_4/junit-4.8.1.jar diff --git a/nbproject/project.properties b/nbproject/project.properties index 8622d80a..41e04c4a 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -23,6 +23,7 @@ dist.javadoc.dir=${dist.dir}/javadoc endorsed.classpath= excludes= +file.reference.junit-4.8.1.jar=lib/junit_4/junit-4.8.1.jar #default value, overwrite this or pass it as -D ant property if required gluegen.root=../gluegen jogl.root=../jogl @@ -47,7 +48,7 @@ javac.target=1.5 javac.test.classpath=\ ${javac.classpath}:\ ${build.classes.dir}:\ - ${libs.junit_4.classpath} + ${file.reference.junit-4.8.1.jar} javadoc.additionalparam= javadoc.author=true javadoc.encoding=${source.encoding} diff --git a/src/com/mbien/opencl/CLBuildConfiguration.java b/src/com/mbien/opencl/CLBuildConfiguration.java index 7b6ee1e2..86b5cfa2 100644 --- a/src/com/mbien/opencl/CLBuildConfiguration.java +++ b/src/com/mbien/opencl/CLBuildConfiguration.java @@ -1,20 +1,28 @@ package com.mbien.opencl; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.util.Map; /** * Configuration representing everything needed to build an OpenCL program. * @author Michael Bien */ -public interface CLBuildConfiguration { +public interface CLBuildConfiguration extends Cloneable { /** * Builds or rebuilds the program. - * @param program The program which should be build. + * @param program The program which should be build. */ public CLProgram build(CLProgram program); /** + * Sets the program which should be build. + */ + public CLProgramConfiguration setProgram(CLProgram program); + + /** * Adds the device as build target. */ public CLBuildConfiguration forDevice(CLDevice device); @@ -31,38 +39,47 @@ public interface CLBuildConfiguration { /** * Adds the definition to the build configuration. - * @see public CLProgram#define(java.lang.String) + * @see CLProgram#define(java.lang.String) */ public CLBuildConfiguration withDefine(String name); /** * Adds the definition to the build configuration. - * @see public CLProgram#define(java.lang.String, java.lang.String) + * @see CLProgram#define(java.lang.String, java.lang.String) */ public CLBuildConfiguration withDefine(String name, Object value); /** * Adds the definitions to the build configuration. - * @see public CLProgram#define(java.lang.String) + * @see CLProgram#define(java.lang.String) */ public CLBuildConfiguration withDefines(String... names); /** * Adds the definitions to the build configuration. - * @see public CLProgram#define(java.lang.String, java.lang.String) + * @see CLProgram#define(java.lang.String, java.lang.String) */ public CLBuildConfiguration withDefines(Map<String, String> defines); /** * Adds the compiler option to the build configuration. - * @see CompilerOptions + * @see CLProgram.CompilerOptions */ public CLBuildConfiguration withOption(String option); /** * Adds the compiler options to the build configuration. - * @see CompilerOptions + * @see CLProgram.CompilerOptions */ public CLBuildConfiguration withOptions(String... options); + /** + * Clones this configuration. + */ + public CLBuildConfiguration clone(); + + public void save(ObjectOutputStream oos) throws IOException; + + public CLBuildConfiguration load(ObjectInputStream ois) throws IOException, ClassNotFoundException; + } diff --git a/src/com/mbien/opencl/CLProgram.java b/src/com/mbien/opencl/CLProgram.java index 44e747bf..a5d1311c 100644 --- a/src/com/mbien/opencl/CLProgram.java +++ b/src/com/mbien/opencl/CLProgram.java @@ -9,6 +9,7 @@ import java.nio.IntBuffer; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.Set; import java.util.Map; @@ -250,7 +251,7 @@ public class CLProgram extends CLObject implements CLResource { * Prepares the build for this program by returning a new {@link CLProgramConfiguration}. */ public CLProgramConfiguration prepare() { - return CLProgramBuilder.createForProgram(this); + return CLProgramBuilder.createConfiguration(this); } /** @@ -437,7 +438,7 @@ public class CLProgram extends CLObject implements CLResource { } /** - * Returns the binaries for this program in a Map containing the device as key + * Returns the binaries for this program in an ordered Map containing the device as key * and the program binaries as value. */ public Map<CLDevice, byte[]> getBinaries() { @@ -471,7 +472,7 @@ public class CLProgram extends CLObject implements CLResource { ret = cl.clGetProgramInfo(ID, CL_PROGRAM_BINARIES, addresses.capacity(), addresses.getBuffer(), null); checkForError(ret, "on clGetProgramInfo"); - Map<CLDevice, byte[]> map = new HashMap<CLDevice, byte[]>(); + Map<CLDevice, byte[]> map = new LinkedHashMap<CLDevice, byte[]>(); sizes.rewind(); for (int i = 0; i < devices.length; i++) { byte[] bytes = new byte[(int)sizes.getLong()]; diff --git a/src/com/mbien/opencl/CLProgramBuilder.java b/src/com/mbien/opencl/CLProgramBuilder.java index e342101e..5948309e 100644 --- a/src/com/mbien/opencl/CLProgramBuilder.java +++ b/src/com/mbien/opencl/CLProgramBuilder.java @@ -1,7 +1,14 @@ package com.mbien.opencl; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; import java.util.ArrayList; -import java.util.HashSet; +import java.util.Arrays; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -11,33 +18,65 @@ import java.util.Set; * CLProgramBuilder is a helper for building programs with more complex configurations or * building multiple programs with the same configuration. * @see CLProgram#prepare() + * @see #createConfiguration() + * @see #createConfiguration(com.mbien.opencl.CLProgram) + * @see #load(java.io.ObjectInputStream) * @author Michael Bien */ -public final class CLProgramBuilder implements CLProgramConfiguration { +public final class CLProgramBuilder implements CLProgramConfiguration, Serializable { + + static final long serialVersionUID = 42; + + private static final byte[] NO_BINARIES = new byte[0]; private transient CLProgram program; + private transient Map<CLDevice, byte[]> binariesMap = new LinkedHashMap<CLDevice, byte[]>(); + private String source; - private Map<CLDevice, byte[]> binaries; - private final Set<String> optionSet = new HashSet<String>(); - private final Set<String> defineSet = new HashSet<String>(); - private final Set<CLDevice> deviceSet = new HashSet<CLDevice>(); + private final Set<String> optionSet = new LinkedHashSet<String>(); + private final Set<String> defineSet = new LinkedHashSet<String>(); - private CLProgramBuilder() { } + private CLProgramBuilder() { + this(null); + } private CLProgramBuilder(CLProgram program) { + this(program, null, null); + } + + private CLProgramBuilder(CLProgram program, String source, Map<CLDevice, byte[]> map) { this.program = program; + this.source = source; + if(map != null) { + this.binariesMap.putAll(map); + } } - public static CLProgramConfiguration createForProgram(CLProgram program) { + public static CLBuildConfiguration createConfiguration() { + return createConfiguration(null); + } + + public static CLProgramConfiguration createConfiguration(CLProgram program) { return new CLProgramBuilder(program); } - public static CLBuildConfiguration createConfiguration() { - return new CLProgramBuilder(); + public void save(ObjectOutputStream oos) throws IOException { + if(program != null) { + this.source = program.getSource(); + if(program.isExecutable()) { + binariesMap = program.getBinaries(); + } + } + oos.writeObject(this); } + public CLProgramConfiguration load(ObjectInputStream ois) throws IOException, ClassNotFoundException { + return (CLProgramConfiguration) ois.readObject(); + } + + public CLProgramBuilder withOption(String option) { optionSet.add(option); return this; @@ -75,13 +114,13 @@ public final class CLProgramBuilder implements CLProgramConfiguration { } public CLProgramBuilder forDevice(CLDevice device) { - deviceSet.add(device); + binariesMap.put(device, NO_BINARIES); return this; } public CLProgramBuilder forDevices(CLDevice... devices) { for (CLDevice device : devices) { - deviceSet.add(device); + binariesMap.put(device, NO_BINARIES); } return this; } @@ -98,24 +137,57 @@ public final class CLProgramBuilder implements CLProgramConfiguration { setup.addAll(optionSet); setup.addAll(defineSet); String options = CLProgram.optionsOf(setup.toArray(new String[setup.size()])); - CLDevice[] devices = deviceSet.toArray(new CLDevice[deviceSet.size()]); + CLDevice[] devices = binariesMap.keySet().toArray(new CLDevice[binariesMap.size()]); return program.build(options, devices); } public CLProgramBuilder reset() { optionSet.clear(); defineSet.clear(); - deviceSet.clear(); + binariesMap.clear(); return this; } + private void writeObject(ObjectOutputStream out) throws IOException { + out.defaultWriteObject(); + out.writeInt(binariesMap.size()); + + for (CLDevice device : binariesMap.keySet()) { + byte[] binaries = binariesMap.get(device); + out.writeLong(device.ID); + out.writeInt(binaries.length); + out.write(binaries); + } + } + + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { + in.defaultReadObject(); + this.binariesMap = new LinkedHashMap<CLDevice, byte[]>(); + int mapSize = in.readInt(); + + for (int i = 0; i < mapSize; i++) { + long deviceID = in.readLong(); + int length = in.readInt(); + byte[] binaries = new byte[length]; + in.readFully(binaries); + + CLDevice device = new CLDevice(CLPlatform.getLowLevelCLInterface(), deviceID); + binariesMap.put(device, binaries); + } + } + + @Override + public CLProgramBuilder clone() { + CLProgramBuilder builder = new CLProgramBuilder(program, source, binariesMap); + builder.defineSet.addAll(defineSet); + builder.optionSet.addAll(optionSet); + return builder; + } + public CLProgram getProgram() { return program; } - /** - * Sets the program which should be build. - */ public CLProgramBuilder setProgram(CLProgram program) { this.program = program; return this; @@ -127,7 +199,7 @@ public final class CLProgramBuilder implements CLProgramConfiguration { sb.append("CLProgramBuilder"); sb.append("{options=").append(optionSet); sb.append(", defines=").append(defineSet); - sb.append(", devices=").append(deviceSet); + sb.append(", devices=").append(binariesMap); sb.append('}'); return sb.toString(); } @@ -139,10 +211,26 @@ public final class CLProgramBuilder implements CLProgramConfiguration { CLProgramBuilder that = (CLProgramBuilder) o; + if (source != null ? !source.equals(that.source) : that.source != null) return false; if (defineSet != null ? !defineSet.equals(that.defineSet) : that.defineSet != null) return false; - if (deviceSet != null ? !deviceSet.equals(that.deviceSet) : that.deviceSet != null) return false; if (optionSet != null ? !optionSet.equals(that.optionSet) : that.optionSet != null) return false; + if(binariesMap != null && that.binariesMap != null) { + if(binariesMap.size() != that.binariesMap.size()) { + return false; + } + Iterator<CLDevice> iterator0 = binariesMap.keySet().iterator(); + Iterator<CLDevice> iterator1 = that.binariesMap.keySet().iterator(); + for (int i = 0; i < binariesMap.size(); i++) { + CLDevice device0 = iterator0.next(); + CLDevice device1 = iterator1.next(); + if(!device0.equals(device1) || !Arrays.equals(binariesMap.get(device0), that.binariesMap.get(device1))) + return false; + } + }else if(binariesMap != null || that.binariesMap != null) { + return false; + } + return true; } @@ -150,7 +238,7 @@ public final class CLProgramBuilder implements CLProgramConfiguration { public int hashCode() { int result = optionSet != null ? optionSet.hashCode() : 0; result = 31 * result + (defineSet != null ? defineSet.hashCode() : 0); - result = 31 * result + (deviceSet != null ? deviceSet.hashCode() : 0); + result = 31 * result + (binariesMap != null ? binariesMap.hashCode() : 0); return result; } diff --git a/src/com/mbien/opencl/CLProgramConfiguration.java b/src/com/mbien/opencl/CLProgramConfiguration.java index e13ffd63..fdd0b804 100644 --- a/src/com/mbien/opencl/CLProgramConfiguration.java +++ b/src/com/mbien/opencl/CLProgramConfiguration.java @@ -4,6 +4,9 @@ import java.util.Map; /** * Configuration representing everything needed to build an OpenCL program (program included). + * CLProgramConfiguration is a helper for building programs with more complex configurations or + * building multiple programs with the similar configuration. + * @see CLProgram#prepare() * @author Michael Bien */ public interface CLProgramConfiguration extends CLBuildConfiguration { @@ -18,11 +21,6 @@ public interface CLProgramConfiguration extends CLBuildConfiguration { */ public CLProgram getProgram(); - /** - * Sets the program which should be build. - */ - public CLProgramConfiguration setProgram(CLProgram program); - // overwrite with CLProgramConfiguration as return type @Override public CLProgramConfiguration forDevice(CLDevice device); @@ -34,5 +32,6 @@ public interface CLProgramConfiguration extends CLBuildConfiguration { @Override public CLProgramConfiguration withOption(String option); @Override public CLProgramConfiguration withOptions(String... options); @Override public CLProgramConfiguration reset(); + @Override public CLProgramConfiguration clone(); } diff --git a/test/com/mbien/opencl/CLProgramTest.java b/test/com/mbien/opencl/CLProgramTest.java index a1392bfc..16684738 100644 --- a/test/com/mbien/opencl/CLProgramTest.java +++ b/test/com/mbien/opencl/CLProgramTest.java @@ -1,9 +1,16 @@ package com.mbien.opencl; import com.mbien.opencl.CLProgram.Status; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.util.Map; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import static org.junit.Assert.*; import static java.lang.System.*; @@ -15,6 +22,10 @@ import static com.mbien.opencl.CLProgram.CompilerOptions.*; */ public class CLProgramTest { + @Rule + public TemporaryFolder tmpFolder = new TemporaryFolder(); + + @Test public void enumsTest() { @@ -143,8 +154,8 @@ public class CLProgramTest { } @Test - public void builderTest() throws IOException { - out.println(" - - - CLProgramTest; builder test - - - "); + public void builderTest() throws IOException, ClassNotFoundException { + out.println(" - - - CLProgramTest; program builder test - - - "); CLContext context = CLContext.create(); CLProgram program = context.createProgram(getClass().getResourceAsStream("testkernels.cl")); @@ -153,7 +164,6 @@ public class CLProgramTest { program.prepare().build(); assertTrue(program.isExecutable()); -// program.release(); // complex build @@ -164,18 +174,34 @@ public class CLProgramTest { .build(); assertTrue(program.isExecutable()); -// program.release(); // reusable builder CLBuildConfiguration builder = CLProgramBuilder.createConfiguration() .withOption(ENABLE_MAD) - .forDevice(context.getMaxFlopsDevice()) + .forDevices(context.getDevices()) .withDefine("RADIUS", 5) .withDefine("ENABLE_FOOBAR"); - builder.build(program); + + builder.setProgram(program).build(); + assertTrue(program.isExecutable()); + // serialization test + File file = tmpFolder.newFile("foobar.builder"); + ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file)); + builder.save(oos); + oos.close(); + + ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); + CLBuildConfiguration builder2 = builder.load(ois); + ois.close(); + + assertEquals(builder, builder2); + + builder2.build(program); assertTrue(program.isExecutable()); -// program.release(); + + // cloneing + assertEquals(builder, builder.clone()); } |