2015-11-04 48 views
0
open FSCL 
open FSCL.Compiler 
open FSCL.Language 
open FSCL.Runtime 

[<ReflectedDefinition; Kernel>] 
let SimpleAdd(a: float32[], b: float32[], c: float32[], wi: WorkItemInfo) = 
    let i = wi.GlobalID(0) 

    c.[i] <- a.[i] + b.[i] 


[<EntryPoint>] 
let main argv = 
    let a = [| 1.0f; 2.0f |] 
    let b = [| 2.0f; 3.0f |] 
    let c = Array.zeroCreate 2 
    let workItem = WorkSize(a.LongLength) 
    <@ SimpleAdd(a, b, c, workItem) @>.Run() |> ignore 
    printfn "%A" c 
    0 

這會輸出[| 0.0F; 0.0f |]FSCL中的簡單加法不會更新結果數組

我正在使用VS2015附帶的F#編譯器。在.NET控制檯應用程序的目標F#4.0 4.5.1

更新

奇怪的是,這種工作方式exptected:

[<ReflectedDefinition; Kernel>] 
let SimpleAdd(a: float32[], b: float32[], wi: WorkItemInfo) = 
    let i = wi.GlobalID(0) 
    let c = Array.zeroCreate a.Length 

    c.[i] <- a.[i] + b.[i] 

    c 
+0

也就是說,因爲F#函數中的最後一個表達式是函數的返回值。 –

+0

嗨Functional_S,如果你在談論爲什麼第二個樣本的作品 - 我知道。問題是:爲什麼1st不? – LOST

回答

0

第二個例子不工作,因爲在最後一個表達式F#函數是函數的返回值。

第一個示例不起作用 因爲F#引用添加了其他抽象級別。

它的報價看起來像這樣

val x : Quotations.Expr<unit> = 
    Call (None, SimpleAdd, 
     [PropertyGet (None, a, []), 
     PropertyGet (None, b, []), 
     PropertyGet (None, c, []), Value (1)]) 

這不允許(或依賴於評估實現報價的)在後面的陣列(.NET引用類型)的突變的副作用接口。

你可以在這裏檢查編譯器的實現https://github.com/FSCL/FSCL.Compiler 或者可以聯繫FSCL的作者[email protected]

相關問題