intel opencl hello程序实例

1 篇文章 0 订阅
1 篇文章 0 订阅

刚刚配好opencl的环境,本来想找网上找一个hello opencl实例来测试一试,结果网上的代码有好几个错误,最后基本等于是自己写的,所以发布一下正确的代码:


此代码支持系统:

linux

支持GPU:intel,AMD,NVIDA


我是在linux上实现的,CPU和GPU都是intel的:

注意opencl头文件的引用,因为cl.h是通过usr/include/CL指向/etc/alternatives/opencl-headers/文件夹的,所以引用的时候一定要用<CL/cl.h>

已表示引用的cl.h在CL文件夹下(其实是在CL指向的文件夹下)

#include <stdio.h>
#include <stdlib.h>
#include <CL/cl.h>


int main()
{
    cl_uint NumPlatforms;
    clGetPlatformIDs(0,NULL,&NumPlatforms);


    cl_platform_id PlatformIDs[NumPlatforms];
    clGetPlatformIDs(NumPlatforms,PlatformIDs,NULL);


    char platformName[64];
    size_t nameLen;
    cl_int res = clGetPlatformInfo(PlatformIDs[0],CL_PLATFORM_NAME,64,platformName,&nameLen);
    if (res != CL_SUCCESS) {
    fprintf(stderr,"Err:1 \n");
    exit(1);
    }
    platformName[nameLen]=0;
   // printf("%s\n",platformName);


    char openclVersion[128];
    res = clGetPlatformInfo(PlatformIDs[0],CL_PLATFORM_VERSION,128,openclVersion,&nameLen);
    if(res!=CL_SUCCESS){
        fprintf(stderr,"Err:2 \n");
        exit(1);
    }
    openclVersion[nameLen] = 0;
    printf("hello,%s's %s\n",platformName,openclVersion);
    return 0;
}

写好.c文件后,输入指令:

gcc hello.c -lOpenCL


然后会得到一个文件

a.out

输入指令:

./a.out


可以得到输出:

hello,Intel(R) OpenCL's OpenCL 1.2 LINUX

截图如下:



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值