Ask Your Question
0

Unknown CUDA error : Gpu API call

asked 2014-10-23 05:57:35 -0600

shawshank gravatar image

I'm trying to make a simple program using CUDA for Opencv on ubuntu 14.04. But whenever I try to call any function in the gpu header file it gives me the same error
Opencv error : Gpu API call (unknown error)
Here is the sample code I'm trying to run

#include <iostream>
#include <vector>

#include "opencv/cv.h"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/gpu/gpu.hpp"

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

static void download(const GpuMat& d_mat, vector<Point2f>& vec)
{
    vec.resize(d_mat.cols);
    Mat mat(1, d_mat.cols, CV_32FC2, (void*)&vec[0]);
    d_mat.download(mat);
}

static void download(const GpuMat& d_mat, vector<uchar>& vec)
{
    vec.resize(d_mat.cols);
    Mat mat(1, d_mat.cols, CV_8UC1, (void*)&vec[0]);
    d_mat.download(mat);
}

static void drawArrows(Mat& frame, const vector<Point2f>& prevPts, const vector<Point2f>& nextPts, const vector<uchar>& status, Scalar line_color = Scalar(0, 0, 255))
{
    for (size_t i = 0; i < prevPts.size(); ++i)
    {
        if (status[i])
        {
            int line_thickness = 1;

            Point p = prevPts[i];
            Point q = nextPts[i];

            double angle = atan2((double) p.y - q.y, (double) p.x - q.x);

            double hypotenuse = sqrt( (double)(p.y - q.y)*(p.y - q.y) + (double)(p.x - q.x)*(p.x - q.x) );

            if (hypotenuse < 1.0)
                continue;

            // Here we lengthen the arrow by a factor of three.
            q.x = (int) (p.x - 3 * hypotenuse * cos(angle));
            q.y = (int) (p.y - 3 * hypotenuse * sin(angle));

            // Now we draw the main line of the arrow.
            line(frame, p, q, line_color, line_thickness);

            // Now draw the tips of the arrow. I do some scaling so that the
            // tips look proportional to the main line of the arrow.

            p.x = (int) (q.x + 9 * cos(angle + CV_PI / 4));
            p.y = (int) (q.y + 9 * sin(angle + CV_PI / 4));
            line(frame, p, q, line_color, line_thickness);

            p.x = (int) (q.x + 9 * cos(angle - CV_PI / 4));
            p.y = (int) (q.y + 9 * sin(angle - CV_PI / 4));
            line(frame, p, q, line_color, line_thickness);
        }
    }
}

template <typename T> inline T clamp (T x, T a, T b)
{
    return ((x) > (a) ? ((x) < (b) ? (x) : (b)) : (a));
}

template <typename T> inline T mapValue(T x, T a, T b, T c, T d)
{
    x = clamp(x, a, b);
    return c + (d - c) * (x - a) / (b - a);
}

static void getFlowField(const Mat& u, const Mat& v, Mat& flowField)
{
    float maxDisplacement = 1.0f;

    for (int i = 0; i < u.rows; ++i)
    {
        const float* ptr_u = u.ptr<float>(i);
        const float* ptr_v = v.ptr<float>(i);

        for (int j = 0; j < u.cols; ++j)
        {
            float d = max(fabsf(ptr_u[j]), fabsf(ptr_v[j]));

            if (d > maxDisplacement)
                maxDisplacement = d;
        }
    }

    flowField.create(u.size(), CV_8UC4);

    for (int i = 0; i < flowField.rows; ++i)
    {
        const float* ptr_u = u.ptr<float>(i);
        const float* ptr_v = v.ptr<float>(i);


        Vec4b* row = flowField.ptr<Vec4b>(i);

        for (int j = 0; j < flowField.cols; ++j)
        {
            row[j][0 ...
(more)
edit retag flag offensive close merge delete

Comments

Do the cuda test programs work? Are you in debug mode? I had an odd issue with the ubuntu repository drivers that sounded similar to that. I fixed it by manually installing the newest drivers directly from nvidia, via their ".sh" installer.
Also sometimes I have errors with cuda functions in debug mode that don't appear in release mode.

dtmoodie gravatar imagedtmoodie ( 2015-02-02 13:55:43 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-10-26 02:51:27 -0600

AJW gravatar image

I don't have a lot of experience with OpenCV CUDA functionality, so I'm not sure about the problem from the details given. However, one thing to check would be that your build of OpenCV is configured for CUDA. I think (though could be wrong) that the builds provided on the OpenCV site site aren't configured to use CUDA by default. The details are here:

http://docs.opencv.org/modules/gpu/doc/introduction.html

I think there's a WITH_CUDA flag that needs to be set for the build, and you need to have the CUDA toolkit installed before you build. If you don't have it installed, OpenCV still builds, but just doesn't work (helpful right?).

Try calling the function gpu::getCudaEnabledDeviceCount(). If you have a CUDA-enabled device available and OpenCV is built for CUDA, it should return a positive number. If it returns 0, and you definitely have a CUDA device available, then my guess is it's a build issue.

edit flag offensive delete link more

Comments

If opencv is built without cuda, any cuda function call will throw an error clearly saying that it wasn't built with cuda.

dtmoodie gravatar imagedtmoodie ( 2015-02-02 13:56:32 -0600 )edit

Question Tools

Stats

Asked: 2014-10-23 05:57:35 -0600

Seen: 3,580 times

Last updated: Oct 26 '14