Skip to content

Unexpected behavior with local array and match #144

Open
@artemgl

Description

@artemgl

Describe the bug
Unexpected behavior when using a local array and match expression.

To Reproduce

let op =
    <@
        fun x y ->
            let mutable res = x * y

            if res = 0uy then None else (Some res)
    @>

let run =
    <@
        fun (ndRange: Range1D) (x: ClArray<byte>) (y: ClArray<byte>) (array: ClArray<byte>) ->
            let mutable i = ndRange.GlobalID0
            let a = x.[i]
            let b = y.[i]
            let increase = (%op) a b
            match increase with
            | Some v -> if i = 1 then array.[i] <- 1uy
            | _      -> ()

            let lid = ndRange.LocalID0

            let la = localArray<bool> 32
            la.[lid] <- false

            let buff: byte option = None

            match buff, increase with
            | Some _, Some _ -> if i = 1 then array.[i] <- array.[i] + 4uy
            | None,   Some _ -> if i = 1 then array.[i] <- array.[i] + 8uy
            | Some _, None   -> if i = 1 then array.[i] <- array.[i] + 16uy
            | None,   None   -> if i = 1 then array.[i] <- array.[i] + 32uy
    @>

let program = context.Compile(run)
let kernel = program.GetKernel()

let workGroupSize = 32

let array = context.CreateClArray<byte>(Array.create workGroupSize 0uy)
let x = context.CreateClArray<byte>(Array.create workGroupSize 240uy)
let y = context.CreateClArray<byte>(Array.create workGroupSize 112uy)

let ndRange = Range1D.CreateValid(workGroupSize, workGroupSize)

q.Post(
    Msg.MsgSetArguments
        (fun () ->
            kernel.KernelFunc
                ndRange
                x
                y
                array)
)

q.Post(Msg.CreateRunMsg<_, _>(kernel))

The value array.[1] is always 33 after starting this code. This may mean that the code is executed in two contradictory match branches.

The problem disappears if the line la.[lid] <- false is deleted.

The problem disappears as well if the line | _ -> () is replaced by | _ -> if i = 1 then array.[i] <- 0uy in the first match expression.

Expected behavior
The value array.[1] must be 32.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions