-
Notifications
You must be signed in to change notification settings - Fork 6
SpMV #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
SpMV #49
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
26ad23b
BoolSum now supports dense vectors
kirillgarbar 0ea4d0f
SpMV
kirillgarbar 3abd3d4
SpMV tests
kirillgarbar 4190289
Merge remote-tracking branch 'YaccConstructor/net5' into spmv
kirillgarbar ef998aa
Merge fix
kirillgarbar 0c4add8
Methods to compute local array size and SpMV vars names
kirillgarbar 0352783
Old SpMV removed
kirillgarbar 58e8a38
Reduced code duplication in tests, new helpers modules
kirillgarbar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| namespace GraphBLAS.FSharp.Backend | ||
|
|
||
| open Brahma.FSharp | ||
| open GraphBLAS.FSharp.Backend | ||
| open GraphBLAS.FSharp.Backend.ArraysExtensions | ||
| open GraphBLAS.FSharp.Backend.Common | ||
| open Microsoft.FSharp.Quotations | ||
|
|
||
| module Vector = | ||
| let spMV | ||
| (clContext: ClContext) | ||
| (add: Expr<'c option -> 'c option -> 'c option>) | ||
| (mul: Expr<'a option -> 'b option -> 'c option>) | ||
| workGroupSize | ||
| = | ||
| //Until LocalMemSize added to ClDevice as member | ||
| let localMemorySize = Utils.getLocalMemorySize clContext | ||
|
|
||
| let localPointersArraySize = workGroupSize + 1 | ||
|
|
||
| let localMemoryLeft = | ||
| localMemorySize | ||
| - localPointersArraySize * sizeof<int> | ||
|
|
||
| let localValuesArraySize = | ||
| Utils.getClArrayOfOptionTypeSize localMemoryLeft | ||
|
|
||
| let multiplyValues = | ||
| <@ fun (ndRange: Range1D) matrixLength (matrixColumns: ClArray<int>) (matrixValues: ClArray<'a>) (vectorValues: ClArray<'b option>) (intermediateArray: ClArray<'c option>) -> | ||
|
|
||
| let i = ndRange.GlobalID0 | ||
| let value = matrixValues.[i] | ||
| let column = matrixColumns.[i] | ||
|
|
||
| if i < matrixLength then | ||
| intermediateArray.[i] <- (%mul) (Some value) vectorValues.[column] @> | ||
|
|
||
| let reduceValuesByRows = | ||
| <@ fun (ndRange: Range1D) (numberOfRows: int) (intermediateArray: ClArray<'c option>) (matrixPtr: ClArray<int>) (outputVector: ClArray<'c option>) -> | ||
|
|
||
| let gid = ndRange.GlobalID0 | ||
| let lid = ndRange.LocalID0 | ||
|
|
||
| if gid <= numberOfRows then | ||
| let threadsPerBlock = | ||
| min (numberOfRows - gid + lid) workGroupSize //If number of rows left is lesser than number of threads in a block | ||
|
|
||
| let localPtr = localArray<int> localPointersArraySize | ||
| localPtr.[lid] <- matrixPtr.[gid] | ||
|
|
||
| if lid = 0 then | ||
| localPtr.[threadsPerBlock] <- matrixPtr.[gid + threadsPerBlock] | ||
|
|
||
| barrierLocal () | ||
|
|
||
| let localValues = | ||
| localArray<'c option> localValuesArraySize | ||
|
|
||
| let workEnd = localPtr.[threadsPerBlock] | ||
| let mutable blockLowerBound = localPtr.[0] | ||
| let numberOfBlocksFitting = localValuesArraySize / threadsPerBlock | ||
| let workPerIteration = threadsPerBlock * numberOfBlocksFitting | ||
|
|
||
| let mutable sum: 'c option = None | ||
|
|
||
| while blockLowerBound < workEnd do | ||
| let mutable index = blockLowerBound + lid | ||
|
|
||
| barrierLocal () | ||
| //Loading values to the local memory | ||
| for block in 0 .. numberOfBlocksFitting - 1 do | ||
| if index < workEnd then | ||
| localValues.[lid + block * threadsPerBlock] <- intermediateArray.[index] | ||
| index <- index + threadsPerBlock | ||
|
|
||
| barrierLocal () | ||
| //Reduction | ||
| //Check if any part of the row is loaded into local memory on this iteration | ||
| if (localPtr.[lid + 1] > blockLowerBound | ||
| && localPtr.[lid] < blockLowerBound + workPerIteration) then | ||
| let rowStart = max (localPtr.[lid] - blockLowerBound) 0 | ||
|
|
||
| let rowEnd = | ||
| min (localPtr.[lid + 1] - blockLowerBound) workPerIteration | ||
|
|
||
| for j in rowStart .. rowEnd - 1 do | ||
| let newSum = (%add) sum localValues.[j] //For some reason sum <- (%add) ... causes Brahma exception | ||
| sum <- newSum | ||
|
|
||
| blockLowerBound <- blockLowerBound + workPerIteration | ||
|
|
||
| if gid < numberOfRows then | ||
| outputVector.[gid] <- sum @> | ||
|
|
||
| let multiplyValues = clContext.Compile multiplyValues | ||
| let reduceValuesByRows = clContext.Compile reduceValuesByRows | ||
|
|
||
| fun (queue: MailboxProcessor<_>) (matrix: CSRMatrix<'a>) (vector: ClArray<'b option>) -> | ||
|
|
||
| let matrixLength = matrix.Values.Length | ||
|
|
||
| let ndRange1 = | ||
| Range1D.CreateValid(matrixLength, workGroupSize) | ||
|
|
||
| let ndRange2 = | ||
| Range1D.CreateValid(matrix.RowCount, workGroupSize) | ||
|
|
||
| let intermediateArray = | ||
| clContext.CreateClArray<'c option>( | ||
| matrixLength, | ||
| deviceAccessMode = DeviceAccessMode.ReadWrite, | ||
| hostAccessMode = HostAccessMode.NotAccessible, | ||
| allocationMode = AllocationMode.Default | ||
| ) | ||
|
|
||
| let multiplyValues = multiplyValues.GetKernel() | ||
|
|
||
| queue.Post( | ||
| Msg.MsgSetArguments | ||
| (fun () -> | ||
| multiplyValues.KernelFunc | ||
| ndRange1 | ||
| matrixLength | ||
| matrix.Columns | ||
| matrix.Values | ||
| vector | ||
| intermediateArray) | ||
| ) | ||
|
|
||
| queue.Post(Msg.CreateRunMsg<_, _>(multiplyValues)) | ||
|
|
||
| let outputArray = | ||
| clContext.CreateClArray<'c option>( | ||
| matrix.RowCount, | ||
| deviceAccessMode = DeviceAccessMode.ReadWrite, | ||
| hostAccessMode = HostAccessMode.NotAccessible, | ||
| allocationMode = AllocationMode.Default | ||
| ) | ||
|
|
||
| let reduceValuesByRows = reduceValuesByRows.GetKernel() | ||
|
|
||
| queue.Post( | ||
| Msg.MsgSetArguments | ||
| (fun () -> | ||
| reduceValuesByRows.KernelFunc | ||
| ndRange2 | ||
| matrix.RowCount | ||
| intermediateArray | ||
| matrix.RowPointers | ||
| outputArray) | ||
| ) | ||
|
|
||
| queue.Post(Msg.CreateRunMsg<_, _>(reduceValuesByRows)) | ||
|
|
||
| queue.Post(Msg.CreateFreeMsg intermediateArray) | ||
|
|
||
| outputArray | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like if it's the last work group, indices are out of bounds sometimes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the last work group
threadsPerBlockis computed asnumberOfRows - gid + lid, soworkEndwill be equal to the last value of row pointers array and indices will not be out of bounds