当从位置读取时,推力:: min_element访问冲突

萨拉赫·埃丁(Salah Eddine)

我想使用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");
}
kangshiyin

这是一个错误thrust::min_element使用原始指针时,程序崩溃。此错误仅存在于CUDA7.5 Thrust1.8.2或更早版本中。

您可以使用thrust::device_vectorthrust::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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在gpu上的数组上进行推力:: min_element

来自分类Dev

如何在设备和主机之间使用无推力的推力min_element算法

来自分类Dev

访问冲突读取位置fprintf

来自分类Dev

例外:“访问冲突读取位置”

来自分类Dev

访问冲突读取位置fprintf

来自分类Dev

读取6位数字时读取位置错误时发生访问冲突

来自分类Dev

读取内存时访问冲突

来自分类Dev

读取内存时访问冲突

来自分类Dev

CreateBuffer抛出“访问冲突读取位置”

来自分类Dev

访问冲突读取位置0xFFFFFFFFFFFFFFFFFF

来自分类Dev

c ++向量插入访问冲突读取位置

来自分类Dev

访问冲突读取位置0xCDCDCDCD

来自分类Dev

多线程访问冲突读取位置C ++

来自分类Dev

使类可迭代:访问冲突读取位置

来自分类Dev

C ++ CImg访问冲突读取位置

来自分类Dev

使用Cudd的访问冲突读取位置

来自分类Dev

#define导致“访问冲突读取位置”

来自分类Dev

未处理的异常:访问冲突读取位置

来自分类Dev

CreateBuffer抛出“访问冲突读取位置”

来自分类Dev

访问冲突读取位置0xCDCDCDCD

来自分类Dev

多线程访问冲突读取位置C ++

来自分类Dev

C++访问冲突读取位置错误

来自分类Dev

vc++访问冲突读取位置

来自分类Dev

读取/写入向量时访问冲突写入位置 0x00000000

来自分类Dev

AVL树插入-访问冲突读取位置(无法读取内存)

来自分类Dev

读取TMemoryStream时发生访问冲突错误

来自分类Dev

从文件读取时发生C ++访问冲突

来自分类Dev

使用对象指针时的访问冲突读取

来自分类Dev

c-读取数组时访问冲突