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]