Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.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++ 主旨:对另一个数组索引的数组元素求和[Matlab的语法和(x(索引))]_C++_Cuda_Thrust - Fatal编程技术网

C++ 主旨:对另一个数组索引的数组元素求和[Matlab的语法和(x(索引))]

C++ 主旨:对另一个数组索引的数组元素求和[Matlab的语法和(x(索引))],c++,cuda,thrust,C++,Cuda,Thrust,我试图使用推力库对另一个数组索引的数组元素求和,但找不到示例。换句话说,我想实现Matlab的语法 sum(x(indices)) 下面是一个指导代码,试图指出我想要实现的目标: #define N 65536 // device array copied using cudaMemcpyToSymbol __device__ int global_array[N]; // function to implement with thrust __device__ int support(u

我试图使用推力库对另一个数组索引的数组元素求和,但找不到示例。换句话说,我想实现Matlab的语法

sum(x(indices))
下面是一个指导代码,试图指出我想要实现的目标:

#define N 65536

// device array copied using cudaMemcpyToSymbol
__device__ int global_array[N];

// function to implement with thrust
__device__ int support(unsigned short* _memory, unsigned short* _memShort)
{
   int support = 0;

  for(int i=0; i < _memSizeShort; i++)
        support += global_array[_memory[i]];

  return support;     
}
#定义N 65536
//使用CUDAMEMCPITOSYMBOL复制的设备阵列
__设备\uuuuu int全局\u数组[N];
//带推力执行的功能
__设备输入支持(无符号短*内存、无符号短*内存短)
{
int支持=0;
对于(int i=0;i<\u memSizeShort;i++)
支持+=全局_数组[_内存[i]];
回归支持;
}
另外,在主机代码中,我是否可以使用全局数组[N],而不使用cudaMemcpyFromSymbol将其复制回来

感谢您的每一条评论/回答:)


谢谢

这是一个很晚才提供的答案,用于从未回答列表中删除此问题。我确信OP已经找到了一个解决方案(自2012年5月起:-),但我相信以下内容对其他用户可能有用

正如@Talonmes所指出的,这个问题可以通过融合聚集归约来解决。这个解决方案实际上是Thurst的
置换迭代器
reduce
的应用。
置换迭代器
允许(隐式地)根据
索引
数组中的索引对目标数组
x
重新排序
reduce
执行(隐式)重新排序数组的和

此应用程序是的一部分,为方便起见,下面报告

#include <thrust/iterator/permutation_iterator.h>
#include <thrust/reduce.h>
#include <thrust/device_vector.h>

// this example fuses a gather operation with a reduction for
// greater efficiency than separate gather() and reduce() calls

int main(void)
{
    // gather locations
    thrust::device_vector<int> map(4);
    map[0] = 3;
    map[1] = 1;
    map[2] = 0;
    map[3] = 5;

    // array to gather from
    thrust::device_vector<int> source(6);
    source[0] = 10;
    source[1] = 20;
    source[2] = 30;
    source[3] = 40;
    source[4] = 50;
    source[5] = 60;

    // fuse gather with reduction: 
    //   sum = source[map[0]] + source[map[1]] + ...
    int sum = thrust::reduce(thrust::make_permutation_iterator(source.begin(), map.begin()),
                             thrust::make_permutation_iterator(source.begin(), map.end()));

    // print sum
    std::cout << "sum is " << sum << std::endl;

    return 0;
}

如果您只想迭代索引数组
映射的第一个
N


最后,关于从主机使用
global\u array
的可能性,您应该注意到这是一个驻留在设备上的向量,因此您确实需要
cudamemcpyfromsymsymbol
将其首先移动到主机。

您能解释一下您要做的事情吗?总和是一个总的总和(即你的
支持
函数应该是一个融合的聚集减少)还是其他什么?你有选择某种原因来显示<代码>支持<代码>作为一个设备功能,或者根本不相关?如果你使用推力,你应该用一个合适的C++风格来编码,IMO @ TalnMyes你解决了我的问题,只是说“融合的聚集减少”!这正是我要找的!但是有两件事:从《推力快速入门指南》(permutation_iterator)中的示例来看,它们只是迭代整体数组。取而代之的是,我想迭代一个特定的数字(如上面的for循环);我该怎么做?我应该从设备上复制回全局_数组[N]吗?
int sum = thrust::reduce(thrust::make_permutation_iterator(source.begin(), map.begin()),
                         thrust::make_permutation_iterator(source.begin(), map.end()));
int sum = thrust::reduce(thrust::make_permutation_iterator(source.begin(), map.begin()),
                         thrust::make_permutation_iterator(source.begin(), map.begin()+N));