Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8529797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:06:39+00:00 2026-06-11T09:06:39+00:00

I’m new to C, C++ and OpenCL and doing my best to learn them

  • 0

I’m new to C, C++ and OpenCL and doing my best to learn them at the moment. Here’s a preexisting C++ function that I’m trying to figure out how to port to OpenCL using either the C or C++ bindings.

#include <vector>

using namespace std;

class Test {

private:

    double a;
    vector<double> b;
    vector<long> c;
    vector<vector<double> > d;

public:

    double foo(long x, double y) {
        // mathematical operations
        // using x, y, a, b, c, d
        // and also b.size()
        // to calculate return value
        return 0.0;
    }

};

Broadly my question is how to pass in all the class members that this function accesses into the binding and the kernel. I understand how to pass in the scalar values but the vector values I’m not sure about. Is there perhaps a way to pass in pointers to each of the above members or memory map them so that OpenCL’s view of them is in sync with host memory? Broken down my questions are as below.

  1. How do I pass in member b and c to the binding and the kernel given that these are of variable size?
  2. How do I pass in member d given that it is two dimensional?
  3. How do I access these members from within the kernel and what types will they be declared as in the arguments to the kernel? Will simply using array index notation i.e. b[0] work for access?
  4. How would I invoke an operation equivalent to b.size() within the kernel function or would I not and instead pass in the size from the binding into the kernel as an extra argument? What happens if it changes?

I would really appreciate either C or C++ binding and kernel code example source code in answers.

Many thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:06:41+00:00Added an answer on June 11, 2026 at 9:06 am
    1. You have to allocate an OpenCL buffer and copy your CPU data into it. An OpenCL buffer has a fixed size, so you either have to recreate it if your data size changes or you make it “big enough” and use only a subsection of it if less memory is needed. For example, to create a buffer for b and at the same time copy all of its data to the device:

      cl_mem buffer_b = clCreateBuffer(
          context, // OpenCL context
          CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, // Only read access from kernel,
                                                   // copy data from host
          sizeof(cl_double) * b.size(), // Buffer size in bytes
          &b[0], // Pointer to data to copy
          &errorcode); // Return code
      

      It is also possible to directly map host memory (CL_MEM_USE_HOST_PTR), but this imposes some restrictions on the alignment and the access to the host memory after creating the buffer. Basically, the host memory can contain garbage when you are not currently mapping it.

    2. It depends. Are the sizes of the vectors in the second dimension consistenly equal? Then just flatten them when uploading them to the OpenCL device. Otherwise it gets more complicated.

    3. You declare buffer arguments as __global pointers in your kernel. For example, __global double *b would be appropiate for the buffer created in 1. You can simply use array notation in the kernel to access the individual elements in the buffer.

    4. You cannot query the buffer size from within the kernel, so you have to pass it manually. This can also happen implicitly, e.g. if the number of work items matches the size of b.

    A kernel which can access all of the data for the computation could look like this:

    __kernel void foo(long x, double y, double a, __global double* b, int b_size,
                      __global long* c, __global double* d,
                      __global double* result) {
      // Here be dragons
      *result = 0.0;
    }
    

    Note that you also have to allocate memory for the result. It might be necessary to pass additional size arguments should you need them. You would call the kernel as follows:

    // Create/fill buffers
    // ...
    
    // Set arguments
    clSetKernelArg(kernel, 0, sizeof(cl_long), &x);
    clSetKernelArg(kernel, 1, sizeof(cl_double), &y);
    clSetKernelArg(kernel, 2, sizeof(cl_double), &a);
    clSetKernelArg(kernel, 3, sizeof(cl_mem), &b_buffer);
    cl_int b_size = b.size();
    clSetKernelArg(kernel, 4, sizeof(cl_int), &b_size);
    clSetKernelArg(kernel, 5, sizeof(cl_mem), &c_buffer);
    clSetKernelArg(kernel, 6, sizeof(cl_mem), &d_buffer);
    clSetKernelArg(kernel, 7, sizeof(cl_mem), &result_buffer);
    // Enqueue kernel
    clEnqueueNDRangeKernel(queue, kernel, /* ... depends on your domain */);
    
    // Read back result
    cl_double result;
    clEnqueueReadBuffer(queue, result_buffer, CL_TRUE, 0, sizeof(cl_double), &result,
                        0, NULL, NULL);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.