是否有一种方法可以使用推力将数组的所有元素相乘?

问题描述 投票:1回答:1

假设我有一个数组,我想将该数组的所有元素相乘并将其存储在变量中。我该怎么做?我想将A中的所有元素相乘并将结果存储在S中。这样做使我为零。

thrust::device_vector<double> A(10);
thrust::sequence(A.begin(), A.end(), 1);
double S = thrust::reduce(thrust::host, A.begin(), A.end(), 
                             0, thrust::multiplies<double>());
cuda thrust
1个回答
3
投票

这里有两个问题:

  1. thrust::host执行策略的使用是没有意义的(实际上,如果您使用推力矢量作为输入,则没有执行策略是有意义的)。如果您实际尝试运行此代码,则可能会遇到某种segfault或运行时错误。请改用基于标签的执行,即只需删除第一个参数,推力就会从类型中推断出执行策略。
  2. 在您的代码中调用thrust::reduce中的第四个参数是产品的初始值。您将其设置为0。这意味着该代码计算的任何乘积将为零。大概您希望初始值为1。

解决了这两个明显的问题都让我明白了:

#include <thrust/device_vector.h>
#include <thrust/reduce.h>
#include <thrust/sequence.h>

#include <iostream>
#include <iomanip>

int main()
{
    thrust::device_vector<double> A(10);
    thrust::sequence(A.begin(), A.end(), 1.0);

    for(auto p = A.begin(); p != A.end(); ++p) {
        double v = *p;
        std::cout << v << std::endl;
    }

    double S = thrust::reduce(A.begin(), A.end(), 1.0, thrust::multiplies<double>());

    std::cout << "S = " << std::fixed << std::setprecision(1) << S << std::endl;

    return 0;
}

编译并运行它得到了这个:

$ nvcc -std=c++11 -o prodprob prodprob.cu 

$ ./prodprob 
1
2
3
4
5
6
7
8
9
10
S = 3628800.0

正确答案应该是10! = 3628800,所以我认为这工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.