【问题标题】:error: calling a __host__ function from a __global__ function is not allowed错误:不允许从 __global__ 函数调用 __host__ 函数
【发布时间】:2017-01-03 09:39:36
【问题描述】:

我已经为特征点的密集采样编写了 cuda 函数,但我遇到了错误。我的 cuda 代码如下所示。我正在使用 cuda 7.5 工具包。

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/gpu/gpu.hpp>
#include <opencv2/opencv.hpp>


using namespace cv::gpu;
using namespace cv;
using namespace std;

__global__ void densefun(std::vector<int>* d_counters,std::vector<Point2f>* d_points,int d_x_max,int d_y_max,int width, int min_distance)
{
  int i = blockDim.x * blockIdx.x + threadIdx.x;
  Point2f point = (*d_points)[i];
  int x = cvFloor(point.x);
  int y = cvFloor(point.y);
  //if(x >= d_x_max || y >= d_y_max)
      //continue;
  x /= min_distance;
  y /= min_distance;
  (*d_counters)[y*width+x]++;
}


void dense(std::vector<int>& counters,std::vector<Point2f>& points,int x_max,int y_max,int width)
{
  std::vector<int>* d_counters;
  std::vector<Point2f>* d_points;
  int min_distance=5; 
  cudaMalloc(&d_counters,counters.size());
  cudaMalloc(&d_points,points.size());
  cudaMemcpy(d_points, &points, points.size(), cudaMemcpyHostToDevice);
  densefun<<<1,points.size()>>>(d_counters,d_points,x_max,y_max,width,min_distance);
  cudaMemcpy(&counters, d_counters, counters.size(), cudaMemcpyDeviceToHost);
  cudaFree(d_counters);
  cudaFree(d_points);
}

输出:

/home/supriya/Desktop/5Dec/CalculateFV_merged_gpu_old/build/denseCuda.cu(28): 错误:调用 host 函数(“cv::Point_ ::Point_”) 全局函数(“densefun”)是不允许的

/home/supriya/Desktop/5Dec/CalculateFV_merged_gpu_old/build/denseCuda.cu(28): 错误:调用 host 函数("std::vector , std::allocator > > ::operator []") 来自 全局 不允许使用函数(“densefun”)

/home/supriya/Desktop/5Dec/CalculateFV_merged_gpu_old/build/denseCuda.cu(29) (第 7 栏):错误:调用 host 函数(“cvFloor”) 全局函数(“densefun”)是不允许的

/home/supriya/Desktop/5Dec/CalculateFV_merged_gpu_old/build/denseCuda.cu(30) (第 7 栏):错误:调用 host 函数(“cvFloor”) 全局函数(“densefun”)是不允许的

/home/supriya/Desktop/5Dec/CalculateFV_merged_gpu_old/build/denseCuda.cu(35): 错误:从 global 调用 host 函数("std::vector > ::operator []") 不允许使用函数(“densefun”)

在编译过程中检测到 5 个错误 “/tmp/tmpxft_00000c85_00000000-7_denseCuda.cpp1.ii”。 CMake 错误 testVideo_generated_denseCuda.cu.o.cmake:260(消息):错误 生成文件
/home/supriya/Desktop/5Dec/CalculateFV_merged_gpu_old/build/CMakeFiles/testVideo.dir//./testVideo_generated_denseCuda.cu.o

CMakeFiles/testVideo.dir/build.make:392:目标配方 'CMakeFiles/testVideo.dir/./testVideo_generated_denseCuda.cu.o' 失败 制作[2]: * [CMakeFiles/testVideo.dir/./testVideo_generated_denseCuda.cu.o] 错误 1 CMakeFiles/Makefile2:130:目标配方 'CMakeFiles/testVideo.dir/all' 失败 [1]: * [CMakeFiles/testVideo.dir/all] 错误 2 Makefile:76: 目标配方 'all' 失败 make: *** [all] 错误 2

【问题讨论】:

    标签: cuda


    【解决方案1】:

    您不能在 CUDA 内核中使用 C++ 标准库、OpenCV 或任何其他非 CUDA 特定库。

    您需要使用指向设备上分配的数组的原始指针而不是std::vector,而不是Point2f,您需要使用CUDA特定的向量类型float2,而不是cvFloor,您需要使用__device__ ​ floorf()等等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 2014-06-09
      相关资源
      最近更新 更多