Put your features/bug requests here. Read the guidelines on how to post bug reports. Keep new feature requests as specific and informative as possible, badly thought out or phrased requests might get passed over even if they have any merit.
|
||||||
Features/BugsPut your features/bug requests here. Read the guidelines on how to post bug reports. Keep new feature requests as specific and informative as possible, badly thought out or phrased requests might get passed over even if they have any merit. 6 comments to Features/BugsLeave a Reply |
||||||
|
Copyright © 2012 Ananth's very own website - All Rights Reserved |
||||||
Ananth,
Can’t wait till the OpenCL Brahma is available! Had a feature question.
I’m wondering about the choice to use Queries for Brahma. I’ve read your complaints about lack of multi-line lambdas and the limitations on the if statement syntax. Is it not possible to use delegates as the base structure? Using delegates might allow for a more natural coding style for Brahma’s users.
Below is “Your First Brahma Program” rewritten as if Brahma used delegates instead of queries.
// Create the computation provider
var gpu = new GPUComputer();
// Create a data-parallel array and fill it with data
var data = new Buffer(gpu, new[]
{0f, 1f, 2f, 3f, 4f, 5f, 6f} );
// Compile the delegate
//methods 1, 2, and 3 do the same thing
var method1 = gpu.Compile(
(val) => { return val * 2f; }
);
var method2 = gpu.Compile( val => val * 2f );
var method3 = gpu.Compile(TestMethod);
// Run the delegate on this data
var result = gpu.Run(method1, data);
// Print out the results
foreach (float value in result)
Console.WriteLine(value);
// Get rid of all the stuff we created
data.Dispose();
result.Dispose();
gpu.Dispose();
Why is this not possible, or why did you choose against it? If it is possible, it is my feature request – I would find Brahma easier to use if I could just pass it a delegate. I hope to include Brahma as part of a plug-in framework, so the easier it is to use, the more likely developers create plug-ins for my application.
Thanks,
Benjamin Popp
Hello, Benjamin. Brahma transforms an Expression tree provided by the C# compiler to target shader/kernel code. It is not possible to use delegates because those are already compiled to IL, and I don’t reverse engineer IL to generate target code (I did have an earlier version that did this, but I moved away from this method for several reasons).
The Compile call as you’ve written it won’t even compile because it expects an Expression<Func>. When the compiler sees the Expression type on a parameter, it tries to generate an expression tree of the given argument which will fail because:
“The C# and Visual Basic compilers can generate expression trees only from expression lambdas (or single-line lambdas). It cannot parse statement lambdas (or multi-line lambdas). For more information about lambda expressions in C#, see Lambda Expressions (C# Programming Guide); for Visual Basic, see Lambda Expressions (Visual Basic).” from here.
Related to what you’re saying, every query IS a LambdaExpression (just one with a single line). I just use the query syntax to represent more complicated constructs like intermediate results (let), sub-queries (sorta like sub-methods) and nested loops (SelectMany). You can use the extension method syntax (.Select(…)) and everything will still work!
That was the long. The short is that what you’re asking for is currently not possible but should be with the next version of C#.
Just wanted to share my love for the ‘delegates instead of queries’ concept as well. Thanks Ananth for the detailed response on why this isn’t possible just yet.
What in the next version of C# do you expect will change this?
The idea of being able to run a nice clean command like the following very much excites me…
gpu.exec(computationFunction, inputParamArray, outputArray, onChangeFunction)Where onChangeFunction is executed after each instance of computationFunction exits (with or without exception) so that aggregation and intermediate cancellation can be done. Maybe I’m dreaming.
I am currently working on something very close to this functionality. Using a custom tool that uses Roslyn to transform code that has a CS0834 error into raw source-level expression trees that are then passed in directly to Brahma. Since this translation happens at compile (custom-tool) run time, I am even able to catch and report #errors that Brahma cannot transform to OpenCL. Unfortunately, the work isn’t going as fast as I’d like. I hope to have something soon, though – so stay tuned!
Hey Ananth,
first of all: I’m very impressed of your work. A year ago I already did some testing with your lib and liked it very much. I’m glad to see that it continues to get better.
I’m currently developing a component that heavily relies on
IQueryableandIQbservableto do some data mining, calculations and analysis on different kinds of data. I would love to integrate the possibility to offload some work on the GPU there.My dream would be to have a simple
.OnGPU()extension method forIQueryableandIQbservablethat automatically offloads all following work to the GPU. Is that possible? I’m not very deep into the current codebase, I just had a quick look, therefore I’m not sure how to start with that or if there are any constraints that makes my idea senseless.Best regards,
Patrick
Thank you for the kind words! At this time, getting something to run on the GPU consists of two bits
1. Writing and compiling a “query” (in the current release, LINQ lambda only – but a Roslyn powered regular method coming soon).
2. Running this “compiled query” with real arguments in a command queue, with synchronization markers.
If you can perform the “compile” step ahead of time, you can execute the compiled query as many times as you want in the “OnGPU” method (and pass in the right arguments) so the kernel runs on the GPU and returns its results to you. I would recommend you start looking at the code samples in the source to understand the process I’ve outlined above. You can then move the appropriate pieces to the methods/classes you want.