anchor.diff:      member this.Buffer = buffer
       member this.Length = length
   
  -type Kernel<'t>(ctx, f) =
  +type GpuKernel<'t>(device, context, srcLambda: Expr<'TRange ->'a>) =
  +
  +    let kernelName = "brahmaKernel"
  +
  +    let clCode =
  +        let translatorOptions = []
  +        let codeGenerator = new Translator.FSQuotationToOpenCLTranslator()
  +        let ast, newLambda = codeGenerator.Translate srcLambda translatorOptions
  +        let code = Printer.AST.Print ast
  +        printfn "Code = %A" code
  +        code
  +    let compileQuery translatorOptions additionalSources =
  +        let program, error =
  +            let sources = additionalSources @ [clCode] |> List.toArray
  +            Cl.CreateProgramWithSource(context, uint32 (sources.Length), sources, null)
  +        if error <> ErrorCode.Success
  +        then failwithf "Program creation failed: %A" error
  +        let error = Cl.BuildProgram(program, 1u, [|device|], "", null, System.IntPtr.Zero)
  +        if error <> ErrorCode.Success
  +        then failwithf "Program compilation failed: %A" error
  +        program
  +
  +    let createKernel program =
  +        let clKernel,error = Cl.CreateKernel(program, kernelName)
  +        if error <> ErrorCode.Success
  +        then failwithf "OpenCL kernel creation problem. Error: %A" error
  +        clKernel
  +
  +    let additionalSources = []
  +
  +    let kernel =
  +        let program = compileQuery [] additionalSources
  +        let clKernel = createKernel program
  +        clKernel
  +
  +    let toIMem a =
  +        //var isIMem = arg is IMem;
  +        match box a with
  +        | :? Brahma.IMem as mem ->
  +           mem.Size, mem.Data
  +        | :? int as i -> System.IntPtr(System.Runtime.InteropServices.Marshal.SizeOf(i)),
  +                         box i
  +        | x -> failwithf "Unexpected argument: %A" x
  +
  +    let setupArgument index arg =
  +        let argSize, argVal = toIMem arg
  +        let error =
  +                Cl.SetKernelArg(kernel, (uint32 index)
  +                , argSize
  +                , argVal)
  +        if error <> ErrorCode.Success
  +        then raise (new CLException(error))
  +
  +    let argsSetupFunction =
  +        let rng = ref Unchecked.defaultof<'TRange>
  +        let args = ref [||]
  +        //let run = ref Unchecked.defaultof<Commands.Run<'TRange>>
  +        let getStarterFunction qExpr =
  +            let rec go expr vars =
  +                match expr with
  +                | Patterns.Lambda (v, body) ->
  +                    Expr.Lambda(v, go body (v::vars))
  +                | e ->
  +                    let arr =
  +                        let c = Expr.NewArray(typeof<obj>,vars |> List.rev
  +                            |> List.map
  +                                 (fun v -> Expr.Coerce (Expr.Var(v), typeof<obj>)))
  +                        <@@
  +                            let x = %%c |> List.ofArray
  +                            rng := (box x.Head) :?> 'TRange
  +                            args := x.Tail |> Array.ofList
  +                            //let brahmsRunCls = new Brahma.OpenCL.Commands.Run<_>(kernel :> Kernel<'TRange>,!rng)
  +                            !args |> Array.iteri (fun i x -> setupArgument i x)
  +                            //run := kernel.Run(!rng, !args)
  +                        @@>
  +                    arr
  +            let res = <@ %%(go qExpr []):'TRange ->'a @>.Compile()
  +
  +            res
  +        getStarterFunction srcLambda
  +
       member this.Run () = ()
  +    member this.SetArguments = argsSetupFunction
   
   type ToHost<'t>(src:GpuArray<'t>, dst: array<'t>, ?replyChannel:AsyncReplyChannel<array<'t>>) =
       member this.Destination = dst
  
positive.code:     member this.Source = src
      member this.ReplyChannel = replyChannel
  
  type RunKernel<'t>(kernelRunFun, ?replyChannel:AsyncReplyChannel<Kernel<'t>>) =
      member this.KernelRunFunction = kernelRunFun
      member this.ReplyChannel = replyChannel
  
  
negative.code:         loop 0)
  
  type Host() =
  
      let kernelFun = <@fun (range:_1D) (buf:array<_>) -> buf.[0] <- 1L@>
  
      member this.Do () =
          let devices = Device.getDevices "*" DeviceType.Gpu
          printfn "Device: %A" devices.[0]
  