【问题标题】:thrust::device_vector use thrust::replace or thrust::transform with custom functor/predicate推力::设备向量使用推力::替换或推力::转换与自定义函子/谓词
【发布时间】:2016-02-28 15:56:37
【问题描述】:

我使用 cuda 内核对推力向量进行 sigmoid 激活:

thrust::device_vector<float> output = input;
float * output_ptr = thrust::raw_pointer_cast( output.data() );
sigmoid_activation<<<num_blocks_x,block_threads_x>>>( output_ptr );

我的内核在哪里:

__device__ float sigmoid_function( float input, float skew )
{
    // -X: Neg X
    float x_neg = __fmul_rz( -1.f, input );
    // Y: exponential value
    float exp_val = __expf( x_neg );
    // 1 + exp^(-X)
    float denom = __fadd_rz( 1.f, e_to_x_neg );
     // 1 / 1 + exp^(-X)
    float output  = __fdividef( 1.f, denom );

    if ( skew != 0.0 )
        return _fadd_rz( output, skew );
    else
        return output;
}

__global__ void sigmoid_activation( float * input float skew )
{
    // Iterate Input vector
    int x = blockIdx.x * blockDim.x + threadIdx.x;
    // Update value
    input[x]  = sigmoid_function( input[x], skew );  
}

如何使用带有仿函数/谓词的推力::替换来做同样的事情?

我看到的例子太简单了,无法演示这种用法:

thrust::replace(Y.begin(), Y.end(), 1, 10);

或者

thrust::transform(X.begin(), X.end(), Y.begin(),thrust::negate<int>());

【问题讨论】:

    标签: c++ cuda thrust


    【解决方案1】:

    在第 8-9 页的“Thrust 快速入门指南”中,有一个关于如何创建自己的变换函数的示例。

    我想出了一个解决方案,但请注意这不会在主机端运行,因为您使用的是 CUDA 内部函数。

    代码

    #include <thrust/device_vector.h>
    #include <thrust/transform.h>
    #include <thrust/sequence.h>
    #include <thrust/copy.h>
    #include <thrust/fill.h>
    #include <thrust/replace.h>
    #include <thrust/functional.h>
    #include <iostream>
    
    template<typename T>
    struct sigmoid_function
    {
    
      float _skew;
    
      sigmoid_function(float skew) : _skew(skew) { /*Empty */ }
    
      typedef T argument_type;
    
      typedef T result_type;
    
      __device__ T operator()(const T &x) const {
    
        float x_neg = __fmul_rz( -1.f, x );
        float exp_val = __expf( x_neg );
        float denom = __fadd_rz( 1.f, __expf(-exp_val) );
        float output  = __fdividef( 1.f, denom );
    
        if ( _skew != 0.0 )
            return __fadd_rz( output, _skew );
        else
            return output;
      }
    };
    
    int main(void) {
        // allocate three device_vectors with 10 elements
        thrust::device_vector<float> X(10);
    
        // initialize X to 0,1,2,3, ....
        thrust::sequence(X.begin(), X.end());
    
        // Before
        thrust::copy(X.begin(),X.end(),std::ostream_iterator<float>(std::cout, "\n"));
    
        // Apply
        thrust::transform(X.begin(), X.end(), X.begin(), sigmoid_function<float>(0.1));
    
        // After
        thrust::copy(X.begin(),X.end(),std::ostream_iterator<float>(std::cout, "\n"));
    
        return 0;
    }
    

    【讨论】:

    • 非常感谢!我没有看到,但我的理解是大多数推力函子都使用相同的struct-operator()() 模式?无论如何,非常感谢。
    • 模式相似。根据推力函数和它需要的参数数量(C++ 允许重载函数),它会要求你的仿函数有一定数量的参数。
    猜你喜欢
    • 2017-02-14
    • 2011-11-22
    • 2017-03-01
    • 2013-03-27
    • 2012-07-18
    • 2012-09-06
    • 2014-07-01
    • 1970-01-01
    • 2020-07-27
    相关资源
    最近更新 更多