Skip to content

Commit a87785a

Browse files
committed
add: ClArray.map2Inplace, *.map2, refacor: DenseVector.map2
1 parent 572f876 commit a87785a

2 files changed

Lines changed: 16 additions & 31 deletions

File tree

src/GraphBLAS-sharp.Backend/Common/ClArray.fs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ module ClArray =
328328

329329
result
330330

331-
let map2<'a, 'b, 'c> (clContext: ClContext) workGroupSize (map: Expr<'a -> 'b -> 'c>) =
331+
let map2Inplace<'a, 'b, 'c> (clContext: ClContext) workGroupSize (map: Expr<'a -> 'b -> 'c>) =
332332

333333
let kernel =
334334
<@ fun (ndRange: Range1D) length (leftArray: ClArray<'a>) (rightArray: ClArray<'b>) (resultArray: ClArray<'c>) ->
@@ -341,10 +341,7 @@ module ClArray =
341341

342342
let kernel = clContext.Compile kernel
343343

344-
fun (processor: MailboxProcessor<_>) flag (leftArray: ClArray<'a>) (rightArray: ClArray<'b>) ->
345-
346-
let resultArray =
347-
clContext.CreateClArrayWithSpecificAllocationMode(flag, leftArray.Length)
344+
fun (processor: MailboxProcessor<_>) (leftArray: ClArray<'a>) (rightArray: ClArray<'b>) (resultArray: ClArray<'c>) ->
348345

349346
let ndRange = Range1D.CreateValid(resultArray.Length, workGroupSize)
350347

@@ -354,4 +351,12 @@ module ClArray =
354351

355352
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
356353

357-
resultArray
354+
let map2<'a, 'b, 'c> (clContext: ClContext) workGroupSize map =
355+
let map2 = map2Inplace<'a, 'b, 'c> clContext workGroupSize map
356+
357+
fun (processor: MailboxProcessor<_>) allocationMode (leftArray: ClArray<'a>) (rightArray: ClArray<'b>) ->
358+
359+
let resultArray =
360+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, leftArray.Length)
361+
362+
map2 processor leftArray rightArray resultArray

src/GraphBLAS-sharp.Backend/Vector/DenseVector/DenseVector.fs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,26 @@ module DenseVector =
1515
workGroupSize
1616
=
1717

18-
let map2 =
19-
<@ fun (ndRange: Range1D) resultLength (leftVector: ClArray<'a option>) (rightVector: ClArray<'b option>) (resultVector: ClArray<'c option>) ->
20-
21-
let gid = ndRange.GlobalID0
22-
23-
if gid < resultLength then
24-
resultVector.[gid] <- (%opAdd) leftVector.[gid] rightVector.[gid] @>
25-
26-
let kernel = clContext.Compile(map2)
18+
let map2InPlace = ClArray.map2Inplace clContext workGroupSize opAdd
2719

2820
fun (processor: MailboxProcessor<_>) (leftVector: ClArray<'a option>) (rightVector: ClArray<'b option>) (resultVector: ClArray<'c option>) ->
2921

30-
let ndRange =
31-
Range1D.CreateValid(leftVector.Length, workGroupSize)
22+
map2InPlace processor leftVector rightVector resultVector
3223

33-
let kernel = kernel.GetKernel()
34-
35-
processor.Post(
36-
Msg.MsgSetArguments
37-
(fun () -> kernel.KernelFunc ndRange leftVector.Length leftVector rightVector resultVector)
38-
)
39-
40-
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
4124

4225
let map2<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct>
4326
(clContext: ClContext)
4427
(opAdd: Expr<'a option -> 'b option -> 'c option>)
4528
workGroupSize
4629
=
4730

48-
let elementWiseTo =
49-
map2Inplace clContext opAdd workGroupSize
31+
let map2 =
32+
ClArray.map2 clContext workGroupSize opAdd
5033

5134
fun (processor: MailboxProcessor<_>) allocationMode (leftVector: ClArray<'a option>) (rightVector: ClArray<'b option>) ->
52-
let resultVector =
53-
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, leftVector.Length)
5435

55-
elementWiseTo processor leftVector rightVector resultVector
36+
map2 processor allocationMode leftVector rightVector
5637

57-
resultVector
5838

5939
let map2AtLeastOne clContext op workGroupSize =
6040
map2 clContext (Convert.atLeastOneToOption op) workGroupSize

0 commit comments

Comments
 (0)