OpenCL Kernel Compilation
Slides taken from Hands On OpenCL by Simon McIntosh-Smith, Tom Deakin, James Price, Tim Mattson and Benedict Gaster under the "attribution CC BY" creative commons license.
OpenCL Kernel Compilation Slides taken from Hands On OpenCL by Simon - - PowerPoint PPT Presentation
OpenCL Kernel Compilation Slides taken from Hands On OpenCL by Simon McIntosh-Smith, Tom Deakin, James Price, Tim Mattson and Benedict Gaster under the "attribution CC BY" creative commons license. Shipping OpenCL Kernels OpenCL
Slides taken from Hands On OpenCL by Simon McIntosh-Smith, Tom Deakin, James Price, Tim Mattson and Benedict Gaster under the "attribution CC BY" creative commons license.
* OpenCL 2.2 C++ kernels are offline compiled – more later
5
6
7
// Create and compile program program = clCreateProgramWithSource(context, 1, &kernel_source, NULL, NULL); clBuildProgram(program, 0, NULL, NULL, NULL, NULL); // Get compiled binary from runtime size_t size; clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t), &size, NULL); unsigned char *binaries = malloc(sizeof(unsigned char) * size); clGetProgramInfo(program, CL_PROGRAM_BINARIES, size, &binaries, NULL); // Then write binary to file …
// Load compiled program binary from file … // Create program using binary program = clCreateProgramWithBinary(context, 1, devices, &size, &binaries,NULL,NULL); clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
9
10
11
– Two different ‘flavors’ of SPIR-V – Environment specifications describe which features supported by each
– Open-source tools* provided for SPIR-V<->LLVM translation
– OpenCL 2.2 introduces a C++ kernel language using SPIR-V 1.2
– Lowered to native ISA at runtime
*http://github.khronos.org
12
(IWOCL 2015, Stanford University)
13
14
15
clGetProgramInfo(program,CL_PROGRAM_NUM_KERNELS, …); clGetProgramInfo(program,CL_PROGRAM_KERNEL_NAMES, …);
clGetKernelInfo(kernel, CL_KERNEL_NUM_ARGS, …); clGetKernelInfo(kernel, CL_KERNEL_ARG_*, …);
17
19
20
21
__attribute__((reqd_work_group_size(x, y, z)))
22
23
Passing the value as an argument
kernel void vecmul( global float *data, const float factor) { int i = get_global_id(0); data[i] *= factor; } clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
Value of ‘factor’ not known at application build time (e.g. passed as a command-line argument)
24
Passing the value as an argument
kernel void vecmul( global float *data, const float factor) { int i = get_global_id(0); data[i] *= factor; } clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
Defining the value as a preprocessor macro
kernel void vecmul( global float *data) { int i = get_global_id(0); data[i] *= factor; } sprintf(options, “-Dfactor=%f”, userFactor); clBuildProgram(program, 0, NULL,
25
26
// OpenCL C++ kernel code // Create specialization constant with ID 1 and default value of 3.0f cl::spec_constant<float, 1> factor = {3.0f}; data[i] *= factor.get(); // Host code // Set value of specialization constant and then build program cl_uint spec_id = 1; clSetProgramSpecializationConstant(program, spec_id, sizeof(float), &userFactor); clBuildProgram(program, 1, &device, "", NULL, NULL);
27
32
– For block-based algorithms (e.g. matrix multiplication) – Different devices might run faster on different block sizes
– Array of Structures or Structure of Arrays (AoS vs. SoA) – Column or Row major
– Use of local memory or not – Extra loads and stores assist hardware cache?
– Related to data layout – Also how you parallelize the work
– Specific hardware differences – Built-in trig / special function hardware – Double vs. float (vs. half)
From Zhang, Sinclair II and Chien: Improving Performance Portability in OpenCL Programs – ISC13
33
34
X values Y values Runtimes – lower is better Best: 60x1 Collected with Flamingo (mistymountain.co.uk/flamingo)
35
36
“Analyzing and improving performance portability of OpenCL applications via auto-tuning”, J.Price and S.McIntosh-Smith, IWOCL 2017, https://dl.acm.org/citation.cfm?id=3078173