MCMContext.java
6.23 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package mcmas.core;
import java.nio.Buffer;
import mcmas.utils.Objects7;
import org.jocl.CL;
import org.jocl.Pointer;
import org.jocl.cl_context;
import org.jocl.cl_device_id;
import org.jocl.cl_image_format;
import org.jocl.cl_platform_id;
public class MCMContext extends MCMObject {
private final cl_context context;
//private final static Logger logger = LoggerFactory.getLogger(OCLContext.class);
/**
* Create a new context using the first available hardware among
* default devices types.
*/
public MCMContext() {
this(getDefaultContextTypes());
}
/**
* Create a new context using the first available hardware among
* indicated device types.
*
* @param deviceTypes device types to search for
*/
public MCMContext(long... deviceTypes) {
MCMPlatform[] platforms = MCMPlatform.getPlatforms();
if (platforms.length <= 0) {
throw new RuntimeException("No OpenCL platform found");
}
this.context = MCMHelpers.createContextFromType(platforms[0].getId(), deviceTypes);
if (context == null) {
throw new RuntimeException("No OpenCL device found");
}
}
/**
* Create a new context using the indicated platform.
*
* @param platform platform to consider
*/
public MCMContext(MCMPlatform platform) {
this(platform, getDefaultContextTypes());
}
/**
* Create a new context using the indicated platform and device types.
*
* @param platform platform to consider
* @param deviceTypes device types to search for
*/
public MCMContext(MCMPlatform platform, long... deviceTypes) {
platform = Objects7.requireNonNull(platform, "Can't create an OpenCL context with a null platform");
System.out.println("platform vendor : " + platform.getInfoString(CL.CL_PLATFORM_VENDOR));
System.out.println("platform name : " + platform.getInfoString(CL.CL_PLATFORM_NAME));
this.context = MCMHelpers.createContextFromType(platform.getId(), deviceTypes);
if (context == null) {
throw new RuntimeException("No OpenCL device found");
}
}
/**
* Return a list of default device types to test when nothing
* is indicated at the MCMContext creation.
*
* The default list indicates to use these kind of hardware in this order:
* GPU, Accelerators (such as Xeon Phi) and CPU.
*
* It can be overriden using the MCM_CONTEXT_TYPE environment variable,
* which admits the following values: "CPU", "GPU", "ACCELERATOR", "ALL".
*
* "ALL" considers all the devices available on the system, but the
* order is not garanteed (even for the same type of device) and depends
* of the installed runtimes.
*
* @return a list of context type to try.
*/
public static long[] getDefaultContextTypes() {
String context_type = System.getenv("MCM_CONTEXT_TYPE");
if ("CPU".equals(context_type)) {
return new long[] { CL.CL_DEVICE_TYPE_CPU };
} else if ("GPU".equals(context_type)) {
return new long[] { CL.CL_DEVICE_TYPE_GPU };
} else if ("ACCELERATOR".equals(context_type)) {
return new long[] { CL.CL_DEVICE_TYPE_ACCELERATOR };
} else if ("ALL".equals(context_type)) {
return new long[] { CL.CL_DEVICE_TYPE_ALL };
} else {
return new long[] {
CL.CL_DEVICE_TYPE_GPU,
CL.CL_DEVICE_TYPE_ACCELERATOR,
CL.CL_DEVICE_TYPE_CPU
};
}
}
public cl_context getContext() {
return context;
}
public MCMPlatform[] getPlatforms() {
cl_platform_id[] platform_ids = MCMHelpers.getPlatforms();
MCMPlatform[] platforms = new MCMPlatform[platform_ids.length];
for (int i = 0; i < platform_ids.length; i++) {
platforms[i] = new MCMPlatform(platform_ids[i]);
}
return platforms;
}
public MCMDevice[] getDevices() {
cl_device_id[] device_ids = MCMHelpers.getDevices(context);
MCMDevice[] devices = new MCMDevice[device_ids.length];
for (int i = 0; i < device_ids.length; i++) {
devices[i] = new MCMDevice(device_ids[i]);
}
return devices;
}
public MCMCommandQueue createCommandQueue(MCMCommandQueueProperty... properties) {
MCMCommandQueue q = new MCMCommandQueue(this, properties);
this.register(q);
return q;
}
public MCMCommandQueue createCommandQueue(MCMDevice device, MCMCommandQueueProperty... properties) {
MCMCommandQueue q = new MCMCommandQueue(this, device, properties);
this.register(q);
return q;
}
public MCMProgram createProgram(String source, String... options) {
MCMProgram p = new MCMProgram(this, source, options);
this.register(p);
return p;
}
public MCMProgram createProgram(String source, MCMProgramOptions options) {
MCMProgram p = new MCMProgram(this, source, options.toString());
this.register(p);
return p;
}
public MCMProgram createProgramFromFile(String file, String... options) {
MCMProgram p = new MCMProgram(this, MCMUtils.readFileAsString(file), options);
this.register(p);
return p;
}
public MCMProgram createProgramFromFile(String file, MCMProgramOptions options) {
MCMProgram p = new MCMProgram(this, MCMUtils.readFileAsString(file), options.toString());
this.register(p);
return p;
}
public MCMMemBuilder newBuffer() {
return new MCMMemBuilder(this);
}
public MCMBufferBuilder newBuffer2() {
return new MCMBufferBuilder(this);
}
@Deprecated
public MCMMem createImage2d(long flags, int channelType, int width, int height) {
cl_image_format[] format = new cl_image_format[1];
format[0].image_channel_order = CL.CL_RGBA;
format[0].image_channel_data_type = channelType;
return new MCMMem(CL.clCreateImage2D(context, flags, format, width, height, 0, null, null));
}
@Deprecated
public MCMMem createImage2d(long flags, int channelType, int width, int height, Buffer data) {
cl_image_format[] format = new cl_image_format[1];
format[0].image_channel_order = CL.CL_RGBA;
format[0].image_channel_data_type = channelType;
return new MCMMem(CL.clCreateImage2D(context, flags, format, width, height, 0, Pointer.to(data), null));
}
@Deprecated
public MCMMem createImage2d(long flags, int channelType, int width, int height, Pointer data) {
cl_image_format[] format = new cl_image_format[1];
format[0].image_channel_order = CL.CL_RGBA;
format[0].image_channel_data_type = channelType;
return new MCMMem(CL.clCreateImage2D(context, flags, format, width, height, 0, data, null));
}
@Override
protected void releaseImpl() {
CL.clReleaseContext(context);
}
}