c++ CUDA编程《一》 计算矩阵加减乘除

本文介绍了使用C++和CUDA进行矩阵运算的基础步骤。首先通过init_cuda初始化并检测GPU,接着调用calculate函数计算两个向量的差的平方。重点在于calculate_distance_img函数,它创建GPU内存并复制数据,利用自定义的add内核函数在GPU上执行运算,最后通过cudaMemcpy将结果取出。CMakeLists.txt中设置了CUDA库的查找和可执行程序的生成配置。
摘要由CSDN通过智能技术生成
#include "calculator.h"
#include <cuda_runtime.h>
#include "iostream"

bool InitCUDA()
{
	//used to count the device numbers
	int count; 

	// get the cuda device count
	cudaGetDeviceCount(&count);
	// print("%d\n", count);
	std::cout << "count: " <<count << std::endl;
	if (count == 0) 
	{
		return false;
	}

	// find the device >= 1.X
	int i;
	for (i = 0; i < count; ++i) 
	{
		cudaDeviceProp prop;
		if (cudaGetDeviceProperties(&prop, i) == cudaSuccess) 
		{
			if (prop.major >= 1) 
			{
				break;
			}
		}
	}
	// if can't find the device
	if (i == count) 
	{
		std::cout<<"count: "<<count<<" i:"<<i<<std::endl;
		return false;
	}

	// set cuda device
	cudaSetDevice(i);

	return true;
}

__global__ void add(const float *dev_a,const float *dev_b,float *dev_c)
{
    int i=thread
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值