高效能計算 OpenCL入門1

2021-09-10 18:54:21 字數 3746 閱讀 5438

獲取平台資料

*/void getplatforminfo() }/*

建立平台

建立裝置

根據裝置建立上下文

*/cl_context createcontext(cl_device_id *device)

if (errnum != cl_success)

if (errnum != cl_success)

clgetdeviceids(firstplatformid, cl_device_type_gpu, 1, device, null);

context = clcreatecontext(null, 1, device, null, null, &errnum);

if (errnum != cl_success)

return context;}/*

在上下文可用的第乙個裝置中建立命令佇列

*/cl_command_queue createcommandqueue(cl_context context, cl_device_id device)

return commandqueue;}/*

讀取核心原始碼,建立opencl程式

*/cl_program createprogram(

cl_context context,

cl_device_id device,

const char *filename

) errnum = clbuildprogram(program, 0, null, null, null, null);

if (errnum != cl_success)

return program;}/*

建立記憶體物件(申請記憶體空間)

*/bool creatememobjects(cl_context context, cl_mem memobjects[3], float *a, float *b)

return true;}/*

清除opencl資源

*/void cleanup(

cl_context context,

cl_command_queue commandqueue,

cl_program program,

cl_kernel kernel,

cl_mem memobjects[3]

) }

if (commandqueue != 0)

if (program != 0)

if (kernel != 0)

if (context != 0)

}int main(int argc,char **ar**) ;

cl_int errnum;

//建立opencl上下文

context = createcontext(&device);

if (context == null)

//獲得opencl裝置,建立命令佇列

commandqueue = createcommandqueue(context, device);

if (commandqueue == null)

//建立opencl程式

program = createprogram(context, device, "vecadd.cl");

if (program == null)

//建立opencl核心

kernel = clcreatekernel(program, "vector_add", &errnum);

if (kernel == null)

//建立opencl記憶體物件

float result[array_size];

float a[array_size];

float b[array_size];

for (int i = 0; i < array_size; i++)

if (!creatememobjects(context, memobjects, a, b))

//設定記憶體引數

errnum = clsetkernelarg(kernel, 0, sizeof(cl_mem),&memobjects[0]);

errnum |= clsetkernelarg(kernel, 1, sizeof(cl_mem), &memobjects[1]);

errnum |= clsetkernelarg(kernel, 2, sizeof(cl_mem), &memobjects[2]);

if (errnum != cl_success)

size_t globalworksize[1] = ;

size_t localworksize[1] = ;

//執行核心

errnum = clenqueuendrangekernel(

commandqueue,kernel,

1,null,

globalworksize,localworksize,

0,null,null

); if (errnum != cl_success)

//計算結果拷貝回主機

errnum = clenqueuereadbuffer(

commandqueue, memobjects[2],

cl_true, 0,

array_size * sizeof(float),

result, 0, null, null

); if (errnum != cl_success)

for (int i = 0; i < array_size; i++)

printf("執行程式完成\n");

cleanup(context, commandqueue, program, kernel, memobjects);

system("pause");

return 0;

}

__kernel void vector_add(

global const float *a,

global const float *b,

global float *result

)

HPC高效能計算

l0 伺服器,儲存,網路等硬體環境 l1 系統環境 windows,l2 集群管理 l3 行業應用 集群管理 執行庫環境 mpiopenmpi mvapic h2 mpi 標準 message passing inte ce 訊息傳遞介面 mpi是程序級別的,這些函式庫裡面主要涉及的是兩個程序之間通...

高效能計算複習

高效能計算 high performance computing,縮寫hpc 指通 常使用很多處理器 作為單個機器的一部分 或者某一集群 中組織的幾台計算機 作為單個計算資源操作 的計算系統 和環境floatskm gtp 36912 1518千百萬 十億萬億 千萬億百億億 hello world的...

高效能計算 HPCC 評述

在 看到如下內容 關鍵句 soon after releasing all phases,we will release the source code.the binaries below are provided under this license agreement.評述 1 hpcc到目前...