This is a mirror of official site: http://jasper-net.blogspot.com/

CUDA integration with C#

| Sunday, November 28, 2010
This article will focus on how to create an unmanaged dll with CUDA code and use it in a C# program. The example will show some differences between execution times of managed, unmanaged and new .NET 4 parallel versions of for() loops used to do computations on arrays.

I will show in brief how to configure CUDA environment and run example program. CUDA itself is out of scope for this article because of some great online documents, about which I have added links to. There will be a few words only regarding efficiency and execution speed of CUDA kernels and memory management.

CUDA enabled hardware and .NET 4 (Visual Studio 2010 IDE or C# Express 2010) is needed to successfully run the example code. Visual C++ Express 2008 has been used as a CUDA C editor (2010 version has changed custom build rules feature and cannot work with that provided by CUDA SDK for easy VS integration).

Part 1: Environment and tools configuration for CUDA

CUDA is a general purpose parallel computing architecture introduced by NVIDIA. CUDA programs (kernels) run on GPU instead of CPU for better performance (hundreds of cores that can collectively run thousands of computing threads). It comes with a software environment that allows developers to use C as a high-level programming language. This computation technology is used in mathematics, science, finance, modeling, image processing and so on.

Basic CUDA configuration for developing purpose:

  • Download and install CUDA toolkit for correct OS, use version 3.1 or latest 3.2RC or version 3.0 for device emulation (limited usage but works without CUDA enabled device, not supported after version 3.0)
  • Download and install SDK for the same OS and toolkit version
  • Update display drivers if you encounter problems with SDK installation

Visual C++ Express 2008 (or VS2008) configuration:

1. Syntax coloring

  • Open Tools -> Options from main window, then Text Editor -> File Extension, add .cu and .cuh extensions with Microsoft Visual C++ Editor
  • Copy file usertype.dat from [sdk dir]\C\doc\syntax_highlighting\visual_studio_8\ to Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ folder Restart Visual Studio

2. New project, 32bit Windows XP version

  • Default SDK location is c:\Documents and Settings\All Users\Application Data\NVIDIA Corporation\NVIDIA GPU Computing SDK\
  • Default toolkit location is C:\CUDA\
Create empty Win32 console application and add source file with .cu extension
Select newly created project in the Solution Explorer window and then right mouse key ï‚® Custom Build Rules, use Find Existing button to locate Cuda.rules file in [sdk dir] \C\common\ folder, add it and mark on the list of available rule files

Select again project -> Properties, select Release Configuration and then from the tree view:

- Configuration Properties -> Linker -> General -> Additional Library Directories:
add this line (default folders) C:\CUDA\lib;"C:\Documents and Settings\All Users\Application Data\NVIDIA Corporation\NVIDIA GPU Computing SDK\C\common\lib"

Configuration Properties -> Linker -> Input -> Additional Dependencies: cudart.lib
Use the same settings for Debug Configuration
For emulator configurations use Configuration Manager to add new configurations based on Release and Debug ones, name them as EMU-Release and EMU-Debug (or as you wish)

Read more: C# Corner

Posted via email from .NET Info

0 comments: