Project
Loading...
Searching...
No Matches
opencl_obtain_program.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
15#ifndef MAKEFILES_OPENCL_OBTAIN_PROGRAMH
16#define MAKEFILES_OPENCL_OBTAIN_PROGRAMH
17
18#include <CL/opencl.h>
19#include <vector>
21
22static int32_t _makefiles_opencl_obtain_program_helper(cl_context context, cl_uint num_devices, cl_device_id* devices, cl_program* program, char* binaries)
23{
24 const char* magic_bytes = "QOCLPB";
25 if (strncmp(magic_bytes, binaries, strlen(magic_bytes)) != 0) {
26 printf("Internal error accessing opencl program\n");
27 return (1);
28 }
29 char* current_ptr = binaries + strlen(magic_bytes) + 1;
31 current_ptr += sizeof(_makefiles_opencl_platform_info);
32
33 if (num_devices != pinfo->count) {
34 printf("Number of devices differs from number of devices in opencl program\n");
35 return (1);
36 }
37 // printf("Obtaining program for OpenCL Platform: (%s %s) %s %s\n", pinfo->platform_profile, pinfo->platform_version, pinfo->platform_vendor, pinfo->platform_name);
38
39 std::vector<size_t> program_sizes(pinfo->count);
40 std::vector<char*> program_binaries(pinfo->count);
41
42 for (uint32_t i = 0; i < pinfo->count; i++) {
43 char device_name[64], device_vendor[64];
44 cl_uint nbits;
45 clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 64, device_name, nullptr);
46 clGetDeviceInfo(devices[i], CL_DEVICE_VENDOR, 64, device_vendor, nullptr);
47 clGetDeviceInfo(devices[i], CL_DEVICE_ADDRESS_BITS, sizeof(nbits), &nbits, nullptr);
49 if (strcmp(device_name, dinfo->device_name) != 0 || strcmp(device_vendor, dinfo->device_vendor) != 0) {
50 printf("Device list is different to device list from opencl program (Device %d: '%s - %s' != '%s - %s')\n", i, device_vendor, device_name, dinfo->device_vendor, dinfo->device_name);
51 return (1);
52 }
53 if (nbits != dinfo->nbits) {
54 printf("Pointer size of device and stored device binary differs\n");
55 return (1);
56 }
57 current_ptr += sizeof(_makefiles_opencl_device_info);
58 // printf("Device %d: %s %s (size %ld)\n", i, dinfo->device_vendor, dinfo->device_name, (int64_t) dinfo->binary_size);
59 program_sizes[i] = dinfo->binary_size;
60 program_binaries[i] = current_ptr;
61 current_ptr += dinfo->binary_size;
62 }
63
64 cl_int return_status[pinfo->count];
65 cl_int ocl_error;
66 *program = clCreateProgramWithBinary(context, num_devices, devices, program_sizes.data(), (const uint8_t**)program_binaries.data(), return_status, &ocl_error);
67
68 if (ocl_error != CL_SUCCESS) {
69 printf("Error loading program\n");
70 return (1);
71 }
72
73 for (uint32_t i = 0; i < pinfo->count; i++) {
74 if (return_status[i] != CL_SUCCESS) {
75 printf("Error loading program for device %d\n", i);
76 clReleaseProgram(*program);
77 return (1);
78 }
79 }
80
81 ocl_error = clBuildProgram(*program, num_devices, devices, "", nullptr, nullptr);
82 if (ocl_error != CL_SUCCESS) {
83 printf("Error building program\n");
84 clReleaseProgram(*program);
85 return (1);
86 }
87
88 return (0);
89}
90
91#endif
int32_t i
GLbitfield GLuint program
Definition glcorearb.h:1905