Skip to content

Commit d55d34f

Browse files
committed
add: Gather
1 parent b9ef287 commit d55d34f

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace GraphBLAS.FSharp.Backend.Common.Gather
2+
3+
open Brahma.FSharp
4+
5+
module internal Gather =
6+
/// <summary>
7+
/// Creates a new array obtained from positions replaced with values from the given array at these positions (indices).
8+
/// </summary>
9+
/// <example>
10+
/// <code>
11+
/// let positions = [| 2; 0; 2; 1 |]
12+
/// let array = [| 1.4; 2.5; 3.6 |]
13+
/// ...
14+
/// > val result = [| 3.6; 1.4; 3.6; 2.5 |]
15+
/// </code>
16+
/// </example>
17+
let run (clContext: ClContext) workGroupSize =
18+
19+
let gather =
20+
<@ fun (ndRange: Range1D) (positions: ClArray<int>) (inputArray: ClArray<'a>) (outputArray: ClArray<'a>) (size: int) ->
21+
22+
let i = ndRange.GlobalID0
23+
24+
if i < size then
25+
outputArray.[i] <- inputArray.[positions.[i]] @>
26+
27+
let program = clContext.Compile(gather)
28+
29+
fun (processor: MailboxProcessor<_>) (positions: ClArray<int>) (inputArray: ClArray<'a>) (outputArray: ClArray<'a>) ->
30+
31+
let size = outputArray.Length
32+
33+
let kernel = program.GetKernel()
34+
35+
let ndRange = Range1D.CreateValid(size, workGroupSize)
36+
37+
processor.Post(
38+
Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange positions inputArray outputArray size)
39+
)
40+
41+
processor.Post(Msg.CreateRunMsg<_, _>(kernel))

src/GraphBLAS-sharp.Backend/GraphBLAS-sharp.Backend.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<Compile Include="Common/PrefixSum.fs" />
3333
<Compile Include="Common/ClArray.fs" />
3434
<Compile Include="Common/BitonicSort.fs" />
35+
<Compile Include="Common\Gather.fs" />
3536
<Compile Include="Predefined/PrefixSum.fs" />
3637
<!--Compile Include="Matrices.fs" /-->
3738
<Compile Include="Matrix/Common.fs" />

0 commit comments

Comments
 (0)