#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
c++ CUDA编程《一》 计算矩阵加减乘除
最新推荐文章于 2024-08-15 19:57:38 发布
本文介绍了使用C++和CUDA进行矩阵运算的基础步骤。首先通过init_cuda初始化并检测GPU,接着调用calculate函数计算两个向量的差的平方。重点在于calculate_distance_img函数,它创建GPU内存并复制数据,利用自定义的add内核函数在GPU上执行运算,最后通过cudaMemcpy将结果取出。CMakeLists.txt中设置了CUDA库的查找和可执行程序的生成配置。
摘要由CSDN通过智能技术生成