OpenCL(二)从矢量相加理解OpenCL异构编程过程

本文通过一个矢量相加的OpenCL demo,详细解释了OpenCL异构编程的过程,包括Kernel代码、平台与设备的选择、Context与Commandqueue的创建、内存对象、程序对象与Kernel对象的建立以及执行与数据传输。通过实例,帮助读者深入理解OpenCL编程的各个环节。
摘要由CSDN通过智能技术生成

在上一篇博客中介绍了异构编程相关概念以及OpenCL框架,都是比较抽象的概念。本文从矢量相加的demo出发,对相关概念做进一步说明,来更深入、直观地理解OpenCL异构编程的过程。

首先,直接将实现矢量相加(每个矢量128个元素)的完整源码贴出来

#include<stdio.h>
#include<stdlib.h>

#include<CL/cl.h>


const char* programSource =
"__kernel void vecadd(__global int* A, __global int* B, __global int* C)\n"
"{                                                                        \n"
"    int idx=get_global_id(0);                                            \n"
"    C[idx]=A[idx]+B[idx];                                                \n"
"}                                                                        \n"
;

int main()
{
    int *A = NULL;
    int *B = NULL;
    int *C = NULL;
    const  int elements= 128;



    size_t datasize = sizeof(int)*elements;

    A = (int*)malloc(datasize);
    B = (int*)malloc(datasize);
    C = (int*)malloc(datasize);


    for (int i = 0; i < elements; i++)
    { 
        A[i]=i;
        B[i]=i;
    }


    cl_int status;

    /*Discover and initialize the platforms*/

    cl_uint numPlatforms = 0;
    cl_platform_id* platforms = NULL;

    status = clGetPlatformIDs(0, NULL, &numPlatforms);  //retrieve number of platforms

    printf("# of platform:%d\n", numPlatforms);

    platforms = (cl_platform_id*)malloc(numPlatforms*sizeof(cl_platform_id)); // malloct memery for platform 

    status = clGetPlatformIDs(numPlatforms, platforms, NULL); // initialize platforms

    /*print  platform informations*/
    for (int i = 0; i < numPlatforms; i++)
    {
        size_t size=0;

        //name
        status = clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, 0, NULL, &size);
        char* name = (char*)malloc(size);
        status = clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, size, name, NULL);
        printf("CL_PLATFORM_NAME:%s\n", name);

        //vendor
        status = clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, 0, NULL, &size);
        char *vendor = (char *)malloc(size);
        status = clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, size, vendor, NULL);
        printf("CL_PLATFORM_VENDOR:%s\n", vendor);

        //version
        status = clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION, 0, NULL, &size);
        char *version = (char *)malloc(size);
        status = clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION, size, version, NULL);
        printf("CL_PLATFORM_VERSION:%s\n", version);

        // profile
        status = clGetPlatformInfo(platforms[i], CL_PLATFORM_PROFILE, 0, NULL, &size);
        char *profile = (char *)malloc(size);
        status = clGetPlatformInfo(platforms[i], CL_PLATFORM_PROFILE, size, profile, NULL);
        printf(
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值