c++11 - 在cuda中使用推力实验::pinned_allocator的奇怪行为

标签 c++11 cuda iterator thrust

我目前正在尝试从我的代码中删除部分繁琐的cudaMallocHost/cudaFreeHost。为此,我愿意仅使用 std::vector,但我绝对需要底层内存必须是固定的 cuda 内存类型。

但是,我使用 thrust::system::cuda::experimental::pinned_allocator<> 时遇到奇怪的行为来自推力库:

//STL
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

//CUDA
#include <cuda_runtime.h>
#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include <thrust/system/cuda/experimental/pinned_allocator.h>

#define SIZE 4
#define INITVAL 2
#define ENDVAL 4

//Compile using nvcc ./main.cu -o test -std=c++11
int main( int argc, char* argv[] )
{
    // init host
    std::vector<float,thrust::system::cuda::experimental::pinned_allocator<float> > hostVec(SIZE);
    std::fill(hostVec.begin(),hostVec.end(),INITVAL);

    //Init device
    thrust::device_vector<float> thrustVec(hostVec.size());

    //Copy
    thrust::copy(hostVec.begin(), hostVec.end(), thrustVec.begin());

    //std::cout << "Dereferencing values of the device, values should be "<< INITVAL << std::endl;
    std::for_each(thrustVec.begin(),thrustVec.end(),[](float in){ std::cout <<"val is "<<in<<std::endl;} );
    std::cout << "------------------------" << std::endl;

    //Do Stuff
    thrust::transform( thrustVec.begin(), thrustVec.end(), thrust::make_constant_iterator(2), thrustVec.begin(), thrust::multiplies<float>() );

    //std::cout << "Dereferencing values of the device, values should now be "<< ENDVAL << std::endl;
    std::for_each(thrustVec.begin(),thrustVec.end(),[](float in){ std::cout <<"val is "<<in<<std::endl;} );
    std::cout << "------------------------" << std::endl;

    //Copy back
    thrust::copy(thrustVec.begin(), thrustVec.end(), hostVec.begin());

    //Synchronize
    //cudaDeviceSynchronize(); //makes the weird behaviour to go away

    //Check result
    //std::cout << "Dereferencing values on the host, values should now be "<< ENDVAL << std::endl;//Also makes the weird behaviour to go away

    std::for_each(hostVec.begin(),hostVec.end(),[](float in){ std::cout <<"val is "<<in<<std::endl;} ); 

    return EXIT_SUCCESS;
}

在我的设置中,给出:

val is 2
val is 2
val is 2
val is 2
------------------------
val is 4
val is 4
val is 4
val is 4
------------------------
val is 2
val is 4
val is 4
val is 4

为什么从设备到主机的复制似乎失败?然而,Nvvp 显示了一个完美的计时图,具有正确的复制值。

顺便说一句,我使用 7.5 软件包中的 NVCC/cuda/thrust,以及带有 titanX 卡的 gcc (GCC) 4.8.5。

预先感谢您的帮助。

最佳答案

这是一个真正的错误,推力开发人员已经意识到了它,请参阅 https://github.com/thrust/thrust/issues/775

使用 github 存储库中最新的 1.8.3 版本的推力解决了我的问题。

关于c++11 - 在cuda中使用推力实验::pinned_allocator的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36578414/

相关文章:

c# - 在 GPU 全局内存中存储选择性元素

c++ - 生成数据的类似迭代器的概念

使用带有虚函数的协变返回类型的 C++ 无效转换错误

c++ - 根据标准,std::vector 是否受静态初始化顺序问题的影响?

c++ - 递归内存错误

python-2.7 - cudaGetDevice() 失败。状态 : CUDA driver version is insufficient for CUDA runtime version

c++ - 自动和复制构造函数 : what's wrong?

c++ - 在设备上运行类构造函数/方法。如何?

python - Python 中的迭代器选择器

rust - 在Rust中,如何创建可变的迭代器? [复制]