[Beignet] [PATCH 2/2] Refine create context APIs.

junyan.he at inbox.com junyan.he at inbox.com
Thu Nov 10 05:44:46 UTC 2016


From: Junyan He <junyan.he at intel.com>

Add multi-device logic, make it more readable
and move it to new file.

Signed-off-by: Junyan He <junyan.he at intel.com>
---
 src/cl_api.c         |  92 ------------------------------------------
 src/cl_api_context.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 111 insertions(+), 92 deletions(-)

diff --git a/src/cl_api.c b/src/cl_api.c
index ac6f695..5ed8c99 100644
--- a/src/cl_api.c
+++ b/src/cl_api.c
@@ -169,98 +169,6 @@ clReleaseDevice(cl_device_id device)
   return CL_SUCCESS;
 }
 
-cl_context
-clCreateContext(const cl_context_properties *  properties,
-                cl_uint                        num_devices,
-                const cl_device_id *           devices,
-                void (* pfn_notify) (const char*, const void*, size_t, void*),
-                void *                         user_data,
-                cl_int *                       errcode_ret)
-{
-  cl_int err = CL_SUCCESS;
-  cl_context context = NULL;
-
-  /* Assert parameters correctness */
-  INVALID_VALUE_IF (devices == NULL);
-  INVALID_VALUE_IF (num_devices == 0);
-  INVALID_VALUE_IF (pfn_notify == NULL && user_data != NULL);
-
-  /* Now check if the user is asking for the right device */
-  INVALID_DEVICE_IF (cl_device_id_is_ok(*devices) == CL_FALSE);
-
-  context = cl_create_context(properties,
-                           num_devices,
-                           devices,
-                           pfn_notify,
-                           user_data,
-                           &err);
-  initialize_env_var();
-error:
-  if (errcode_ret)
-    *errcode_ret = err;
-  return context;
-}
-
-cl_context
-clCreateContextFromType(const cl_context_properties *  properties,
-                        cl_device_type                 device_type,
-                        void (CL_CALLBACK *pfn_notify) (const char *, const void *, size_t, void *),
-                        void *                         user_data,
-                        cl_int *                       errcode_ret)
-{
-  cl_context context = NULL;
-  cl_int err = CL_SUCCESS;
-  cl_device_id devices[1];
-  cl_uint num_devices = 1;
-
-  INVALID_VALUE_IF (pfn_notify == NULL && user_data != NULL);
-
-  err = cl_check_device_type(device_type);
-  if(err != CL_SUCCESS) {
-    goto error;
-  }
-
-  err = cl_get_device_ids(NULL,
-                          device_type,
-                          1,
-                          &devices[0],
-                          &num_devices);
-  if (err != CL_SUCCESS) {
-    goto error;
-  }
-
-  context = cl_create_context(properties,
-                              num_devices,
-                              devices,
-                              pfn_notify,
-                              user_data,
-                              &err);
-error:
-  if (errcode_ret)
-    *errcode_ret = err;
-  return context;
-}
-
-cl_int
-clRetainContext(cl_context context)
-{
-  cl_int err = CL_SUCCESS;
-  CHECK_CONTEXT (context);
-  cl_context_add_ref(context);
-error:
-  return err;
-}
-
-cl_int
-clReleaseContext(cl_context context)
-{
-  cl_int err = CL_SUCCESS;
-  CHECK_CONTEXT (context);
-  cl_context_delete(context);
-error:
-  return err;
-}
-
 cl_command_queue
 clCreateCommandQueue(cl_context                   context,
                      cl_device_id                 device,
diff --git a/src/cl_api_context.c b/src/cl_api_context.c
index d2adb41..2160950 100644
--- a/src/cl_api_context.c
+++ b/src/cl_api_context.c
@@ -17,6 +17,117 @@
  */
 
 #include "cl_context.h"
+#include "cl_device_id.h"
+#include "cl_alloc.h"
+
+cl_context
+clCreateContext(const cl_context_properties *properties,
+                cl_uint num_devices,
+                const cl_device_id *devices,
+                void (*pfn_notify)(const char *, const void *, size_t, void *),
+                void *user_data,
+                cl_int *errcode_ret)
+{
+  cl_int err = CL_SUCCESS;
+  cl_context context = NULL;
+
+  do {
+    /* Assure parameters correctness */
+    if (devices == NULL) {
+      err = CL_INVALID_VALUE;
+      break;
+    }
+
+    if (num_devices == 0) {
+      err = CL_INVALID_VALUE;
+      break;
+    }
+
+    if (pfn_notify == NULL && user_data != NULL) {
+      err = CL_INVALID_VALUE;
+      break;
+    }
+
+    err = cl_devices_list_check(num_devices, devices);
+    if (err != CL_SUCCESS)
+      break;
+
+    context = cl_create_context(properties, num_devices, devices, pfn_notify, user_data, &err);
+  } while (0);
+
+  if (errcode_ret)
+    *errcode_ret = err;
+  return context;
+}
+
+cl_context
+clCreateContextFromType(const cl_context_properties *properties,
+                        cl_device_type device_type,
+                        void(CL_CALLBACK *pfn_notify)(const char *, const void *, size_t, void *),
+                        void *user_data,
+                        cl_int *errcode_ret)
+{
+  cl_context context = NULL;
+  cl_int err = CL_SUCCESS;
+  cl_device_id *devices = NULL;
+  cl_uint num_devices = 0;
+  const cl_device_type valid_type = CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_CPU | CL_DEVICE_TYPE_ACCELERATOR |
+                                    CL_DEVICE_TYPE_DEFAULT | CL_DEVICE_TYPE_CUSTOM;
+
+  do {
+    /* Assure parameters correctness */
+    if (pfn_notify == NULL && user_data != NULL) {
+      err = CL_INVALID_VALUE;
+      break;
+    }
+
+    if ((device_type & valid_type) == 0) {
+      err = CL_INVALID_DEVICE_TYPE;
+      break;
+    }
+
+    /* Get the devices num first. */
+    err = cl_get_device_ids(NULL, device_type, 0, NULL, &num_devices);
+    if (err != CL_SUCCESS)
+      break;
+
+    assert(num_devices > 0);
+    devices = cl_malloc(num_devices * sizeof(cl_device_id));
+    err = cl_get_device_ids(NULL, device_type, num_devices, &devices[0], &num_devices);
+    if (err != CL_SUCCESS)
+      break;
+
+    context = cl_create_context(properties, num_devices, devices, pfn_notify, user_data, &err);
+  } while (0);
+
+  if (devices)
+    cl_free(devices);
+  if (errcode_ret)
+    *errcode_ret = err;
+  return context;
+}
+
+cl_int
+clRetainContext(cl_context context)
+{
+  if (!CL_OBJECT_IS_CONTEXT(context)) {
+    return CL_INVALID_CONTEXT;
+  }
+
+  cl_context_add_ref(context);
+  return CL_SUCCESS;
+}
+
+cl_int
+clReleaseContext(cl_context context)
+{
+  if (!CL_OBJECT_IS_CONTEXT(context)) {
+    return CL_INVALID_CONTEXT;
+  }
+
+  cl_context_delete(context);
+  return CL_SUCCESS;
+}
 
 cl_int
 clGetContextInfo(cl_context context,
-- 
2.7.4





More information about the Beignet mailing list