Warning about the creating buffer objects

  • James Douglas

    James Douglas - 2011-06-12

    I realize this is probably common knowledge to others more experienced with programming, but for a newbie in C# it absolutely confounded me for several weeks.  Just figured I'd throw it out there in case it saves someone else from pulling out their hair.

    It appears that when creating the OpenCL buffers using Cloo, the data is not transferred immediately to the device.  This means that when using a local method variable to store, say, an array of data to send to the GPU, there is a chance that the variable can fall out of scope and be garbage collected before the data is entirely transferred.

    It's probably a pretty "duh" thing for the experts out there, but the bug was absolute hell to track down because it disappeared as soon as I inserted a break, and only happened roughly once every 500 executions.  I guess it's possible that some of the buffer creation arguments can be changed to prevent this, but storing the array outside of the method solved the problem completely =)

  • nythrix

    nythrix - 2011-06-14

    I should add this to the documentation of the constructors. Thanks for the heads up!

  • nythrix

    nythrix - 2011-06-14

    By the way: do you recall what flags where you using? UseHostPointer could cause this too, even when storing the array.

  • James Douglas

    James Douglas - 2011-06-14

    for creating the buffer, i was using:

    new ComputeBuffer<float>(Context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, floatData)

    The floatData was just a temporary variable because i was casting from an array of doubles.  As soon as I stored it outside of the method, the problem disappeared and hasn't come back in a couple hundred thousand iterations.

  • nythrix

    nythrix - 2011-06-15

    Note, that UseHostPointer means OpenCL may use the data in-place (which is managed memory) without copying them. If the GC will move floatData during compacting, you may hit an invalid memory access thingy. Although current implementations probably cache the data, it doesn't mean you won't hit an error in the future. Either replace UseHostPointer with a much safer CopyHostPointer or pin the array so it doesn't get thrown around the heap.

  • James Douglas

    James Douglas - 2011-06-15

    Ah, that makes much more sense.  Thank you.  The OpenCL documentation probably mentioned this somewhere, but I'm still a newbie at programming and the OpenCL Specification is a bit… cryptic.

  • Anonymous - 2011-07-24

    Thanks all
    I'm new member too,
    I think you should use UseHostPointer flag for best performance In/Out data from Host and Device, Finally , You can read the result back to Host. Because Copy data from Host to Device very expensively. :)



Log in to post a comment.