Skip to content

Instantly share code, notes, and snippets.

@soloman817
Created March 12, 2013 08:19
Show Gist options
  • Save soloman817/5141112 to your computer and use it in GitHub Desktop.
Save soloman817/5141112 to your computer and use it in GitHub Desktop.
CUDA scripting inside Excel (Part II)
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment