Created
October 7, 2023 05:59
-
-
Save kant2002/3db984c7e2914138fbc595430b83b744 to your computer and use it in GitHub Desktop.
Trivial ILGPU sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "nuget: ILGPU" | |
open ILGPU | |
open ILGPU.Runtime | |
let ShiftBy42 (index: Index1D) (buffer: ArrayView1D<int, Stride1D.Dense>) (constant: int) = | |
buffer.[index] <- int(index) + constant | |
// Create main context | |
use context = Context.CreateDefault() | |
// For each available device... | |
for device in context do | |
// Create accelerator for the given device | |
use accelerator = device.CreateAccelerator(context) | |
printfn "Performing operations on %O" accelerator | |
use stream = accelerator.CreateStream() | |
let kernel = accelerator.LoadAutoGroupedStreamKernel<_, _, _>(ShiftBy42) | |
use buffer = accelerator.Allocate1D<_>(1024L) | |
kernel.Invoke(Index1D(int(buffer.Length)), buffer.View, 42) | |
// Calls stream.Synchronize() to ensure | |
// that the kernel and memory copy are completed first. | |
accelerator.Synchronize(); | |
// Reads data from the GPU buffer into a new CPU array. | |
buffer.GetAsArray1D(stream) | |
|> Array.iteri (fun index element -> printfn $"{index} = {element}") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment