This project is read-only.

How To Pass Any "Type-parameter" to a Kernel Using ManagedCUDA C# ?

Nov 4, 2016 at 3:36 PM

Today I'm looking for some explaination/informations.

I've got an application in C# and I also have a CUDA C/C++ program. But what I wanted to do know is to launch the CUDA C/C++ program from this C#application. So I heard about managedCUDA and did not find that much informations. So I'm here to figure out how to proceed.

I'll explain, I followed this page :

And I've made something like this :

```using ManagedCuda;
using ManagedCuda.BasicTypes;
using System;
using System.Linq;

namespace Cs_cuda_link
public struct Cartesian
    public double x;
    public double y;
    public double z;
    public Cartesian(double a, double b, double c) { x = a; y = b; z = c; }
class Program
    static CudaKernel kernel;

    static void InitKernels()
        CudaContext cntxt = new CudaContext();
        CUmodule cumodule = cntxt.LoadModule(@"C:\Users\stage\Documents\Visual Studio 2013\Projects\Cs_link_test\Cs_link_test\x64\Release\kernel.ptx");
        kernel = new CudaKernel("kernel", cumodule, cntxt);
    static Func<Cartesian, Cartesian, Cartesian> cudakernel = (a, b) =>
        CudaDeviceVariable<Cartesian> result_dev = new CudaDeviceVariable<Cartesian>(1);
        Cartesian result_host;
        kernel.Run(a, b, result_dev.DevicePointer);
        result_dev.CopyToHost(ref result_host);
        return result_host;
    static void Main(string[] args)
        Cartesian alpha = new Cartesian(1, 2, 3);
        Cartesian beta = new Cartesian(3, 2, 1);

        Console.WriteLine(cudakernel(alpha, beta));
}``` I'm not really familiar with C#...

So I've this Kernel code :
#include "cuda_runtime.h"
#include <stdio.h>

class Cartesian
    double x;
    double y;
    double z;

extern "C"
    __global__ void kernel(Cartesian a, Cartesian b, Cartesian *c)
        c->x = a.x + b.x;
        c->y = a.y + b.y;
        c->z = a.z + b.z;

int main()
    return 0;
And I wanted to know, how can I compile without error... I got an error with line :
kernel.Run(a, b, result_dev.DevicePointer);
result_dev.CopyToHost(ref result_host);

because of "Use of unassigned local variable 'result_*'

So how to proceed ?

And an other thing. If I have a kernel that takes <Class*, Class*, Class, double> How should I do ?
If someone have documentation or anything about managedCUDA it'll be really cool !!