cuda使用thrust的api求一个向量的最值

懒得自己写了,就使用了thrust的api算了。还挺不错。

cuda程序
#include <thrust/extrema.h>
#include <thrust/device_ptr.h>
#include <iostream>


int main(){

  float* deviceArray;
  float max, test;
  int length = 1025;

  max = 0.0f;
  test = 2.5f;
  int size = (int) length*sizeof(float);

  cudaMalloc(&deviceArray, size);
  cudaMemset(deviceArray, 0.0f, size);
  cudaMemcpy(deviceArray, &test, sizeof(float),cudaMemcpyHostToDevice);

  thrust::device_ptr<float> d_ptr = thrust::device_pointer_cast(deviceArray);
  max = *(thrust::max_element(d_ptr, d_ptr + length));
  std::cout << max << std::endl;
}
求最值

具体变成代码就变成了下面的代码,但是加入caffe里面的时候会有编译错误。

template<typename Dtype>
Dtype get_device_array_max(Dtype * deviceArray,int length){
	thrust::device_ptr<Dtype> d_ptr = thrust::device_pointer_cast(deviceArray);
	Dtype max_value = *(thrust::max_element(d_ptr, d_ptr + length));
	return max_value;
}
template<typename Dtype>
Dtype get_device_array_min(Dtype * deviceArray,int length){
	thrust::device_ptr<Dtype> d_ptr = thrust::device_pointer_cast(deviceArray);
	Dtype min_value = *(thrust::min_element(d_ptr, d_ptr + length));
	return min_value;
}

可以把头文件

#include <thrust/extrema.h>
#include <thrust/device_ptr.h>

加到math_functions.hpp中问题解决,具体不知道什么原因。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

andeyeluguo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值