Skip to content
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

Kronecker optimization #91

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions benchmarks/GraphBLAS-sharp.Benchmarks/Configs/Kronecker.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
can_634
Si2
lshp1561
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<Compile Include="Matrix/SpGeMM/Expand.fs" />
<Compile Include="Matrix/Map2/Map2.fs" />
<Compile Include="Matrix/Map2/MathNET.fs" />
<Compile Include="Matrix/Kronecker.fs" />
<Compile Include="Vector/Map2.fs" />
<Compile Include="Algorithms/BFS.fs" />
<Compile Include="Program.fs" />
Expand Down
142 changes: 142 additions & 0 deletions benchmarks/GraphBLAS-sharp.Benchmarks/Matrix/Kronecker.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
module GraphBLAS.FSharp.Benchmarks.Matrix.Kronecker

open System.IO
open BenchmarkDotNet.Attributes
open Brahma.FSharp
open GraphBLAS.FSharp
open GraphBLAS.FSharp.IO
open GraphBLAS.FSharp.Backend.Quotes
open GraphBLAS.FSharp.Objects
open GraphBLAS.FSharp.Objects.ClContextExtensions
open GraphBLAS.FSharp.Benchmarks

[<AbstractClass>]
[<IterationCount(100)>]
[<WarmupCount(10)>]
[<Config(typeof<Configs.Matrix>)>]
type Benchmarks<'elem when 'elem : struct>(
buildFunToBenchmark,
converter: string -> 'elem,
converterBool,
buildMatrix) =

let mutable funToBenchmark = None

let mutable matrix = Unchecked.defaultof<ClMatrix<'elem>>

let mutable matrixHost = Unchecked.defaultof<_>

member val ResultMatrix = Unchecked.defaultof<ClMatrix<'elem> option> with get, set

[<ParamsSource("AvailableContexts")>]
member val OclContextInfo = Unchecked.defaultof<Utils.BenchmarkContext * int> with get, set

[<ParamsSource("InputMatrixProvider")>]
member val InputMatrixReader = Unchecked.defaultof<MtxReader> with get, set

member this.OclContext: ClContext = (fst this.OclContextInfo).ClContext
member this.WorkGroupSize = snd this.OclContextInfo

member this.Processor =
let p = (fst this.OclContextInfo).Queue
p.Error.Add(fun e -> failwithf "%A" e)
p

static member AvailableContexts = Utils.availableContexts

static member InputMatrixProviderBuilder pathToConfig =
let datasetFolder = ""
pathToConfig
|> Utils.getMatricesFilenames
|> Seq.map
(fun matrixFilename ->
printfn "%A" matrixFilename

match Path.GetExtension matrixFilename with
| ".mtx" ->
MtxReader(Utils.getFullPathToMatrix datasetFolder matrixFilename)
| _ -> failwith "Unsupported matrix format")

member this.FunToBenchmark =
match funToBenchmark with
| None ->
let x = buildFunToBenchmark this.OclContext this.WorkGroupSize
funToBenchmark <- Some x
x
| Some x -> x

member this.ReadMatrix (reader: MtxReader) =
let converter =
match reader.Field with
| Pattern -> converterBool
| _ -> converter

reader.ReadMatrix converter

member this.Mxm() =
this.ResultMatrix <- this.FunToBenchmark this.Processor DeviceOnly matrix matrix

member this.ClearInputMatrices() =
matrix.Dispose this.Processor

member this.ClearResult() =
match this.ResultMatrix with
| Some matrix -> matrix.Dispose this.Processor
| None -> ()

member this.ReadMatrices() =
matrixHost <- this.ReadMatrix this.InputMatrixReader

member this.LoadMatricesToGPU () =
matrix <- buildMatrix this.OclContext matrixHost
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking as Kirill


abstract member GlobalSetup : unit -> unit

abstract member Benchmark : unit -> unit

abstract member IterationCleanup : unit -> unit

abstract member GlobalCleanup : unit -> unit

module WithoutTransfer =
type Benchmark<'elem when 'elem : struct>(
buildFunToBenchmark,
converter: string -> 'elem,
converterBool,
buildMatrix) =

inherit Benchmarks<'elem>(
buildFunToBenchmark,
converter,
converterBool,
buildMatrix)

[<GlobalSetup>]
override this.GlobalSetup() =
this.ReadMatrices()
this.LoadMatricesToGPU()

[<Benchmark>]
override this.Benchmark() =
this.Mxm()
this.Processor.PostAndReply(Msg.MsgNotifyMe)

[<IterationCleanup>]
override this.IterationCleanup () =
this.ClearResult()

[<GlobalCleanup>]
override this.GlobalCleanup () =
this.ClearInputMatrices()

type Float32() =

inherit Benchmark<float32>(
Operations.kronecker (ArithmeticOperations.float32MulOption),
float32,
(fun _ -> Utils.nextSingle (System.Random())),
(fun context matrix -> ClMatrix.CSR <| matrix.ToCSR.ToDevice context)
)

static member InputMatrixProvider =
Benchmarks<_>.InputMatrixProviderBuilder "Kronecker.txt"
30 changes: 23 additions & 7 deletions src/GraphBLAS-sharp.Backend/Common/ClArray.fs
Original file line number Diff line number Diff line change
Expand Up @@ -728,11 +728,11 @@ module ClArray =
bound<'a, int> Search.Bin.lowerBound clContext

/// <summary>
/// Gets the value at the specified position from the input array.
/// Gets the value at the specified position from the input array and insert it into given ClCell.
/// </summary>
/// <param name="clContext">OpenCL context.</param>
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
let item<'a> (clContext: ClContext) workGroupSize =
let itemTo<'a> (clContext: ClContext) workGroupSize =

let kernel =
<@ fun (ndRange: Range1D) index (array: ClArray<'a>) (result: ClCell<'a>) ->
Expand All @@ -744,21 +744,37 @@ module ClArray =

let program = clContext.Compile kernel

fun (processor: MailboxProcessor<_>) (index: int) (array: ClArray<'a>) ->
fun (processor: MailboxProcessor<_>) (index: int) (array: ClArray<'a>) (output: ClCell<'a>) ->

if index < 0 || index >= array.Length then
failwith "Index out of range"

let result =
clContext.CreateClCell Unchecked.defaultof<'a>

let kernel = program.GetKernel()

let ndRange = Range1D.CreateValid(1, workGroupSize)

processor.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange index array result))
processor.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange index array output))
processor.Post(Msg.CreateRunMsg<_, _> kernel)

/// <summary>
/// Gets the value at the specified position from the input array.
/// </summary>
/// <param name="clContext">OpenCL context.</param>
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
let item<'a> (clContext: ClContext) workGroupSize =

let itemTo = itemTo clContext workGroupSize

fun (processor: MailboxProcessor<_>) (index: int) (array: ClArray<'a>) ->

if index < 0 || index >= array.Length then
failwith "Index out of range"

let result =
clContext.CreateClCell Unchecked.defaultof<'a>

itemTo processor index array result

result

/// <summary>
Expand Down
Loading
Loading