在上一篇博客中介绍了异构编程相关概念以及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(