Commit db47aedc5292c153dca880a43d0c6556bb0c316c
1 parent
1b1e928cc6
Exists in
master
Refactor MCMContext to allow overriding device types searched by default
using the MCM_CONTEXT_TYPE environement variable, to simply benchmarking
Showing 1 changed file with 66 additions and 17 deletions Inline Diff
src/mcmas/core/MCMContext.java
View file @
db47aed
package mcmas.core; | 1 | 1 | package mcmas.core; | |
2 | 2 | |||
import java.nio.Buffer; | 3 | 3 | import java.nio.Buffer; | |
4 | 4 | |||
import mcmas.utils.Objects7; | 5 | 5 | import mcmas.utils.Objects7; | |
6 | 6 | |||
import org.jocl.CL; | 7 | 7 | import org.jocl.CL; | |
import org.jocl.Pointer; | 8 | 8 | import org.jocl.Pointer; | |
import org.jocl.cl_context; | 9 | 9 | import org.jocl.cl_context; | |
import org.jocl.cl_device_id; | 10 | 10 | import org.jocl.cl_device_id; | |
import org.jocl.cl_image_format; | 11 | 11 | import org.jocl.cl_image_format; | |
import org.jocl.cl_platform_id; | 12 | 12 | import org.jocl.cl_platform_id; | |
13 | 13 | |||
14 | 14 | |||
public class MCMContext extends MCMObject { | 15 | 15 | public class MCMContext extends MCMObject { | |
16 | 16 | |||
private final cl_context context; | 17 | 17 | private final cl_context context; | |
//private final static Logger logger = LoggerFactory.getLogger(OCLContext.class); | 18 | 18 | //private final static Logger logger = LoggerFactory.getLogger(OCLContext.class); | |
19 | 19 | |||
20 | /** | |||
21 | * Create a new context using the first available hardware among | |||
22 | * default devices types. | |||
23 | */ | |||
public MCMContext() { | 20 | 24 | public MCMContext() { | |
MCMPlatform[] platforms = MCMPlatform.getPlatforms(); | 21 | 25 | this(getDefaultContextTypes()); | |
22 | ||||
if (platforms.length <= 0) { | 23 | |||
throw new RuntimeException("No OpenCL plaform found"); | 24 | |||
} | 25 | |||
26 | ||||
this.context = MCMHelpers.createContextFromType(platforms[0].getId(), | 27 | |||
CL.CL_DEVICE_TYPE_GPU, CL.CL_DEVICE_TYPE_CPU); | 28 | |||
29 | ||||
if (context == null) { | 30 | |||
throw new RuntimeException("No CPU or GPU device found"); | 31 | |||
} | 32 | |||
} | 33 | 26 | } | |
34 | 27 | |||
public MCMContext(MCMPlatform platform) { | 35 | 28 | /** | |
this(platform, CL.CL_DEVICE_TYPE_GPU, CL.CL_DEVICE_TYPE_CPU); | 36 | 29 | * Create a new context using the first available hardware among | |
} | 37 | 30 | * indicated device types. | |
38 | 31 | * | ||
32 | * @param deviceTypes device types to search for | |||
33 | */ | |||
public MCMContext(long... deviceTypes) { | 39 | 34 | public MCMContext(long... deviceTypes) { | |
MCMPlatform[] platforms = MCMPlatform.getPlatforms(); | 40 | 35 | MCMPlatform[] platforms = MCMPlatform.getPlatforms(); | |
41 | 36 | |||
if (platforms.length <= 0) { | 42 | 37 | if (platforms.length <= 0) { | |
throw new RuntimeException("No OpenCL plaform found"); | 43 | 38 | throw new RuntimeException("No OpenCL platform found"); | |
} | 44 | 39 | } | |
45 | 40 | |||
this.context = MCMHelpers.createContextFromType(platforms[0].getId(), deviceTypes); | 46 | 41 | this.context = MCMHelpers.createContextFromType(platforms[0].getId(), deviceTypes); | |
47 | 42 | |||
if (context == null) { | 48 | 43 | if (context == null) { | |
throw new RuntimeException("No OpenCL device found"); | 49 | 44 | throw new RuntimeException("No OpenCL device found"); | |
} | 50 | 45 | } | |
} | 51 | 46 | } | |
52 | 47 | |||
48 | /** | |||
49 | * Create a new context using the indicated platform. | |||
50 | * | |||
51 | * @param platform platform to consider | |||
52 | */ | |||
53 | public MCMContext(MCMPlatform platform) { | |||
54 | this(platform, getDefaultContextTypes()); | |||
55 | } | |||
56 | ||||
57 | /** | |||
58 | * Create a new context using the indicated platform and device types. | |||
59 | * | |||
60 | * @param platform platform to consider | |||
61 | * @param deviceTypes device types to search for | |||
62 | */ | |||
public MCMContext(MCMPlatform platform, long... deviceTypes) { | 53 | 63 | public MCMContext(MCMPlatform platform, long... deviceTypes) { | |
platform = Objects7.requireNonNull(platform, "Can't create an OpenCL context with a null platform"); | 54 | 64 | platform = Objects7.requireNonNull(platform, "Can't create an OpenCL context with a null platform"); | |
55 | 65 | |||
System.out.println("platform vendor : " + platform.getInfoString(CL.CL_PLATFORM_VENDOR)); | 56 | 66 | System.out.println("platform vendor : " + platform.getInfoString(CL.CL_PLATFORM_VENDOR)); | |
System.out.println("platform name : " + platform.getInfoString(CL.CL_PLATFORM_NAME)); | 57 | 67 | System.out.println("platform name : " + platform.getInfoString(CL.CL_PLATFORM_NAME)); | |
58 | 68 | |||
this.context = MCMHelpers.createContextFromType(platform.getId(), deviceTypes); | 59 | 69 | this.context = MCMHelpers.createContextFromType(platform.getId(), deviceTypes); | |
60 | 70 | |||
if (context == null) { | 61 | 71 | if (context == null) { | |
throw new RuntimeException("No OpenCL device found"); | 62 | 72 | throw new RuntimeException("No OpenCL device found"); | |
} | 63 | 73 | } | |
} | 64 | 74 | } | |
65 | 75 | |||
76 | /** | |||
77 | * Return a list of default device types to test when nothing | |||
78 | * is indicated at the MCMContext creation. | |||
79 | * | |||
80 | * The default list indicates to use these kind of hardware in this order: | |||
81 | * GPU, Accelerators (such as Xeon Phi) and CPU. | |||
82 | * | |||
83 | * It can be overriden using the MCM_CONTEXT_TYPE environment variable, | |||
84 | * which admits the following values: "CPU", "GPU", "ACCELERATOR", "ALL". | |||
85 | * | |||
86 | * "ALL" considers all the devices available on the system, but the | |||
87 | * order is not garanteed (even for the same type of device) and depends | |||
88 | * of the installed runtimes. | |||
89 | * | |||
90 | * @return a list of context type to try. | |||
91 | */ | |||
92 | public static long[] getDefaultContextTypes() { | |||
93 | String context_type = System.getenv("MCM_CONTEXT_TYPE"); | |||
94 | ||||
95 | if ("CPU".equals(context_type)) { | |||
96 | return new long[] { CL.CL_DEVICE_TYPE_CPU }; | |||
97 | } else if ("GPU".equals(context_type)) { | |||
98 | return new long[] { CL.CL_DEVICE_TYPE_GPU }; | |||
99 | } else if ("ACCELERATOR".equals(context_type)) { | |||
100 | return new long[] { CL.CL_DEVICE_TYPE_ACCELERATOR }; | |||
101 | } else if ("ALL".equals(context_type)) { | |||
102 | return new long[] { CL.CL_DEVICE_TYPE_ALL }; | |||
103 | } else { | |||
104 | return new long[] { | |||
105 | CL.CL_DEVICE_TYPE_GPU, | |||
106 | CL.CL_DEVICE_TYPE_ACCELERATOR, | |||
107 | CL.CL_DEVICE_TYPE_CPU | |||
108 | }; | |||
109 | } | |||
110 | } | |||
111 | ||||
public cl_context getContext() { | 66 | 112 | public cl_context getContext() { | |
return context; | 67 | 113 | return context; | |
} | 68 | 114 | } | |
69 | 115 | |||
public MCMPlatform[] getPlatforms() { | 70 | 116 | public MCMPlatform[] getPlatforms() { | |
cl_platform_id[] platform_ids = MCMHelpers.getPlatforms(); | 71 | 117 | cl_platform_id[] platform_ids = MCMHelpers.getPlatforms(); | |
MCMPlatform[] platforms = new MCMPlatform[platform_ids.length]; | 72 | 118 | MCMPlatform[] platforms = new MCMPlatform[platform_ids.length]; | |
73 | 119 | |||
for (int i = 0; i < platform_ids.length; i++) { | 74 | 120 | for (int i = 0; i < platform_ids.length; i++) { | |
platforms[i] = new MCMPlatform(platform_ids[i]); | 75 | 121 | platforms[i] = new MCMPlatform(platform_ids[i]); | |
} | 76 | 122 | } | |
77 | 123 | |||
return platforms; | 78 | 124 | return platforms; | |
} | 79 | 125 | } | |
80 | 126 | |||
public MCMDevice[] getDevices() { | 81 | 127 | public MCMDevice[] getDevices() { | |
cl_device_id[] device_ids = MCMHelpers.getDevices(context); | 82 | 128 | cl_device_id[] device_ids = MCMHelpers.getDevices(context); | |
MCMDevice[] devices = new MCMDevice[device_ids.length]; | 83 | 129 | MCMDevice[] devices = new MCMDevice[device_ids.length]; | |
for (int i = 0; i < device_ids.length; i++) { | 84 | 130 | for (int i = 0; i < device_ids.length; i++) { | |
devices[i] = new MCMDevice(device_ids[i]); | 85 | 131 | devices[i] = new MCMDevice(device_ids[i]); | |
} | 86 | 132 | } | |
return devices; | 87 | 133 | return devices; | |
} | 88 | 134 | } | |
89 | 135 | |||
public MCMCommandQueue createCommandQueue(MCMCommandQueueProperty... properties) { | 90 | 136 | public MCMCommandQueue createCommandQueue(MCMCommandQueueProperty... properties) { | |
MCMCommandQueue q = new MCMCommandQueue(this, properties); | 91 | 137 | MCMCommandQueue q = new MCMCommandQueue(this, properties); | |
this.register(q); | 92 | 138 | this.register(q); | |
return q; | 93 | 139 | return q; | |
} | 94 | 140 | } | |
95 | 141 | |||
public MCMCommandQueue createCommandQueue(MCMDevice device, MCMCommandQueueProperty... properties) { | 96 | 142 | public MCMCommandQueue createCommandQueue(MCMDevice device, MCMCommandQueueProperty... properties) { | |
MCMCommandQueue q = new MCMCommandQueue(this, device, properties); | 97 | 143 | MCMCommandQueue q = new MCMCommandQueue(this, device, properties); | |
this.register(q); | 98 | 144 | this.register(q); | |
return q; | 99 | 145 | return q; | |
} | 100 | 146 | } | |
101 | 147 | |||
public MCMProgram createProgram(String source, String... options) { | 102 | 148 | public MCMProgram createProgram(String source, String... options) { | |
MCMProgram p = new MCMProgram(this, source, options); | 103 | 149 | MCMProgram p = new MCMProgram(this, source, options); | |
this.register(p); | 104 | 150 | this.register(p); | |
return p; | 105 | 151 | return p; | |
} | 106 | 152 | } | |
107 | 153 | |||
public MCMProgram createProgram(String source, MCMProgramOptions options) { | 108 | 154 | public MCMProgram createProgram(String source, MCMProgramOptions options) { | |
MCMProgram p = new MCMProgram(this, source, options.toString()); | 109 | 155 | MCMProgram p = new MCMProgram(this, source, options.toString()); | |
this.register(p); | 110 | 156 | this.register(p); | |
return p; | 111 | 157 | return p; | |
} | 112 | 158 | } | |
113 | 159 | |||
public MCMProgram createProgramFromFile(String file, String... options) { | 114 | 160 | public MCMProgram createProgramFromFile(String file, String... options) { | |
MCMProgram p = new MCMProgram(this, MCMUtils.readFileAsString(file), options); | 115 | 161 | MCMProgram p = new MCMProgram(this, MCMUtils.readFileAsString(file), options); | |
this.register(p); | 116 | 162 | this.register(p); | |
return p; | 117 | 163 | return p; | |
} | 118 | 164 | } | |
119 | 165 | |||
public MCMProgram createProgramFromFile(String file, MCMProgramOptions options) { | 120 | 166 | public MCMProgram createProgramFromFile(String file, MCMProgramOptions options) { | |
MCMProgram p = new MCMProgram(this, MCMUtils.readFileAsString(file), options.toString()); | 121 | 167 | MCMProgram p = new MCMProgram(this, MCMUtils.readFileAsString(file), options.toString()); | |
this.register(p); | 122 | 168 | this.register(p); | |
return p; | 123 | 169 | return p; | |
} | 124 | 170 | } | |
125 | 171 | |||
public MCMMemBuilder newBuffer() { | 126 | 172 | public MCMMemBuilder newBuffer() { | |
return new MCMMemBuilder(this); | 127 | 173 | return new MCMMemBuilder(this); | |
} | 128 | 174 | } | |
129 | 175 | |||
public MCMBufferBuilder newBuffer2() { | 130 | 176 | public MCMBufferBuilder newBuffer2() { | |
return new MCMBufferBuilder(this); | 131 | 177 | return new MCMBufferBuilder(this); | |
} | 132 | 178 | } |