YARP  2.3.70.2
Yet Another Robot Platform
cuda/cuda_gpu.cpp

Example showing how to use the CUDA driver to execute custom code onto nVidia graphics cards (for supported cards, try to search on Google something like "cuda supported gpu")

/*
* Copyright (C) 2007 Giacomo Spigler
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
*/
#include <stdio.h>
#include <stdlib.h>
#include <yarp/sig/all.h>
#include <yarp/os/all.h>
#include <cv.h>
#include <cvaux.h>
#include <highgui.h>
#include <cutil.h> //only needed for timing..
using namespace yarp::os;
using namespace yarp::sig;
using namespace yarp::sig::file;
using namespace yarp::dev;
int main(int argc, char *argv[]) {
//Instantiate the GPU driver
yarp::sig::file::read(img, "../dev/image/img0250.ppm");
// use YARP to create and configure an instance of CUDAGPU
Property config;
// no arguments, use a default
char str[80];
sprintf(str, "(device cuda) (w %d) (h %d) (bpp 3)", img.width(), img.height());
config.fromString(str);
PolyDriver dd(config);
if (!dd.isValid()) {
printf("Failed to create and configure a device\n");
exit(1);
}
IGPUDevice *gpu;
if (!dd.view(gpu)) {
printf("Failed to view device through IGPUDevice interface\n");
exit(1);
}
//Load and execute a program for the GPU
int prog;
if(argc!=2) {
prog = gpu->load("progs/bgr.cubin");
} else {
prog = gpu->load(argv[1]);
}
write(out,"test0.ppm");
unsigned int hTimer;
cutCreateTimer(&hTimer);
cutStartTimer(hTimer);
gpu->execute(prog, &img, &out);
write(out,"test1.ppm");
cutStopTimer(hTimer);
printf("Processing time: %fms\n", cutGetTimerValue(hTimer));
//Show the resulting image onto the screen using OpenCV
IplImage *cvImage = cvCreateImage(cvSize(out.width(), out.height()), IPL_DEPTH_8U, 3);
cvCvtColor((IplImage*)out.getIplImage(), cvImage, CV_RGB2BGR);
cvNamedWindow("CUDA", 1);
cvShowImage("CUDA", cvImage);
cvWaitKey(3000);
cvDestroyWindow("CUDA");
cvReleaseImage(&cvImage);
dd.close();
return 0;
}