summaryrefslogtreecommitdiffstats
path: root/src/com/mbien/opencl/CLProgramBuilder.java
blob: 3f7ab07b15082c655e1d85855e41a2766e662e6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.mbien.opencl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
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()
 * @author Michael Bien
 */
public final class CLProgramBuilder {

    private transient CLProgram program;
    private final Set<String> optionList = new HashSet<String>();
    private final Set<String> defineList = new HashSet<String>();
    private final Set<CLDevice> deviceList = new HashSet<CLDevice>();

    public CLProgramBuilder() {  }

    public CLProgramBuilder(CLProgram program) {
        this.program = program;
    }

    public CLProgramBuilder withOption(String option) {
        this.optionList.add(option);
        return this;
    }

    public CLProgramBuilder withOptions(String... options) {
        for (String option : options) {
            this.optionList.add(option);
        }
        return this;
    }

    public CLProgramBuilder withDefine(String name) {
        this.defineList.add(CLProgram.define(name));
        return this;
    }

    public CLProgramBuilder withDefines(String... names) {
        for (String name : names) {
            this.defineList.add(CLProgram.define(name));
        }
        return this;
    }

    public CLProgramBuilder withDefine(String name, Object value) {
        this.defineList.add(CLProgram.define(name, value.toString()));
        return this;
    }

    public CLProgramBuilder withDefines(Map<String, String> defines) {
        for (String name : defines.keySet()) {
            defineList.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);
        }
        return this;
    }

    public CLProgramBuilder forDevices(CLDevice... devices) {
        for (CLDevice device : devices) {
            this.deviceList.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<String> setup = new ArrayList<String>();
        setup.addAll(optionList);
        setup.addAll(defineList);
        String options = CLProgram.optionsOf(setup.toArray(new String[setup.size()]));
        CLDevice[] devices = deviceList.toArray(new CLDevice[deviceList.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();
        return this;
    }

    public CLProgram getProgram() {
        return program;
    }

    /**
     * Sets the program which should be build.
     */
    public CLProgramBuilder setProgram(CLProgram program) {
        this.program = program;
        return this;
    }

    @Override
    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('}');
        return sb.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        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;

        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);
        return result;
    }
}