Commit db47aedc5292c153dca880a43d0c6556bb0c316c

Authored by glaville
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 Side-by-side Diff

src/mcmas/core/MCMContext.java View file @ db47aed
... ... @@ -17,30 +17,25 @@
17 17 private final cl_context context;
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 + */
20 24 public MCMContext() {
21   - MCMPlatform[] platforms = MCMPlatform.getPlatforms();
22   -
23   - if (platforms.length <= 0) {
24   - throw new RuntimeException("No OpenCL plaform found");
25   - }
26   -
27   - this.context = MCMHelpers.createContextFromType(platforms[0].getId(),
28   - CL.CL_DEVICE_TYPE_GPU, CL.CL_DEVICE_TYPE_CPU);
29   -
30   - if (context == null) {
31   - throw new RuntimeException("No CPU or GPU device found");
32   - }
  25 + this(getDefaultContextTypes());
33 26 }
34 27  
35   - public MCMContext(MCMPlatform platform) {
36   - this(platform, CL.CL_DEVICE_TYPE_GPU, CL.CL_DEVICE_TYPE_CPU);
37   - }
38   -
  28 + /**
  29 + * Create a new context using the first available hardware among
  30 + * indicated device types.
  31 + *
  32 + * @param deviceTypes device types to search for
  33 + */
39 34 public MCMContext(long... deviceTypes) {
40 35 MCMPlatform[] platforms = MCMPlatform.getPlatforms();
41 36  
42 37 if (platforms.length <= 0) {
43   - throw new RuntimeException("No OpenCL plaform found");
  38 + throw new RuntimeException("No OpenCL platform found");
44 39 }
45 40  
46 41 this.context = MCMHelpers.createContextFromType(platforms[0].getId(), deviceTypes);
... ... @@ -50,6 +45,21 @@
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 + */
53 63 public MCMContext(MCMPlatform platform, long... deviceTypes) {
54 64 platform = Objects7.requireNonNull(platform, "Can't create an OpenCL context with a null platform");
55 65  
... ... @@ -63,6 +73,42 @@
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 +
66 112 public cl_context getContext() {
67 113 return context;
68 114 }
... ... @@ -131,6 +177,7 @@
131 177 return new MCMBufferBuilder(this);
132 178 }
133 179  
  180 + @Deprecated
134 181 public MCMMem createImage2d(long flags, int channelType, int width, int height) {
135 182 cl_image_format[] format = new cl_image_format[1];
136 183 format[0].image_channel_order = CL.CL_RGBA;
... ... @@ -139,6 +186,7 @@
139 186 return new MCMMem(CL.clCreateImage2D(context, flags, format, width, height, 0, null, null));
140 187 }
141 188  
  189 + @Deprecated
142 190 public MCMMem createImage2d(long flags, int channelType, int width, int height, Buffer data) {
143 191 cl_image_format[] format = new cl_image_format[1];
144 192 format[0].image_channel_order = CL.CL_RGBA;
... ... @@ -147,6 +195,7 @@
147 195 return new MCMMem(CL.clCreateImage2D(context, flags, format, width, height, 0, Pointer.to(data), null));
148 196 }
149 197  
  198 + @Deprecated
150 199 public MCMMem createImage2d(long flags, int channelType, int width, int height, Pointer data) {
151 200 cl_image_format[] format = new cl_image_format[1];
152 201 format[0].image_channel_order = CL.CL_RGBA;