OpenCL例程2-数组相加

本文介绍了使用OpenCL进行数组相加的操作,通过cpp文件与ArrayAdd.cl内核代码实现,展示了并行计算的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cpp文件

// OpenCl-1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <CL/cl.h>
#include <iostream>
#include <fstream>
using namespace std;

#define ARRAY_SIZE  128

cl_int ConvertToString(const char *pFileName, std::string &str);


int _tmain(int argc, _TCHAR* argv[])
{
	cl_int			iStatus			= 0;		// 函数返回状态
	cl_uint			uiNumPlatforms	= 0;		// 平台个数
	cl_platform_id	Platform		= NULL;		// 选择的平台
	size_t			uiSize			= 0;		// 平台版本名字字节数	
	cl_int			iErr			= 0;		// 返回参数
	char			*pName			= NULL;		// 平台版本名
	cl_uint			uiNumDevices	= 0;		// 设备数量
	cl_device_id	*pDevices		= NULL;		// 设备
	cl_context		Context			= NULL;		// 设备环境
	cl_command_queue CommandQueue	= NULL;		// 命令队列
	const char		*pFileName		= "ArrayAdd.cl";	// cl文件名
	string			strSource		= "";		// 用于存储cl文件中的代码
	const char		*pSource		= NULL;		// 代码字符串指针
	size_t			uiArrSourceSize[]= {0};		// 代码字符串长度
	cl_program		Program			= NULL;		// 程序对象
	int				arrInA[ARRAY_SIZE];			// 输入数组A
	int				arrInB[ARRAY_SIZE];			// 输入数组B
	int				arrOut[ARRAY_SIZE];			// 输出数组
	cl_mem			memInutBufferA	= NULL;		// 输入内存对象A
	cl_mem			memInutBufferB	= NULL;		// 输入内存对象B
	cl_mem			memOutputBuffer	= NULL;		// 输出内存对象
	cl_kernel		Kernel			= NULL;		// 内核对象
	size_t			uiGlobal_Work_Size[1]= {0};	// 用于设定内核分布	


	//-------------------1. 获得并选择可用平台-----------------------------
	// 获得平台数量
	iStatus = clGetPlatformIDs(0, NULL, &uiNumPlatforms); // 查询可用的平台个数,并返回状态
	if (CL_SUCCESS != iStatus)
	{
		cout << "Error: Getting platforms error" << endl;
		return 0;
	}
	

	// 获得平台地址
	if (uiNumPlatforms > 0)  // 如果有可用平台
	{
		 // 根据平台数为平台分配内存空间
		cl_platform_id *pPlatforms = (cl_platform_id *)malloc(uiNumPlatforms * sizeof(cl_platfo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值