Выполнение одного и того же ядра несколько раз в OpenCL

Я хочу запускать ядро ​​(одно и то же) несколько раз (скажем, 3 раза) параллельно с OpenCL. Я читал темы с похожими названиями, но я все еще в замешательстве. Я уже написал программу, которая выполняет ее один раз. Я знаю, что мне нужно внести изменения в clEnqueueNDKernelRangeKernel(), и я попытался, но потерпел неудачу. Так любезно может кто-нибудь сказать мне, как я могу сделать это несколько раз. Спасибо за помощь.

//Includes
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif

#define DATA_SIZE 10

using namespace std;

const char *ProgramSource =
"__kernel void add(__global float *inputA, __global float *inputB, __global 
float *inputC, __global float *output)\n"\
"{\n"\
"  size_t id = get_global_id(0);\n"\
"float f;\n"\
"float y1 = 0.0f;\n"\
"y1 = inputA[id] + inputB[id] + inputC[id];\n"\
"  output[id] = y1;\n"\
"}\n";

int main(void)
{
cl_context context;
cl_context_properties properties[3];
cl_kernel kernel;
cl_command_queue command_queue;
cl_program program;
cl_int err;
cl_uint num_of_platforms = 0;
cl_platform_id platform_id;
cl_device_id device_id;
cl_uint num_of_devices = 0;
cl_mem inputA, inputB, inputC, output;

size_t global;

float inputDataA[DATA_SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
float inputDataB[DATA_SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
float inputDataC[DATA_SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };    
float y1[DATA_SIZE] = { 0 };
int i;

// retreive a list of platforms avaible
if (clGetPlatformIDs(1, &platform_id, &num_of_platforms) != CL_SUCCESS)
{
    printf("Unable to get platform_id\n");
    return 1;
}

// try to get a supported GPU device
if (clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, 
&num_of_devices) != CL_SUCCESS)
{
    printf("Unable to get device_id\n");
    return 1;
}

// context properties list - must be terminated with 0
properties[0] = CL_CONTEXT_PLATFORM;
properties[1] = (cl_context_properties)platform_id;
properties[2] = 0;

// create a context with the GPU device
context = clCreateContext(properties, 1, &device_id, NULL, NULL, &err);

// create command queue using the context and device
command_queue = clCreateCommandQueue(context, device_id, 0, &err);

// create a program from the kernel source code
program = clCreateProgramWithSource(context, 1, (const char 
**)&ProgramSource, NULL, &err);

// compile the program
if (clBuildProgram(program, 0, NULL, NULL, NULL, NULL) != CL_SUCCESS)
{
    printf("Error building program\n");
    return 1;
}

// specify which kernel from the program to execute
kernel = clCreateKernel(program, "add", &err);

// create buffers for the input and ouput
inputA = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(float) * DATA_SIZE, NULL, NULL);
inputB = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(float) * DATA_SIZE, NULL, NULL);
inputC = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(float) * DATA_SIZE, NULL, NULL);
output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(float) * DATA_SIZE, NULL, NULL);

// load data into the input buffer
clEnqueueWriteBuffer(command_queue, inputA, CL_TRUE, 0, sizeof(float) * DATA_SIZE, inputDataA, 0, NULL, NULL);
clEnqueueWriteBuffer(command_queue, inputB, CL_TRUE, 0, sizeof(float) * DATA_SIZE, inputDataB, 0, NULL, NULL);
clEnqueueWriteBuffer(command_queue, inputC, CL_TRUE, 0, sizeof(float) * DATA_SIZE, inputDataC, 0, NULL, NULL);
// set the argument list for the kernel command
clSetKernelArg(kernel, 0, sizeof(cl_mem), &inputA);
clSetKernelArg(kernel, 1, sizeof(cl_mem), &inputB);
clSetKernelArg(kernel, 2, sizeof(cl_mem), &inputC);
clSetKernelArg(kernel, 3, sizeof(cl_mem), &output);

global = DATA_SIZE;

clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global, NULL, 0, NULL, NULL);
clFinish(command_queue);

// copy the results from out of the output buffer
clEnqueueReadBuffer(command_queue, output, CL_TRUE, 0, sizeof(float) *DATA_SIZE, y1, 0, NULL, NULL);

// print the results
printf("y1: ");

for (i = 0; i<DATA_SIZE; i++)
{
    printf("%f\n ", y1[i]);
}

// cleanup - release OpenCL resources
clReleaseMemObject(inputA);
clReleaseMemObject(inputB);
clReleaseMemObject(inputC);
clReleaseMemObject(output);
clReleaseProgram(program);
clReleaseKernel(kernel);
clReleaseCommandQueue(command_queue);
clReleaseContext(context);

return 0;

}


person ZSA    schedule 25.07.2018    source источник
comment
я пытался, но не удалось - что вы пытались, как это не удалось?   -  person pmdj    schedule 25.07.2018
comment
Я изменил параметр SetkernelArg(), но он не работал, но теперь я получаю правильные результаты, если напишу команду clEnqueueNDKernelRangeKernel() 3 раза. Но я не уверен, что эти 3 команды работают параллельно независимо друг от друга.   -  person ZSA    schedule 25.07.2018
comment
Вам нужно добавить минимально воспроизводимый пример к вашему вопросу: если мы не сможем воспроизвести ваши результаты, это станет намного сложнее для нас, чтобы дать совет о том, что нужно исправить.   -  person Xirema    schedule 25.07.2018


Ответы (1)


В основном вы работаете с очередью команд и заданным контекстом устройства.

Две вещи:

  • If you have not specified something particularly, the commands will be executed in order and never in parallel independently on the queue.
    • Create multiple queues for each kernel execution in order to execute the kernel in parallel independently on each queue (3 queues for 3 executions).
    • Этот подход является расточительным для многих независимых параллельных исполнений. Так что особой пользы в этом не вижу. Однако: OpenCl - это измерение производительности, чтобы увидеть преимущества подхода, и я не знаю вашего.
  • Лучше напишите функцию, которая выполняет шаги (создание буфера, заполнение буфера, установка аргументов, запрос на выполнение ядра, чтение из буфера, удаление буфера) и вызывайте ее несколько раз. Чтобы максимизировать производительность, создавайте буферы только один раз и удаляйте их по завершении программы. Повторно используйте их как можно чаще (если вы делаете это последовательно).

И последнее, но не менее важное: измерьте увеличение производительности и оцените, какой подход лучше всего подходит для вашей проблемы.

person Akar    schedule 27.07.2018
comment
Я добавил простой пример добавления массива, не могли бы вы сказать мне, где мне нужно внести изменения, чтобы запустить это ядро ​​​​3 раза параллельно независимо. Это очень поможет мне. Спасибо. - person ZSA; 28.07.2018