i.MX6Q OpenCL with QT4.8.4

4 篇文章 0 订阅
4 篇文章 0 订阅

开发板:Sabrelite
参考: 飞思卡尔教程i.MX6Q OpenCL Hello World

1、建立qt控制台工程。

2、我的BSP 由英蓓特公司提供的版本,使用自带的文件系统,include 目录位于我的电脑的。。。
     但是我好像并没有找到CL的头文件。
     我复制电脑中Nvida GPU Computing Kit 中的CL文件夹到Ubuntu中来,
     将CL文件夹放入 /home/wps/programs/timesys/i_MX6QSABRELite/rfs/rootfs/usr/include/中,
     OpenCL的库libOpenCL.so 和 libCLC.so 位于/home/wps/programs/timesys/i_MX6QSABRELite/rfs/rootfs/usr/lib/中。

或者在这里下载:i.MX6Q opencl include and lib


3、 将OpenCL的头文件和库文件加入qt工程中的pro文件中。
如:
INCLUDEPATH += //home/wps/programs/timesys/i_MX6QSABRELite/rfs/rootfs/usr/include
LIBS += -L/home/wps/programs/timesys/i_MX6QSABRELite/rfs/rootfs/usr/lib -lOpenCL -lCLC

将源代码复制替换掉main.cpp。

//************************************************************

// Demo OpenCL application to compute a simple vector addition

// computation between 2 arrays on the GPU

// ************************************************************

#include <stdio.h>

#include <stdlib.h>

#include <CL/cl.h>

//

// OpenCL source code

const char* OpenCLSource[] = {

"__kernel void VectorAdd(__global int* c, __global int* a,__global int* b)",

"{",

" // Index of the elements to add \n",

" unsigned int n = get_global_id(0);",

" // Sum the nth element of vectors a and b and store in c \n",

" c[n] = a[n] + b[n];",

"}"


// Some interesting data for the vectors

int InitialData1[20] = {37,50,54,50,56,0,43,43,74,71,32,36,16,43,56,100,50,25,15,17};

int InitialData2[20] = {35,51,54,58,55,32,36,69,27,39,35,40,16,44,55,14,58,75,18,15};

// Number of elements in the vectors to be added

#define SIZE 100

// Main function

// ************************************************************

int main(int argc, char **argv)

{

     // Two integer source vectors in Host memory

     int HostVector1[SIZE], HostVector2[SIZE];

     //Output Vector

     int HostOutputVector[SIZE];

     // Initialize with some interesting repeating data

     for(int c = 0; c < SIZE; c++)

     {

          HostVector1[c] = InitialData1[c%20];

          HostVector2[c] = InitialData2[c%20];

          HostOutputVector[c] = 0;

     }

     //Get an OpenCL platform

     cl_platform_id cpPlatform;

     clGetPlatformIDs(1, &cpPlatform, NULL);

     // Get a GPU device

     cl_device_id cdDevice;

     clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU, 1, &cdDevice, NULL);

     char cBuffer[1024];

     clGetDeviceInfo(cdDevice, CL_DEVICE_NAME, sizeof(cBuffer), &cBuffer, NULL);

     printf("CL_DEVICE_NAME: %s\n", cBuffer);

     clGetDeviceInfo(cdDevice, CL_DRIVER_VERSION, sizeof(cBuffer), &cBuffer, NULL);

     printf("CL_DRIVER_VERSION: %s\n\n", cBuffer);

     // Create a context to run OpenCL enabled GPU

     cl_context GPUContext = clCreateContextFromType(0, CL_DEVICE_TYPE_GPU, NULL, NULL, NULL);

     // Create a command-queue on the GPU device

     cl_command_queue cqCommandQueue = clCreateCommandQueue(GPUContext, cdDevice, 0, NULL);

     // Allocate GPU memory for source vectors AND initialize from CPU memory

     cl_mem GPUVector1 = clCreateBuffer(GPUContext, CL_MEM_READ_ONLY |

     CL_MEM_COPY_HOST_PTR, sizeof(int) * SIZE, HostVector1, NULL);

     cl_mem GPUVector2 = clCreateBuffer(GPUContext, CL_MEM_READ_ONLY |

     CL_MEM_COPY_HOST_PTR, sizeof(int) * SIZE, HostVector2, NULL);

     // Allocate output memory on GPU

     cl_mem GPUOutputVector = clCreateBuffer(GPUContext, CL_MEM_WRITE_ONLY,

     sizeof(int) * SIZE, NULL, NULL);

     // Create OpenCL program with source code

     cl_program OpenCLProgram = clCreateProgramWithSource(GPUContext, 7, OpenCLSource, NULL, NULL);

     // Build the program (OpenCL JIT compilation)

     clBuildProgram(OpenCLProgram, 0, NULL, NULL, NULL, NULL);

     // Create a handle to the compiled OpenCL function (Kernel)

     cl_kernel OpenCLVectorAdd = clCreateKernel(OpenCLProgram, "VectorAdd", NULL);

     // In the next step we associate the GPU memory with the Kernel arguments

     clSetKernelArg(OpenCLVectorAdd, 0, sizeof(cl_mem), (void*)&GPUOutputVector);

     clSetKernelArg(OpenCLVectorAdd, 1, sizeof(cl_mem), (void*)&GPUVector1);

     clSetKernelArg(OpenCLVectorAdd, 2, sizeof(cl_mem), (void*)&GPUVector2);

     // Launch the Kernel on the GPU

     // This kernel only uses global data

     size_t WorkSize[1] = {SIZE}; // one dimensional Range

     clEnqueueNDRangeKernel(cqCommandQueue, OpenCLVectorAdd, 1, NULL,

     WorkSize, NULL, 0, NULL, NULL);

     // Copy the output in GPU memory back to CPU memory

     clEnqueueReadBuffer(cqCommandQueue, GPUOutputVector, CL_TRUE, 0,

     SIZE * sizeof(int), HostOutputVector, 0, NULL, NULL);

     // Cleanup

     clReleaseKernel(OpenCLVectorAdd);

     clReleaseProgram(OpenCLProgram);

     clReleaseCommandQueue(cqCommandQueue);

     clReleaseContext(GPUContext);

     clReleaseMemObject(GPUVector1);

     clReleaseMemObject(GPUVector2);

     clReleaseMemObject(GPUOutputVector);

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

          printf("[%d + %d = %d]\n",HostVector1[i], HostVector2[i], HostOutputVector[i]);

     return 0;

}


结果如下,截取部分结果。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值