From 0857dbb04fe9259f1076e9559b822c5032c23461 Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Thu, 25 Feb 2010 19:13:15 +0100 Subject: introduced CLBuildConfiguration and CLProgramConfiguration interfaces for CLProgramBuilder. --- src/com/mbien/opencl/CLBuildConfiguration.java | 68 +++++++++++++++++++ src/com/mbien/opencl/CLContext.java | 12 ++-- src/com/mbien/opencl/CLProgram.java | 6 +- src/com/mbien/opencl/CLProgramBuilder.java | 83 ++++++++++++------------ src/com/mbien/opencl/CLProgramConfiguration.java | 38 +++++++++++ test/com/mbien/opencl/CLProgramTest.java | 3 +- 6 files changed, 158 insertions(+), 52 deletions(-) create mode 100644 src/com/mbien/opencl/CLBuildConfiguration.java create mode 100644 src/com/mbien/opencl/CLProgramConfiguration.java diff --git a/src/com/mbien/opencl/CLBuildConfiguration.java b/src/com/mbien/opencl/CLBuildConfiguration.java new file mode 100644 index 00000000..7b6ee1e2 --- /dev/null +++ b/src/com/mbien/opencl/CLBuildConfiguration.java @@ -0,0 +1,68 @@ +package com.mbien.opencl; + +import java.util.Map; + +/** + * Configuration representing everything needed to build an OpenCL program. + * @author Michael Bien + */ +public interface CLBuildConfiguration { + + /** + * Builds or rebuilds the program. + * @param program The program which should be build. + */ + public CLProgram build(CLProgram program); + + /** + * Adds the device as build target. + */ + public CLBuildConfiguration forDevice(CLDevice device); + + /** + * Adds the devices as build target. + */ + public CLBuildConfiguration forDevices(CLDevice... devices); + + /** + * Resets this builder's configuration like options, devices and definitions. + */ + public CLBuildConfiguration reset(); + + /** + * Adds the definition to the build configuration. + * @see public 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) + */ + public CLBuildConfiguration withDefine(String name, Object value); + + /** + * Adds the definitions to the build configuration. + * @see public 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) + */ + public CLBuildConfiguration withDefines(Map defines); + + /** + * Adds the compiler option to the build configuration. + * @see CompilerOptions + */ + public CLBuildConfiguration withOption(String option); + + /** + * Adds the compiler options to the build configuration. + * @see CompilerOptions + */ + public CLBuildConfiguration withOptions(String... options); + +} diff --git a/src/com/mbien/opencl/CLContext.java b/src/com/mbien/opencl/CLContext.java index 73146acd..4cc21d0f 100644 --- a/src/com/mbien/opencl/CLContext.java +++ b/src/com/mbien/opencl/CLContext.java @@ -192,15 +192,15 @@ public class CLContext extends CLObject implements CLResource { } /** - * Creates a program and reads the sources from stream, the program is not build yet. + * Creates a program and reads the source from stream, the program is not build yet. * @throws IOException when a IOException occurred while reading or closing the stream. */ - public CLProgram createProgram(InputStream sources) throws IOException { + public CLProgram createProgram(InputStream source) throws IOException { - if(sources == null) - throw new IllegalArgumentException("input stream for program sources must not be null"); + if(source == null) + throw new IllegalArgumentException("input stream for program source must not be null"); - BufferedReader reader = new BufferedReader(new InputStreamReader(sources)); + BufferedReader reader = new BufferedReader(new InputStreamReader(source)); StringBuilder sb = new StringBuilder(); String line; @@ -208,7 +208,7 @@ public class CLContext extends CLObject implements CLResource { while ((line = reader.readLine()) != null) sb.append(line).append("\n"); } finally { - sources.close(); + source.close(); } return createProgram(sb.toString()); diff --git a/src/com/mbien/opencl/CLProgram.java b/src/com/mbien/opencl/CLProgram.java index 562df69b..44e747bf 100644 --- a/src/com/mbien/opencl/CLProgram.java +++ b/src/com/mbien/opencl/CLProgram.java @@ -247,10 +247,10 @@ public class CLProgram extends CLObject implements CLResource { } /** - * Prepares the build for this program by returning a {@link CLProgramBuilder}. + * Prepares the build for this program by returning a new {@link CLProgramConfiguration}. */ - public CLProgramBuilder prepare() { - return new CLProgramBuilder(this); + public CLProgramConfiguration prepare() { + return CLProgramBuilder.createForProgram(this); } /** diff --git a/src/com/mbien/opencl/CLProgramBuilder.java b/src/com/mbien/opencl/CLProgramBuilder.java index 3f7ab07b..e342101e 100644 --- a/src/com/mbien/opencl/CLProgramBuilder.java +++ b/src/com/mbien/opencl/CLProgramBuilder.java @@ -13,99 +13,99 @@ import java.util.Set; * @see CLProgram#prepare() * @author Michael Bien */ -public final class CLProgramBuilder { +public final class CLProgramBuilder implements CLProgramConfiguration { private transient CLProgram program; - private final Set optionList = new HashSet(); - private final Set defineList = new HashSet(); - private final Set deviceList = new HashSet(); + private String source; + private Map binaries; - public CLProgramBuilder() { } + private final Set optionSet = new HashSet(); + private final Set defineSet = new HashSet(); + private final Set deviceSet = new HashSet(); - public CLProgramBuilder(CLProgram program) { + + private CLProgramBuilder() { } + + private CLProgramBuilder(CLProgram program) { this.program = program; } + public static CLProgramConfiguration createForProgram(CLProgram program) { + return new CLProgramBuilder(program); + } + + public static CLBuildConfiguration createConfiguration() { + return new CLProgramBuilder(); + } + public CLProgramBuilder withOption(String option) { - this.optionList.add(option); + optionSet.add(option); return this; } public CLProgramBuilder withOptions(String... options) { for (String option : options) { - this.optionList.add(option); + optionSet.add(option); } return this; } public CLProgramBuilder withDefine(String name) { - this.defineList.add(CLProgram.define(name)); + defineSet.add(CLProgram.define(name)); return this; } public CLProgramBuilder withDefines(String... names) { for (String name : names) { - this.defineList.add(CLProgram.define(name)); + defineSet.add(CLProgram.define(name)); } return this; } public CLProgramBuilder withDefine(String name, Object value) { - this.defineList.add(CLProgram.define(name, value.toString())); + defineSet.add(CLProgram.define(name, value.toString())); return this; } public CLProgramBuilder withDefines(Map defines) { for (String name : defines.keySet()) { - defineList.add(CLProgram.define(name, defines.get(name))); + defineSet.add(CLProgram.define(name, defines.get(name))); } return this; } public CLProgramBuilder forDevice(CLDevice device) { - CLDevice[] devices = new CLDevice[]{device}; - for (CLDevice device1 : devices) { - this.deviceList.add(device1); - } + deviceSet.add(device); return this; } public CLProgramBuilder forDevices(CLDevice... devices) { for (CLDevice device : devices) { - this.deviceList.add(device); + deviceSet.add(device); } return this; } - /** - * Builds or rebuilds a program. - */ public CLProgram build() { return build(program); } - /** - * Builds or rebuilds a program. - */ public CLProgram build(CLProgram program) { if(program == null) { throw new NullPointerException("no program has been set"); } List setup = new ArrayList(); - setup.addAll(optionList); - setup.addAll(defineList); + setup.addAll(optionSet); + setup.addAll(defineSet); String options = CLProgram.optionsOf(setup.toArray(new String[setup.size()])); - CLDevice[] devices = deviceList.toArray(new CLDevice[deviceList.size()]); + CLDevice[] devices = deviceSet.toArray(new CLDevice[deviceSet.size()]); return program.build(options, devices); } - /** - * Resets this builder's configuration like options, devices and defines. - */ public CLProgramBuilder reset() { - optionList.clear(); - defineList.clear(); - deviceList.clear(); + optionSet.clear(); + defineSet.clear(); + deviceSet.clear(); return this; } @@ -125,9 +125,9 @@ public final class CLProgramBuilder { public String toString() { final StringBuilder sb = new StringBuilder(); sb.append("CLProgramBuilder"); - sb.append("{optionList=").append(optionList); - sb.append(", defineList=").append(defineList); - sb.append(", deviceList=").append(deviceList); + sb.append("{options=").append(optionSet); + sb.append(", defines=").append(defineSet); + sb.append(", devices=").append(deviceSet); sb.append('}'); return sb.toString(); } @@ -139,18 +139,19 @@ public final class CLProgramBuilder { CLProgramBuilder that = (CLProgramBuilder) o; - if (defineList != null ? !defineList.equals(that.defineList) : that.defineList != null) return false; - if (deviceList != null ? !deviceList.equals(that.deviceList) : that.deviceList != null) return false; - if (optionList != null ? !optionList.equals(that.optionList) : that.optionList != 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; return true; } @Override public int hashCode() { - int result = optionList != null ? optionList.hashCode() : 0; - result = 31 * result + (defineList != null ? defineList.hashCode() : 0); - result = 31 * result + (deviceList != null ? deviceList.hashCode() : 0); + int result = optionSet != null ? optionSet.hashCode() : 0; + result = 31 * result + (defineSet != null ? defineSet.hashCode() : 0); + result = 31 * result + (deviceSet != null ? deviceSet.hashCode() : 0); return result; } + } diff --git a/src/com/mbien/opencl/CLProgramConfiguration.java b/src/com/mbien/opencl/CLProgramConfiguration.java new file mode 100644 index 00000000..e13ffd63 --- /dev/null +++ b/src/com/mbien/opencl/CLProgramConfiguration.java @@ -0,0 +1,38 @@ +package com.mbien.opencl; + +import java.util.Map; + +/** + * Configuration representing everything needed to build an OpenCL program (program included). + * @author Michael Bien + */ +public interface CLProgramConfiguration extends CLBuildConfiguration { + + /** + * Builds or rebuilds a program. + */ + public CLProgram build(); + + /** + * Returns the program. + */ + 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); + @Override public CLProgramConfiguration forDevices(CLDevice... devices); + @Override public CLProgramConfiguration withDefine(String name); + @Override public CLProgramConfiguration withDefine(String name, Object value); + @Override public CLProgramConfiguration withDefines(String... names); + @Override public CLProgramConfiguration withDefines(Map defines); + @Override public CLProgramConfiguration withOption(String option); + @Override public CLProgramConfiguration withOptions(String... options); + @Override public CLProgramConfiguration reset(); + +} diff --git a/test/com/mbien/opencl/CLProgramTest.java b/test/com/mbien/opencl/CLProgramTest.java index 95ed5e8c..a1392bfc 100644 --- a/test/com/mbien/opencl/CLProgramTest.java +++ b/test/com/mbien/opencl/CLProgramTest.java @@ -167,12 +167,11 @@ public class CLProgramTest { // program.release(); // reusable builder - CLProgramBuilder builder = new CLProgramBuilder() + CLBuildConfiguration builder = CLProgramBuilder.createConfiguration() .withOption(ENABLE_MAD) .forDevice(context.getMaxFlopsDevice()) .withDefine("RADIUS", 5) .withDefine("ENABLE_FOOBAR"); - builder.build(program); assertTrue(program.isExecutable()); -- cgit v1.2.3