簡體   English   中英

Cuda lambda vs 仿函數使用

[英]Cuda lambda vs functor usage

我在 CUDA 中有一個簡單的 function 使用函子

struct MT {
    const float _beta1;
    const float _mb1;

    MT(const float beta1, const float mb1) : _beta1(beta1), _mb1(mb1) { }
    
    __device__
    float operator()(const float& op, const float& gradient) {
        return _beta1 * op + _mb1 * gradient;
    }
};


void example(const thrust::device_vector<float>& gradients, thrust::device_vector<float>& d_weights)
{
    thrust::transform(_mt.begin(), _mt.end(), gradients.begin(), _mt.begin(), MT(_beta1, _mb1));
}

然而,這個等價的例子會崩潰(很好地符合 --extended-lambda flat)。 是否有另一種標志或不同的方式來表達它以使其運行。 函子很好,但 lambda 看起來更整潔。

void example_crash(const thrust::device_vector<float>& gradients, thrust::device_vector<float>& d_weights)
{
    thrust::transform(_mt.begin(), _mt.end(), gradients.begin(), _mt.begin(), [this](const float& op,const float& gradient) { return _beta1 * op + _mb1 * gradient; });
}

錯誤是

Exception thrown at 0x00007FFA833D4FD9 in Optioniser.exe: Microsoft C++ exception: thrust::system::system_error at memory location 0x00000031ED7FCDD0.
Exception thrown: 'System.Runtime.InteropServices.SEHException' in AARC.Optimisation.dll
An exception of type 'System.Runtime.InteropServices.SEHException' occurred in AARC.Optimisation.dll but was not handled in user code
External component has thrown an exception.

您的exampleexample_crash函數對我來說沒有意義,因為我不知道_mt是什么,而且您似乎沒有使用d_weights

如果我們解決了這個問題,那么您的 lambda 至少存在幾個問題,其中一個是沒有__device__裝飾(這里是必需的)。

進行各種更改並修復您未顯示的內容,這對我有用:

$ cat t2093.cu
#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include <thrust/host_vector.h>
#include <thrust/copy.h>
#include <iostream>

struct MT {
    const float _beta1;
    const float _mb1;

    MT(const float beta1, const float mb1) : _beta1(beta1), _mb1(mb1) { }

    __device__
    float operator()(const float& op, const float& gradient) {
        return _beta1 * op + _mb1 * gradient;
    }
};

const float _beta1 = 1.0f;
const float _mb1 = 1.0f;
void example(const thrust::device_vector<float>& gradients, thrust::device_vector<float>& _mt)
{
    thrust::transform(_mt.begin(), _mt.end(), gradients.begin(), _mt.begin(), MT(_beta1, _mb1));
};

void example_crash(const thrust::device_vector<float>& gradients, thrust::device_vector<float>& _mt)
{
    thrust::transform(_mt.begin(), _mt.end(), gradients.begin(), _mt.begin(), [=] __device__ (const float& op,const float& gradient) { return _beta1 * op + _mb1 * gradient; });
};

const int len = 1000;
int main(){

  thrust::device_vector<float> g1(len, 1.0f);
  thrust::device_vector<float> mt1(len, 2.0f);
  example(g1, mt1);
  thrust::host_vector<float> h_mt1 = mt1;
  thrust::copy_n(h_mt1.begin(), 2, std::ostream_iterator<float>(std::cout, ","));
  std::cout << std::endl;
  thrust::device_vector<float> g2(len, 1.0f);
  thrust::device_vector<float> mt2(len, 2.0f);
  example_crash(g2, mt2);
  thrust::host_vector<float> h_mt2 = mt2;
  thrust::copy_n(h_mt2.begin(), 2, std::ostream_iterator<float>(std::cout, ","));
  std::cout << std::endl;
}
$ nvcc -o t2093 t2093.cu --extended-lambda
$ compute-sanitizer ./t2093
========= COMPUTE-SANITIZER
3,3,
3,3,
========= ERROR SUMMARY: 0 errors
$

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM