需要在C中将全局变量转换为局部变量

Chu*_*cky 2 c variables global-variables local opencl

基本上,我使用以下全局变量为分配分配了一个OpenCL程序:

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;
Run Code Online (Sandbox Code Playgroud)

现在,我正在尝试将它们全部本地化,以整理程序。

通过将全局变量N从上面的变量移到main()函数中,我转换了它。然后,我更新了每个使用N的函数标头,以将'int N'作为参数,并将N传递给需要它作为参数的任何函数调用。该程序按预期工作。

因此,我想问的是,对于其余的这些变量,会这么简单吗?我了解按引用和值传递的概念,并意识到某些函数可能会更改变量,因此我将需要使用指针引用/解引用。我担心的是我的指针理论有点粗糙,我担心会遇到问题。我也不确定我定义的函数是否可以接受所有这些cl变量。

另外,在函数中使用相同的变量名有什么问题吗?

编辑:

如我所担心的,尝试本地化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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Joe*_*Joe 5

您确实回答了自己的问题。是的,这就是全部。如果您发现为函数生成了大量的参数列表,您可能要考虑将大量相关变量组合到一个结构中,并仅传递指向该结构的指针,仅此而已。(对于传递给任何函数的参数数量,性能方面的考虑很小,但是我认为现在这是不必要的复杂程度!)

但是,要理解C中的指针(通过引用传递的唯一方法)并没有逃脱的步伐,因此像这样的小型项目很可能是增强该知识的理想时机!

好,让我们举个例子,用这种方式总是可以更好地解释生活。

我们有:

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);
}
Run Code Online (Sandbox Code Playgroud)

我们需要达到的是:

// 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);
}
Run Code Online (Sandbox Code Playgroud)

但是每次传递所有三个值可能会很痛苦,并且由于它们是相关的,因此您可以将它们捆绑在一个结构中,然后只传递指向该结构的指针。