本文整理汇总了C++中clGetDeviceIDs函数的典型用法代码示例。如果您正苦于以下问题:C++ clGetDeviceIDs函数的具体用法?C++ clGetDeviceIDs怎么用?C++ clGetDeviceIDs使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了clGetDeviceIDs函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: main
int main() {
char buf[]="Hello, World!";
size_t srcsize, worksize=strlen(buf);
cl_int error;
cl_platform_id platform;
cl_device_id device;
cl_uint platforms, devices;
// Fetch the Platform and Device IDs; we only want one.
error=clGetPlatformIDs(1, &platform, &platforms);
error=clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, &device, &devices);
cl_context_properties properties[]={
CL_CONTEXT_PLATFORM, (cl_context_properties)platform,
0};
// Note that nVidia's OpenCL requires the platform property
cl_context context=clCreateContext(properties, 1, &device, NULL, NULL, &error);
cl_command_queue cq = clCreateCommandQueue(context, device, 0, &error);
rot13(buf); // scramble using the CPU
puts(buf); // Just to demonstrate the plaintext is destroyed
//char src[8192];
//FILE *fil=fopen("rot13.cl","r");
//srcsize=fread(src, sizeof src, 1, fil);
//fclose(fil);
const char *src=rot13_cl;
srcsize=strlen(rot13_cl);
const char *srcptr[]={src};
// Submit the source code of the rot13 kernel to OpenCL
cl_program prog=clCreateProgramWithSource(context,
1, srcptr, &srcsize, &error);
// and compile it (after this we could extract the compiled version)
error=clBuildProgram(prog, 0, NULL, "", NULL, NULL);
// Allocate memory for the kernel to work with
cl_mem mem1, mem2;
mem1=clCreateBuffer(context, CL_MEM_READ_ONLY, worksize, NULL, &error);
mem2=clCreateBuffer(context, CL_MEM_WRITE_ONLY, worksize, NULL, &error);
// get a handle and map parameters for the kernel
cl_kernel k_rot13=clCreateKernel(prog, "rot13", &error);
clSetKernelArg(k_rot13, 0, sizeof(mem1), &mem1);
clSetKernelArg(k_rot13, 1, sizeof(mem2), &mem2);
// Target buffer just so we show we got the data from OpenCL
char buf2[sizeof buf];
buf2[0]='?';
buf2[worksize]=0;
// Send input data to OpenCL (async, don't alter the buffer!)
error=clEnqueueWriteBuffer(cq, mem1, CL_FALSE, 0, worksize, buf, 0, NULL, NULL);
// Perform the operation
error=clEnqueueNDRangeKernel(cq, k_rot13, 1, NULL, &worksize, &worksize, 0, NULL, NULL);
// Read the result back into buf2
error=clEnqueueReadBuffer(cq, mem2, CL_FALSE, 0, worksize, buf2, 0, NULL, NULL);
// Await completion of all the above
error=clFinish(cq);
// Finally, output out happy message.
puts(buf2);
}
开发者ID:yeerkkiller1,项目名称:Go-AI,代码行数:64,代码来源:hellocl.c
示例2: main
int main() {
// Get platform information
err = clGetPlatformIDs(0, NULL, &numOfPlatforms);
if (err) Error("Fail to get the number of platforms");
printf("The machine has %d platform(s) for OpenCL.\n", numOfPlatforms);
platformIDs = new cl_platform_id [numOfPlatforms];
err = clGetPlatformIDs(numOfPlatforms, platformIDs, NULL);
if (err) Error("Fail to get the platform list");
int cudaPlatformID = -1;
for (int i = 0; i < numOfPlatforms; i++) {
char platformName[50];
err = clGetPlatformInfo(platformIDs[i], CL_PLATFORM_NAME, 50, platformName, NULL);
if (err) Error("Fail to get the platform name");
printf("Platform %d is %s\n", i + 1, platformName);
if (!strcmp(platformName, "NVIDIA CUDA")) cudaPlatformID = i;
}
printf("\n");
if (cudaPlatformID == -1) Error("Fail to find an NVIDIA CUDA platform");
printf("Platform %d (NVIDIA CUDA) is chosen for use.\n", cudaPlatformID + 1);
printf("\n");
// Get device information
err = clGetDeviceIDs(platformIDs[cudaPlatformID], CL_DEVICE_TYPE_GPU, 0, NULL, &numOfDevices);
if (err) Error("Fail to get the number of devices");
printf("CUDA platform has %d device(s).\n", numOfDevices);
deviceIDs = new cl_device_id [numOfDevices];
err = clGetDeviceIDs(platformIDs[cudaPlatformID], CL_DEVICE_TYPE_GPU, numOfDevices, deviceIDs, NULL);
if (err) Error("Fail to get the device list");
for (int i = 0; i < numOfDevices; i++) {
char deviceName[50];
err = clGetDeviceInfo(deviceIDs[i], CL_DEVICE_NAME, 50, deviceName, NULL);
if (err) Error("Fail to get the device name");
printf("Device %d is %s\n", i + 1, deviceName);
}
printf("\n");
// Create a context
context = clCreateContext(NULL, numOfDevices, deviceIDs, NULL, NULL, &err);
if (err) Error("Fail to create a context");
printf("Device 1 is chosen for use.\n");
printf("\n");
// Create a command queue for the first device
commandQueue = clCreateCommandQueue(context, deviceIDs[0],
CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | CL_QUEUE_PROFILING_ENABLE, &err);
if (err) Error("Fail to create a command queue");
// create the program
cl_program program = CreateProgram(exclusiveScanKernels, "exclusive scan");
// create two kernels
cl_kernel scanKernel = clCreateKernel(program, "Scan", &err);
if (err) Error("Fail to create the kernel for scan");
cl_kernel reverseUpdateKernel = clCreateKernel(program, "ReverseUpdate", &err);
if (err) Error("Fail to create the kernel for reverse update");
// Get the work group size
size_t maxWorkGroupSize;
err = clGetKernelWorkGroupInfo(scanKernel, deviceIDs[0], CL_KERNEL_WORK_GROUP_SIZE,
sizeof(size_t), &maxWorkGroupSize, NULL);
printf("maxWorkGroupSize = %d\n", maxWorkGroupSize);
err = clGetKernelWorkGroupInfo(reverseUpdateKernel, deviceIDs[0], CL_KERNEL_WORK_GROUP_SIZE,
sizeof(size_t), &maxWorkGroupSize, NULL);
printf("maxWorkGroupSize = %d\n", maxWorkGroupSize);
// Set work group size to 64
int workGroupSize = 512;
int length = 2048000;
int *arr = new int [length];
for (int i = 0; i < length; i++)
arr[i] = rand() % 100;
int *prefixSum = new int [length];
prefixSum[0] = 0;
int t0 = clock();
for (int i = 1; i < length; i++)
prefixSum[i] = prefixSum[i - 1] + arr[i - 1];
int t1 = clock();
printf("time1: %lf\n", (t1 - t0) * 1.0 / CLOCKS_PER_SEC);
cl_mem d_arr = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int) * length, NULL, &err);
if (err) Error("Fail to create d_arr");
err = clEnqueueWriteBuffer(commandQueue, d_arr, CL_TRUE, 0, sizeof(int) * length, arr, 0, NULL, NULL);
if (err) Error("Fail to write d_arr");
//.........这里部分代码省略.........
开发者ID:linyufly,项目名称:FlowVC,代码行数:101,代码来源:ExclusiveScan.cpp
示例3: sum_gpu
void sum_gpu(long long *in, long long *out, unsigned int n)
{
size_t global_size;
size_t local_size;
char *kernel_src;
cl_int err;
cl_platform_id platform_id;
cl_device_id device_id;
cl_uint max_compute_units;
size_t max_workgroup_size;
cl_context context;
cl_command_queue commands;
cl_program program;
cl_kernel kernel;
cl_mem d_array;
cl_event event;
cl_ulong start, end;
/* start OpenCL */
err = clGetPlatformIDs(1, &platform_id,NULL);
clErrorHandling("clGetPlatformIDs");
err = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, NULL);
clErrorHandling("clGetDeviceIDs");
context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
clErrorHandling("clCreateContext");
commands = clCreateCommandQueue(context, device_id, CL_QUEUE_PROFILING_ENABLE, &err);
clErrorHandling("clCreateCommandQueue");
/* create kernel */
kernel_src = file_to_string(KERNEL_SRC);
program = clCreateProgramWithSource(context, 1, (const char**) &kernel_src, NULL, &err);
free(kernel_src);
clErrorHandling("clCreateProgramWithSource");
err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
clErrorHandling("clBuildProgram");
kernel = clCreateKernel(program, "matrix_mult", &err);
clErrorHandling("clCreateKernel");
/* allocate memory and send to gpu */
d_array = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(long long) * n, NULL, &err);
clErrorHandling("clCreateBuffer");
err = clEnqueueWriteBuffer(commands, d_array, CL_TRUE, 0, sizeof(long long) * n, in, 0, NULL, NULL);
clErrorHandling("clEnqueueWriteBuffer");
err = clGetDeviceInfo(device_id, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint), &max_compute_units, NULL);
err |= clGetDeviceInfo(device_id, CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), &max_workgroup_size, NULL);
clErrorHandling("clGetDeviceInfo");
/* prepare kernel args */
err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &d_array);
err |= clSetKernelArg(kernel, 1, sizeof(unsigned int), &n);
/* execute */
local_size = n / max_compute_units / 8;
if (local_size > max_workgroup_size)
local_size = max_workgroup_size;
/*
* Usually it would be
* global_size = local_size * max_compute_units;
* but that would only be valid if local_size = n / max_compute_units;
* local_size is n / max_compute_units / 8 because it obtains its hightest performance.
*/
for (global_size = local_size; global_size < n; global_size += local_size);
err = clEnqueueNDRangeKernel(commands, kernel, 1, NULL, &global_size, &local_size, 0, NULL, &event);
clErrorHandling("clEnqueueNDRangeKernel");
clWaitForEvents(1, &event);
clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &start, NULL);
clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &end, NULL);
fprintf(stderr, "Time for event (ms): %10.5f \n", (end - start) / 1000000.0);
err = clFinish(commands);
clErrorHandling("clFinish");
/* transfer back */
err = clEnqueueReadBuffer(commands, d_array, CL_TRUE, 0, sizeof(long long), out, 0, NULL, NULL); // a single long long
clErrorHandling("clEnqueueReadBuffer");
/* cleanup*/
clReleaseMemObject(d_array);
clReleaseProgram(program);
clReleaseKernel(kernel);
clReleaseCommandQueue(commands);
clReleaseContext(context);
clReleaseEvent(event);
}
开发者ID:Exceltior,项目名称:dei-hpc,代码行数:98,代码来源:sum.c
示例4: InitOpenCL
void InitOpenCL()
{
// 1. Get a platform.
cl_platform_id platform;
clGetPlatformIDs( 1, &platform, NULL );
// 2. Find a gpu device.
cl_device_id device;
clGetDeviceIDs( platform, CL_DEVICE_TYPE_GPU,
1,
&device,
NULL);
// 3. Create a context and command queue on that device.
cl_context context = clCreateContext( NULL,
1,
&device,
NULL, NULL, NULL);
queue = clCreateCommandQueue( context,
device,
0, NULL );
// 4. Perform runtime source compilation, and obtain kernel entry point.
std::ifstream file("scene.cl");
std::string source;
if (file){
while(!file.eof()){
char line[256];
file.getline(line,255);
source += std::string(line) + "\n";
}
}
if (source.length()==0)
{
std::string err = "fail to load shader";
}
cl_ulong maxSize;
clGetDeviceInfo(device, CL_DEVICE_MAX_MEM_ALLOC_SIZE , sizeof(cl_ulong), &maxSize, 0);
const char* str = source.c_str();
cl_program program = clCreateProgramWithSource( context,
1,
&str,
NULL, NULL );
cl_int result = clBuildProgram( program, 1, &device, NULL, NULL, NULL );
if ( result ){
char* build_log;
size_t log_size;
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
build_log = new char[log_size+1];
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, log_size, build_log, NULL);
build_log[log_size] = '\0';
if( log_size > 2 ) {
std::cout << "build log: " << build_log << std::endl;
}
delete[] build_log;
std::cout << "Error during compilation! (" << result << ")" << std::endl;
}
kernel = clCreateKernel( program, "tracekernel", NULL );
// 5. Create a data buffer.
buffer = clCreateBuffer( context,
CL_MEM_WRITE_ONLY,
kWidth * kHeight *sizeof(cl_float4),
NULL, 0 );
viewTransform = clCreateBuffer( context,
CL_MEM_READ_WRITE,
16 *sizeof(cl_float),
NULL, 0 );
worldTransforms = clCreateBuffer( context,
CL_MEM_READ_WRITE,
16 *sizeof(cl_float)*2,
NULL, 0 );
clSetKernelArg(kernel, 0, sizeof(buffer), (void*) &buffer);
clSetKernelArg(kernel, 1, sizeof(cl_uint), (void*) &kWidth);
clSetKernelArg(kernel, 2, sizeof(cl_uint), (void*) &kWidth);
clSetKernelArg(kernel, 3, sizeof(viewTransform), (void*) &viewTransform);
clSetKernelArg(kernel, 4, sizeof(worldTransforms), (void*) &worldTransforms);
}
开发者ID:autosquid,项目名称:CLRay,代码行数:80,代码来源:raytracer.cpp
示例5: device_check
//.........这里部分代码省略.........
perror("Couldn't read CL_PLATFORM_VENDER data.");
}
ext_data = (char *)malloc(ext_size);
clGetPlatformInfo(platform, CL_PLATFORM_VENDER, ext_size, ext_data, NULL);
printf("Platform %d vender: %s\n", i, ext_data);
free(ext_data)
/*VERSION*/
err = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, 0, NULL, &ext_size);
if(err < 0) {
perror("Couldn't read CL_PLATFORM_VERSION data.");
}
ext_data = (char *)malloc(ext_size);
clGetPlatformInfo(platform, CL_PLATFORM_VERSION, ext_size, ext_data, NULL);
printf("Platform %d support OpenCL version: %s\n", i, ext_data);
free(ext_data)
/*PROFILE*/
err = clGetPlatformInfo(platform, CL_PLATFORM_PROFILE, 0, NULL, &ext_size);
if(err < 0) {
perror("Couldn't read CL_PLATFORM_PROFILE data.");
}
ext_data = (char *)malloc(ext_size);
clGetPlatformInfo(platform, CL_PLATFORM_PROFILE, ext_size, ext_data, NULL);
printf("Platform %d support OpenCL profile: %s\n", i, ext_data);
free(ext_data)
/*EXTENSIONS*/
err = clGetPlatformInfo(platform, CL_PLATFORM_EXTENSIONS, 0, NULL, &ext_size);
if(err < 0) {
perror("Couldn't read CL_PLATFORM_EXTENSIONS data.");
}
ext_data = (char *)malloc(ext_size);
clGetPlatformInfo(platform, CL_PLATFORM_EXTENSIONS, ext_size, ext_data, NULL);
printf("Platform %d support OpenCL extensions: %s\n", i, ext_data);
free(ext_data)
/*现在对这个platform进一步的提取信息*/
/*获取Device信息*/
/*clGetDeviceIDs*/
/*
参数1:平台句柄
参数2:要获取设备的类型
参数3:要获取的数量
参数4:返回信息的数据指针
参数5:返回信息的实际条数
*/
for (cnt=0; cnt<2; cnt++)
{
if (0==cnt) err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &num_devs);
if (1==cnt) err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 0, NULL, &num_devs);
if(err == CL_DEVICE_NOT_FOUND)
{
if (0==cnt) printf("No GPU support OpenCL found.\n");
if (1==cnt) printf("No CPU support OpenCL found.\n");
}
else if(err < 0)
{
if (0==cnt) printf("Couldn't access any GPU devices.\n");
if (1==cnt) printf("Couldn't access any CPU devices.\n");
}
else
{
devs = (cl_device_id *)malloc( sizeof(cl_device_id) * num_devs );
clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, num_devs, devs, NULL);
for (j=0; j<num_devs; j++)
{
cl_device_id dev;
char dev_name_data[48];
cl_uint addr_data;
cl_ulong global_mem_size;
/*name*/
err = clGetDeviceInfo(dev, CL_DEVICE_NAME, 48 * sizeof(char), dev_name_data, NULL);
if(err < 0) {
perror("Couldn't read dev name data");
exit(1);
}
printf("Dev %d: NAME: %s\n", j, name_data);
/*address size*/
err = clGetDeviceInfo(dev, CL_DEVICE_ADDRESS_BITS, sizeof(addr_data), &addr_data, NULL);
printf("Dev %d: ADDRESS_WIDTH: %u\n", j, addr_data);
/*device extensions*/
ext_data=malloc(4096)
clGetDeviceInfo(dev, CL_DEVICE_EXTENSIONS, 4096 * sizeof(char), ext_data, NULL);
printf("Dev %d: EXTENSIONS: %s\n", j, ext_data);
free(ext_data)
err = clGetDeviceInfo(dev, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(global_mem_size), &global_mem_size, NULL);
printf("Dev %d: GLOBAL_MEM_SIZE: %u\n", j, global_mem_size);
}
}
}
}
return 0;
}
开发者ID:abednego1979,项目名称:tipster,代码行数:101,代码来源:device_check.c
示例6: main
int main(void) {
const size_t ARRAY_BYTES = ARRAY_SIZE * sizeof(float);
// Generate the input array on the host.
float h_a[ARRAY_SIZE];
float h_b[ARRAY_SIZE];
for (int i = 0; i < ARRAY_SIZE; i++) {
h_a[i] = (float)i;
h_b[i] = (float)(2 * i);
}
float h_c[ARRAY_SIZE];
FILE *fp;
char *source_str;
size_t source_size;
fp = fopen("vectors_cl.cl", "r");
if (!fp) {
fprintf(stderr, "Failed to load kernel.\n");
exit(1);
}
source_str = (char *)malloc(MAX_SOURCE_SIZE);
source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
fclose(fp);
// Get platform and device information
cl_platform_id platform_id = NULL;
cl_device_id device_id = NULL;
cl_uint ret_num_devices;
cl_uint ret_num_platforms;
cl_int ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1,
&device_id, &ret_num_devices);
// Create an OpenCL context
cl_context context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &ret);
// Create a command queue
cl_command_queue command_queue = clCreateCommandQueue(context, device_id, 0, &ret);
// Create memory buffers on the device for each vector
cl_mem a_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY,
ARRAY_BYTES, NULL, &ret);
cl_mem b_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY,
ARRAY_BYTES, NULL, &ret);
cl_mem c_mem_obj = clCreateBuffer(context, CL_MEM_WRITE_ONLY,
ARRAY_BYTES, NULL, &ret);
// Copy h_a and h_b to memory buffer
ret = clEnqueueWriteBuffer(command_queue, a_mem_obj, CL_TRUE, 0,
ARRAY_BYTES, h_a, 0, NULL, NULL);
ret = clEnqueueWriteBuffer(command_queue, b_mem_obj, CL_TRUE, 0,
ARRAY_BYTES, h_b, 0, NULL, NULL);
// Create a program from the kernel source
cl_program program = clCreateProgramWithSource(context, 1,
(const char **)&source_str, (const size_t *)&source_size, &ret);
if (ret != 0) {
printf("clCreateProgramWithSource returned non-zero status %d\n\n", ret);
exit(1);
}
// Build the program
ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
if (ret != 0) {
printf("clBuildProgram returned non-zero status %d: ", ret);
if (ret == CL_INVALID_PROGRAM) {
printf("invalid program\n");
} else if (ret == CL_INVALID_VALUE) {
printf("invalid value\n");
} else if (ret == CL_INVALID_DEVICE) {
printf("invalid device\n");
} else if (ret == CL_INVALID_BINARY) {
printf("invalid binary\n");
} else if (ret == CL_INVALID_BUILD_OPTIONS) {
printf("invalid build options\n");
} else if (ret == CL_INVALID_OPERATION) {
printf("invalid operation\n");
} else if (ret == CL_COMPILER_NOT_AVAILABLE) {
printf("compiler not available\n");
} else if (ret == CL_BUILD_PROGRAM_FAILURE) {
printf("build program failure\n");
// Determine the size of the log
size_t log_size;
clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
// Allocate memory for the log
char *log = (char *) malloc(log_size);
// Get the log
clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, log_size, log, NULL);
// Print the log
printf("%s\n", log);
} else if (ret == CL_OUT_OF_HOST_MEMORY) {
printf("out of host memory\n");
}
//.........这里部分代码省略.........
开发者ID:Unit03,项目名称:gpgpu_talk,代码行数:101,代码来源:vectors_cl.c
示例7: main
int main()
{
cl_platform_id platform_id = NULL;
cl_device_id device_id = NULL;
cl_context context = NULL;
cl_command_queue command_queue = NULL;
cl_mem memobj = NULL;
cl_program program = NULL;
cl_kernel kernel = NULL;
cl_uint ret_num_devices;
cl_uint ret_num_platforms;
cl_int ret;
float mem[MEM_SIZE];
FILE *fp;
char fileName[] = "./kernel.clbin";
size_t binary_size;
char *binary_buf;
cl_int binary_status;
cl_int i;
/* カーネルを含むオブジェクトファイルをロード */
fp = fopen(fileName, "r");
if (!fp) {
fprintf(stderr, "Failed to load kernel.\n");
exit(1);
}
binary_buf = (char *)malloc(MAX_BINARY_SIZE);
binary_size = fread( binary_buf, 1, MAX_BINARY_SIZE, fp );
fclose( fp );
/* データを初期化 */
for( i = 0; i < MEM_SIZE; i++ ) {
mem[i] = i;
}
/* プラットフォーム・デバイスの情報の取得 */
ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, &ret_num_devices);
/* OpenCLコンテキストの作成 */
context = clCreateContext( NULL, 1, &device_id, NULL, NULL, &ret);
/* コマンドキューの作成 */
command_queue = clCreateCommandQueue(context, device_id, 0, &ret);
/* メモリバッファの作成 */
memobj = clCreateBuffer(context, CL_MEM_READ_WRITE, MEM_SIZE * sizeof(float), NULL, &ret);
/* メモリバッファにデータを転送 */
ret = clEnqueueWriteBuffer(command_queue, memobj, CL_TRUE, 0, MEM_SIZE * sizeof(float), mem, 0, NULL, NULL);
/* 読み込んだバイナリからカーネルプログラムを作成 */
program = clCreateProgramWithBinary(context, 1, &device_id, (const size_t *)&binary_size,
(const unsigned char **)&binary_buf, &binary_status, &ret);
/* OpenCLカーネルの作成 */
kernel = clCreateKernel(program, "vecAdd", &ret);
printf("err:%d\n", ret);
/* OpenCLカーネル引数の設定 */
ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&memobj);
size_t global_work_size[3] = {MEM_SIZE, 0, 0};
size_t local_work_size[3] = {MEM_SIZE, 0, 0};
/* OpenCLカーネルを実行 */
ret = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, global_work_size, local_work_size, 0, NULL, NULL);
/* メモリバッファから結果を取得 */
ret = clEnqueueReadBuffer(command_queue, memobj, CL_TRUE, 0, MEM_SIZE * sizeof(float), mem, 0, NULL, NULL);
/* 結果の表示 */
for(i=0; i<MEM_SIZE; i++) {
printf("mem[%d] : %f\n", i, mem[i]);
}
/* 終了処理 */
ret = clFlush(command_queue);
ret = clFinish(command_queue);
ret = clReleaseKernel(kernel);
ret = clReleaseProgram(program);
ret = clReleaseMemObject(memobj);
ret = clReleaseCommandQueue(command_queue);
ret = clReleaseContext(context);
free(binary_buf);
return 0;
}
开发者ID:intersense,项目名称:ocl_dev,代码行数:91,代码来源:offline.cpp
示例8: main
int main(void)
{
hwloc_topology_t topology;
cl_int clret;
cl_platform_id *platform_ids;
unsigned nrp, nrd, count, i, j;
int err;
hwloc_topology_init(&topology);
hwloc_topology_set_flags(topology, HWLOC_TOPOLOGY_FLAG_IO_DEVICES);
hwloc_topology_load(topology);
clret = clGetPlatformIDs(0, NULL, &nrp);
if (CL_SUCCESS != clret || !nrp)
return 0;
platform_ids = malloc(nrp * sizeof(*platform_ids));
if (!platform_ids)
return 0;
clret = clGetPlatformIDs(nrp, platform_ids, &nrp);
if (CL_SUCCESS != clret || !nrp)
return 0;
count = 0;
for(i=0; i<nrp; i++) {
cl_device_id *device_ids;
clret = clGetDeviceIDs(platform_ids[i], CL_DEVICE_TYPE_ALL, 0, NULL, &nrd);
if (CL_SUCCESS != clret || !nrd)
continue;
device_ids = malloc(nrd * sizeof(*device_ids));
if (!device_ids)
continue;
clret = clGetDeviceIDs(platform_ids[i], CL_DEVICE_TYPE_ALL, nrd, device_ids, &nrd);
if (CL_SUCCESS != clret || !nrd)
continue;
for(j=0; j<nrd; j++) {
hwloc_bitmap_t set;
hwloc_obj_t osdev, osdev2, ancestor;
const char *value;
osdev = hwloc_opencl_get_device_osdev(topology, device_ids[j]);
osdev2 = hwloc_opencl_get_device_osdev_by_index(topology, i, j);
assert(osdev == osdev2);
if (!osdev) {
printf("no osdev for platform %d device %d\n", i, j);
continue;
}
ancestor = hwloc_get_non_io_ancestor_obj(topology, osdev);
set = hwloc_bitmap_alloc();
err = hwloc_opencl_get_device_cpuset(topology, device_ids[j], set);
if (err < 0) {
printf("no cpuset for platform %d device %d\n", i, j);
} else {
char *cpuset_string = NULL;
hwloc_bitmap_asprintf(&cpuset_string, set);
printf("got cpuset %s for platform %d device %d\n", cpuset_string, i, j);
free(cpuset_string);
assert(hwloc_bitmap_isequal(set, ancestor->cpuset));
}
hwloc_bitmap_free(set);
printf("found OSDev %s\n", osdev->name);
err = strncmp(osdev->name, "opencl", 6);
assert(!err);
assert(atoi(osdev->name+6) == (int) count);
value = hwloc_obj_get_info_by_name(osdev, "Backend");
err = strcmp(value, "OpenCL");
assert(!err);
value = hwloc_obj_get_info_by_name(osdev, "Name");
printf("found OSDev name %s\n", value);
count++;
}
}
hwloc_topology_destroy(topology);
return 0;
}
开发者ID:BlueBrain,项目名称:hwloc,代码行数:84,代码来源:opencl.c
示例9: main
int
main(void)
{
cl_int err;
cl_platform_id platform = 0;
cl_device_id device = 0;
cl_context_properties props[3] = { CL_CONTEXT_PLATFORM, 0, 0 };
cl_context ctx = 0;
cl_command_queue queue = 0;
cl_mem bufX, bufY;
cl_event event = NULL;
int ret = 0;
int lenX = 1 + (N-1)*abs(incx);
int lenY = 1 + (N-1)*abs(incy);
/* Setup OpenCL environment. */
err = clGetPlatformIDs(1, &platform, NULL);
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 1, &device, NULL);
if (err != CL_SUCCESS) {
printf( "clGetPlatformIDs() failed with %d\n", err );
return 1;
}
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
if (err != CL_SUCCESS) {
printf( "clGetDeviceIDs() failed with %d\n", err );
return 1;
}
props[1] = (cl_context_properties)platform;
ctx = clCreateContext(props, 1, &device, NULL, NULL, &err);
if (err != CL_SUCCESS) {
printf( "clCreateContext() failed with %d\n", err );
return 1;
}
queue = clCreateCommandQueue(ctx, device, 0, &err);
if (err != CL_SUCCESS) {
printf( "clCreateCommandQueue() failed with %d\n", err );
clReleaseContext(ctx);
return 1;
}
/* Setup clblas. */
err = clblasSetup();
if (err != CL_SUCCESS) {
printf("clblasSetup() failed with %d\n", err);
clReleaseCommandQueue(queue);
clReleaseContext(ctx);
return 1;
}
/* Prepare OpenCL memory objects and place matrices inside them. */
bufX = clCreateBuffer(ctx, CL_MEM_READ_WRITE, (lenX*sizeof(cl_float)), NULL, &err);
bufY = clCreateBuffer(ctx, CL_MEM_READ_WRITE, (lenY*sizeof(cl_float)), NULL, &err);
err = clEnqueueWriteBuffer(queue, bufX, CL_TRUE, 0, (lenX*sizeof(cl_float)), X, 0, NULL, NULL);
err = clEnqueueWriteBuffer(queue, bufY, CL_TRUE, 0, (lenY*sizeof(cl_float)), Y, 0, NULL, NULL);
printResult();
/* Call clblas function. */
err = clblasSrot(N, bufX, 0, incx, bufY, 0, incy, C, S, 1, &queue, 0, NULL, &event);
// printf("here\n");
if (err != CL_SUCCESS) {
printf("clblasSrot() failed with %d\n", err);
ret = 1;
}
else {
/* Wait for calculations to be finished. */
err = clWaitForEvents(1, &event);
/* Fetch results of calculations from GPU memory. */
err = clEnqueueReadBuffer(queue, bufY, CL_TRUE, 0, (lenY*sizeof(cl_float)),
Y, 0, NULL, NULL);
err = clEnqueueReadBuffer(queue, bufX, CL_TRUE, 0, (lenX*sizeof(cl_float)),
X, 0, NULL, NULL);
/* At this point you will get the result of SROT placed in vector Y. */
printResult();
}
/* Release OpenCL events. */
clReleaseEvent(event);
/* Release OpenCL memory objects. */
clReleaseMemObject(bufY);
clReleaseMemObject(bufX);
/* Finalize work with clblas. */
clblasTeardown();
/* Release OpenCL working objects. */
clReleaseCommandQueue(queue);
clReleaseContext(ctx);
return ret;
}
开发者ID:AndreasMiller,项目名称:clBLAS,代码行数:100,代码来源:example_srot.c
示例10: main
int main(int argc, char** argv)
{
int err; // error code returned from api calls
float data[DATA_SIZE]; // original data set given to device
float results[DATA_SIZE]; // results returned from device
unsigned int correct; // number of correct results returned
size_t global; // global domain size for our calculation
size_t local; // local domain size for our calculation
cl_device_id device_id; // compute device id
cl_context context; // compute context
cl_command_queue commands; // compute command queue
cl_program program; // compute program
cl_kernel kernel; // compute kernel
cl_mem input; // device memory used for the input array
cl_mem output; // device memory used for the output array
// Fill our data set with random float values
//
int i = 0;
unsigned int count = DATA_SIZE;
for(i = 0; i < count; i++)
data[i] = rand() / (float)RAND_MAX;
// Connect to a compute device
//
int gpu = 1;
err = clGetDeviceIDs(NULL, gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU, 1, &device_id, NULL);
if (err != CL_SUCCESS)
{
printf("Error: Failed to create a device group!\n");
return EXIT_FAILURE;
}
// Create a compute context
//
context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
if (!context)
{
printf("Error: Failed to create a compute context!\n");
return EXIT_FAILURE;
}
// Create a command commands
//
commands = clCreateCommandQueue(context, device_id, 0, &err);
if (!commands)
{
printf("Error: Failed to create a command commands!\n");
return EXIT_FAILURE;
}
// Create the compute program from the source buffer
//
program = clCreateProgramWithSource(context, 1, (const char **) & KernelSource, NULL, &err);
if (!program)
{
printf("Error: Failed to create compute program!\n");
return EXIT_FAILURE;
}
// Build the program executable
//
err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if (err != CL_SUCCESS)
{
size_t len;
char buffer[2048];
printf("Error: Failed to build program executable!\n");
clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
printf("%s\n", buffer);
exit(1);
}
// Create the compute kernel in the program we wish to run
//
kernel = clCreateKernel(program, "square", &err);
if (!kernel || err != CL_SUCCESS)
{
printf("Error: Failed to create compute kernel!\n");
exit(1);
}
// Create the input and output arrays in device memory for our calculation
//
input = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(float) * count, NULL, NULL);
output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(float) * count, NULL, NULL);
if (!input || !output)
{
printf("Error: Failed to allocate device memory!\n");
exit(1);
}
// Write our data set into the input array in device memory
//
err = clEnqueueWriteBuffer(commands, input, CL_TRUE, 0, sizeof(float) * count, data, 0, NULL, NULL);
//.........这里部分代码省略.........
开发者ID:BisariaLove,项目名称:HelloOpenCL,代码行数:101,代码来源:hello_world.c
示例11: main
int main() {
// Set the image rotation (in degrees)
float theta = 3.14159/6;
float cos_theta = cosf(theta);
float sin_theta = sinf(theta);
printf("theta = %f (cos theta = %f, sin theta = %f)\n", theta, cos_theta,
sin_theta);
// Rows and columns in the input image
int imageHeight;
int imageWidth;
const char* inputFile = "input.bmp";
const char* outputFile = "output.bmp";
// Homegrown function to read a BMP from file
float* inputImage = readImage(inputFile, &imageWidth,
&imageHeight);
// Size of the input and output images on the host
int dataSize = imageHeight*imageWidth*sizeof(float);
// Output image on the host
float* outputImage = NULL;
outputImage = (float*)malloc(dataSize);
// Set up the OpenCL environment
cl_int status;
// Discovery platform
cl_platform_id platforms[2];
cl_platform_id platform;
status = clGetPlatformIDs(2, platforms, NULL);
chk(status, "clGetPlatformIDs");
platform = platforms[PLATFORM_TO_USE];
// Discover device
cl_device_id device;
clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 1, &device, NULL);
chk(status, "clGetDeviceIDs");
// Create context
cl_context_properties props[3] = {CL_CONTEXT_PLATFORM,
(cl_context_properties)(platform), 0};
cl_context context;
context = clCreateContext(props, 1, &device, NULL, NULL, &status);
chk(status, "clCreateContext");
// Create command queue
cl_command_queue queue;
queue = clCreateCommandQueue(context, device, 0, &status);
chk(status, "clCreateCommandQueue");
// Create the input and output buffers
cl_mem d_input;
d_input = clCreateBuffer(context, CL_MEM_READ_ONLY, dataSize, NULL,
&status);
chk(status, "clCreateBuffer");
cl_mem d_output;
d_output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, dataSize, NULL,
&status);
chk(status, "clCreateBuffer");
// Copy the input image to the device
status = clEnqueueWriteBuffer(queue, d_input, CL_TRUE, 0, dataSize,
inputImage, 0, NULL, NULL);
chk(status, "clEnqueueWriteBuffer");
const char* source = readSource("rotation.cl");
// Create a program object with source and build it
cl_program program;
program = clCreateProgramWithSource(context, 1, &source, NULL, NULL);
chk(status, "clCreateProgramWithSource");
status = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
chk(status, "clBuildProgram");
// Create the kernel object
cl_kernel kernel;
kernel = clCreateKernel(program, "img_rotate", &status);
chk(status, "clCreateKernel");
// Set the kernel arguments
status = clSetKernelArg(kernel, 0, sizeof(cl_mem), &d_output);
status |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &d_input);
status |= clSetKernelArg(kernel, 2, sizeof(int), &imageWidth);
status |= clSetKernelArg(kernel, 3, sizeof(int), &imageHeight);
status |= clSetKernelArg(kernel, 4, sizeof(float), &sin_theta);
status |= clSetKernelArg(kernel, 5, sizeof(float), &cos_theta);
chk(status, "clSetKernelArg");
// Set the work item dimensions
size_t globalSize[2] = {imageWidth, imageHeight};
status = clEnqueueNDRangeKernel(queue, kernel, 2, NULL, globalSize, NULL, 0,
NULL, NULL);
chk(status, "clEnqueueNDRange");
// Read the image back to the host
//.........这里部分代码省略.........
开发者ID:Soledad89,项目名称:learnOpenCL,代码行数:101,代码来源:rotation.c
示例12: CreateContext
cl_context CreateContext()
{
cl_int errNum;
cl_uint numPlatforms;
cl_platform_id * platformIDs;
cl_uint numDevices;
cl_device_id * deviceIDs;
cl_context context = NULL;
// First, select an OpenCL platform to run on.
errNum = clGetPlatformIDs(0, NULL, &numPlatforms);
platformIDs = (cl_platform_id *)alloca(
sizeof(cl_platform_id) * numPlatforms);
errNum = clGetPlatformIDs(numPlatforms, platformIDs, NULL);
if (errNum != CL_SUCCESS || numPlatforms <= 0)
{
std::cerr << "Failed to find any OpenCL platforms." << std::endl;
return NULL;
}
else std::cout<<"number of platforms:"<<numPlatforms<<std::endl;
deviceIDs = NULL;
errNum = clGetDeviceIDs(
platformIDs[0],
CL_DEVICE_TYPE_GPU, //寻找GPU device 的个数
0,
NULL,
&numDevices);
if (errNum != CL_SUCCESS && errNum != CL_DEVICE_NOT_FOUND)
{
checkErr(errNum, "clGetDeviceIDs");
}
else std::cout<<"number of devices:"<<numDevices<<std::endl;
deviceIDs = (cl_device_id *)alloca(sizeof(cl_device_id) * numDevices);
errNum = clGetDeviceIDs(
platformIDs[0],
CL_DEVICE_TYPE_GPU,
numDevices,
&deviceIDs[0],
NULL);
checkErr(errNum, "clGetDeviceIDs");
// Next, create an OpenCL context on the platform. Attempt to
// create a GPU-based context, and if that fails, try to create
// a CPU-based context.
cl_context_properties contextProperties[] =
{
CL_CONTEXT_PLATFORM,
(cl_context_properties)platformIDs[0],
0
};
/*context = clCreateContext(
contextProperties,
numDevices,
deviceIDs,
NULL,
NULL,
&errNum);
checkErr(errNum, "clCreateContext");*/
//如果有平台和设备类型,可以使用clCreateContextFromType()创建上下文。上下文较随意,可以包含不同设备类型
context = clCreateContextFromType(contextProperties, CL_DEVICE_TYPE_GPU,
NULL, NULL, &errNum);
if (errNum != CL_SUCCESS)
{
std::cout << "Could not create GPU context, trying CPU..." << std::endl;
context = clCreateContextFromType(contextProperties, CL_DEVICE_TYPE_CPU,
NULL, NULL, &errNum);
if (errNum != CL_SUCCESS)
{
std::cerr << "Failed to create an OpenCL GPU or CPU context." << std::endl;
return NULL;
}
}
return context;
}
开发者ID:sunrong1990,项目名称:cplusplus_lerning,代码行数:75,代码来源:OpenCL+之+矩阵相加-标准版.cpp
示例13: OpenCLDevice
OpenCLDevice(DeviceInfo& info, Stats &stats, bool background_)
: Device(stats)
{
background = background_;
cpPlatform = NULL;
cxContext = NULL;
cqCommandQueue = NULL;
cpProgram = NULL;
ckPathTraceKernel = NULL;
ckFilmConvertKernel = NULL;
null_mem = 0;
device_initialized = false;
/* setup platform */
cl_uint num_platforms;
ciErr = clGetPlatformIDs(0, NULL, &num_platforms);
if(opencl_error(ciErr))
return;
if(num_platforms == 0) {
opencl_error("OpenCL: no platforms found.");
return;
}
ciErr = clGetPlatformIDs(1, &cpPlatform, NULL);
if(opencl_error(ciErr))
return;
char name[256];
clGetPlatformInfo(cpPlatform, CL_PLATFORM_NAME, sizeof(name), &name, NULL);
platform_name = name;
/* get devices */
vector<cl_device_id> device_ids;
cl_uint num_devices;
if(opencl_error(clGetDeviceIDs(cpPlatform, opencl_device_type(), 0, NULL, &num_devices)))
return;
if(info.num > num_devices) {
if(num_devices == 0)
opencl_error("OpenCL: no devices found.");
else
opencl_error("OpenCL: specified device not found.");
return;
}
device_ids.resize(num_devices);
if(opencl_error(clGetDeviceIDs(cpPlatform, opencl_device_type(), num_devices, &device_ids[0], NULL)))
return;
cdDevice = device_ids[info.num];
/* create context */
cxContext = clCreateContext(0, 1, &cdDevice, NULL, NULL, &ciErr);
if(opencl_error(ciErr))
return;
cqCommandQueue = clCreateCommandQueue(cxContext, cdDevice, 0, &ciErr);
if(opencl_error(ciErr))
return;
null_mem = (device_ptr)clCreateBuffer(cxContext, CL_MEM_READ_ONLY, 1, NULL, &ciErr);
device_initialized = true;
}
开发者ID:baysmith,项目名称:blender,代码行数:67,代码来源:device_opencl.cpp
示例14: main
int main(int argc, const char** argv) {
// start logs
printf("clDeviceQuery Starting...\n\n");
bool bPassed = true;
std::string sProfileString = "clDeviceQuery, Platform Name = ";
// Get OpenCL platform ID for NVIDIA if avaiable, otherwise default
char cBuffer[1024];
cl_platform_id clSelectedPlatformID = NULL;
cl_platform_id* clPlatformIDs;
cl_uint num_platforms;
cl_int ciErrNum = clGetPlatformIDs(0, NULL, &num_platforms);
if (ciErrNum != CL_SUCCESS) {
printf(" Error %i in clGetPlatformIDs Call!\n\n", ciErrNum);
bPassed = false;
} else {
if (num_platforms == 0) {
printf("No OpenCL platform found!\n\n");
bPassed = false;
} else {
// if there's one platform or more, make space for ID's
if ((clPlatformIDs = (cl_platform_id*)malloc(num_platforms * sizeof(cl_platform_id))) == NULL) {
printf("Failed to allocate memory for cl_platform ID's!\n\n");
bPassed = false;
}
printf("%d OpenCL Platforms found\n\n", num_platforms);
// get platform info for each platform
ciErrNum = clGetPlatformIDs (num_platforms, clPlatformIDs, NULL);
for(cl_uint i = 0; i < num_platforms; ++i) {
ciErrNum = clGetPlatformInfo (clPlatformIDs[i], CL_PLATFORM_NAME, 1024, &cBuffer, NULL);
if(ciErrNum == CL_SUCCESS) {
clSelectedPlatformID = clPlatformIDs[i];
// Get OpenCL platform name and version
ciErrNum = clGetPlatformInfo (clSelectedPlatformID, CL_PLATFORM_NAME, sizeof(cBuffer), cBuffer, NULL);
if (ciErrNum == CL_SUCCESS) {
printf(" CL_PLATFORM_NAME: \t%s\n", cBuffer);
sProfileString += cBuffer;
} else {
printf(" Error %i in clGetPlatformInfo Call !!!\n\n", ciErrNum);
bPassed = false;
}
sProfileString += ", Platform Version = ";
ciErrNum = clGetPlatformInfo (clSelectedPlatformID, CL_PLATFORM_VERSION, sizeof(cBuffer), cBuffer, NULL);
if (ciErrNum == CL_SUCCESS) {
printf(" CL_PLATFORM_VERSION: \t%s\n", cBuffer);
sProfileString += cBuffer;
} else {
printf(" Error %i in clGetPlatformInfo Call !!!\n\n", ciErrNum);
bPassed = false;
}
// Log OpenCL SDK Version # (for convenience: not specific to OpenCL)
sProfileString += ", NumDevs = ";
// Get and log OpenCL device info
cl_uint ciDeviceCount;
cl_device_id *devices;
printf("OpenCL Device Info:\
|
请发表评论