首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在cuda中实现一个简化的argwhere

如何在cuda中实现一个简化的argwhere
EN

Stack Overflow用户
提问于 2015-09-10 18:27:14
回答 1查看 191关注 0票数 1

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-10 19:03:05

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

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

下面是这两种方法的工作示例:

代码语言:javascript
复制
$ 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
$

最后,如果您对这两种方法进行定时比较,您将发现当匹配密度较低时(例如,1%或更少),原子/核方法显着地更快,而当匹配密度较高(例如,50%或更高)时,推力方法更快。确切的比较将取决于您正在运行的GPU,也可能还取决于其他因素,如总体数据集大小。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32509189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档