此变量值在函数中如何变化?

时间:2018-07-25 18:04:05

标签: c variables gcc memory opencl

我有此代码:

device_params_t *device_params = malloc(sizeof(device_params_t));

get_device_params(device_params, platform_id, workWithUser ? "check_pdfs_user" : "check_pdfs_owner", program_buffer, program_size);

int password_len = strlen(password_prefix) + password_digits + strlen(password_suffix);

unsigned int prefix_length = strlen(password_prefix), suffix_length = strlen(password_suffix);
unsigned long count;

// while (number < max_password) {
    count = max_password - number;
    count = count > batch_size ? batch_size : count;
    printf("%lu\n", count);
    run_on_gpu(device_params, params, number, password_prefix, prefix_length, password_suffix, suffix_length, password_len, count, &found);
    printf("%lu\n", count);
    if (found) {
        // break;
    }
    number += count;
// }

这是get_device_params函数:

void get_device_params(device_params_t* params, cl_platform_id platform_id, const char *kernel_func, char *program_buffer, size_t program_size) {
    clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 0, NULL, &params->device_count);
    params->device_ids = (cl_device_id*) malloc(sizeof(cl_device_id) * params->device_count);
    int err = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, params->device_count, params->device_ids, NULL);
    if (err != CL_SUCCESS)
    {
        printf("Error: Failed to create a device group! %d\n", err);
        free(params->device_ids);
        return;
    }

    int i;
    size_t local, min_local = 0;

    params->context = clCreateContext(NULL, params->device_count, params->device_ids, NULL, NULL, &err);
    if (err != CL_SUCCESS)
    {
        printf("Error: Failed to create a compute context!\n");
        goto out1;
    }

    cl_program program = build_program(params->context, params->device_ids, program_buffer, program_size);
    if (!program)
    {
        printf("Error: Failed to create compute program!\n");
        goto out1;
    }

    params->kernel = clCreateKernel(program, kernel_func, &err);
    if (!params->kernel || err != CL_SUCCESS)
    {
        printf("Error: Failed to create compute kernel!\n");
        goto out1;
    }

    params->commands = malloc(sizeof(cl_command_queue) * params->device_count);

    for(i = 0; i < params->device_count; i++) {
        params->commands[i] = clCreateCommandQueue(params->context, params->device_ids[i], 0, &err);
        if (!params->commands[i])
        {
            printf("Error: Failed to create a command commands! %d\n", err);
            goto out1;
        }

        err = clGetKernelWorkGroupInfo(params->kernel, params->device_ids[i], CL_KERNEL_WORK_GROUP_SIZE, sizeof(local), &local, NULL);
        if (err != CL_SUCCESS)
        {
            printf("Error: Failed to retrieve kernel work group info! %d\n", err);
            goto out1;
        }

        min_local = min_local > local ? local : min_local;
    }

    params->local = min_local;

    return;

    out1:
    if (params->kernel) {
        clReleaseKernel(params->kernel);
    }
    if (params->commands) {
        for(i = 0; i < params->device_count; i++) {
            clReleaseCommandQueue(params->commands[i]);
        }
    }
    if (params->context) {
        clReleaseKernel(params->context);
    }
    clReleaseProgram(program);

    return;
}

run_on_gpu定义为:

void run_on_gpu(device_params_t *device_params, PDFParams *params, unsigned long start, char *prefix, unsigned int prefix_length, char *suffix, unsigned int suffix_length, int password_length, const unsigned long cnt, bool *found)
{
    printf("size %lu\n", cnt);
}

现在这是怎么回事:

9
size 549755813890
9

我希望是这样

9
size 9
9

不确定发生了什么事!

1 个答案:

答案 0 :(得分:2)

在SWAG中,run_on_gpu()的使用和定义存在参数不匹配的情况。除非您将549755813890视为一个64位十六进制数字,否则它并不是很有趣。这时它变成0x00000080:00000002,冒号插入了32位组之间。这只是让我大叫一声,cnt中的参数run_on_gpu()正在覆盖另外两个32位值。

值得注意的是,这将与64位系统上的GCC一致,但与任何平台上的32位GCC或MSVC不一致。据我所知,64位GCC是唯一将unsigned long视为64位,而所有其他人都将其视为32位的“桌面”编译器。