diff --git a/src/mcmas/core/MCMContext.java b/src/mcmas/core/MCMContext.java index 1908e6b..83ff95b 100644 --- a/src/mcmas/core/MCMContext.java +++ b/src/mcmas/core/MCMContext.java @@ -17,30 +17,25 @@ 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() { - MCMPlatform[] platforms = MCMPlatform.getPlatforms(); - - if (platforms.length <= 0) { - throw new RuntimeException("No OpenCL plaform found"); - } - - this.context = MCMHelpers.createContextFromType(platforms[0].getId(), - CL.CL_DEVICE_TYPE_GPU, CL.CL_DEVICE_TYPE_CPU); - - if (context == null) { - throw new RuntimeException("No CPU or GPU device found"); - } - } - - public MCMContext(MCMPlatform platform) { - this(platform, CL.CL_DEVICE_TYPE_GPU, CL.CL_DEVICE_TYPE_CPU); + 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 plaform found"); + throw new RuntimeException("No OpenCL platform found"); } this.context = MCMHelpers.createContextFromType(platforms[0].getId(), deviceTypes); @@ -50,6 +45,21 @@ public class MCMContext extends MCMObject { } } + /** + * 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"); @@ -63,6 +73,42 @@ public class MCMContext extends MCMObject { } } + /** + * 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; } @@ -131,6 +177,7 @@ public class MCMContext extends MCMObject { 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; @@ -139,6 +186,7 @@ public class MCMContext extends MCMObject { 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; @@ -147,6 +195,7 @@ public class MCMContext extends MCMObject { 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;