Matrix multiplication example is described below. Full code you can found in FsMatrixMultiply.fs. More examples:
PrerequirementsFist, you should add next references into your project:
Configure providerYou should configure queue for executing commands on GPU. First step: device provider creating. let platformName = "*" let deviceType = Cl.DeviceType.Default let provider = try ComputeProvider.Create(platformName, deviceType) with | ex -> failwith ex.Message let mutable commandQueue = new CommandQueue(provider, provider.Devices |> Seq.head) Write quotationWell, now all are ready for specifying kernel code. It should be a quoted function with all required parameters passed explicitly. Using of bindings that declared out of quotation is not supported. This function will be translated and executed on GPU. let command = <@ fun (r:_2D) columns (a:array<_>) (b:array<_>) (c:array<_>) -> let tx = r.GlobalID0 let ty = r.GlobalID1 let mutable buf = c.[ty * columns + tx] for k in 0 .. columns - 1 do buf <- buf + (a.[ty * columns + k] * b.[k * columns + tx]) c.[ty * columns + tx] <- buf @> Compile&RunFirst step is kernel function compilation. let kernel, kernelPrepare, kernelRun = provider.Compile command
Next is computation grid configuration: 2D grid with size = rows*columns and local block with size=localWorkSize*localWorkSize (e.g. 10*10) let d =(new _2D(rows, columns, localWorkSize, localWorkSize)) Prepare kernel. Pass actual parameters for computation: kernelPrepare d columns aValues bValues cParallel Add command into queue and finish it. let _ = commandQueue.Add(kernelRun()).Finish() Note, that now all command queue methods are return command queue (it's the same object, not clone), so we should use " |
Home > Brahma.FSharp >