45

I've been looking all over but have had little luck. Are there any well documented .NET binding implementations for OpenCL? (I'd take something for CUDA if I had to). I've run into a variety of implementations, CUDA.NET, OpenCL.NET, OpenTK / Cloo (I know, they are mentioned often of stackoverflow), but they all seem to either be in alpha stage or have absolutely no available examples. CUDA.NET has some help files, but it's just a library reference, which doesn't really help you get started.

What I'm hoping to find is a mature library for GPU programming in .NET. Eventually I need to be able to write the code in F#, but I would take any .NET compliant language as I could always just convert it later and use whatever examples are included to get up and running.

Probably a long shot since I've searched all over, but I'm hoping this is just one of those case where I don't know the right thing to search for.

Any help would be greatly appreciated.

talonmies
  • 70,661
  • 34
  • 192
  • 269
Adam Haile
  • 30,705
  • 58
  • 191
  • 286
  • Why do you need an "OpenCL in .NET" example? Any OpenCL example should work fine, as long as you "use" the correct namespace first. – Ben Voigt Apr 26 '11 at 20:06
  • For anyone from the future, I have made an OpenCL.Net fork for .NET Core: https://github.com/FracturedCodes/OpenCL.NetCore – Gabe Sep 21 '20 at 12:27

7 Answers7

34

Well, all libraries you've stated are simple wrappers for opencl native libraries. They pose relatively small amount of additional abstractions and are very close to general opencl functions. So if you are familiar with opencl in general you will get familiar with these libraries in no time.

I think the 'OpenCL.NET' implementation is complete, it is free of anything that is not OpenCL. But after using it several times I've found it too low level.

I've created my own wrapper it serves me good job by simplifying the host part dramatically here's the host part of one of my projects (if you are interested I can publish my OpenCl wrapper in github or any other svn service):

using System;
using System.Net;
using System.Collections.Generic;
using System.IO;

using Shared;
using Shared.IO;
using Shared.OpenCL;

namespace Testing
{
    public class ApplicationClass
    {
        static Random rand = new Random();

        static Single[] RandomArray(Int32 length)
        {
            Single[] result = new Single[length];

            for (int i = 0; i < result.Length; i++)
            {
                result[i] = (Single)rand.NextDouble();
            }

            return result;
        }

        static void Main(string[] args)
        {
            DeviceGlobalMemory output = new Byte[4096];

            DeviceGlobalMemory indeces = RandomArray(102400);
            DeviceGlobalMemory ops = new Byte[3072];
            DeviceGlobalMemory data = RandomArray(1048576);

            Console.Write("Creating kernel...");

            Kernel kernel = Kernel.Create("Kernel", File.ReadAllText("Test.c"), data, indeces, ops, output);

            Console.Write("Executing kernel...");

            Event e = kernel.Execute(256, 256);

            kernel.CommandQueue.Finish();

            Console.WriteLine("done, operation took {0}", Profiler.DurationSeconds(e));

            UnmanagedReader reader = new UnmanagedReader(new DeviceBufferStream(output));

            for (int i = 0; i < 256; i++)
            {
                if (i % 4 == 0) Console.WriteLine();
                if (i % 16 == 0) Console.WriteLine();

                Console.Write("{0}\t", reader.Read<Single>());
            }
        }
    }
}
steve cook
  • 3,116
  • 3
  • 30
  • 51
Lu4
  • 14,873
  • 15
  • 79
  • 132
  • I'd love to get a copy of your wrapper. Could you send it to me (address available on request) or post it somewhere? – 3Dave May 04 '11 at 15:22
  • 3
    Here's the project link http://code.google.com/p/manocl both svn and git are supported git repo path: ssh://gitslave.xp-dev.com/ManOCL – Lu4 May 05 '11 at 21:06
  • 1
    In case someone is interested in image processing with OpenCL, you can use the code provided here: http://stackoverflow.com/questions/13624016/image-processing-with-opencl-net – Ilya Suzdalnitski Nov 29 '12 at 12:22
  • 5
    What do mean by *"OpenCL.NET not being scalable enough"*? Do you mean it cannot handle large amounts of work? Or not multi-thread safe? – Ian Boyd Mar 30 '13 at 15:19
  • Probably not the best term to use, I meant that when your application starts growing you end up with allot of mess in your project... – Lu4 Feb 14 '14 at 21:34
14

There's another bindings library from OpenTK http://sourceforge.net/projects/cloo/.

They position it as "It provides a much cleaner OpenCL API compared to raw bindings (which are also available in case you need them)" and "Cloo is an open source, easy to use, managed library which enables .NET/Mono applications to take full advantage of the OpenCL framework."

The samples look very promising.

olegz
  • 1,189
  • 10
  • 20
  • I can't find any examples nor documentation on the SourceForge site you linked, did I overlook it? – M. Mimpen Feb 25 '14 at 14:14
  • 1
    See the files in downloaded archive. Clootils contains various samples. See Documentation folder for reference. – olegz Mar 03 '14 at 17:10
  • 1
    Nice, it even has a NuGet package, just search for "Cloo" (the exact package Id is "Sourceforge.Cloo"). – BrainSlugs83 May 04 '14 at 10:22
8

I also have a set of open source OpenCL bindings for. NET. I wrote this for myself primarily so it would mirror the OpenCL API faithfully while being .NET friendly. There will never be any OOP abstractions for this library, and it's quite performant.

http://openclnet.codeplex.com

Hope this helps.

Ani
  • 10,826
  • 3
  • 27
  • 46
3

I have used some different C# OpenCL wrappers over the years and am using NOpenCL now. It is well written and is under the MIT License.

https://github.com/tunnelvisionlabs/NOpenCL

SunsetQuest
  • 8,041
  • 2
  • 47
  • 42
1

I have been using OpenCLTemplate.

You can find a great tutorial here: http://www.cmsoft.com.br/index.php?option=com_content&view=category&layout=blog&id=41&Itemid=75

It worked fine for me. I suggest you should check out Cloo also.

Attila
  • 3,206
  • 2
  • 31
  • 44
1

for F# i found this one: https://github.com/GabrieleCocco/FSCL.Compiler (FSharp to OpenCL Compiler)

iBoy
  • 423
  • 3
  • 9
1

I agree with olegz answer. I've only started using OpenCL but Cloo seems to be a very good choice. I find it quite straightforward to use and from many sources I hear that it's also very rich in functionality.

Check out the following site which also includes some examples for Cloo: http://www.cmsoft.com.br/

  • 1
    IMO the examples are not straight-forward to find thus here is a deep link: http://www.cmsoft.com.br/index.php?option=com_content&view=category&layout=blog&id=41&Itemid=75 – M. Mimpen Feb 25 '14 at 14:18