2017-01-03 79 views
0

我已經爲特徵點的密集採樣編寫了cuda函數,但出現錯誤。我的cuda代碼如下。我正在使用cuda 7.5工具包。錯誤:不允許從__global__函數調用__host__函數

#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): error: calling a host function("cv::Point_ ::Point_") from a global function("densefun") is not allowed

/home/supriya/Desktop/5Dec/CalculateFV_merged_gpu_old/build/denseCuda.cu(28): error: calling a host function("std::vector , std::allocator > > ::operator []") from a global function("densefun") is not allowed

/home/supriya/Desktop/5Dec/CalculateFV_merged_gpu_old/build/denseCuda.cu(29) (col. 7): error: calling a host function("cvFloor") from a global function("densefun") is not allowed

/home/supriya/Desktop/5Dec/CalculateFV_merged_gpu_old/build/denseCuda.cu(30) (col. 7): error: calling a host function("cvFloor") from a global function("densefun") is not allowed

/home/supriya/Desktop/5Dec/CalculateFV_merged_gpu_old/build/denseCuda.cu(35): error: calling a host function("std::vector > ::operator []") from a global function("densefun") is not allowed

5 errors detected in the compilation of "/tmp/tmpxft_00000c85_00000000-7_denseCuda.cpp1.ii". CMake Error at testVideo_generated_denseCuda.cu.o.cmake:260 (message): Error generating file
/home/supriya/Desktop/5Dec/CalculateFV_merged_gpu_old/build/CMakeFiles/testVideo.dir//./testVideo_generated_denseCuda.cu.o

CMakeFiles/testVideo.dir/build.make:392: recipe for target 'CMakeFiles/testVideo.dir/./testVideo_generated_denseCuda.cu.o' failed make[2]: * [CMakeFiles/testVideo.dir/./testVideo_generated_denseCuda.cu.o] Error 1 CMakeFiles/Makefile2:130: recipe for target 'CMakeFiles/testVideo.dir/all' failed make[1]: * [CMakeFiles/testVideo.dir/all] Error 2 Makefile:76: recipe for target 'all' failed make: *** [all] Error 2

回答

3

不能使用C++標準庫,OpenCV的或CUDA內核中的任何其他非CUDA專用庫。

而不是你需要使用原始指針std::vector到數組分配的設備上,而不是Point2f你需要使用CUDA特定載體類型float2,而不是cvFloor你需要使用__device__ ​ floorf()等。

相關問題