Skip to content

Commit

Permalink
Fix remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
dpanfilyonok committed Jan 2, 2022
1 parent 7aea127 commit e3b4690
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 85 deletions.
15 changes: 7 additions & 8 deletions src/Brahma.FSharp.OpenCL.Core/CustomMarshaler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ type CustomMarshaler<'a>() =
size, mem

member this.WriteToUnmanaged(array: 'a[], ptr: IntPtr) =
Parallel.For(0, array.Length, fun j ->
Array.Parallel.iteri (fun j item ->
let start = IntPtr.Add(ptr, j * this.ElementTypeSize)
let mutable i = 0
let rec go (structure: obj) =
Expand All @@ -161,7 +161,8 @@ type CustomMarshaler<'a>() =
[ 0 .. tupleSize - 1 ] |> List.iter (fun i -> go tuple.[i])

| Record ->
FSharpValue.GetRecordFields structure |> Array.iter go
FSharpValue.GetRecordFields structure
|> Array.iter go

| Union -> failwithf "Union not supported"

Expand All @@ -180,9 +181,8 @@ type CustomMarshaler<'a>() =
Marshal.StructureToPtr(structure, IntPtr.Add(start, offset), false)
i <- i + 1

go array.[j]
)
|> ignore
go item
) array

array.Length * this.ElementTypeSize

Expand All @@ -192,7 +192,7 @@ type CustomMarshaler<'a>() =
array

member this.ReadFromUnmanaged(ptr: IntPtr, array: 'a[]) =
Parallel.For(0, array.Length, fun j ->
Array.Parallel.iteri (fun j _ ->
let start = IntPtr.Add(ptr, j * this.ElementTypeSize)
let mutable i = 0
let rec go (type': Type) =
Expand Down Expand Up @@ -230,8 +230,7 @@ type CustomMarshaler<'a>() =
structure

array.[j] <- unbox<'a> <| go typeof<'a>
)
|> ignore
) array

override this.ToString() =
sprintf "%O\n%A" elementPacking offsets
125 changes: 65 additions & 60 deletions src/Brahma.FSharp.OpenCL.Translator/Body.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,68 @@ open FSharp.Quotations.Evaluator
// Translations restricts the generic parameter of the AST nodes to the type Lang
#nowarn "64"

module rec Body =
let (|Name|_|) (str: string) (var': Var) =
[<AutoOpen>]
module private BodyPatterns =
let (|VarName|_|) (str: string) (var': Var) =
match var'.Name with
| tName when tName = str -> Some Name
| tName when tName = str -> Some VarName
| _ -> None

let (|ForLoopWithStep|_|) = function
| Patterns.Let
(
VarName "inputSequence",
DerivedPatterns.SpecificCall <@ (.. ..) @> (
_,
_,
[start; step; finish]
),
Patterns.Let (
VarName "enumerator",
_,
Patterns.TryFinally (
Patterns.WhileLoop (
_,
Patterns.Let (
loopVar,
_,
loopBody
)
),
_
)
)
) -> Some (loopVar, (start, step, finish), loopBody)
| _ -> None

let (|ForLoop|_|) = function
| Patterns.Let
(
VarName "inputSequence",
DerivedPatterns.SpecificCall <@ (..) @> (
_,
_,
[start; finish]
),
Patterns.Let (
VarName "enumerator",
_,
Patterns.TryFinally (
Patterns.WhileLoop (
_,
Patterns.Let (
loopVar,
_,
loopBody
)
),
_
)
)
) -> Some (loopVar, (start, finish), loopBody)
| _ -> None

module rec Body =
// new var scope
let private clearContext (targetContext: TranslationContext<'a, 'b>) =
{ targetContext with VarDecls = ResizeArray() }
Expand Down Expand Up @@ -157,6 +213,8 @@ module rec Body =
| "touint16" -> return Cast(args.[0], PrimitiveType UShort) :> Statement<_>
| "toint64" -> return Cast(args.[0], PrimitiveType Long) :> Statement<_>
| "touint64" -> return Cast(args.[0], PrimitiveType ULong) :> Statement<_>
| "min"
| "max"
| "acos"
| "asin"
| "atan"
Expand All @@ -181,7 +239,6 @@ module rec Body =
return raise <| InvalidKernelException(
sprintf "Seems, that you use math function with name %s not from System.Math or Microsoft.FSharp.Core.Operators" fName
)

| "abs" as fName ->
if mInfo.DeclaringType.AssemblyQualifiedName.StartsWith("Microsoft.FSharp.Core.Operators") then
return FunCall("fabs", args) :> Statement<_>
Expand Down Expand Up @@ -394,7 +451,7 @@ module rec Body =
}

// NOTE reversed loops not supported
let translateForIntegerRangeLoop (loopVar: Var) (from': Expr) (to': Expr) (step: Expr option) (body: Expr) = translation {
let translateForLoop (loopVar: Var) (from': Expr) (to': Expr) (step: Expr option) (body: Expr) = translation {
let! loopVarName = State.gets (fun context -> context.Namer.LetStart loopVar.Name)
let loopVarType = loopVar.Type

Expand Down Expand Up @@ -651,60 +708,6 @@ module rec Body =

return Const(type', value) :> Node<_>

// TODO convert to active pattern?
// for loop with step
| Patterns.Let
(
Name "inputSequence",
DerivedPatterns.SpecificCall <@ (.. ..) @> (
_,
_,
[start; step; finish]
),
Patterns.Let (
Name "enumerator",
_,
Patterns.TryFinally (
Patterns.WhileLoop (
_,
Patterns.Let (
loopVar,
_,
loopBody
)
),
_
)
)
) ->
return! translateForIntegerRangeLoop loopVar start finish (Some step) loopBody >>= toNode

| Patterns.Let
(
Name "inputSequence",
DerivedPatterns.SpecificCall <@ (..) @> (
_,
_,
[start; finish]
),
Patterns.Let (
Name "enumerator",
_,
Patterns.TryFinally (
Patterns.WhileLoop (
_,
Patterns.Let (
loopVar,
_,
loopBody
)
),
_
)
)
) ->
return! translateForIntegerRangeLoop loopVar start finish None loopBody >>= toNode

| Patterns.Call (exprOpt, mInfo, args) -> return! translateCall exprOpt mInfo args >>= toNode
| Patterns.Coerce (expr, sType) -> return raise <| InvalidKernelException(sprintf "Coerce is not suported: %O" expr)
| Patterns.DefaultValue sType -> return raise <| InvalidKernelException(sprintf "DefaulValue is not suported: %O" expr)
Expand All @@ -719,7 +722,9 @@ module rec Body =
| Some e -> return! translateFieldSet e fldInfo.Name expr >>= toNode
| None -> return raise <| InvalidKernelException(sprintf "Fileld set with empty host is not supported. Field: %A" fldInfo)

| Patterns.ForIntegerRangeLoop (i, from', to', body) -> return! translateForIntegerRangeLoop i from' to' None body >>= toNode
| ForLoopWithStep (loopVar, (start, step, finish), loopBody) -> return! translateForLoop loopVar start finish (Some step) loopBody >>= toNode
| ForLoop (loopVar, (start, finish), loopBody) -> return! translateForLoop loopVar start finish None loopBody >>= toNode
| Patterns.ForIntegerRangeLoop (loopVar, start, finish, loopBody) -> return! translateForLoop loopVar start finish None loopBody >>= toNode
| Patterns.IfThenElse (cond, thenExpr, elseExpr) -> return! translateIf cond thenExpr elseExpr >>= toNode

| Patterns.Lambda (var, _expr) -> return raise <| InvalidKernelException(sprintf "Lambda is not suported: %A" expr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module Transformer =
/// Returns kernel and other methods
let transformQuotation (expr: Expr) =
expr
|> transformMinMax
// |> transformMinMax
|> processAtomic
|> replacePrintf
|> makeVarNameUnique
Expand Down
18 changes: 2 additions & 16 deletions tests/Brahma.FSharp.Tests/Expected/MAX.Transformation.cl
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
double ItemUnitFunc (__global double * buf, private double tempVarY)
{double tempVarX = buf [0] ;
double tempVarY1 = tempVarY ;
if ((tempVarX > tempVarY1))
{return tempVarX ;}
else
{return tempVarY1 ;} ;}
double ItemUnitFunc2 (__global double * buf, private double tempVarY)
{double tempVarX2 = buf [0] ;
double tempVarY3 = tempVarY ;
if ((tempVarX2 > tempVarY3))
{return tempVarX2 ;}
else
{return tempVarY3 ;} ;}
__kernel void brahmaKernel (__global double * buf)
{double tempVarY = 1 ;
buf [0] = ItemUnitFunc (buf, tempVarY) ;
buf [0] = ItemUnitFunc2 (buf, tempVarY) ;}
buf [0] = max (buf [0], tempVarY) ;
buf [0] = max (buf [0], tempVarY) ;}
11 changes: 11 additions & 0 deletions tests/Brahma.FSharp.Tests/FullTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,17 @@ let operatorsAndMathFunctionsTests =
[|5.f; 6.f; 7.f; 8.f|]
[|1.f; 2.f; 3.f; 4.f|]

ptestCase "MAX on int16 with const" <| fun () ->
let command =
<@
fun (range: Range1D) (buf: int16 clarray) ->
let gid = range.GlobalID0
buf.[gid] <- max buf.[gid] 1s
@>

let inA = [|0s; 1s; 2s; 3s|]
checkResult command inA (Array.map (max 1s) inA)

// Failed: due to precision
ptestCase "Math sin" <| fun _ ->
let command =
Expand Down

0 comments on commit e3b4690

Please sign in to comment.