Results 1 to 5 of 5

Thread: copying a variable from host memory to device memory

  1. #1

    copying a variable from host memory to device memory

    I want to copy a variable from host memory to device memory which is not stored in array it is a single variable can you specify how to copy and read it back from the device memory

  2. #2
    Junior Member
    Join Date
    Aug 2012
    Posts
    15

    Re: copying a variable from host memory to device memory

    You can set an integer or a float variable simple as a parameter with setKernelArgs, like you set the pointer to array. However, i noticed, there can be issues if you try to set the byte of char value (1 byte variable), just use 4 byte value instead. You can explicit put the variable into constant memory.
    Another thing, you can import a constant value to a Kernel via MACRO .
    The openCL support common C- macro syntax. And you can set both string or digital value Macros by creating and building operators, look in your OpenCl documentation for that. In my project, i can even customyze a type of variables, for example

    Code :
    _kernel blablabla (TYPE* inputBuffer)
    In Host you can set a Macro ("TYPE","uint"), and that will work fine.

    Of course, you can export a variable as an array with one element, but that is kinda silly...

  3. #3

    Re: copying a variable from host memory to device memory

    Since you want to copy the result back to host memory, you will have to store the value in a cl_mem object. You will have to allocate an array of one element and an OpenCL buffer with enough bytes for one element. Sadly, this is the only way.

  4. #4

    Re: copying a variable from host memory to device memory

    Can't we do this as we do in C language pass the address of the variable and then what changes we do inside the kernel; they will automatically change the value of the variable and we can get the new value in the host function

  5. #5
    Senior Member
    Join Date
    Oct 2012
    Posts
    165

    Re: copying a variable from host memory to device memory

    You can use the COPY_HOST_PTR Memory Object initiailizer to pass the pointer of yor variable to the device. This will copy your already allocated memory to the device. I'm not shure if this works, but hopefully mapping that buffer again will give you the same adress. Or you initialize a buffer with ALLOC_HOST_PTR and use that Pointer. It should always stay the same for each mapping.
    But no matter what, you will always have to mat/unmap that memory, to have a correct sync.

    Google for pinned memory for some more informations about the alloc example.

Similar Threads

  1. Transfers between host and device memory
    By ldsolano in forum OpenCL
    Replies: 5
    Last Post: 11-09-2011, 05:35 PM
  2. Device-host memory communication
    By jbasic in forum OpenCL
    Replies: 2
    Last Post: 10-07-2009, 10:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •