简体   繁体   English

推力:: device_vector使用推力::替换或推力::使用自定义函子/谓词进行转换

[英]thrust::device_vector use thrust::replace or thrust::transform with custom functor/predicate

I use a cuda kernel to do a sigmoid activation on a thrust vector: 我使用cuda内核对推力矢量进行S型激活:

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 );

where my kernel is: 我的内核在哪里:

__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 );  
}

How can I use thrust::replace with a functor / predicate to do the same? 如何使用推力:::用函子/谓词替换以完成相同操作?

The examples I have seen are too simplistic to demonstrate such use: 我所看到的示例过于简单,无法展示这种用法:

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

Or 要么

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

In the "Thrust Quick Start Guide" on page 8-9 there is an example on how to create your own functions for transform. 在第8-9页的“推力快速入门指南”中,有一个有关如何创建自己的变换函数的示例。

I came up with a solution, but notice this will not run on the host side because you use CUDA intrinsics. 我想出了一个解决方案,但是请注意,由于您使用CUDA内部函数,因此不会在主机端运行。

Code

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM