Brahma.FSharp.Examples


Brahma.FSharp.Examples

Examples of Brahma.FSharp usage.

Brahma.FSharp

Brahma.FSharp is a library for F# quotations to OpenCL translation.

The Brahma.FSharp library can be installed from NuGet:
PM> Install-Package Brahma.FSharp

Features of Brahma.FSharp:

  • We are aimed to translate native F# code to OpenCL with minimization of different wrappers and custom types.
  • We use OpenCL for communication with GPU. So, you can work not only with NVIDIA hardware but with any device, which supports OpenCL (e.g. with AMD devices).
  • We support tuples and structures.
  • We can use strongly typed kernels from OpenCL code in F#.

Example

This example demonstrates using a function defined in this library. Following function computes the result of the addition of matrix 'mat2' to the matrix 'mat1'

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
let Addition (mat1: array<float>) (mat2: array<float>) rows columns =

    let localWorkSize0 = 2
    let localWorkSize1 = 2
    let platformName = "NVIDIA*"
    let deviceType = DeviceType.Default        
    
    let provider =
        try  ComputeProvider.Create(platformName, deviceType)
        with 
        | ex -> failwith ex.Message
   
    let commandQueue = new CommandQueue(provider, provider.Devices |> Seq.head)    

    let command = 
        <@
            fun (rng:_2D) (a:array<_>) (b:array<_>) (c:array<_>) ->
                let x = rng.GlobalID1
                let y = rng.GlobalID0                
                c.[y * columns + x] <- a.[y * columns + x] + b.[y * columns + x] 
        @>

    let a = mat1
    let b = mat2
    let res = Array.zeroCreate(rows * columns)

    let kernel, kernelPrepare, kernelRun = provider.Compile command
    let d =(new _2D(rows, columns, localWorkSize0, localWorkSize1))
    kernelPrepare d a b res

    let _ = commandQueue.Add(kernelRun())
    let _ = commandQueue.Add(res.ToHost provider).Finish()
    commandQueue.Dispose()
    provider.Dispose()
    provider.CloseAllBuffers()

    res

See more examples here

Samples & documentation

  • Tutorial contains a further explanation of this sample library.

  • API Reference contains automatically generated documentation for all types, modules and functions in the library. This includes additional brief samples on using most of the functions.

Contributing and copyright

The project is hosted on GitHub where you can report issues, fork the project and submit pull requests. If you're adding a new public API, please also consider adding samples that can be turned into a documentation. You might also want to read the library design notes to understand how it works.

The library is available under Public Domain license, which allows modification and redistribution for both commercial and non-commercial purposes. For more information see the License file in the GitHub repository.

Fork me on GitHub