Bevezetés
Ebben a leckében egy olyan példát mutatunk be, ahol lekérdezzük a platformok számát és azonosítóit. Lehetőség van kiválasztani egy platformot és azután a platformhoz tartozó eszközön tudjuk az adott kernelt lefuttatni
Ebben a leckében egy olyan példát mutatunk be, ahol lekérdezzük a platformok számát és azonosítóit. Lehetőség van kiválasztani egy platformot és azután a platformhoz tartozó eszközön tudjuk az adott kernelt lefuttatni
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#include <iostream>
#include <fstream>
#include <sstream>
#include <CL/cl.h>
#include <Windows.h>
// Konstansok
//
const int ARRAY_SIZE = 40;
const int MAX_PLATFORMS = 5;
const int MAX_DEVICES = 5;
// Platform információ megjelenítése
void DisplayPlatformInfo(
cl_platform_id id,
cl_platform_info name,
std::string str)
{
cl_int errNum;
std::size_t paramValueSize;
errNum = clGetPlatformInfo(
id,
name,
0,
NULL,
¶mValueSize);
if (errNum != CL_SUCCESS)
{
std::cerr << "Failed to find OpenCL platform " << str << "." << std::endl;
return;
}
char * info = (char *)alloca(sizeof(char) * paramValueSize);
errNum = clGetPlatformInfo(
id,
name,
paramValueSize,
info,
NULL);
if (errNum != CL_SUCCESS)
{
std::cerr << "Failed to find OpenCL platform " << str << "." << std::endl;
return;
}
std::cout << "\t" << str << ":\t" << info << std::endl;
}
//
// Platform létrehozása
//
cl_context CreateContext( cl_uint *numDevices, cl_device_id *devices)
{
cl_int errNum;
cl_uint numPlatforms;
cl_platform_id platformIds[MAX_PLATFORMS];
cl_context context = NULL;
// Platofrmok számának a lekérdezése
errNum = clGetPlatformIDs(0, NULL, &numPlatforms);
std::cout << "Number of platforms: \t" << numPlatforms << std::endl;
if (errNum != CL_SUCCESS || numPlatforms <= 0)
{
std::cerr << "Failed to find any OpenCL platforms." << std::endl;
return NULL;
}
// Platformok azonosítójának a lekérdezése
errNum = clGetPlatformIDs(numPlatforms, platformIds, NULL);
// Platform kiválasztása
// Itt az első plaformot válaszottuk ki
cl_uint selPlatform = 0;
DisplayPlatformInfo(
platformIds[selPlatform],
CL_PLATFORM_PROFILE,
"CL_PLATFORM_PROFILE");
DisplayPlatformInfo(
platformIds[selPlatform],
CL_PLATFORM_VERSION,
"CL_PLATFORM_VERSION");
DisplayPlatformInfo(
platformIds[selPlatform],
CL_PLATFORM_VENDOR,
"CL_PLATFORM_VENDOR");
// Platform tulajdonságot tartalmazó struktúra
cl_context_properties contextProperties[] =
{
CL_CONTEXT_PLATFORM,
(cl_context_properties)platformIds[selPlatform],
0
};
// Adott platform eszköz számának a lekérdezése
errNum = clGetDeviceIDs(
platformIds[selPlatform],
CL_DEVICE_TYPE_ALL,
0,
NULL,
numDevices);
std::cout << "\tNumber of devices: \t" << *numDevices << std::endl;
// eszköz azonosítók lekérdezése
errNum = clGetDeviceIDs(
platformIds[selPlatform],
CL_DEVICE_TYPE_ALL,
*numDevices,
devices,
NULL);
// Környezet létrehozása
context = clCreateContext(contextProperties, *numDevices, devices, NULL, NULL, NULL);
return context;
}
//
// Parancssor létrehozása adott környezeten a kiválaszott eszközzel
//
cl_command_queue CreateCommandQueue(cl_context context, cl_device_id *device, cl_uint deviceNum, cl_device_id *devices)
{
//cl_device_id *devices;
cl_command_queue commandQueue = NULL;
size_t deviceBufferSize = -1;
// Adott környezet kiválaszott eszközéhez tartozó parancssor létrehozása
commandQueue = clCreateCommandQueue(context, devices[deviceNum], 0, NULL);
if (commandQueue == NULL)
{
delete[] devices;
std::cerr << "Failed to create commandQueue for device 0";
return NULL;
}
*device = devices[deviceNum];
//delete[] devices;
return commandQueue;
}
//
// OpenCL program létrehozása forrásból
//
cl_program CreateProgram(cl_context context, cl_device_id device, const char* fileName)
{
cl_int errNum;
cl_program program;
std::ifstream kernelFile(fileName, std::ios::in);
if (!kernelFile.is_open())
{
std::cerr << "Failed to open file for reading: " << fileName << std::endl;
return NULL;
}
std::ostringstream oss;
oss << kernelFile.rdbuf();
std::string srcStdStr = oss.str();
const char *srcStr = srcStdStr.c_str();
program = clCreateProgramWithSource(context, 1,
(const char**)&srcStr,
NULL, NULL);
if (program == NULL)
{
std::cerr << "Failed to create CL program from source." << std::endl;
return NULL;
}
errNum = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if (errNum != CL_SUCCESS)
{
// Determine the reason for the error
char buildLog[16384];
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG,
sizeof(buildLog), buildLog, NULL);
std::cerr << "Error in kernel: " << std::endl;
std::cerr << buildLog;
clReleaseProgram(program);
return NULL;
}
return program;
}
//
// Memória objektum létrehozása
//
bool CreateMemObjects(cl_context context, cl_mem memObjects[1])
{
memObjects[0] = clCreateBuffer(context, CL_MEM_READ_WRITE,
sizeof(float) * ARRAY_SIZE, NULL, NULL);
if (memObjects[0] == NULL || memObjects[1] == NULL || memObjects[2] == NULL)
{
std::cerr << "Error creating memory objects." << std::endl;
return false;
}
return true;
}
//
// Erőforrások felszabadítása
//
void Cleanup(cl_context context, cl_command_queue commandQueue,
cl_program program, cl_kernel kernel, cl_mem memObjects[1])
{
for (int i = 0; i < 1; i++)
{
if (memObjects[i] != 0)
clReleaseMemObject(memObjects[i]);
}
if (commandQueue != 0)
clReleaseCommandQueue(commandQueue);
if (kernel != 0)
clReleaseKernel(kernel);
if (program != 0)
clReleaseProgram(program);
if (context != 0)
clReleaseContext(context);
}
//
// Main program
//
int main(int argc, char** argv)
{
cl_context context = 0;
cl_command_queue commandQueue = 0;
cl_program program = 0;
cl_device_id device = 0;
cl_device_id devices[MAX_DEVICES];
cl_uint numDevices;
cl_kernel kernel = 0;
cl_mem memObjects[1] = { 0 };
cl_int errNum;
// OpenCL környezet létrehozása
context = CreateContext(&numDevices, devices);
if (context == NULL)
{
std::cerr << "Failed to create OpenCL context." << std::endl;
return 1;
}
// Parancssor létrehozása
commandQueue = CreateCommandQueue(context, &device, 0, devices);
if (commandQueue == NULL)
{
Cleanup(context, commandQueue, program, kernel, memObjects);
return 1;
}
// Kernel program az adott forrásból
program = CreateProgram(context, device, "device.cl");
if (program == NULL)
{
Cleanup(context, commandQueue, program, kernel, memObjects);
return 1;
}
// Kernel program létrehozása
kernel = clCreateKernel(program, "helloWorld", NULL);
if (kernel == NULL)
{
std::cerr << "Failed to create kernel" << std::endl;
Cleanup(context, commandQueue, program, kernel, memObjects);
return 1;
}
if (!CreateMemObjects(context, memObjects))
{
Cleanup(context, commandQueue, program, kernel, memObjects);
return 1;
}
// Kernel argumentumok beállítása
errNum = clSetKernelArg(kernel, 0, sizeof(cl_mem), &memObjects[0]);
if (errNum != CL_SUCCESS)
{
std::cerr << "Error setting kernel arguments." << std::endl;
Cleanup(context, commandQueue, program, kernel, memObjects);
return 1;
}
size_t globalWorkSize[1] = { ARRAY_SIZE };
size_t localWorkSize[1] = { 1 };
LARGE_INTEGER perfFrequency;
LARGE_INTEGER performanceCountNDRangeStart;
LARGE_INTEGER performanceCountNDRangeStop;
QueryPerformanceCounter(&performanceCountNDRangeStart);
// Kernel elhelyezése a parancssorban
errNum = clEnqueueNDRangeKernel(commandQueue, kernel, 1, NULL,
globalWorkSize, localWorkSize,
0, NULL, NULL);
clFinish(commandQueue);
QueryPerformanceCounter(&performanceCountNDRangeStop);
QueryPerformanceFrequency(&perfFrequency);
printf("NDRange performance counter time %f ms.\n",
1000.0f*(float)(performanceCountNDRangeStop.QuadPart - performanceCountNDRangeStart.QuadPart) / (float)perfFrequency.QuadPart);
if (errNum != CL_SUCCESS)
{
std::cerr << "Error queuing kernel for execution." << std::endl;
Cleanup(context, commandQueue, program, kernel, memObjects);
return 1;
}
char result[ARRAY_SIZE];
// Eredmény betöltése a gazdagép meóriájába
errNum = clEnqueueReadBuffer(commandQueue, memObjects[0], CL_TRUE,
0, ARRAY_SIZE * sizeof(char), result,
0, NULL, NULL);
if (errNum != CL_SUCCESS)
{
std::cerr << "Error reading result buffer." << std::endl;
Cleanup(context, commandQueue, program, kernel, memObjects);
return 1;
}
// Eredmény kiírása
std::cout << "Result: "<< result << std::endl;
std::cout << "Executed program succesfully." << std::endl;
Cleanup(context, commandQueue, program, kernel, memObjects);
system("pause");
return 0;
}
Forrás letöltése itt.
// Add you device OpenCL code
__kernel void helloWorld(__global char* output)
{
int p= get_global_id(0);
switch (p)
{
case 0:
output[p] = 'H';
break;
case 1:
output[p] = 'e';
break;
case 2:
case 3:
case 9:
output[p] = 'l';
break;
case 4:
case 7:
output[p] = 'o';
break;
case 5:
output[p] = ' ';
break;
case 6:
output[p] = 'w';
break;
case 8:
output[p] = 'r';
break;
case 10:
output[p] = 'd';
break;
case 11:
output[p] = '!';
break;
default:
output[p] = '\0';
break;
}
}
Forrás letöltése itt.
Írja át a gazdagép kódját úgy, hogy az összes platform első eszközén fusson le a kernel!
Licensed under the Creative Commons Attribution Share Alike License 4.0