【发布时间】:2022-08-13 23:57:42
【问题描述】:
我在 CUDA 中有一个使用函子的简单函数
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.
-
我不清楚您从示例函数中获得
_beta1、_mb1和_mt的位置。example实际上是更大类中的方法吗? -
对于 lambda 版本,我会期待
[_beta1,_mb1](const float& op,const float& gradient) { return _beta1 * op + _mb1 * gradient; },即缺少捕获,对吗? -
或者,占位符表达式也应该起作用:
thrust::transform(_mt.begin(), _mt.end(), gradients.begin(), _mt.begin(), _beta1 * _1 + _mb1 * _2);。 -
请尝试提供minimal, reproducible example。所以,
main(),除非它对您的示例至关重要,否则也没有推力。 -
推力是这个例子的重点。 std::transform 很好