我想使用gpu找到一个数组的最小值,然后再使用推力:: min_element我的数据在设备中,这就是为什么我必须使用推力:: device但我遇到“从位置读取时发生访问冲突”的原因0x0000000701240000”,并且正在使用tuple.inl函数:
inline __host__ __device__
cons( T1& t1, T2& t2, T3& t3, T4& t4, T5& t5,
T6& t6, T7& t7, T8& t8, T9& t9, T10& t10 )
但是,如果我使用推力::主机,它可以工作!!!这是我的代码。如果有什么问题,请告诉我。
#include <thrust/extrema.h>
#include <thrust/execution_policy.h>
#include <time.h>
int main()
{
int nx=200;
int ny=200;
float cpt=0;
clock_t start,end;
double time;
float *in,*d_in;
float moy,*d_moy;
in=(float*)malloc(nx*ny*sizeof(float));
moy=0.0f;
cudaMalloc((void**)&d_in,nx*ny*sizeof(float));
cudaMalloc((void**)&d_moy,sizeof(float));
for(int i=0;i<nx*ny;i++){in[i]=i+0.07;cpt+=in[i];}
cudaMemcpy(d_in,in,nx*ny*sizeof(float),cudaMemcpyHostToDevice);
start=clock();
//float result= thrust::reduce(thrust::device, d_in, d_in + nx*ny);
float *result=thrust::min_element(thrust::device, d_in , d_in+ nx*ny);
end=clock();
time=((double)(end-start))/CLOCKS_PER_SEC;
printf("result= %f and correct result is %f time= %lf \n",*result,in[0],time);
system("pause");
}
这是一个错误thrust::min_element
。使用原始指针时,程序崩溃。此错误仅存在于CUDA7.5 Thrust1.8.2或更早版本中。
您可以使用thrust::device_vector
或thrust::device_ptr
代替。这是使用推力的更好方法。
#include <iostream>
#include <thrust/sequence.h>
#include <thrust/extrema.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
int main() {
int n = 1000;
thrust::device_vector<float> in(n);
thrust::sequence(in.begin(), in.end(), 123);
std::cerr << "by iterator:" << std::endl;
thrust::device_vector<float>::iterator it_result =
thrust::min_element(in.begin(), in.end());
std::cerr << *it_result << std::endl;
std::cerr << "by device_ptr:" << std::endl;
thrust::device_ptr<float> ptr_in = in.data();
thrust::device_ptr<float> ptr_result =
thrust::min_element(ptr_in, ptr_in + in.size());
std::cerr << *ptr_result << std::endl;
std::cerr << "by pointer:" << std::endl;
float* raw_in = thrust::raw_pointer_cast(in.data());
std::cerr << "before min_element" << std::endl;
float* result = thrust::min_element(thrust::device, raw_in, raw_in + in.size());
std::cerr << "after min_element" << std::endl;
std::cerr << in[result - raw_in] << std::endl;
return 0;
}
结果表明,thrust::min_element
使用原始指针会导致崩溃。
$ nvcc -o test test.cu && ./test
by iterator:
123
by device_ptr:
123
by pointer:
before min_element
Segmentation fault (core dumped)
另一方面,如果此错误不存在,则您的原始代码仍然有问题。正如@talonmies所说,result
是设备指针。您需要先将其指向的数据从设备复制到主机,然后才能打印出来。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句