Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在cuda中实现简化的argwhere_C++_Cuda - Fatal编程技术网

C++ 如何在cuda中实现简化的argwhere

C++ 如何在cuda中实现简化的argwhere,c++,cuda,C++,Cuda,在cuda中实现简化的argwhere的最佳方法是什么。基本上,我想写一个内核,它接收两个大小相同的图像,并返回一个图像位置数组,两个图像上的值相等。比较是微不足道的,但是,我一直在尝试生成结果数组。我不知道如何跨所有线程同步附加到数组。一种可能的方法是使用缩减/流压缩方法,在这种情况下,我认为这是一个明显的选择 另一种可能的方法是使用基于原子的方法,在这种方法中,找到匹配的每个线程都以原子方式请求全局数组中的一个插槽,以存储匹配索引。如果匹配密度较低,这种方法可能会比并行缩减快 这是两种方法的

在cuda中实现简化的
argwhere
的最佳方法是什么。基本上,我想写一个内核,它接收两个大小相同的图像,并返回一个图像位置数组,两个图像上的值相等。比较是微不足道的,但是,我一直在尝试生成结果数组。我不知道如何跨所有线程同步附加到数组。

一种可能的方法是使用缩减/流压缩方法,在这种情况下,我认为这是一个明显的选择

另一种可能的方法是使用基于原子的方法,在这种方法中,找到匹配的每个线程都以原子方式请求全局数组中的一个插槽,以存储匹配索引。如果匹配密度较低,这种方法可能会比并行缩减快

这是两种方法的一个有效示例:

$ cat t909.cu
#include <thrust/device_vector.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/copy.h>

#include <iostream>

#define MAX_SIZE 1048576
#define nTPB 256

__device__ int match_indices[MAX_SIZE];
__device__ int next_idx = 0;

__device__ void add_idx(int idx){

  int my_idx = atomicAdd(&next_idx, 1);
  if (my_idx < MAX_SIZE) match_indices[my_idx] = idx;
}

template <typename T>
__device__ bool match_func(T &d1, T &d2){

  return (d1 == d2);
}

template <typename T>
__global__ void k1(const T * __restrict__ d1, const T * __restrict__ d2, const int dsize){

  int idx = threadIdx.x+blockDim.x*blockIdx.x;
  if (idx < dsize){
    if (match_func(d1[idx], d2[idx])) add_idx(idx);
    }
}

typedef thrust::tuple<int, int> mytuple;
struct my_comp : public thrust::unary_function<mytuple, int>
{
  __host__ __device__
  int operator()(mytuple &t1){
    if (thrust::get<0>(t1) == thrust::get<1>(t1)) return 0;
    else return 1;
  }
};

using namespace thrust::placeholders;

int main(){

  thrust::device_vector<int> d1(MAX_SIZE, 1);
  thrust::device_vector<int> d2(MAX_SIZE, 2);
  d1[12] = 2; d1[16] = 2; d1[MAX_SIZE-1] = 2;

  //method 2
  k1<<<(MAX_SIZE+nTPB-1)/nTPB,nTPB>>>(thrust::raw_pointer_cast(d1.data()), thrust::raw_pointer_cast(d2.data()), MAX_SIZE);
  int total_matches;
  cudaMemcpyFromSymbol(&total_matches, next_idx, sizeof(int));
  int *matches = new int[total_matches];
  cudaMemcpyFromSymbol(matches, match_indices, total_matches*sizeof(int));
  std::cout << "Kernel results: " << std::endl;
  for (int i = 0; i < total_matches; i++)
    std::cout << matches[i] << ",";
  std::cout << std::endl;

  //method 1
  thrust::device_vector<int> result(MAX_SIZE);
  int result_size = thrust::copy_if(thrust::counting_iterator<int>(0), thrust::counting_iterator<int>(MAX_SIZE), thrust::make_transform_iterator(thrust::make_zip_iterator(thrust::make_tuple(d1.begin(), d2.begin())), my_comp()), result.begin(), _1 == 0) - result.begin();
  std::cout << "Thrust results: " << std::endl;
  thrust::copy_n(result.begin(), result_size, std::ostream_iterator<int>(std::cout, ","));
  std::cout << std::endl;
}
$ nvcc -o t909 t909.cu
$ cuda-memcheck ./t909
========= CUDA-MEMCHECK
Kernel results:
12,16,1048575,
Thrust results:
12,16,1048575,
========= ERROR SUMMARY: 0 errors
$
$cat t909.cu
#包括
#包括
#包括
#包括
#包括
#包括
#定义最大尺寸1048576
#定义nTPB 256
__设备匹配索引[最大大小];
__设备\uuuuu int next\u idx=0;
__设备\无效添加\ idx(int idx){
int my_idx=atomicAdd(&next_idx,1);
如果(my_idxstd::请原谅我的无知,但是
argwhere
做什么呢?有两种方法:1.将其作为缩减/流压缩处理(这将非常有用).2.在全局内存中创建一个数组,该数组初始为空,但分配的大小足以容纳所有比较匹配。然后让找到匹配的每个线程原子地请求数组中的插槽。如果只有几个像素值(~1%),哪种方法会更快几乎可以肯定,在低密度匹配的情况下,原子方法会更快。