Skip to content

Commit 2199287

Browse files
committed
add: matrix intersect
1 parent 483b644 commit 2199287

4 files changed

Lines changed: 106 additions & 2 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Compile Include="Matrix/COO/Map.fs" />
5454
<Compile Include="Matrix/COO/Merge.fs" />
5555
<Compile Include="Matrix/COO/Map2.fs" />
56+
<Compile Include="Matrix/COO/Intersect.fs" />
5657
<Compile Include="Matrix/COO/Matrix.fs" />
5758
<Compile Include="Matrix/CSR/Merge.fs" />
5859
<Compile Include="Matrix/CSR/Map2.fs" />
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
namespace GraphBLAS.FSharp.Backend.Matrix.COO
2+
3+
open Brahma.FSharp
4+
open GraphBLAS.FSharp
5+
open GraphBLAS.FSharp.Objects
6+
open GraphBLAS.FSharp.Backend.Quotes
7+
open GraphBLAS.FSharp.Objects.ClContextExtensions
8+
9+
module internal Intersect =
10+
let findIntersectionByKeys (clContext: ClContext) workGroupSize =
11+
12+
let findIntersection =
13+
<@ fun (ndRange: Range1D) (leftNNZ: int) (rightNNZ: int) (leftRows: ClArray<int>) (leftColumns: ClArray<int>) (rightRows: ClArray<int>) (rightColumns: ClArray<int>) (bitmap: ClArray<int>) ->
14+
15+
let gid = ndRange.GlobalID0
16+
let bitmapSize = min leftNNZ rightNNZ
17+
18+
if gid < bitmapSize then
19+
20+
let row, column, rows, columns =
21+
if rightNNZ >= leftNNZ then
22+
leftRows.[gid], leftColumns.[gid], leftRows, leftColumns
23+
else
24+
rightRows.[gid], rightColumns.[gid], rightRows, rightColumns
25+
26+
let index: uint64 = ((uint64 row) <<< 32) ||| (uint64 column)
27+
28+
let intersect = (%Search.Bin.existsByKey2D) bitmapSize index rows columns
29+
30+
if intersect then
31+
bitmap.[gid] <- 1
32+
else
33+
bitmap.[gid] <- 0 @>
34+
35+
let kernel =
36+
clContext.Compile <| findIntersection
37+
38+
fun (processor: MailboxProcessor<_>) allocationMode (leftMatrix: ClMatrix.COO<'a>) (rightMatrix: ClMatrix.COO<'b>) ->
39+
40+
let bitmapSize = min leftMatrix.NNZ rightMatrix.NNZ
41+
42+
let bitmap = clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, bitmapSize)
43+
44+
let ndRange = Range1D.CreateValid(bitmapSize, workGroupSize)
45+
46+
let kernel = kernel.GetKernel()
47+
48+
processor.Post(
49+
Msg.MsgSetArguments
50+
(fun () ->
51+
kernel.KernelFunc
52+
ndRange
53+
leftMatrix.NNZ
54+
rightMatrix.NNZ
55+
leftMatrix.Rows
56+
leftMatrix.Columns
57+
rightMatrix.Rows
58+
rightMatrix.Columns
59+
bitmap)
60+
)
61+
62+
processor.Post(Msg.CreateRunMsg<_, _> kernel)
63+
64+
bitmap

src/GraphBLAS-sharp.Backend/Matrix/COO/Matrix.fs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ module Matrix =
197197
/// <summary>
198198
/// Transposes the given matrix and returns result as a new matrix.
199199
/// </summary>
200-
///<param name="clContext">OpenCL context.</param>
201-
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
200+
/// <param name="clContext">OpenCL context.</param>
201+
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
202202
let transpose (clContext: ClContext) workGroupSize =
203203

204204
let transposeInPlace = transposeInPlace clContext workGroupSize
@@ -216,3 +216,12 @@ module Matrix =
216216
Columns = copy queue allocationMode matrix.Columns
217217
Values = copyData queue allocationMode matrix.Values }
218218
|> transposeInPlace queue
219+
220+
/// <summary>
221+
/// Build a bitmap. Maps non-zero elements of the matrix with minimum nnz to 1
222+
/// if the second matrix has non zero element under the same row and column pair, otherwise 0.
223+
/// </summary>
224+
/// <param name="clContext">OpenCL context.</param>
225+
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
226+
let findIntersectionByKeys (clContext: ClContext) workGroupSize =
227+
Intersect.findIntersectionByKeys clContext workGroupSize

src/GraphBLAS-sharp.Backend/Quotes/Search.fs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,36 @@ module Search =
9696

9797
result @>
9898

99+
/// <summary>
100+
/// Searches value in array by two keys.
101+
/// In case there is a value at the given keys position, it is returned.
102+
/// </summary>
103+
let existsByKey2D<'a> =
104+
<@ fun length sourceIndex (rowIndices: ClArray<int>) (columnIndices: ClArray<int>) ->
105+
106+
let mutable leftEdge = 0
107+
let mutable rightEdge = length - 1
108+
109+
let mutable result = false
110+
111+
while leftEdge <= rightEdge do
112+
let middleIdx = (leftEdge + rightEdge) / 2
113+
114+
let currentIndex: uint64 =
115+
((uint64 rowIndices.[middleIdx]) <<< 32)
116+
||| (uint64 columnIndices.[middleIdx])
117+
118+
if sourceIndex = currentIndex then
119+
result <- true
120+
121+
rightEdge <- -1 // TODO() break
122+
elif sourceIndex < currentIndex then
123+
rightEdge <- middleIdx - 1
124+
else
125+
leftEdge <- middleIdx + 1
126+
127+
result @>
128+
99129
/// <summary>
100130
/// Find lower position of item in array.
101131
/// </summary>

0 commit comments

Comments
 (0)