这个简单的推力代码如何在两台机器上吐出不同的结果?

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

我写一个数值积分代码:

#include <thrust/inner_product.h>
#include <thrust/transform.h>
#include <thrust/for_each.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/iterator/permutation_iterator.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/iterator/discard_iterator.h>
#include <thrust/device_vector.h>
#include <thrust/reduce.h>
#include <array>

#include <iostream>


template<typename val_type, std::size_t n, std::size_t n_batch,
                template<typename...> typename Container>
struct Integrator {

    const val_type coeff;

    Integrator(val_type a, val_type b) :
        coeff((b-a)/3./static_cast<val_type>(n)) {}

    template <typename itor_type>
    void operator()(itor_type f_begin, itor_type dens_begin) {

        val_type C = this->coeff;
        auto zitor_begin = thrust::make_zip_iterator(
                            thrust::make_tuple(
                              thrust::make_counting_iterator(0),f_begin));

        auto titor_begin = make_transform_iterator(zitor_begin,
        [C](auto _tuple){ 
            
             return static_cast<val_type>(thrust::get<1>(_tuple) 
                          * (thrust::get<0>(_tuple)==0 ? C 
                                             : thrust::get<0>(_tuple)%2==0? 2.*C:4.*C)); 
        });

        auto binary_pred = [](int i,int j) { return i/n==j/n ; };

        thrust::reduce_by_key(thrust::make_counting_iterator(0),  // input key
                              thrust::make_counting_iterator(static_cast<int>(n*n_batch)),
                              titor_begin,                        // input value
                                                    thrust::make_discard_iterator(),    // output key
                              dens_begin,                         // output value
                              binary_pred);
    } // end of operator()

};

该算法是一个简单的辛普森积分。这是

main.cu

#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include "Integrator.hpp"

using Real = double;


constexpr std::size_t n = 1000, m = 4;
constexpr Real h = 2.*M_PI/(static_cast<Real>(n)-1.);

int main(int argc, char* argv[]) {


    Integrator<Real,n,m,thrust::device_vector> inttest(0,2.*M_PI);
    
    thrust::host_vector<Real> _fff(n*m), _x(n);
    thrust::device_vector<Real> fff(n*m), fint(m);
    for (int i=0; i<n; ++i)
        _x[i] = i*h;
    for (int I=0; I<m; ++I)
        for (int i=0; i<n; ++i)
            _fff[I*n+i] = std::sin(0.5*_x[i]);
    
    fff = _fff;
    inttest(fff.begin(),fint.begin());
    
    thrust::copy(fint.begin(),fint.end(),
                  std::ostream_iterator<Real>(std::cout," "));
    std::cout << std::endl;


}



我在window11系统的WSL和Ubuntu22.4 Linux服务器上编译这段代码。在这两台机器上都编译成功。 (我简单的用了

nvc++ main.cu

两个系统(WSL和Ubuntu Linux)具有完全相同的gcc版本(11.3),cuda版本(12.0)和nvc++版本(23.3)。但是,只有 Linux 机器产生正确的结果(应该是 3.99 3.99 3.99 3.99),而 WSL windows11 机器产生 0 0 0 0。我知道 a.out 文件的大小在 WSL 上是 24Mb,在 Linux 机器上是 8Mb一定是出了什么问题,但是哪里出了问题?这真的是一个简单的代码。

cuda gpu thrust
© www.soinside.com 2019 - 2024. All rights reserved.