nsresult dpoCContext::InitContext(cl_platform_id platform) { cl_int err_code; cl_device_id *devices; size_t cb; cl_context_properties context_properties[3] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platform, NULL}; context = clCreateContextFromType(context_properties, CL_DEVICE_TYPE_CPU, ReportCLError, this, &err_code); if (err_code != CL_SUCCESS) { DEBUG_LOG_ERROR("InitContext", err_code); return NS_ERROR_NOT_AVAILABLE; } err_code = clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &cb); if (err_code != CL_SUCCESS) { DEBUG_LOG_ERROR("InitContext", err_code); return NS_ERROR_NOT_AVAILABLE; } devices = (cl_device_id *)nsMemory::Alloc(sizeof(cl_device_id)*cb); if (devices == NULL) { DEBUG_LOG_STATUS("InitContext", "Cannot allocate device list"); return NS_ERROR_OUT_OF_MEMORY; } err_code = clGetContextInfo(context, CL_CONTEXT_DEVICES, cb, devices, NULL); if (err_code != CL_SUCCESS) { DEBUG_LOG_ERROR("InitContext", err_code); nsMemory::Free(devices); return NS_ERROR_NOT_AVAILABLE; } cmdQueue = clCreateCommandQueue(context, devices[0], #ifdef CLPROFILE CL_QUEUE_PROFILING_ENABLE | #endif /* CLPROFILE */ #ifdef OUTOFORDERQUEUE CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | #endif /* OUTOFORDERQUEUE */ 0, &err_code); if (err_code != CL_SUCCESS) { DEBUG_LOG_ERROR("InitContext", err_code); nsMemory::Free(devices); return NS_ERROR_NOT_AVAILABLE; } DEBUG_LOG_STATUS("InitContext", "queue is " << cmdQueue); nsMemory::Free(devices); kernelFailureMem = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int), NULL, &err_code); if (err_code != CL_SUCCESS) { DEBUG_LOG_ERROR("InitContext", err_code); return NS_ERROR_NOT_AVAILABLE; } return NS_OK; }
static int initialize(int use_gpu) { cl_int result; size_t size; #ifndef POCL_HSA // create OpenCL context cl_platform_id platform_id; if (clGetPlatformIDs(1, &platform_id, NULL) != CL_SUCCESS) { printf("ERROR: clGetPlatformIDs(1,*,0) failed\n"); return -1; } cl_context_properties ctxprop[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id, 0}; device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU; context = clCreateContextFromType( ctxprop, device_type, NULL, NULL, NULL ); #else context = poclu_create_any_context(); #endif if( !context ) { printf("ERROR: clCreateContextFromType(%s) failed\n", use_gpu ? "GPU" : "CPU"); return -1; } // get the list of GPUs result = clGetContextInfo( context, CL_CONTEXT_DEVICES, 0, NULL, &size ); num_devices = (int) (size / sizeof(cl_device_id)); if( result != CL_SUCCESS || num_devices < 1 ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; } device_list = new cl_device_id[num_devices]; if( !device_list ) { printf("ERROR: new cl_device_id[] failed\n"); return -1; } result = clGetContextInfo( context, CL_CONTEXT_DEVICES, size, device_list, NULL ); if( result != CL_SUCCESS ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; } // create command queue for the first device cmd_queue = clCreateCommandQueue( context, device_list[0], 0, NULL ); if( !cmd_queue ) { printf("ERROR: clCreateCommandQueue() failed\n"); return -1; } return 0; }
bool getDeviceIDs() { cl_int err = 0; size_t deviceBufferSize = -1; /* Retrieve the size of the buffer needed to contain information about the devices in this OpenCL context. */ err = clGetContextInfo(ocl.context, CL_CONTEXT_DEVICES, 0, NULL, &deviceBufferSize); if (err != CL_SUCCESS) { printf("Failed to get OpenCL context information.\n"); return false; } if(deviceBufferSize == 0) { printf("No OpenCL devices found.\n"); return false; } /* Retrieve the list of devices available in this context. */ DeviceCount = deviceBufferSize / sizeof(cl_device_id);; printf("Device Count %d\n", DeviceCount); ocl.device = new cl_device_id[DeviceCount]; err = clGetContextInfo(ocl.context, CL_CONTEXT_DEVICES, deviceBufferSize, ocl.device, NULL); if (err != CL_SUCCESS) { printf("Failed to get the OpenCL context information.\n"); delete [] ocl.device; return false; } return true; }
oclContext::oclContext(cl_context iContext, cl_platform_id iPlatform, char* iVendor) : oclObject("") , mContext(iContext) , mVendor(VENDOR_UNKNOWN) , mPlatform(iPlatform) { if (!strcmp(iVendor, VENDOR_NVIDIA)) { mVendor = VENDOR_NVIDIA; } else if (!strcmp(iVendor, VENDOR_AMD)) { mVendor = VENDOR_AMD; } else if (!strncmp(iVendor, VENDOR_INTEL, 5)) { mVendor = VENDOR_INTEL; } setName(mVendor); size_t lDeviceCount; sStatusCL = clGetContextInfo(mContext, CL_CONTEXT_DEVICES, 0, NULL, &lDeviceCount); oclSuccess("clGetContextInfo", this); cl_device_id* lDevice = new cl_device_id[lDeviceCount]; clGetContextInfo(mContext, CL_CONTEXT_DEVICES, lDeviceCount, lDevice, NULL); for (cl_uint i=0; i<lDeviceCount/sizeof(cl_device_id); i++) { mDevices.push_back(new oclDevice(*this, lDevice[i])); } delete [] lDevice; };
int createCommandQueue(cl_context context, cl_command_queue* commandQueue, cl_device_id* device) { cl_int errorNumber = 0; size_t deviceBufferSize = 8; size_t deviceNum = 0; /* Retrieve the size of the buffer needed to contain information about the devices in this OpenCL context. */ clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &deviceBufferSize); /* Retrieve the list of devices available in this context. */ cl_device_id devices[2]; clGetContextInfo(context, CL_CONTEXT_DEVICES, deviceBufferSize, devices, &deviceNum); LOGD("deviceNum %d",deviceNum); /* Use the first available device in this context. */ *device = devices[0]; /* Set up the command queue with the selected device. */ *commandQueue = clCreateCommandQueue(context, *device, CL_QUEUE_PROFILING_ENABLE, &errorNumber); LOGD("createCommandQueue code %d",errorNumber); char str[25]; size_t str_size = 0; clGetDeviceInfo(*device,CL_DEVICE_NAME,25,str,&str_size); LOGD("device name %s",str); return 1; }
void ocl_create_context_and_comm_queue(){ // Create a context to run OpenCL on the OCL-enabled Device cl_int err; #if BE_OCL_VERBOSE printf("--- Creating OpenCL context"); #endif device_context = clCreateContext(0, 1, &ocl_device, NULL, NULL, &err); ocl_error_check(OCL_CREATE_CONTEXT, err); // Get the list of OCL_ devices associated with this context size_t ParmDataBytes; clGetContextInfo(device_context, CL_CONTEXT_DEVICES, 0, NULL, &ParmDataBytes); cl_device_id* OCL_Devices = (cl_device_id*)malloc(ParmDataBytes); clGetContextInfo(device_context, CL_CONTEXT_DEVICES, ParmDataBytes, OCL_Devices, NULL); // Create a command-queue on the first OCL_ device #if BE_OCL_VERBOSE printf("--- Creating OpenCL command queue"); #endif device_comm_queue = clCreateCommandQueue(device_context, OCL_Devices[0], 0, &err); ocl_error_check(OCL_CREATE_COMMAND_QUEUE, err); free(OCL_Devices); }
static int initialize(int use_device) { cl_int result; size_t size; // create OpenCL context cl_platform_id platform_id; if (clGetPlatformIDs(1, &platform_id, NULL) != CL_SUCCESS) { printf("ERROR: clGetPlatformIDs(1,*,0) failed\n"); return -1; } cl_context_properties ctxprop[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id, 0}; //device_type = use_device ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU; switch(use_device) { case 0: device_type = CL_DEVICE_TYPE_CPU; break; case 1: device_type = CL_DEVICE_TYPE_GPU; break; case 2: device_type = CL_DEVICE_TYPE_ACCELERATOR; break; } context = clCreateContextFromType( ctxprop, device_type, NULL, NULL, NULL ); if( !context ) { printf("ERROR: clCreateContextFromType(%s) failed\n", use_device ? "GPU" : "CPU"); return -1; } // get the list of GPUs result = clGetContextInfo( context, CL_CONTEXT_DEVICES, 0, NULL, &size ); num_devices = (int) (size / sizeof(cl_device_id)); printf("num_devices = %d\n", num_devices); if( result != CL_SUCCESS || num_devices < 1 ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; } device_list = new cl_device_id[num_devices]; //device_list = (cl_device_id *)malloc(sizeof(cl_device_id)*num_devices); if( !device_list ) { printf("ERROR: new cl_device_id[] failed\n"); return -1; } result = clGetContextInfo( context, CL_CONTEXT_DEVICES, size, device_list, NULL ); if( result != CL_SUCCESS ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; } // create command queue for the first device cmd_queue = clCreateCommandQueue( context, device_list[0], 0, NULL ); if( !cmd_queue ) { printf("ERROR: clCreateCommandQueue() failed\n"); return -1; } return 0; }
bool get_cl_context(cl_context *context, cl_device_id **devices, int num_platform) { if (context == NULL || devices == NULL) return false; cl_platform_id *platforms = NULL; // The iteration variable int i; cl_uint num; check_err(clGetPlatformIDs(0, 0, &num), "Unable to get platforms"); platforms = (cl_platform_id *)malloc(sizeof(cl_platform_id) * num); check_err(clGetPlatformIDs(num, platforms, NULL), "Unable to get platform ID"); check_err(clGetPlatformIDs(0, 0, &num), "Unable to get platforms"); printf("Found %d platforms:\n", num); for (i = 0; i < num; i++) { char str[1024]; clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, 1024, str, NULL); printf("\t%d: %s\n", i, str); } cl_context_properties prop[3]; prop[0] = CL_CONTEXT_PLATFORM; prop[1] = (cl_context_properties)platforms[num_platform]; prop[2] = 0; cl_int err; *context = clCreateContextFromType(prop, CL_DEVICE_TYPE_ALL, NULL, NULL, &err); if (err != CL_SUCCESS) { printf("Can't create OpenCL context\n"); return false; } size_t size_b; int num_total_devices; clGetContextInfo(*context, CL_CONTEXT_DEVICES, 0, NULL, &size_b); *devices = (cl_device_id *)malloc(size_b); clGetContextInfo(*context, CL_CONTEXT_DEVICES, size_b, *devices, 0); if (size_b == 0) { printf("Can't get devices\n"); return false; } num_total_devices = size_b / sizeof(cl_device_id); printf("Found %d devices:\n", num_total_devices); for (i = 0; i < num_total_devices; i++) { char devname[16][256] = {}; clGetDeviceInfo(*devices[i], CL_DEVICE_NAME, 256, devname[i], 0); printf("\t%d: %s", i, devname[i]); clGetDeviceInfo(*devices[i], // Set the device info CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(int), &size_b, 0); printf(" - %d\n", (int)size_b); } return true; }
/// /// Gets the id of device with maximal FLOPS from the context (from NVIDIA SDK) /// static cl_device_id getMaxFlopsDev(cl_context cxGPUContext) { size_t szParmDataBytes; cl_device_id* cdDevices; // get the list of GPU devices associated with context clGetContextInfo(cxGPUContext, CL_CONTEXT_DEVICES, 0, NULL, &szParmDataBytes); cdDevices = (cl_device_id*) malloc(szParmDataBytes); size_t device_count = szParmDataBytes / sizeof(cl_device_id); clGetContextInfo(cxGPUContext, CL_CONTEXT_DEVICES, szParmDataBytes, cdDevices, NULL); cl_device_id max_flops_device = cdDevices[0]; int max_flops = 0; size_t current_device = 0; // CL_DEVICE_MAX_COMPUTE_UNITS cl_uint compute_units; clGetDeviceInfo(cdDevices[current_device], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(compute_units), &compute_units, NULL); // CL_DEVICE_MAX_CLOCK_FREQUENCY cl_uint clock_frequency; clGetDeviceInfo(cdDevices[current_device], CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof(clock_frequency), &clock_frequency, NULL); max_flops = compute_units * clock_frequency; ++current_device; while (current_device < device_count) { // CL_DEVICE_MAX_COMPUTE_UNITS cl_uint compute_units; clGetDeviceInfo(cdDevices[current_device], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(compute_units), &compute_units, NULL); // CL_DEVICE_MAX_CLOCK_FREQUENCY cl_uint clock_frequency; clGetDeviceInfo(cdDevices[current_device], CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof(clock_frequency), &clock_frequency, NULL); int flops = compute_units * clock_frequency; if (flops > max_flops) { max_flops = flops; max_flops_device = cdDevices[current_device]; } ++current_device; } free(cdDevices); return max_flops_device; }
int main() { /* Host/device data structures */ cl_platform_id platform; cl_device_id device; cl_context context; cl_int err; cl_uint ref_count; /* Access the first installed platform */ err = clGetPlatformIDs(1, &platform, NULL); if(err < 0) { perror("Couldn't find any platforms"); exit(1); } /* Access the first available device */ err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL); if(err == CL_DEVICE_NOT_FOUND) { err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 1, &device, NULL); } if(err < 0) { perror("Couldn't find any devices"); exit(1); } /* Create the context */ context = clCreateContext(NULL, 1, &device, NULL, NULL, &err); if(err < 0) { perror("Couldn't create a context"); exit(1); } /* Determine the reference count */ err = clGetContextInfo(context, CL_CONTEXT_REFERENCE_COUNT, sizeof(ref_count), &ref_count, NULL); if(err < 0) { perror("Couldn't read the reference count."); exit(1); } printf("Initial reference count: %u\n", ref_count); /* Update and display the reference count */ clRetainContext(context); clGetContextInfo(context, CL_CONTEXT_REFERENCE_COUNT, sizeof(ref_count), &ref_count, NULL); printf("Reference count: %u\n", ref_count); clReleaseContext(context); clGetContextInfo(context, CL_CONTEXT_REFERENCE_COUNT, sizeof(ref_count), &ref_count, NULL); printf("Reference count: %u\n", ref_count); clReleaseContext(context); return 0; }
cl_int OCL_Platform::init(cl_platform_id id) { cl_int err; mID = id; // store name size_t nameSize; OCL_RETURN_ON_ERR( clGetPlatformInfo( mID, CL_PLATFORM_NAME, 0, NULL, &nameSize ) ); sPlatformName = new char[nameSize]; OCL_RETURN_ON_ERR( clGetPlatformInfo( mID, CL_PLATFORM_NAME, nameSize, sPlatformName, NULL ) ); // store extensions size_t extensionsSize; OCL_RETURN_ON_ERR( clGetPlatformInfo( mID, CL_PLATFORM_EXTENSIONS, 0, NULL, &extensionsSize ) ); sPlatformExtensions = new char[extensionsSize]; OCL_RETURN_ON_ERR( clGetPlatformInfo( mID, CL_PLATFORM_EXTENSIONS, extensionsSize, sPlatformExtensions, NULL ) ); // default device type cl_device_type deviceType = CL_DEVICE_TYPE_ALL; cl_context_properties ctxProps[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)mID, 0 }; OCL_RETURN_ON_ERR( ( mContext = clCreateContextFromType( ctxProps, deviceType, NULL, NULL, &err ), err)); // Create array of devices size_t dev_ids_size; OCL_RETURN_ON_ERR( clGetContextInfo( mContext, CL_CONTEXT_DEVICES, 0, NULL, &dev_ids_size ) ); uiNumDevices = (cl_uint)dev_ids_size/sizeof(cl_device_id); cl_device_id* pDeviceIDs = (cl_device_id*)calloc(1,dev_ids_size); mpDevices = new OCL_DeviceAndQueue[uiNumDevices]; OCL_RETURN_ON_ERR( clGetContextInfo( mContext, CL_CONTEXT_DEVICES, dev_ids_size, pDeviceIDs, NULL ) ); for( cl_uint j=0; j<uiNumDevices; j++ ) { OCL_RETURN_ON_ERR( mpDevices[j].init( mContext, pDeviceIDs[j] ) ); OCL_RETURN_ON_ERR( (mpDevices[j].mContext = clCreateContextFromType( ctxProps, deviceType, NULL, NULL, &err ) ,err )); } delete pDeviceIDs; return CL_SUCCESS; }
bool OsdCLKernelBundle::Compile(cl_context clContext, int numVertexElements, int numVaryingElements) { cl_int ciErrNum; _numVertexElements = numVertexElements; _numVaryingElements = numVaryingElements; char constantDefine[256]; snprintf(constantDefine, sizeof(constantDefine), "#define NUM_VERTEX_ELEMENTS %d\n" "#define NUM_VARYING_ELEMENTS %d\n", numVertexElements, numVaryingElements); const char *sources[] = { constantDefine, clSource }; _clProgram = clCreateProgramWithSource(clContext, 2, sources, 0, &ciErrNum); CL_CHECK_ERROR(ciErrNum, "clCreateProgramWithSource\n"); ciErrNum = clBuildProgram(_clProgram, 0, NULL, NULL, NULL, NULL); if (ciErrNum != CL_SUCCESS) { OsdError(OSD_CL_PROGRAM_BUILD_ERROR, "CLerr=%d", ciErrNum); cl_int numDevices = 0; clGetContextInfo(clContext, CL_CONTEXT_NUM_DEVICES, sizeof(cl_uint), &numDevices, NULL); cl_device_id *devices = new cl_device_id[numDevices]; clGetContextInfo(clContext, CL_CONTEXT_DEVICES, sizeof(cl_device_id)*numDevices, devices, NULL); for (int i = 0; i < numDevices; ++i) { char cBuildLog[10240]; clGetProgramBuildInfo(_clProgram, devices[i], CL_PROGRAM_BUILD_LOG, sizeof(cBuildLog), cBuildLog, NULL); OsdError(OSD_CL_PROGRAM_BUILD_ERROR, cBuildLog); } delete[] devices; return false; } _clBilinearEdge = buildKernel(_clProgram, "computeBilinearEdge"); _clBilinearVertex = buildKernel(_clProgram, "computeBilinearVertex"); _clCatmarkFace = buildKernel(_clProgram, "computeFace"); _clCatmarkEdge = buildKernel(_clProgram, "computeEdge"); _clCatmarkVertexA = buildKernel(_clProgram, "computeVertexA"); _clCatmarkVertexB = buildKernel(_clProgram, "computeVertexB"); _clLoopEdge = buildKernel(_clProgram, "computeEdge"); _clLoopVertexA = buildKernel(_clProgram, "computeVertexA"); _clLoopVertexB = buildKernel(_clProgram, "computeLoopVertexB"); _clVertexEditAdd = buildKernel(_clProgram, "editVertexAdd"); return true; }
END_TEST START_TEST (test_object_tree) { cl_device_id device; cl_context ctx; cl_command_queue queue; cl_int result; cl_uint refcount; cl_platform_id platform = 0; cl_uint num_platforms = 0; clGetPlatformIDs(1, &platform, &num_platforms); result = clGetDeviceIDs(platform, CL_DEVICE_TYPE_DEFAULT, 1, &device, 0); fail_if( result != CL_SUCCESS, "unable to get the default device" ); ctx = clCreateContext(0, 1, &device, 0, 0, &result); fail_if( result != CL_SUCCESS || ctx == 0, "unable to create a valid context" ); queue = clCreateCommandQueue(ctx, device, 0, &result); fail_if( result != CL_SUCCESS || queue == 0, "cannot create a command queue" ); result = clGetContextInfo(ctx, CL_CONTEXT_REFERENCE_COUNT, sizeof(cl_uint), (void *)&refcount, 0); fail_if( result != CL_SUCCESS || refcount != 2, "the queue must increment the refcount of its context" ); clReleaseCommandQueue(queue); result = clGetContextInfo(ctx, CL_CONTEXT_REFERENCE_COUNT, sizeof(cl_uint), (void *)&refcount, 0); fail_if( result != CL_SUCCESS || refcount != 1, "the queue must decrement the refcount of its context when it's destroyed" ); clReleaseContext(ctx); }
TEST_P(ocl_engine_test, BasicInteropC) { auto p = GetParam(); cl_device_id ocl_dev = (p.adev_kind == dev_kind::gpu) ? gpu_ocl_dev : (p.adev_kind == dev_kind::cpu) ? cpu_ocl_dev : nullptr; cl_context ocl_ctx = (p.actx_kind == ctx_kind::gpu) ? gpu_ocl_ctx : (p.actx_kind == ctx_kind::cpu) ? cpu_ocl_ctx : nullptr; SKIP_IF(p.adev_kind != dev_kind::null && !ocl_dev, "Required OpenCL device not found."); SKIP_IF(p.actx_kind != ctx_kind::null && !ocl_ctx, "Required OpenCL context not found."); SKIP_IF(cpu_ocl_dev == gpu_ocl_dev && (p.adev_kind == dev_kind::cpu || p.actx_kind == ctx_kind::cpu), "OpenCL CPU-only device not found."); mkldnn_engine_t eng; mkldnn_status_t s = mkldnn_engine_create_ocl(&eng, mkldnn_gpu, ocl_dev, ocl_ctx); EXPECT_EQ(s, p.expected_status); if (s == mkldnn_success) { cl_device_id dev; cl_context ctx; MKLDNN_CHECK(mkldnn_engine_get_ocl_device(eng, &dev)); MKLDNN_CHECK(mkldnn_engine_get_ocl_context(eng, &ctx)); EXPECT_EQ(dev, ocl_dev); EXPECT_EQ(ctx, ocl_ctx); cl_uint ref_count; OCL_CHECK(clGetContextInfo(ocl_ctx, CL_CONTEXT_REFERENCE_COUNT, sizeof(ref_count), &ref_count, nullptr)); int i_ref_count = int(ref_count); EXPECT_EQ(i_ref_count, 2); MKLDNN_CHECK(mkldnn_engine_destroy(eng)); OCL_CHECK(clGetContextInfo(ocl_ctx, CL_CONTEXT_REFERENCE_COUNT, sizeof(ref_count), &ref_count, nullptr)); i_ref_count = int(ref_count); EXPECT_EQ(i_ref_count, 1); } }
cl_command_queue CreateCommandQueue(cl_context context, cl_device_id *device) { cl_int error_number; cl_device_id *devices; cl_command_queue command_queue = NULL; size_t device_buffer_size = -1; error_number = clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &device_buffer_size); if (error_number != CL_SUCCESS) { std::cerr << "Failed call to clGetContextInfo(..., CL_CONTEXT_DEVICES,...)"; return NULL; } if (device_buffer_size <= 0) { std::cerr << "No devices available."; return NULL; } devices = new cl_device_id[device_buffer_size / sizeof(cl_device_id)]; error_number = clGetContextInfo(context, CL_CONTEXT_DEVICES, device_buffer_size, devices, NULL); if (error_number != CL_SUCCESS) { delete []devices; std::cerr << "Failed to get device IDs!"; return NULL; } command_queue = clCreateCommandQueue(context, devices[0], 0, NULL); if (command_queue == NULL) { delete []devices; std::cerr << "Failed to create commandQueue for device 0."; return NULL; } *device = devices[0]; delete []devices; return command_queue; }
TEST_P(ocl_engine_test, BasicInteropCpp) { auto p = GetParam(); cl_device_id ocl_dev = (p.adev_kind == dev_kind::gpu) ? gpu_ocl_dev : (p.adev_kind == dev_kind::cpu) ? cpu_ocl_dev : nullptr; cl_context ocl_ctx = (p.actx_kind == ctx_kind::gpu) ? gpu_ocl_ctx : (p.actx_kind == ctx_kind::cpu) ? cpu_ocl_ctx : nullptr; SKIP_IF(p.adev_kind != dev_kind::null && !ocl_dev, "Required OpenCL device not found."); SKIP_IF(p.actx_kind != ctx_kind::null && !ocl_ctx, "Required OpenCL context not found."); SKIP_IF(cpu_ocl_dev == gpu_ocl_dev && (p.adev_kind == dev_kind::cpu || p.actx_kind == ctx_kind::cpu), "OpenCL CPU-only device not found."); catch_expected_failures( [&]() { { engine eng(engine::kind::gpu, ocl_dev, ocl_ctx); if (p.expected_status != mkldnn_success) { FAIL() << "Success not expected"; } cl_device_id dev = eng.get_ocl_device(); cl_context ctx = eng.get_ocl_context(); EXPECT_EQ(dev, ocl_dev); EXPECT_EQ(ctx, ocl_ctx); cl_uint ref_count; OCL_CHECK(clGetContextInfo(ocl_ctx, CL_CONTEXT_REFERENCE_COUNT, sizeof(ref_count), &ref_count, nullptr)); int i_ref_count = int(ref_count); EXPECT_EQ(i_ref_count, 2); } cl_uint ref_count; OCL_CHECK(clGetContextInfo(ocl_ctx, CL_CONTEXT_REFERENCE_COUNT, sizeof(ref_count), &ref_count, nullptr)); int i_ref_count = int(ref_count); EXPECT_EQ(i_ref_count, 1); }, p.expected_status != mkldnn_success, p.expected_status); }
ScriptValue WebCLContext::getInfo(ScriptState* scriptState, int paramName, ExceptionState& es) { v8::Handle<v8::Object> creationContext = scriptState->context()->Global(); v8::Isolate* isolate = scriptState->isolate(); if (isReleased()) { es.throwWebCLException(WebCLException::InvalidContext, WebCLException::invalidContextMessage); return ScriptValue(scriptState, v8::Null(isolate)); } cl_int err = CL_SUCCESS; cl_uint uintUnits = 0; Vector<RefPtr<WebCLDevice>> result; switch (paramName) { case CL_CONTEXT_NUM_DEVICES: err = clGetContextInfo(m_clContext, CL_CONTEXT_NUM_DEVICES, sizeof(cl_uint), &uintUnits, nullptr); if (err == CL_SUCCESS) return ScriptValue(scriptState, v8::Integer::NewFromUnsigned(isolate, static_cast<unsigned>(uintUnits))); WebCLException::throwException(err, es); return ScriptValue(scriptState, v8::Null(isolate)); case CL_CONTEXT_DEVICES: return ScriptValue(scriptState, toV8(m_devices, creationContext, isolate)); default: es.throwWebCLException(WebCLException::InvalidValue, WebCLException::invalidValueMessage); return ScriptValue(scriptState, v8::Null(isolate)); } }
////////////////////////////////////////////////////////////////////////////// //! Gets the id of the first device from the context //! //! @return the id //! @param cxMainContext OpenCL context ////////////////////////////////////////////////////////////////////////////// cl_device_id btOclGetFirstDev(cl_context cxMainContext) { size_t szParmDataBytes; cl_device_id* cdDevices; // get the list of GPU devices associated with context clGetContextInfo(cxMainContext, CL_CONTEXT_DEVICES, 0, NULL, &szParmDataBytes); cdDevices = (cl_device_id*) malloc(szParmDataBytes); clGetContextInfo(cxMainContext, CL_CONTEXT_DEVICES, szParmDataBytes, cdDevices, NULL); cl_device_id first = cdDevices[0]; free(cdDevices); return first; }
/// // Create a command queue on the first device available on the // context // cl_command_queue CreateCommandQueue(cl_context context, cl_device_id *device) { cl_int errNum; cl_device_id *devices; cl_command_queue commandQueue = NULL; size_t deviceBufferSize = -1; // First get the size of the devices buffer errNum = clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &deviceBufferSize); if (errNum != CL_SUCCESS) { std::cerr << "Failed call to clGetContextInfo(...,GL_CONTEXT_DEVICES,...)"; return NULL; } if (deviceBufferSize <= 0) { std::cerr << "No devices available."; return NULL; } // Allocate memory for the devices buffer devices = new cl_device_id[deviceBufferSize / sizeof(cl_device_id)]; errNum = clGetContextInfo(context, CL_CONTEXT_DEVICES, deviceBufferSize, devices, NULL); if (errNum != CL_SUCCESS) { delete [] devices; std::cerr << "Failed to get device IDs"; return NULL; } // In this example, we just choose the first available device. In a // real program, you would likely use all available devices or choose // the highest performance device based on OpenCL device queries commandQueue = clCreateCommandQueue(context, devices[0], 0, NULL); if (commandQueue == NULL) { delete [] devices; std::cerr << "Failed to create commandQueue for device 0"; return NULL; } *device = devices[0]; delete [] devices; return commandQueue; }
void init() { cl_uint n; cl_platform_id *ids,id=NULL; int i,status; char s[MAXCHAR]; /* choose AMD platform if possible, otherwise just pick one */ if(CL_SUCCESS!=clGetPlatformIDs(0,NULL,&n)) error("error getting platforms 1"); if(NULL==(ids=malloc(sizeof(cl_platform_id)*n))) error("out of memory"); if(CL_SUCCESS!=clGetPlatformIDs(n,ids,NULL)) error("error getting platforms 2"); for(i=0;i<n;i++) { if(CL_SUCCESS!=clGetPlatformInfo(ids[i],CL_PLATFORM_VENDOR,MAXCHAR,s,NULL)) error("error getting platform 3"); if(!strcmp(s,"Advanced Micro Devices, Inc.")) { id=ids[i]; break; } } if(i==n) id=ids[0]; free(ids); /* get context stuff */ cl_context_properties cps[3]={CL_CONTEXT_PLATFORM,(cl_context_properties)id,0}; context=clCreateContextFromType(cps,CL_DEVICE_TYPE_CPU,NULL,NULL,&status); if(CL_SUCCESS!=status) error("couldn't create context"); if(CL_SUCCESS!=clGetContextInfo(context,CL_CONTEXT_DEVICES,0,NULL,(size_t *)&n)) error("error getting context info 1"); if(!n) error("no devices!"); if(NULL==(devices=malloc(n))) error("out of memory"); if(CL_SUCCESS!=clGetContextInfo(context,CL_CONTEXT_DEVICES,n,devices,NULL)) error("error getting context info 2"); /* create opencl command queue */ cmdq=clCreateCommandQueue(context,devices[0],0,&status); if(CL_SUCCESS!=status) error("error creating command queue"); /* create memory buffers */ initmem(); /* copy contents of in to inbuffer */ inbuffer=clCreateBuffer(context,CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, MAXP,in,&status); if(CL_SUCCESS!=status) { printf("max alloc %d\n",(int)CL_DEVICE_MAX_MEM_ALLOC_SIZE); printf("status %d\n",status); error("error creating input buffer"); } loadkernel("sieve.cl"); }
int vmd_cl_context_num_devices(cl_context clctx) { size_t parmsz; cl_int clerr = clGetContextInfo(clctx, CL_CONTEXT_DEVICES, 0, NULL, &parmsz); if (clerr != CL_SUCCESS) return 0; return (int) (parmsz / sizeof(size_t)); }
/*! Returns the default device in use by this context, which is typically the first element of the devices() list; or a null QCLDevice if the context has not been created yet. \sa devices() */ QCLDevice QCLContext::defaultDevice() const { Q_D(const QCLContext); if (d->isCreated) { if (!d->defaultDevice.isNull()) return d->defaultDevice; size_t size = 0; if (clGetContextInfo(d->id, CL_CONTEXT_DEVICES, 0, 0, &size) == CL_SUCCESS && size > 0) { QVarLengthArray<cl_device_id> buf(size); if (clGetContextInfo(d->id, CL_CONTEXT_DEVICES, size, buf.data(), 0) == CL_SUCCESS) { return QCLDevice(buf[0]); } } } return QCLDevice(); }
/*! Returns the list of devices that are in use by this context. If the context has not been created, returns an empty list. \sa defaultDevice() */ QList<QCLDevice> QCLContext::devices() const { Q_D(const QCLContext); QList<QCLDevice> devs; if (d->isCreated) { size_t size = 0; if (clGetContextInfo(d->id, CL_CONTEXT_DEVICES, 0, 0, &size) == CL_SUCCESS && size > 0) { QVarLengthArray<cl_device_id> buf(size); if (clGetContextInfo(d->id, CL_CONTEXT_DEVICES, size, buf.data(), 0) == CL_SUCCESS) { for (size_t index = 0; index < size; ++index) devs.append(QCLDevice(buf[index])); } } } return devs; }
cl_int WINAPI wine_clGetContextInfo(cl_context context, cl_context_info param_name, SIZE_T param_value_size, void * param_value, size_t * param_value_size_ret) { cl_int ret; TRACE("(%p, 0x%x, %ld, %p, %p)\n", context, param_name, param_value_size, param_value, param_value_size_ret); ret = clGetContextInfo(context, param_name, param_value_size, param_value, param_value_size_ret); TRACE("(%p, 0x%x, %ld, %p, %p)=%d\n", context, param_name, param_value_size, param_value, param_value_size_ret, ret); return ret; }
ClHelper::ClHelper() : mContext((cl_context)0), mDeviceId((cl_device_id)0), mCommandQueue((cl_command_queue)0), mProgram((cl_program)0), mKernel((cl_kernel)0) { cl_int status; // 플랫폼 cl_platform_id platforms[10]; cl_uint num_platforms; status = clGetPlatformIDs(sizeof(platforms) / sizeof(platforms[0]), platforms, &num_platforms); if (status != CL_SUCCESS || num_platforms <= 0) { fprintf(stderr, "clGetPlatformIDs failed.\n"); printError(status); throw MyError("failed to get platform IDs.", __FUNCTION__); } cl_context_properties properties[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[0], 0}; // 디바이스에 따른 컨텍스트 취득 mContext = clCreateContextFromType(properties, // CL_DEVICE_TYPE_CPU, // CPUを使用 CL_DEVICE_TYPE_GPU, // GPUを使用 NULL, NULL, &status); if (status != CL_SUCCESS) { printError(status); throw MyError("failed to create context.", __FUNCTION__); } // 디바이스 아이디 취득 cl_device_id devices[10]; status = clGetContextInfo(mContext, CL_CONTEXT_DEVICES, sizeof(devices), devices, NULL); if (status != CL_SUCCESS) { printError(status); throw MyError("failed to get device id.", __FUNCTION__); } mDeviceId = devices[0]; // 커맨드 큐 생성 mCommandQueue = clCreateCommandQueue(mContext, devices[0], 0, &status); if (status != CL_SUCCESS) { printError(status); throw MyError("failed to create command queue.", __FUNCTION__); } }
Ensure(eclGetContextInteractively, returnsAValidContext) { eclGetContextInteractively(&ctx); if (ctx.context) { size_t size; err = clGetContextInfo(ctx.context, CL_CONTEXT_PROPERTIES, 0, 0, &size); assert_that(err, is_equal_to(CL_SUCCESS)); } }
cl_command_queue vmd_cl_create_command_queue(cl_context clctx, int dev) { size_t parmsz; cl_int clerr = clGetContextInfo(clctx, CL_CONTEXT_DEVICES, 0, NULL, &parmsz); if (clerr != CL_SUCCESS) return NULL; cl_device_id* cldevs = (cl_device_id *) malloc(parmsz); clerr = clGetContextInfo(clctx, CL_CONTEXT_DEVICES, parmsz, cldevs, NULL); if (clerr != CL_SUCCESS) return NULL; cl_command_queue clcmdq = clCreateCommandQueue(clctx, cldevs[dev], 0, &clerr); free(cldevs); if (clerr != CL_SUCCESS) return NULL; return clcmdq; }
cl_kernel vmd_cl_compile_kernel(cl_context clctx, const char *kernname, const char *srctext, const char *flags, cl_int *clerr, int verbose) { char buildlog[8192]; cl_program clpgm = NULL; cl_kernel clkern = NULL; clpgm = clCreateProgramWithSource(clctx, 1, &srctext, NULL, clerr); if (clerr != CL_SUCCESS) { if (verbose) printf("Failed to compile OpenCL kernel: '%s'\n", kernname); return NULL; } *clerr = clBuildProgram(clpgm, 0, NULL, flags, NULL, NULL); #if 1 if (verbose) { memset(buildlog, 0, sizeof(buildlog)); size_t parmsz; *clerr |= clGetContextInfo(clctx, CL_CONTEXT_DEVICES, 0, NULL, &parmsz); cl_device_id* cldevs = (cl_device_id *) malloc(parmsz); *clerr |= clGetContextInfo(clctx, CL_CONTEXT_DEVICES, parmsz, cldevs, NULL); size_t len=0; *clerr = clGetProgramBuildInfo(clpgm, cldevs[0], CL_PROGRAM_BUILD_LOG, sizeof(buildlog), buildlog, &len); if (len > 1) { printf("OpenCL kernel compilation log:\n"); printf(" '%s'\n", buildlog); } } #endif clkern = clCreateKernel(clpgm, kernname, clerr); if (clerr != CL_SUCCESS) { if (verbose) printf("Failed to create OpenCL kernel: '%s'\n", kernname); return NULL; } return clkern; }
//------------------------------------------------------------------------------ cl_device_id get_device_id(cl_context ctx) { cl_device_id deviceID; // retrieve actual device id from context cl_int status = clGetContextInfo(ctx, CL_CONTEXT_DEVICES, sizeof(cl_device_id), &deviceID, 0); check_cl_error(status, "clGetContextInfo"); return deviceID; }
/// // Create a command queue on the first device available on the context // cl_command_queue CreateCommandQueue(cl_context context, cl_device_id *cldevice) { cl_int errNum; cl_device_id *cldevices; cl_command_queue commandQueue = NULL; size_t deviceBufferSize = -1; // First get the size of the devices buffer errNum = clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &deviceBufferSize); if (errNum != CL_SUCCESS) { std::cerr << "Failed call to clGetContextInfo(...,GL_CONTEXT_DEVICES,...)"; return NULL; } if (deviceBufferSize <= 0) { std::cerr << "No devices available."; return NULL; } // Allocate memory for the devices buffer cldevices = new cl_device_id[deviceBufferSize / sizeof(cl_device_id)]; errNum = clGetContextInfo(context, CL_CONTEXT_DEVICES, deviceBufferSize, cldevices, NULL); if (errNum != CL_SUCCESS) { delete [] cldevices; std::cerr << "Failed to get device IDs"; return NULL; } commandQueue = clCreateCommandQueue(context, cldevices[0], 0, NULL); if (commandQueue == NULL) { delete [] cldevices; std::cerr << "Failed to create commandQueue for device "; return NULL; } *cldevice = cldevices[0]; delete [] cldevices; return commandQueue; }