Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hello-world for CUDA.Net

Tags:

cuda.net

I'm trying to write a childish app with CUDA.Net, but I'm out of luck.

I've figured out to:

using GASS.CUDA;

// ...

var c = new CUDA();

// c.Launch(myfunc); // ???? how ???

myfunc apparently should be of type GASS.CUDA.Types.CUfunction but I didn't find how to define one.

like image 788
bohdan_trotsenko Avatar asked Jan 04 '11 16:01

bohdan_trotsenko


People also ask

What is a typical CUDA Hello world application?

Since CUDA introduces extensions to C and is not it’s own language, the typical Hello World application would be identical to C’s but wouldn’t provide any insight into using CUDA. Here is my attempt to produce Hello World while actually showcasing the basic common features of a CUDA kernel. Enjoy

How many lines of code is Cuda's Hello World?

At 30 lines of code (44 with comments and blank lines), and a single-line kernel, this is both simple, relevant and can be called a real "Hello World!". // This is the REAL "hello world" for CUDA!

What is CUDA Runtime API and how to use it?

We will use CUDA runtime API throughout this tutorial. CUDA is a platform and programming model for CUDA-enabled GPUs. The platform exposes GPUs for general purpose computing. CUDA provides C/C++ language extension and APIs for programming and managing GPUs. In CUDA programming, both CPUs and GPUs are used for computing.

What is the CUDA platform?

The platform exposes GPUs for general purpose computing. CUDA provides C/C++ language extension and APIs for programming and managing GPUs. In CUDA programming, both CPUs and GPUs are used for computing. Typically, we refer to CPU and GPU system as host and device, respectively. CPUs and GPUs are separated platforms with their own memory space.


2 Answers

First you need a .cu file with your kernel (function to be executed on a GPU). Let's have a file mykernel.cu:

extern "C" 
__global__ void fooFunction(float4* data) {
    // there can be some CUDA code ...
}

This have to be compiled into a .cubin file with the nvcc compiler. In order to let the compiler know of the Visual C++ compiler, you need to call it from within the Visual Studio Command Prompt:

nvcc mykernel.cu --cubin

This creates the mykernel.cubin file in the same directory.

Then in a C# code you can load this binary module and execute the kernel. In the higher-level object API of GASS.CUDA it can look like this:

using GASS.CUDA;

// ...

CUDA cuda = new CUDA(true);
// select first available device (GPU)
cuda.CreateContext(0);
// load binary kernel module (eg. relative to from bin/Debug/)
CUmodule module = cuda.LoadModule("../../mykernel.cubin");
// select function from the module
CUfunction function = cuda.GetModuleFunction(module, "fooFunction");
// execute the function fooFunction() on a GPU
cuda.Launch(function);

That's it!

The nvcc compiler should be called as a build action better than calling it by hand. If anyone knows how to accomplish that, please let us know.

like image 153
Bohumir Zamecnik Avatar answered Nov 20 '22 12:11

Bohumir Zamecnik


Unfortunately CUDA.net is very badly documented, but http://www.hoopoe-cloud.com/files/cuda.net/2.0/CUDA.NET_2.0.pdf should help you get started. Furthermore you still need to write your kernel in CUDA C, so http://developer.download.nvidia.com/compute/cuda/3_2_prod/toolkit/docs/CUDA_C_Programming_Guide.pdf will be a good idea to have a look at as well, and perhaps try to start with a start CUDA C application before porting it to CUDA.net.

like image 30
zpon Avatar answered Nov 20 '22 12:11

zpon