OpenCL项目模板

文件树

在这里插入图片描述

CMakeLists.txt固定部分写法

CMAKE_MINIMUM_REQUIRED(VERSION 3.10)
PROJECT(DEMO4-15-1)   #项目名
INCLUDE_DIRECTORIES(/usr/include/)   #OpenCL库.h文件位置
LINK_DIRECTORIES(/usr/local/lib /usr/lib/aarch64-linux-gnu/) #存放OpenCL的动态库.so文件夹位置
ADD_EXECUTABLE(demo4-15-1 src/main.c) #运行
TARGET_LINK_LIBRARIES(demo4-15-1 -lOpenCL)  #链接OpenCL动态库

c语言文件写法

#pragma warning( disable : 4996 ) //如果设备只支持OpenCL1.2需要加上这一行,若支持2.0无需添加
#include <stdio.h>
#include <CL/cl.h>
#include <stdlib.h>
#include <string.h>
#define FILE_PATH "../src/kernel.cl"  //kernel文件相对build文件夹内可执行文件的相对位置
#define RAN_X 6
#define RAN_Y 4


char *readFile(){
    FILE *file = fopen(FILE_PATH, "r");
    if(file == NULL){
        printf("Failed to open file.\n");
        return NULL;
    }
    fseek(file, 0, SEEK_END);
    size_t length = ftell(file);
    fseek(file, 0, SEEK_SET);
    char *source;
    if(length == 0) {
        printf("The file is empty.\n");
        return NULL;
    }
    source = (char *)malloc(length+1);
    size_t ret = fread(source, length, 1, file);
    if(ret == 0){
        printf("Failed to read file.\n");
        free(source);
        return NULL;
    }
    fclose(file);
    source[length] = '\0';
    return source;
}

void cleanUp(cl_context context, cl_command_queue queue, 
            cl_program program, cl_kernel kernel, cl_mem mem){
    if(mem != 0){
        clReleaseMemObject(mem);
    }
    if(queue != 0){
        clReleaseCommandQueue(queue);
    }
    if(kernel != 0){
        clReleaseKernel(kernel);
    }
    if(program != 0){
        clReleaseProgram(program);
    }
    if(context != 0){
        clReleaseContext(context);
    }
}

int main(){
    cl_int status;
    cl_platform_id platform;
    cl_device_id   device;
    cl_context context = NULL;
    cl_command_queue queue = NULL;
    //cl_queue_properties props[] = {CL_QUEUE_PROPERTIES,
    //                              CL_QUEUE_ON_DEVICE_DEFAULT,0};
    cl_program program = NULL;
    cl_kernel kernel = NULL;

    status = clGetPlatformIDs(1, &platform, NULL);
    status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
    context = clCreateContext(NULL, 1, &device, NULL, NULL, &status);
    
    queue = clCreateCommandQueue(context, device, 0, &status);
    //queue = clCreateCommandQueueWithProperties(context, device, props, &status);
    if(status != CL_SUCCESS){
        printf("Failed to create context, status = %d.\n", status);
        cleanUp(context, queue, NULL, NULL, NULL);
        return 0;
    }
    char *source = readFile();
    if(source == NULL) return 0;
    size_t source_length = strlen(source);
    program = clCreateProgramWithSource(context, 1, (const char **)&source, &source_length, &status);
    if(program == NULL){
        printf("Failed to read create program");
        cleanUp(context, queue, program, NULL, NULL);
        free(source);
        return 0;
    }
    status = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
    if(status != CL_SUCCESS){
        char buildlog[16384];
        clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(buildlog), buildlog, NULL);
        printf("Error in kernel:\n%s", buildlog);
        cleanUp(context, queue, program, NULL, NULL);
        free(source);
        return 0;
    }
    

/*
@custom-made code
*/
   
   /*********/


 /*
 @end of custom-made code
 */   
    free(source);
    return 0;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值