在caffe的_全局函数中使用CUDA数学函数
由于某种原因,我不得不更改caffe的源代码。这是修改后的代码 头锉在caffe的_全局函数中使用CUDA数学函数,cuda,caffe,Cuda,Caffe,由于某种原因,我不得不更改caffe的源代码。这是修改后的代码 头锉 #include <algorithm> #include <cfloat> #include <vector> #include "caffe/layer.hpp" #include "caffe/util/math_functions.hpp" #include "caffe/vision_layers.hpp" #包括 由此,我知道pow()必须具有所有双精度或所有单精度参数。 问题
#include <algorithm>
#include <cfloat>
#include <vector>
#include "caffe/layer.hpp"
#include "caffe/util/math_functions.hpp"
#include "caffe/vision_layers.hpp"
#包括
由此,我知道pow()必须具有所有双精度或所有单精度参数。
问题是,当我使用p1(double),lp+=pow(bottom_slice[h*width+w],p1)
时,发生了这种情况
不允许从全局函数调用主机函数(“std::pow”)
当我使用p(float),lp+=pow(bottom_slice[h*width+w],p)
这就发生了
错误:不允许从全局函数(“caffe::LPPoolForward”)调用主机函数(“std::pow”)
为什么当我改变pow的第二个参数的精度时,第一个参数也改变了?我对caffe不是很熟悉,所以关于如何解决这个问题有什么想法吗?现在您已经展示了更多的代码,原因很明显。所讨论的内核是一个模板,这意味着可以为单精度和双精度类型实例化代码。通过修复单精度代码,您可以将其分解为双精度。反之亦然
解决方案是使您声明的中间变量Dtype
。然后,根据内核实例化的类型,参数将始终匹配,并且编译期间,pow
不会出现问题。您正在修改的内核是模板化的吗?您没有提供足够的信息让其他人能够回答您的问题以添加背景:出现错误的直接原因是CUDA[1]使用主机工具链的math.h
,[2]提供了pow(float,float)
,pow(double,double)
,pow(float,int)
的实现,对于设备代码,pow(double,int)
。因此,编译器找不到设备代码pow(float,double)
和pow(double,float)
的实现,但它显然找到了这些函数的主机端版本的原型。但是,无法从设备代码调用主机函数,从而导致问题中显示的错误。
template <typename Dtype>
__global__ void LPPoolForward(const int nthreads,
const Dtype* const bottom_data, const int num, const int channels,
const int height, const int width, const int pooled_height,
const int pooled_width, const int kernel_h, const int kernel_w,
const int stride_h, const int stride_w, const int pad_h, const int pad_w,float p,
Dtype* const top_data) {
CUDA_KERNEL_LOOP(index, nthreads) {
const int pw = index % pooled_width;
const int ph = (index / pooled_width) % pooled_height;
const int c = (index / pooled_width / pooled_height) % channels;
const int n = index / pooled_width / pooled_height / channels;
int hstart = ph * stride_h - pad_h;
int wstart = pw * stride_w - pad_w;
int hend = min(hstart + kernel_h, height + pad_h);
int wend = min(wstart + kernel_w, width + pad_w);
hstart = max(hstart, 0);
wstart = max(wstart, 0);
hend = min(hend, height);
wend = min(wend, width);
Dtype lp = 0;
double p1=p;
const Dtype* const bottom_slice =bottom_data + (n * channels + c)*height* width;
for (int h = hstart; h < hend; ++h) {
for (int w = wstart; w < wend; ++w) {
lp += pow(bottom_slice[h * width + w],p1);
lp += pow(bottom_slice[h * width + w],p);
}
}
top_data[index] = pow(lp,1/p1);
}
}
}