Running and Debugging a Simple Example

(Updated January 30, 2015.)

Prerequisites:

(1) You must have Visual Studio 2013 C++ installed.

(2) Download Campy.NET: Go to the Source tab and select download.

(3) Unpack the ZIP file and set up an environmental variable CAMPYNETROOT (either through the Control Panel, or specific for a command-line shell, like bash) to the path of the Campy.NET directory (using Windows backslash style, not forward slash Unix style).

(4) Download the source from ILSpy (ilspy.net). Unpack the ZIP file in the Campy.NET directory.

(5) Download the download and unpack the binaries for Uncrustify, and place it on your PATH. See http://uncrustify.sourceforge.net/

The Example:

Start Visual Studio 2013, and create a C# CLR Console Application. Open the C# source file, and paste the following code into the file. The example provided is the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using Campy;
using Campy.Types;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            int size = 100000;
            int[] data = new int[size];
            Extent e = new Extent(size);
            Array_View<int> d = new Array_View<int>(ref data);
            AMP.Parallel_For_Each(d.Extent, (Index idx) =>
            {
                int j = idx[0];
                d[j] = size - j - 1; // Capture size and d.
            });
            d.Synchronize();
            for (int i = 0; i < size; ++i)
            {
                System.Console.WriteLine(data[i]);
            }
        }
    }
}

Add Campy.Types.dll and Campy.NET.dll to the references. Compile and run the project. The output should be “99999 … 99998 … 99997 …”. NOTE: IF YOU MAKE CHANGES TO THE CODE AND REBUILD, THE PROGRAM MAY FAIL TO PERFORM A DYNAMIC LINK! YOU MUST STOP THE PROGRAM AND RERUN IT. THIS IS A PROBLEM WITH WINDOWS, AND NO WAY AT THE MOMENT TO BYPASS IT (YOU CANNOT UNLOAD A DLL THAT HAS ALREADY BEEN LOADED). CAMPY WILL RELINK THE DYNAMIC CODE THAT IT BUILDS AND PLACES IN THE DLL UPON PROGRAM START UP!

To debug the kernel, create a C++ CLR application. Open the Property Pages for the project, select Debugging in the left pane. In the right pane, set the Debugger Type to GPU Only. Select Command in the right pane, and enter the path of the C# executable, “…\ConsoleApplication1.exe”. Select Working Directory and set the path for the C# executable. In Visual Studio, open the generated file System_Void_Test_Program___c__DisplayClass1__Main_b__0_Campy_Types_Index__unmanaged.cpp. Set a breakpoint on a line in the kernel, then start a debug session. The program will stop in the kernel.

Screenshot 2015-01-18 09.22.52 Screenshot 2015-01-18 09.24.39

(Note: updated Feb 7, 2015 for API changes.)

Campy.NET – An API for GPGPU Computing for C#

Welcome to Campy.NET! Campy.NET is a library for enabling general purpose GPU computing within the Microsoft Common Language Infrastructure, such as C#. Campy.NET is a API similar to Microsoft’s C++ AMP. The API converts your methods and delegates into kernels via a JIT compiler, which can then be run on a GPU. The source is free and open source.

Campy.NET is written in C#, C++ CLI, and C++ native code, and uses .NET Reflection, ILSpy/Mono.Cecil, Uncrustify, and Microsoft Visual C++, C++ AMP. Unlike CUDA, OpenCL, CUDA.NET, Cudafy, and other solutions for GPGPU programming, users write their programs naturally, without requiring the kernel being defined separately from the calling context, using an anonymous lambda delegate in a AMP.Parallel_For_Each call. When Parallel_For_Each is executed, Campy converts the anonymous lambda delegate into C++ AMP source code, compiles the code, loads the generated DLL, then executes the kernel, performing all the required parameter passing to C++ AMP. Campy retains the generated DLL for future calls to the Parallel_For_Each lambda, regenerating the DLL if it is out of date with respect to the executable program.

Campy.NET is available at http://campynet.codeplex.com/. It is in the preliminary stages of development, and has no documentation. If you would like to help out, please let us know by contacting the administrator for the project at http://campynet.codeplex.com/. To keep up to date, sign up for the newsletter.