From 75d392f3fff6d47fdccae9a8915fea61cdbef990 Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Fri, 26 Feb 2010 17:32:34 +0100 Subject: improved CLProgramBuilder and implemented propper configuration loading. --- src/com/mbien/opencl/CLBuildConfiguration.java | 24 ++++++- src/com/mbien/opencl/CLProgramBuilder.java | 90 ++++++++++++++++++++++-- src/com/mbien/opencl/CLProgramConfiguration.java | 11 +++ 3 files changed, 116 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/com/mbien/opencl/CLBuildConfiguration.java b/src/com/mbien/opencl/CLBuildConfiguration.java index 86b5cfa2..eee641a0 100644 --- a/src/com/mbien/opencl/CLBuildConfiguration.java +++ b/src/com/mbien/opencl/CLBuildConfiguration.java @@ -1,13 +1,14 @@ 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 + * @see CLProgramBuilder#createConfiguration() + * @see CLProgramBuilder#loadConfiguration(java.io.ObjectInputStream) */ public interface CLBuildConfiguration extends Cloneable { @@ -37,6 +38,21 @@ public interface CLBuildConfiguration extends Cloneable { */ public CLBuildConfiguration reset(); + /** + * Resets this builder's configuration options. + */ + public CLBuildConfiguration resetOptions(); + + /** + * Resets this builder's macro definitions. + */ + public CLBuildConfiguration resetDefines(); + + /** + * Resets this builder's device list. + */ + public CLBuildConfiguration resetDevices(); + /** * Adds the definition to the build configuration. * @see CLProgram#define(java.lang.String) @@ -78,8 +94,10 @@ public interface CLBuildConfiguration extends Cloneable { */ public CLBuildConfiguration clone(); + /** + * Saves this configuration to the ObjectOutputStream. + * The caller is responsible for closing the stream. + */ public void save(ObjectOutputStream oos) throws IOException; - public CLBuildConfiguration load(ObjectInputStream ois) throws IOException, ClassNotFoundException; - } diff --git a/src/com/mbien/opencl/CLProgramBuilder.java b/src/com/mbien/opencl/CLProgramBuilder.java index 5948309e..fef7d878 100644 --- a/src/com/mbien/opencl/CLProgramBuilder.java +++ b/src/com/mbien/opencl/CLProgramBuilder.java @@ -20,7 +20,8 @@ import java.util.Set; * @see CLProgram#prepare() * @see #createConfiguration() * @see #createConfiguration(com.mbien.opencl.CLProgram) - * @see #load(java.io.ObjectInputStream) + * @see #loadConfiguration(java.io.ObjectInputStream) + * @see #loadConfiguration(java.io.ObjectInputStream, com.mbien.opencl.CLContext) * @author Michael Bien */ public final class CLProgramBuilder implements CLProgramConfiguration, Serializable { @@ -54,14 +55,58 @@ public final class CLProgramBuilder implements CLProgramConfiguration, Serializa } } + /** + * Creates a new CLBuildConfiguration. + */ public static CLBuildConfiguration createConfiguration() { return createConfiguration(null); } + /** + * Creates a new CLProgramConfiguration for this program. + */ public static CLProgramConfiguration createConfiguration(CLProgram program) { return new CLProgramBuilder(program); } + /** + * Loads a CLBuildConfiguration. + * @param ois The ObjectInputStream for reading the object. + */ + public static CLBuildConfiguration loadConfiguration(ObjectInputStream ois) throws IOException, ClassNotFoundException { + return (CLBuildConfiguration) ois.readObject(); + } + + /** + * Loads a CLProgramConfiguration containing a CLProgram. + * The CLProgram is initialized and ready to be build after this method call. + * This method preferes program initialization from binaries if this fails or if + * no binaries have been found, it will try to load the program from sources. If + * This also fails an appropriate exception will be thrown. + * @param ois The ObjectInputStream for reading the object. + * @param context The context used for program initialization. + */ + public static CLProgramConfiguration loadConfiguration(ObjectInputStream ois, CLContext context) throws IOException, ClassNotFoundException { + CLProgramBuilder config = (CLProgramBuilder) ois.readObject(); + if(config.binariesMap.size() > 0 && config.binariesMap.values().iterator().next().length > 0) { + try{ + config.program = context.createProgram(config.binariesMap); + }catch(CLException.CLInvalidBinaryException ex) { + if(config.source != null) { + config.program = context.createProgram(config.source); + }else{ + throw new IOException("Program configuration contained invalid program binaries and no source.", ex); + } + } + }else if(config.source != null) { + config.program = context.createProgram(config.source); + }else{ + throw new IOException("Program configuration did not contain program sources or binaries"); + } + return config; + } + + @Override public void save(ObjectOutputStream oos) throws IOException { if(program != null) { this.source = program.getSource(); @@ -72,16 +117,14 @@ public final class CLProgramBuilder implements CLProgramConfiguration, Serializa oos.writeObject(this); } - public CLProgramConfiguration load(ObjectInputStream ois) throws IOException, ClassNotFoundException { - return (CLProgramConfiguration) ois.readObject(); - } - + @Override public CLProgramBuilder withOption(String option) { optionSet.add(option); return this; } + @Override public CLProgramBuilder withOptions(String... options) { for (String option : options) { optionSet.add(option); @@ -89,11 +132,13 @@ public final class CLProgramBuilder implements CLProgramConfiguration, Serializa return this; } + @Override public CLProgramBuilder withDefine(String name) { defineSet.add(CLProgram.define(name)); return this; } + @Override public CLProgramBuilder withDefines(String... names) { for (String name : names) { defineSet.add(CLProgram.define(name)); @@ -101,11 +146,13 @@ public final class CLProgramBuilder implements CLProgramConfiguration, Serializa return this; } + @Override public CLProgramBuilder withDefine(String name, Object value) { defineSet.add(CLProgram.define(name, value.toString())); return this; } + @Override public CLProgramBuilder withDefines(Map defines) { for (String name : defines.keySet()) { defineSet.add(CLProgram.define(name, defines.get(name))); @@ -113,11 +160,13 @@ public final class CLProgramBuilder implements CLProgramConfiguration, Serializa return this; } + @Override public CLProgramBuilder forDevice(CLDevice device) { binariesMap.put(device, NO_BINARIES); return this; } + @Override public CLProgramBuilder forDevices(CLDevice... devices) { for (CLDevice device : devices) { binariesMap.put(device, NO_BINARIES); @@ -125,10 +174,12 @@ public final class CLProgramBuilder implements CLProgramConfiguration, Serializa return this; } + @Override public CLProgram build() { return build(program); } + @Override public CLProgram build(CLProgram program) { if(program == null) { throw new NullPointerException("no program has been set"); @@ -141,13 +192,32 @@ public final class CLProgramBuilder implements CLProgramConfiguration, Serializa return program.build(options, devices); } + @Override public CLProgramBuilder reset() { - optionSet.clear(); + resetOptions(); + resetDefines(); + resetDevices(); + return this; + } + + @Override + public CLProgramConfiguration resetDefines() { defineSet.clear(); + return this; + } + + @Override + public CLProgramConfiguration resetDevices() { binariesMap.clear(); return this; } + @Override + public CLProgramConfiguration resetOptions() { + optionSet.clear(); + return this; + } + private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeInt(binariesMap.size()); @@ -176,6 +246,14 @@ public final class CLProgramBuilder implements CLProgramConfiguration, Serializa } } + @Override + public CLProgramBuilder asBuildConfiguration() { + CLProgramBuilder builder = new CLProgramBuilder(); + builder.defineSet.addAll(defineSet); + builder.optionSet.addAll(optionSet); + return builder; + } + @Override public CLProgramBuilder clone() { CLProgramBuilder builder = new CLProgramBuilder(program, source, binariesMap); diff --git a/src/com/mbien/opencl/CLProgramConfiguration.java b/src/com/mbien/opencl/CLProgramConfiguration.java index fdd0b804..fc074f33 100644 --- a/src/com/mbien/opencl/CLProgramConfiguration.java +++ b/src/com/mbien/opencl/CLProgramConfiguration.java @@ -7,6 +7,8 @@ import java.util.Map; * CLProgramConfiguration is a helper for building programs with more complex configurations or * building multiple programs with the similar configuration. * @see CLProgram#prepare() + * @see CLProgramBuilder#createConfiguration(com.mbien.opencl.CLProgram) + * @see CLProgramBuilder#loadConfiguration(java.io.ObjectInputStream, com.mbien.opencl.CLContext) * @author Michael Bien */ public interface CLProgramConfiguration extends CLBuildConfiguration { @@ -21,6 +23,12 @@ public interface CLProgramConfiguration extends CLBuildConfiguration { */ public CLProgram getProgram(); + /** + * Returns a new instance of of this configuration without a {@link CLProgram}, + * program binaries or sources associated with it. + */ + public CLBuildConfiguration asBuildConfiguration(); + // overwrite with CLProgramConfiguration as return type @Override public CLProgramConfiguration forDevice(CLDevice device); @@ -32,6 +40,9 @@ public interface CLProgramConfiguration extends CLBuildConfiguration { @Override public CLProgramConfiguration withOption(String option); @Override public CLProgramConfiguration withOptions(String... options); @Override public CLProgramConfiguration reset(); + @Override public CLProgramConfiguration resetOptions(); + @Override public CLProgramConfiguration resetDefines(); + @Override public CLProgramConfiguration resetDevices(); @Override public CLProgramConfiguration clone(); } -- cgit v1.2.3