Skip to content

Commit 53c6715

Browse files
committed
Encapsulate ClCell management inside operation
1 parent ca7b275 commit 53c6715

File tree

5 files changed

+25
-15
lines changed

5 files changed

+25
-15
lines changed

src/GraphBLAS-sharp.Backend/Algorithms/BFS.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module internal BFS =
5050
level <- level + 1
5151

5252
//Assigning new level values
53-
fillSubVectorTo queue levels front (clContext.CreateClCell level)
53+
fillSubVectorTo queue levels front level
5454

5555
//Getting new frontier
5656
spMVInPlace queue matrix front front
@@ -103,7 +103,7 @@ module internal BFS =
103103
level <- level + 1
104104

105105
//Assigning new level values
106-
fillSubVectorTo queue levels front (clContext.CreateClCell level)
106+
fillSubVectorTo queue levels front level
107107

108108
//Getting new frontier
109109
match spMSpV queue matrix front with
@@ -185,7 +185,7 @@ module internal BFS =
185185
level <- level + 1
186186

187187
//Assigning new level values
188-
fillSubVectorInPlace queue levels frontier (clContext.CreateClCell level)
188+
fillSubVectorInPlace queue levels frontier level
189189

190190
match frontier with
191191
| ClVector.Sparse _ ->

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,24 @@ module Vector =
6666

6767
let kernel = clContext.Compile(fillSubVectorKernel)
6868

69-
fun (processor: MailboxProcessor<_>) (leftVector: ClArray<'a option>) (maskVector: ClArray<'b option>) (value: ClCell<'a>) (resultVector: ClArray<'a option>) ->
69+
fun (processor: MailboxProcessor<_>) (leftVector: ClArray<'a option>) (maskVector: ClArray<'b option>) (value: 'a) (resultVector: ClArray<'a option>) ->
7070

7171
let ndRange =
7272
Range1D.CreateValid(leftVector.Length, workGroupSize)
7373

7474
let kernel = kernel.GetKernel()
7575

76+
let valueCell = clContext.CreateClCell(value)
77+
7678
processor.Post(
7779
Msg.MsgSetArguments
78-
(fun () -> kernel.KernelFunc ndRange leftVector.Length leftVector maskVector value resultVector)
80+
(fun () -> kernel.KernelFunc ndRange leftVector.Length leftVector maskVector valueCell resultVector)
7981
)
8082

8183
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
8284

85+
valueCell.Free processor
86+
8387
let assignByMask<'a, 'b when 'a: struct and 'b: struct>
8488
(maskOp: Expr<'a option -> 'b option -> 'a -> 'a option>)
8589
(clContext: ClContext)
@@ -89,7 +93,7 @@ module Vector =
8993
let assignByMask =
9094
assignByMaskInPlace maskOp clContext workGroupSize
9195

92-
fun (processor: MailboxProcessor<_>) allocationMode (leftVector: ClArray<'a option>) (maskVector: ClArray<'b option>) (value: ClCell<'a>) ->
96+
fun (processor: MailboxProcessor<_>) allocationMode (leftVector: ClArray<'a option>) (maskVector: ClArray<'b option>) (value: 'a) ->
9397
let resultVector =
9498
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, leftVector.Length)
9599

@@ -114,13 +118,15 @@ module Vector =
114118

115119
let kernel = clContext.Compile(fillSubVectorKernel)
116120

117-
fun (processor: MailboxProcessor<_>) (leftVector: ClArray<'a option>) (maskVector: Sparse<'b>) (value: ClCell<'a>) (resultVector: ClArray<'a option>) ->
121+
fun (processor: MailboxProcessor<_>) (leftVector: ClArray<'a option>) (maskVector: Sparse<'b>) (value: 'a) (resultVector: ClArray<'a option>) ->
118122

119123
let ndRange =
120124
Range1D.CreateValid(maskVector.NNZ, workGroupSize)
121125

122126
let kernel = kernel.GetKernel()
123127

128+
let valueCell = clContext.CreateClCell(value)
129+
124130
processor.Post(
125131
Msg.MsgSetArguments
126132
(fun () ->
@@ -130,12 +136,14 @@ module Vector =
130136
leftVector
131137
maskVector.Indices
132138
maskVector.Values
133-
value
139+
valueCell
134140
resultVector)
135141
)
136142

137143
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
138144

145+
valueCell.Free processor
146+
139147
let toSparse<'a when 'a: struct> (clContext: ClContext) workGroupSize =
140148

141149
let scatterValues =

src/GraphBLAS-sharp.Backend/Vector/Sparse/Map2.fs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ module internal Map2 =
281281
let setPositions =
282282
Common.setPositions clContext workGroupSize
283283

284-
fun (processor: MailboxProcessor<_>) allocationMode (leftVector: ClVector.Sparse<'a>) (rightVector: ClVector.Sparse<'b>) (value: ClCell<'a>) ->
284+
fun (processor: MailboxProcessor<_>) allocationMode (leftVector: ClVector.Sparse<'a>) (rightVector: ClVector.Sparse<'b>) (value: 'a) ->
285+
286+
let valueCell = clContext.CreateClCell(value)
285287

286288
let bitmap, values, indices =
287289
prepare
@@ -291,11 +293,12 @@ module internal Map2 =
291293
leftVector.Indices
292294
rightVector.Values
293295
rightVector.Indices
294-
value
296+
valueCell
295297

296298
let resultValues, resultIndices =
297299
setPositions processor allocationMode values indices bitmap
298300

301+
processor.Post(Msg.CreateFreeMsg<_>(valueCell))
299302
processor.Post(Msg.CreateFreeMsg<_>(indices))
300303
processor.Post(Msg.CreateFreeMsg<_>(values))
301304
processor.Post(Msg.CreateFreeMsg<_>(bitmap))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ module Vector =
158158
let denseFillVector =
159159
Dense.Vector.assignByMask op clContext workGroupSize
160160

161-
fun (processor: MailboxProcessor<_>) allocationMode (vector: ClVector<'a>) (mask: ClVector<'b>) (value: ClCell<'a>) ->
161+
fun (processor: MailboxProcessor<_>) allocationMode (vector: ClVector<'a>) (mask: ClVector<'b>) (value: 'a) ->
162162
match vector, mask with
163163
| ClVector.Sparse vector, ClVector.Sparse mask ->
164164
ClVector.Sparse
@@ -198,7 +198,7 @@ module Vector =
198198
let assignBySparse =
199199
Dense.Vector.assignBySparseMaskInPlace op clContext workGroupSize
200200

201-
fun (processor: MailboxProcessor<_>) (vector: ClVector<'a>) (mask: ClVector<'b>) (value: ClCell<'a>) ->
201+
fun (processor: MailboxProcessor<_>) (vector: ClVector<'a>) (mask: ClVector<'b>) (value: 'a) ->
202202
match vector, mask with
203203
| ClVector.Dense vector, ClVector.Dense mask -> assignByDense processor vector mask value vector
204204
| ClVector.Dense vector, ClVector.Sparse mask -> assignBySparse processor vector mask value vector

tests/GraphBLAS-sharp.Tests/Backend/Vector/AssignByMask.fs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ let checkResult isZero isComplemented (actual: Vector<'a>) (vector: 'a []) (mask
5151
let makeTest<'a when 'a: struct and 'a: equality>
5252
(isZero: 'a -> bool)
5353
(toDense: MailboxProcessor<_> -> AllocationFlag -> ClVector<'a> -> ClVector<'a>)
54-
(fillVector: MailboxProcessor<Msg> -> AllocationFlag -> ClVector<'a> -> ClVector<'a> -> ClCell<'a> -> ClVector<'a>)
54+
(fillVector: MailboxProcessor<Msg> -> AllocationFlag -> ClVector<'a> -> ClVector<'a> -> 'a -> ClVector<'a>)
5555
isComplemented
5656
case
5757
(vector: 'a [], mask: 'a [], value: 'a)
@@ -72,10 +72,9 @@ let makeTest<'a when 'a: struct and 'a: equality>
7272
let clMaskVector = maskVector.ToDevice context
7373

7474
try
75-
let clValue = context.CreateClCell<'a> value
7675

7776
let clActual =
78-
fillVector q HostInterop clLeftVector clMaskVector clValue
77+
fillVector q HostInterop clLeftVector clMaskVector value
7978

8079
let cooClActual = toDense q HostInterop clActual
8180

0 commit comments

Comments
 (0)