Skip to content

CUDAfy.NET access to work with Visual Studio 2019 and the latest NVIDIA Toolkit CUDA 10.2 library

License

Notifications You must be signed in to change notification settings

bolcman/CUDAfy.NET

 
 

Repository files navigation

Logo

CUDAfy.NET
CUDA 10.2 & Visual Studio 2019 & NET.Framework 4.8

CUDAfy.NET access to work with Visual Studio 2019 and the latest NVIDIA Toolkit CUDA 10.2 library
I was helped by what Cr33zz did in the library's processing at VS 2017.

What is CUDAfy?

CUDAfy .NET allows easy development of high performance GPGPU applications completely from the Microsoft .NET framework. It's developed in C#.
Modern graphics cards provide the potential of massive speed increase over CPUs for non-graphics related intensive numeric operations. Many large data set operations such as matrices can see a 100x or more speed up. CUDAfy allows .NET developers to easily create complex applications that split processing cleanly between host and GPU. There are no separate CUDA cu files or complex set-up procedures to launch GPU device functions. It follows the CUDA programming model and any knowledge gained from tutorials or books on CUDA can be easily transferred to CUDAfy, only in a clean .NET fashion.

How to start with CUDAfy?

Required components

  • x64 Windows or Linux
  • Visual Studio 2019
  • MSVC v142 x64 / 86 build tools (v.14.24) or higher
  • .NET Framework 4.8 SDK

Launching

  1. Download the latest repository
  2. Open the Cudafy.sln project
  3. Choose the "Debug" solution
  4. Choose the "x64" solution platforms
  5. Choose the "CudafyByExample" startup project
  6. Rebuild whole project
  7. Start "CudafyByExample"

What works?

  • The library starts correctly in the .NET Framework 4.8
  • The library works correctly (for my knowledge) with NVIDIA Toolkit CUDA 10.2
  • The library works correctly with Visual Studio 2019 Enterprise 16.5.3
  • Everything starts correctly in the 64-bit version.

What's new?

  • Automatic support for versions 10.2 and 10.1.
  • A new way to detect Visual Studio locations with the MSVC package
  • Corrections in the code of some test programs
  • Almost complete removal of x86 support
  • Removal of unnecessary Solutions
  • Some new errors to display

Where can I find CUDA Toolkit?

You can download the latest CUDA version from the official NVIDIA website NVIDIA CUDA Toolkit

Example program

Example program

Here is an example program you can run using compiled libraries.

class Program
{
    public const int N = 10;
    public static void Main()
    {
        Console.WriteLine("CUDAfy Example\nCollecting necessary resources...");

        CudafyModes.Target = eGPUType.Cuda; // To use OpenCL, change this enum
        CudafyModes.DeviceId = 0;
        CudafyTranslator.Language = CudafyModes.Target == eGPUType.OpenCL ? eLanguage.OpenCL : eLanguage.Cuda;

        //Check for available devices
        if (CudafyHost.GetDeviceCount(CudafyModes.Target) == 0)
            throw new System.ArgumentException("No suitable devices found.", "original");

        //Init device
        GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId);
        Console.WriteLine("Running example using {0}", gpu.GetDeviceProperties(false).Name);

        //Load module for GPU
        CudafyModule km = CudafyTranslator.Cudafy();
        gpu.LoadModule(km);

        //Define local arrays
        int[] a = new int[N];
        int[] b = new int[N];
        int[] c = new int[N];

        // allocate the memory on the GPU
        int[] dev_c = gpu.Allocate<int>(c);

        // fill the arrays 'a' and 'b' on the CPU
        for (int i = 0; i < N; i++)
        {
            a[i] = i;
            b[i] = i * i;
        }

        // copy the arrays 'a' and 'b' to the GPU
        int[] dev_a = gpu.CopyToDevice(a);
        int[] dev_b = gpu.CopyToDevice(b);

        gpu.Launch(1, N).add(dev_a, dev_b, dev_c);

        // copy the array 'c' back from the GPU to the CPU
        gpu.CopyFromDevice(dev_c, c);

        // display the results
        for (int i = 0; i < N; i++)
            Console.WriteLine("{0} + {1} = {2}", a[i], b[i], c[i]);

        // free the memory allocated on the GPU
        gpu.FreeAll();

        Console.WriteLine("Done!");
        Console.ReadKey();
    }

    [Cudafy]
    public static void add(GThread thread, int[] a, int[] b, int[] c)
    {
        int tid = thread.threadIdx.x;
        if (tid < N)
            c[tid] = a[tid] + b[tid];
    }
}

ATTENTION

Cudafy.NET is created by HYBRIDDSP under LGPL v2.1 License. I only used sources on the Internet and searched the files to adapt them to the latest version of CUDA 10.2 and .NET Framework 4.8

I am not the creator of this library, but only a fan who wants to help in using CUDA in newer versions.

Original CUDAfy 1.29 repository on CodePlex Archive

Cudafy.NET can be downloaded from CodePlex

Copyright

The LGPL v2.1 License applies to CUDAfy .NET. If you wish to modify the code then changes should be re-submitted to Hybrid DSP. If you wish to incorporate Cudafy.NET into your own application instead of redistributing the dll's then please consider a commerical license. Visit http://www.hybriddsp.com. This will also provide you with priority support and contribute to on-going development.

The following libraries are made use of: The MIT license applies to ILSpy, NRefactory and ICSharpCode.Decompiler (Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop team). Mono.Cecil also uses the MIT license (Copyright JB Evain). CUDA.NET is a free for use license (Copyright Company for Advanced Supercomputing Solutions Ltd)

About

CUDAfy.NET access to work with Visual Studio 2019 and the latest NVIDIA Toolkit CUDA 10.2 library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 98.1%
  • C++ 1.7%
  • Other 0.2%