本文介紹一下c語言使用opencl的基本步驟,系統是windows10,ide是visual studio2017。
step1:你需要安裝一下opencl的windows版本,然後新建乙個工程,在屬性管理器裡面給這個工程匯入opencl的標頭檔案。
step2:匯入標頭檔案
#include #include #include #include #include //我這裡的核函式操作的是影象,所以需要opencv的包
#include #include #include #include #include using namespace cv;
using namespace std;
#pragma warning( disable : 4996 )
step3:獲取平台和上下文
cl_context createcontext()
//建立乙個opencl上下文環境
cl_context_properties contextproperties =
; context = clcreatecontextfromtype(contextproperties, cl_device_type_gpu,
null, null, &errnum);
return context;
}
step4:建立裝置並建立命令佇列
cl_command_queue createcommandqueue(cl_context context, cl_device_id *device)
// 為裝置分配快取空間
devices = new cl_device_id[devicebuffersize / sizeof(cl_device_id)];
errnum = clgetcontextinfo(context, cl_context_devices, devicebuffersize, devices, null);
//char name_data[100];
//clgetdeviceinfo(devices[0], cl_device_name,sizeof(name_data), name_data, null);
//printf("device:%s\n", name_data);
//選取可用裝置中的第乙個
commandqueue = clcreatecommandqueue(context, devices[0], 0, null);
*device = devices[0];
//delete devices;
return commandqueue;
}
step5:構建並且編譯program
cl_program createprogram(cl_context context, cl_device_id device, const char* filename)
std::ostringstream oss;
oss << kernelfile.rdbuf();
std::string srcstdstr = oss.str();
const char *srcstr = srcstdstr.c_str();
program = clcreateprogramwithsource(context, 1,
(const char**)&srcstr,
null, null);
errnum = clbuildprogram(program, 0, null, null, null, null);
return program;
}
step6:建立kernel
// 一、選擇opencl平台並建立乙個上下文
cl_context context = createcontext();
// 二、 建立裝置並建立命令佇列
cl_command_queue commandqueue = createcommandqueue(context, &device);
cl_event histevent = 0;
//建立和構建程式物件
cl_program program = createprogram(context, device, "main.cl");
cl_kernel kernel = clcreatekernel(program, "kernel_rgb2gray", null);
step7:新建檔案main.cl,在裡面新建乙個核函式
__kernel void kernel_rgb2gray(__global unsigned char * rgbimage,
__global int* result)
step8:為核函式設定引數
mat image = imread("d:");
int imgsize = image.rows * image.cols;
mat srcimage;
cvtcolor(image, srcimage, cv_bgr2gray);
cl_mem memresult = clcreatebuffer(context, cl_mem_write_only,
sizeof(int)*imgsize, null, null);
cl_mem memrgbimage = clcreatebuffer(context, cl_mem_use_host_ptr,
sizeof(uchar) * 3 * imgsize, srcimage.data, null);
errnum = clsetkernelarg(kernel, 0, sizeof(cl_mem), &memrgbimage);
errnum = clsetkernelarg(kernel, 1, sizeof(cl_mem), &memresult);
step9:執行核函式
int img_h = img.rows;
int img_w = img.cols;
size_t globalthreads[2] = ;
//執行核心函式
errnum = clenqueuendrangekernel(commandqueue, kernel, 2, 0, globalthreads, null,
0, null, &histevent);
step10:獲取執行結果
int result* = new int[imgsize];
errnum = clenqueuereadbuffer(commandqueue, memresult, cl_true, 0,
sizeof(float)*img_h*img_w, result, 1, &histevent, null);
素數的幾道小題(c語言版本)
includeint main if i n printf this is not a prime.else printf this is a prime.return 0 include includeint main if i k printf this is not a prime.else ...
C語言版本 單鏈表的實現
slist.h 1 ifndef slist h 2 define slist h 34 include5 include 6 include7 typedef int elemtype 8 typedef struct node node,pnode 12 typedef struct list ...
C語言版本 雙鏈表的實現
dlist.h 1 ifndef dlist h 2 define dlist h 34 include5 include 6 include7 typedef int elemtype 8 typedef struct node node,pnode 13 typedef struct list ...