Tag Archives: GPU scripting

Microsoft_Excel_2013_logo

CUDA Scripting in Excel Part I

In this video, we use the Tsunami IDE to do CUDA scripting inside Excel. We show how to code a CUDA kernel, launch and modify it.

Here is the test code:

[sourcecode language=”fsharp”]
#r @"C:\Users\Xiang\Documents\FCell\Alea.CUDA.dll"
#r @"C:\Users\Xiang\Documents\FCell\FCell.ManagedXll.dll"

namespace Demo

open Alea.CUDA
open FCell.ManagedXll

module UDF =

[<XlConverter>]
let exc (e:exn) = XlScalar(XlString(e.Message))

let private worker = Engine.workers.DefaultWorker
let DeviceName() = worker.Device.Name
let DeviceMemory() = worker.Device.Memory |> int

let private pfunct = cuda {
let! kernel =
<@ fun (A:DevicePtr<float>) (B:DevicePtr<float>) (C:DevicePtr<float>) ->
let tid = threadIdx.x
C.[tid] <- A.[tid] + B.[tid] @>
|> defineKernelFunc

return PFunc(fun (m:Module) (A:float[]) (B:float[]) ->
use A = m.Worker.Malloc(A)
use B = m.Worker.Malloc(B)
use C = m.Worker.Malloc(A.Length)
let lp = LaunchParam(1, A.Length)
kernel.Launch m lp A.Ptr B.Ptr C.Ptr
C.ToHost()) }
let private pfuncm = worker.LoadPModule(pfunct)
let Test value idx =
let A = [| 1.1; 2.2; 3.3 |]
let B = Array.init A.Length (fun _ -> value)
let C = pfuncm.Invoke A B
C.[idx]
[/sourcecode]

Microsoft_Excel_2013_logo

CUDA Scripting in Excel Part II

This video shows how to use some of our extension to quickly code a PI calculation with Monte-Carlo simulation inside Excel with Tsunami F# scripting. In this example we use Sobol sequence.

Here is the test code:

[sourcecode language=”fsharp”]
#r @"C:\Users\Xiang\Documents\FCell\Alea.CUDA.dll"
#r @"C:\Users\Xiang\Documents\FCell\Alea.CUDA.Extension.dll"
#r @"C:\Users\Xiang\Documents\FCell\FCell.ManagedXll.dll"

namespace Demo

open Alea.CUDA
open Alea.CUDA.Extension
open FCell.ManagedXll

module UDF =

[<XlConverter>]
let exc (e:exn) = XlScalar(XlString(e.Message))

let private worker = Engine.workers.DefaultWorker
let private sobol = worker.LoadPModule(PRandom.sobol <@ Sobol.toFloat64 @>).Invoke
let private sum = worker.LoadPModule(PArray.sum()).Invoke
let private map =
let transform =
<@ fun (x:float) (y:float) ->
let x = x – 0.5
let y = y – 0.5
let distance = sqrt(x * x + y * y)
match distance with
| distance when distance <= 0.5 -> 1.0
| _ -> 0.0 @>
worker.LoadPModule(PArray.map2 transform).Invoke
let private calc samples = pcalc {
let dimensions = 2
let vectors = samples
let! numbers = sobol dimensions vectors 0
let x = DArray.subView numbers 0 samples
let y = DArray.subView numbers samples samples
let! insides = map x y
let! inside = sum insides
let! inside = inside.Gather()
let total = float(samples)
return inside / total / 0.25 }
let CalcPI samples = calc samples |> PCalc.run
[/sourcecode]