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 8844179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:25:16+00:00 2026-06-14T11:25:16+00:00

Basically, I sloppily coded an OpenCL program for an assignment using these global variables:

  • 0

Basically, I sloppily coded an OpenCL program for an assignment using these global variables:

int devType = CL_DEVICE_TYPE_GPU;

cl_int err;         /* Error code returned from api calls.  */
size_t global;      /* Global domain size for our calculation.  */
size_t local;           /* Local domain size for our calculation.  */

cl_platform_id cpPlatform;  /* openCL platform.  */
cl_device_id device_id; /* Compute device id.  */
cl_context context;     /* Compute context.  */
cl_command_queue commands;  /* Compute command queue.  */
cl_program program;     /* Compute program.  */
cl_kernel kernel;       /* Compute kernel.  */

/* Create data for the run.  */
float *data = NULL;     /* Original data set given to device.  */
float *results = NULL;  /* Results returned from device.  */
unsigned int correct;       /* Number of correct results returned.  */
cl_mem input;           /* Device memory used for the input array.  */
cl_mem output;      /* Device memory used for the output SUM.  */

int rc = EXIT_FAILURE;

Now I’m trying to make them all local in order to tidy the program up.

I converted a global variable N by just moving it away from the variables above into the main() function. I then updated every function header that used N to have ‘int N’ as a parameter, and passed N into any function calls that needed it as an argument. The program worked as expected.

So I suppose what I’m asking is, for the rest of these variables, will it be that simple? I understand the concepts of passing by reference and value and realise some functions may change variables, so I’ll need to use pointer referencing/dereferencing. My concern is that my pointer theory is a little rough and I’m worried I’ll run into problems. I also am unsure whether my defined functions can take all of these cl variables.

Also, is there anything wrong with using the same variable names within the functions?

EDIT:

As I feared, a problem does occur in the following functions when trying to localise device_id:

void deviceSetup(int devType) {
    cl_platform_id cpPlatform;  /* openCL platform.  */

    /* Connect to a compute device.  */
    if (CL_SUCCESS != clGetPlatformIDs (1, &cpPlatform, NULL))
        die ("Error: Failed to find a platform!");

    /* Get a device of the appropriate type.  */
    if (CL_SUCCESS != clGetDeviceIDs (cpPlatform, devType, 1, &device_id, NULL))
        die ("Error: Failed to create a device group!");
}

/* Create a compute context.  */
void createContext(cl_int err){
    context = clCreateContext (0, 1, &device_id, NULL, NULL, &err);
    if (!context || err != CL_SUCCESS)
        die ("Error: Failed to create a compute context!");
}

/* Create a command commands.  */
void createCommandQueue(cl_int err) {
    commands = clCreateCommandQueue (context, device_id, 0, &err);
    if (!commands || err != CL_SUCCESS)
        die ("Error: Failed to create a command commands!");
}       

void createAndCompile(cl_int err){
    /* Create the compute program from the source buffer.  */
    program = clCreateProgramWithSource (context, 1,
                                         (const char **) &KernelSource,
                                         NULL, &err);
    if (!program || err != CL_SUCCESS)
        die ("Error: Failed to create compute program!");

    /* Build the program executable.  */
    err = clBuildProgram (program, 0, NULL, NULL, NULL, NULL);
    if (err != CL_SUCCESS)
    {
        size_t len;
        char buffer[2048];

        clGetProgramBuildInfo (program, device_id, CL_PROGRAM_BUILD_LOG,
                               sizeof (buffer), buffer, &len);
        die ("Error: Failed to build program executable!\n%s", buffer);
    }
}
  • 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-14T11:25:17+00:00Added an answer on June 14, 2026 at 11:25 am

    You’ve answered your own question really. Yes, that really is all there is to it. You may want to consider combining a large number of related variables into a struct and pass just a pointer to that struct if you find you’ve generated massive parameter lists for your functions but that’s about it. (There is a tiny degree of performance consideration relating to the number of parameters you pass to any function, but I think for now that’s an unnecessary level of complication you could do without!)

    There’s no getting away from understanding pointers in C though (the only way to pass by reference) so a small project like this might well be an ideal time to strengthen that knowledge!

    OK, let’s have an example, life’s always better explained that way.

    We have:

    int cheddar;
    int montereyjack;
    int brie;
    
    void print_cheeses(void)
    {
        printf("I have %d cheddar %d montereyjack and %d brie\n", cheddar, montereyjack, brie);
    }
    
    void add_cheeses(void)
    {
       cheddar = cheddar + 1;
       montereyjack = montereyjack + 1;
       brie = brie + 1;
       print_cheeses();
    }
    
    int main(int argc, char *argv[])
    {
        add_cheeses();
        printf ("Now I have %d cheddars %d jacks %d bries\n", cheddar, montereyjack, brie);
    }
    

    What we need to get to is:

    // By value here because we're not changing anything
    void print_cheeses(int cheds, int jacks, int bries)
    {
        printf("I have %d cheddar %d montereyjack and %d brie\n", cheds, jacks, bries);
    }
    
    // Pointers here because we need to change the values in main
    void add_cheeses(int *cheese_one, int *cheese_two, int *cheese_three)
    {
       *cheese_one = *cheese_one + 1; // We're following the pointer to get to the data we want to change
       *cheese_two = *cheese_two + 1;
       *cheese_three = *cheese_three + 1;
       print_cheeses(*cheese_one, *cheese_two, *cheese_three); // We're following the pointer to get to the data we want to print
    }
    
    int main(int argc, char *argv[])
    {
        int cheddar = 0;
        int montereyjack = 0;
        int brie = 0;
    
        add_cheeses(&cheddar, &montereyjack, &brie);
    
        printf ("Now I have %d cheddars %d jacks %d bries\n", cheddar, montereyjack, brie);
    }
    

    But it can be a pain passing all three values each time, and since they’re related you could bundle them together in one struct and just pass a pointer to that struct about.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, when I type these commands in the terminal by hand, the sift program
Basically, I want to be able to run multiple threads - these threads will
Basically I need to verify that a certain program is not running before installation.
Basically, I'm using python to print out some information in the terminal and some
Basically, I want to only show these fields if checkbox is selected, if it
Basically I need to get older version of a file in the repository without
Basically I want to check the extended permissions the user has granted my app.
Basically I want to convert a virtual path to an absolute path.
Basically, I have a UIImageView that will loop through 8 PNGs over 0.5 seconds.
basically I want to validate whether or not a text box is a certain

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.