我已经为特征点的密集采样编写了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): 错误:调用主机函数(&#34; cv :: Point_ :: Point _&#34;)来自 全局功能(&#34; densefun&#34;)是不允许的
/home/supriya/Desktop/5Dec/CalculateFV_merged_gpu_old/build/denseCuda.cu(28): 错误:调用主机函数(&#34; std :: vector, std :: allocator&gt; &GT;来自全局的:: operator []&#34;) 函数(&#34; densefun&#34;)是不允许的
/home/supriya/Desktop/5Dec/CalculateFV_merged_gpu_old/build/denseCuda.cu(29) (第7栏):错误:从a调用主机功能(&#34; cvFloor&#34;) 全局功能(&#34; densefun&#34;)是不允许的
/home/supriya/Desktop/5Dec/CalculateFV_merged_gpu_old/build/denseCuda.cu(30) (第7栏):错误:从a调用主机功能(&#34; cvFloor&#34;) 全局功能(&#34; densefun&#34;)是不允许的
/home/supriya/Desktop/5Dec/CalculateFV_merged_gpu_old/build/denseCuda.cu(35): 错误:从全局调用主机函数(&#34; std :: vector&gt; :: operator []&#34;) 函数(&#34; densefun&#34;)是不允许的
在编译中检测到5个错误 &#34; /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.oCMakeFiles / testVideo.dir / build.make:392:目标配方 &#39; CMakeFiles / testVideo.dir /./ testVideo_generated_denseCuda.cu.o&#39;失败 make [2]: * [CMakeFiles / testVideo.dir /./ testVideo_generated_denseCuda.cu.o]错误 1 CMakeFiles / Makefile2:130:目标的配方 &#39; CMakeFiles / testVideo.dir /所有&#39;失败了[1]:* [CMakeFiles / testVideo.dir / all]错误2 Makefile:76:目标配方 &#39;所有&#39;失败了:*** [全部]错误2
答案 0 :(得分:4)
您不能在CUDA内核中使用C ++标准库,OpenCV或任何其他非CUDA特定库。
而不是std::vector
您需要使用指向设备上分配的数组的原始指针,而不是Point2f
,而是需要使用CUDA特定的向量类型float2
,而不是cvFloor
你需要使用__device__ floorf()
等等。