Creates an OpenCL context.

cl_context clCreateContext( cl_context_properties *properties,
  cl_uint num_devices,
  const cl_device_id *devices,
  void *pfn_notify (

const char *errinfo, 
const void *private_info, 
size_t cb, 
void *user_data

  void *user_data,
  cl_int *errcode_ret)


An OpenCL context is created with one or more devices. Contexts are used by the OpenCL runtime for managing objects such as command-queues, memory, program and kernel objects and for executing kernels on one or more devices specified in the context.



Specifies a list of context property names and their corresponding values. Each property name is immediately followed by the corresponding desired value. The list is terminated with 0. properties can be NULL in which case the platform that is selected is implementation-defined. The list of supported properties is described in the table below.

cl_context_properties enum Property value Description
CL_CONTEXT_PLATFORM cl_platform_id Specifies the platform to use.

The number of devices specified in the devices argument.


A pointer to a list of unique devices returned by clGetDeviceIDs for a platform.


A callback function that can be registered by the application. This callback function will be used by the OpenCL implementation to report information on errors that occur in this context. This callback function may be called asynchronously by the OpenCL implementation. It is the application's responsibility to ensure that the callback function is thread-safe. If pfn_notify is NULL, no callback function is registered. The parameters to this callback function are:

errinfo is a pointer to an error string.

private_info and cb represent a pointer to binary data that is returned by the OpenCL implementation that can be used to log additional information helpful in debugging the error.

user_data is a pointer to user supplied data.


Passed as the user_data argument when pfn_notify is called. user_data can be NULL.


Returns an appropriate error code. If errcode_ret is NULL, no error code is returned.


clCreateContext returns a valid non-zero context and errcode_ret is set to CL_SUCCESS if the context is created successfully. Otherwise, it returns NULL value with the following error values returned in errcode_ret:

  • CL_INVALID_PLATFORM if properties is NULL and no platform could be selected or if platform value specified in properties is not a valid platform.
  • CL_INVALID_VALUE if context property name in properties is not a supported property name; if devices is NULL; if num_devices is equal to zero; or if pfn_notify is NULL but user_data is not NULL.
  • CL_INVALID_DEVICE if devices contains an invalid device or are not associated with the specified platform.
  • CL_DEVICE_NOT_AVAILABLE if a device in devices is currently not available even though the device was returned by clGetDeviceIDs.
  • CL_OUT_OF_HOST_MEMORY if there is a failure to allocate resources required by the OpenCL implementation on the host.


clCreateContext and clCreateContextFromType perform an implicit retain. This is very helpful for 3rd party libraries, which typically get a context passed to them by the application. However, it is possible that the application may delete the context without informing the library. Allowing functions to attach to (i.e. retain) and release a context solves the problem of a context being used by a library no longer being valid.


OpenCL Specification

Also see

clGetDeviceIDs, clCreateContextFromType, clRetainContext, clReleaseContext, clGetContextInfo

Copyright © Copyright © 2007-2009 The Khronos Group Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and/or associated documentation files (the "Materials"), to deal in the Materials without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, and to permit persons to whom the Materials are furnished to do so, subject to the condition that this copyright notice and permission notice shall be included in all copies or substantial portions of the Materials.