1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include <CL/cl.h>
int main(int argc, char* argv[])
{
cl_int error;
cl_platform_id platformID;
clGetPlatformIDs(1, &platformID, nullptr);
cl_device_id deviceID;
clGetDeviceIDs(platformID, CL_DEVICE_TYPE_GPU, 1, &deviceID, nullptr);
char name[100];
clGetDeviceInfo(deviceID, CL_DEVICE_NAME, 100, name, nullptr);
std::cout << "Device: " << name << std::endl;
const cl_context_properties contextProperties[] =
{
CL_CONTEXT_PLATFORM,
reinterpret_cast<cl_context_properties> (platformID),
0, 0
};
cl_context context = clCreateContext(contextProperties, 1, &deviceID, nullptr, nullptr, &error);
if (error != CL_SUCCESS)
{
//error
}
cl_command_queue queue = clCreateCommandQueue(context, deviceID, 0, &error);
if (error != CL_SUCCESS)
{
//error
}
//Allocation d'une image sur le GPU
static const cl_image_format format = { CL_RGBA, CL_FLOAT };
cl_mem outputImage = clCreateImage2D(context, CL_MEM_WRITE_ONLY, &format, WIDTH, HEIGHT, 0, nullptr, &error);
if (error != CL_SUCCESS)
{
//error
}
//chargement du fichier contenant le code du kernel OpenCL
FILE* file = fopen("kernel1.cl", "rb");
fseek(file, 0, SEEK_END);
const size_t size = ftell(file);
rewind(file);
char* source = new char[size];
fread(source, sizeof(char), size, file);
fclose(file);
// la violation semble se produire lors de cet appel
cl_program program = clCreateProgramWithSource(context, 1, reinterpret_cast<const char**>(source), &size, &error);
if (error != CL_SUCCESS)
{
//error
}
clBuildProgram(program, 1, &deviceID, nullptr, nullptr, nullptr);
[...]
//libération de la mémoire
delete[] source;
clReleaseProgram(program);
clReleaseMemObject(outputImage);
clReleaseCommandQueue(queue);
clReleaseContext(context);
} |
Partager