CLBUILDPROGRAM(3clc)
π NAME π
clBuildProgram β builds (compiles and links) a program executable from the program source or binary. That's it, that's the whole vibe. This is the OpenCL Manual, bestie, and today we're learning how to actually make your program go β¨
cl_int clBuildProgram(cl_program program, cl_uint num_devices,
const cl_device_id *device_list,
const char *options,
void (CL_CALLBACK *pfn_notify)(cl_program program, void *user_data),
void *user_data);
π PARAMETERS π
programβ the program object. the main character, honestly πdevice_listβ a pointer to a list of devices associated withprogram. Ifdevice_listisNULLvalue, the program executable is built for all devices associated withprogramfor which a source or binary has been loaded. Ifdevice_listis a non-NULLvalue, the program executable is built for the devices specified in this list for which a source or binary has been loaded. so like, either everybody or a curated guest list πnum_devicesβ the number of devices listed indevice_list. yes you have to count them yourself. iconic.optionsβ a pointer to a null-terminated string of characters that describes the build options to be used for building the program executable. the full list of supported options is described below, so keep scrolling bestie π«Άpfn_notifyβ a function pointer to a notification routine. The notification routine is a callback function that an application can register and which will be called when the program executable has been built (successfully or unsuccessfully). Ifpfn_notifyis notNULL,clBuildProgramdoes not need to wait for the build to complete and can return immediately once the build operation can begin. The build operation can begin if the context, program whose sources are being compiled and linked, list of devices and build options specified are all valid and appropriate host and device resources needed to perform the build are available. Ifpfn_notifyisNULL,clBuildProgramdoes not return until the build has completed. This callback function may be called asynchronously by the OpenCL implementation. It is the application's responsibility to ensure that the callback function is thread-safe. okay so this is the "text me when it's done" button πuser_dataβ passed as an argument whenpfn_notifyis called.user_datacan beNULL.
π NOTES π
Builds (compiles & links) a program executable from the program source or binary for all the devices or a specific device(s) in the OpenCL context associated with program. OpenCL allows program executables to be built using the source or the binary. clBuildProgram must be called for program created using either clCreateProgramWithSource(3clc) or clCreateProgramWithBinary(3clc) to build the program executable for one or more devices associated with program. If program is created with clCreateProgramWithBinary(3clc), then the program binary must be an executable binary (not a compiled binary or library). no half-finished energy allowed π«
The executable binary can be queried using clGetProgramInfo(3clc)(program, CL_PROGRAM_BINARIES, ...) and can be specified to clCreateProgramWithBinary(3clc) to create a new program object.
π Compiler Options π
The compiler options are categorized as pre-processor options, options for math intrinsics, options that control optimization and miscellaneous options. This specification defines a standard set of options that must be supported by the OpenCL C compiler when building program executables online or offline. These may be extended by a set of vendor- or platform specific options.
π Preprocessor Options π
These options control the OpenCL C preprocessor which is run on each program source before actual compilation.
-D options are processed in the order they are given in the options argument to clBuildProgram or or clCompileProgram.
-D nameβ predefinenameas a macro, with definition1.-D name=definitionβ the contents ofdefinitionare tokenized and processed as if they appeared during translation phase three in a`#define'directive. In particular, the definition will be truncated by embedded newline characters. sneaky newlines, always causing drama π-I dirβ add the directorydirto the list of directories to be searched for header files.
πΈ Math Intrinsics Options πΈ
These options control compiler behavior regarding floating-point arithmetic. These options trade off between speed and correctness.
-cl-single-precision-constantβ treat double precision floating-point constant as single precision constant.-cl-denorms-are-zeroβ this option controls how single precision and double precision denormalized numbers are handled. If specified as a build option, the single precision denormalized numbers may be flushed to zero; double precision denormalized numbers may also be flushed to zero if the optional extension for double precsion is supported. This is intended to be a performance hint and the OpenCL compiler can choose not to flush denorms to zero if the device supports single precision (or double precision) denormalized numbers.
This option is ignored for single precision numbers if the device does not support single precision denormalized numbers i.e. CL_FP_DENORM bit is not set in CL_DEVICE_SINGLE_FP_CONFIG.
This option is ignored for double precision numbers if the device does not support double precision or if it does support double precison but CL_FP_DENORM bit is not set in CL_DEVICE_DOUBLE_FP_CONFIG.
This flag only applies for scalar and vector single precision floating-point variables and computations on these floating-point variables inside a program. It does not apply to reading from or writing to image objects.
-cl-fp32-correctly-rounded-divide-sqrtβ the-cl-fp32-correctly-rounded-divide-sqrtbuild option toclBuildProgramorclCompileProgramallows an application to specify that single precision floating-point divide (x/yand1/x) andsqrtused in the program source are correctly rounded. If this build option is not specified, the minimum numerical accuracy of single precision floating-point divide andsqrtare as defined in section 7.4 of the OpenCL specification.
This build option can only be specified if the CL_FP_CORRECTLY_ROUNDED_DIVIDE_SQRT is set in CL_DEVICE_SINGLE_FP_CONFIG (as defined in in the table of allowed values for param_name for clGetDeviceInfo(3clc)) for devices that the program is being build. clBuildProgram or clCompileProgram will fail to compile the program for a device if the -cl-fp32-correctly-rounded-divide-sqrt option is specified and CL_FP_CORRECTLY_ROUNDED_DIVIDE_SQRT is not set for the device.
heads up bestie: say yes to this option on a device that can't do it and the build just fails. no mercy. π
β¨ Optimization Options β¨
These options control various sorts of optimizations. Turning on optimization flags makes the compiler attempt to improve the performance and/or code size at the expense of compilation time and possibly the ability to debug the program.
-cl-opt-disableβ this option disables all optimizations. The default is optimizations are enabled.
The following options control compiler behavior regarding floating-point arithmetic. These options trade off between performance and correctness and must be specifically enabled. These options are not turned on by default since it can result in incorrect output for programs which depend on an exact implementation of IEEE 754 rules/specifications for math functions.
-cl-mad-enableβ allowa * b + cto be replaced by amad. Themadcomputesa * b + cwith reduced accuracy. For example, some OpenCL devices implementmadas truncate the result ofa * bbefore adding it toc.-cl-no-signed-zerosβ allow optimizations for floating-point arithmetic that ignore the signedness of zero. IEEE 754 arithmetic specifies the distinct behavior of+0.0and-0.0values, which then prohibits simplification of expressions such asx+0.0or0.0*x(even with-clfinite-mathonly). This option implies that the sign of a zero result isn't significant.-cl-unsafe-math-optimizationsβ allow optimizations for floating-point arithmetic that (a) assume that arguments and results are valid, (b) may violate IEEE 754 standard and (c) may violate the OpenCL numerical compliance requirements as defined in section 7.4 for single precision and double precision floating-point, and edge case behavior in section 7.5. This option includes the-cl-no-signed-zerosand-cl-mad-enableoptions.-cl-finite-math-onlyβ allow optimizations for floating-point arithmetic that assume that arguments and results are not NaNs orΒ±β. This option may violate the OpenCL numerical compliance requirements defined in section 7.4 for single precision and double precision floating point, and edge case behavior in section 7.5.-cl-fast-relaxed-mathβ sets the optimization options-cl-finite-math-onlyand-cl-unsafe-math-optimizations. This allows optimizations for floating-point arithmetic that may violate the IEEE 754 standard and the OpenCL numerical compliance requirements defined in the specification in section 7.4 for single-precision and double precision floating-point, and edge case behavior in section 7.5. This option causes the preprocessor macro__FAST_RELAXED_MATH__to be defined in the OpenCL program. π₯
π¦ Options to Request or Suppress Warnings π¦
Warnings are diagnostic messages that report constructions which are not inherently erroneous but which are risky or suggest there may have been an error. The following languageindependent options do not enable specific warnings but control the kinds of diagnostics produced by the OpenCL compiler.
-wβ inhibit all warning messages. shhh. not me ignoring the red flags π-Werrorβ make all warnings into errors. every little thing becomes a whole situation π
π Options Controlling the OpenCL C Version π
The following option controls the version of OpenCL C that the compiler accepts.
-cl-std=β determine the OpenCL C language version to use. A value for this option must be provided. Valid values are:CL1.1- Support all OpenCL C programs that use the OpenCL C language features defined in section 6 of the OpenCL 1.1 specification.CL1.2β Support all OpenCL C programs that use the OpenCL C language features defined in section 6 of the OpenCL 1.2 specification.
Calls to clBuildProgram or clCompileProgram with the -cl-std=CL1.1 option will fail to compile the program for any devices with CL_DEVICE_OPENCL_C_VERSION = OpenCL C 1.0. Calls to clBuildProgram or clCompileProgram with the -cl-std=CL1.2 option will fail to compile the program for any devices with CL_DEVICE_OPENCL_C_VERSION = OpenCL C 1.0 or OpenCL C 1.1.
fr fr, pick your -cl-std= value based on what your devices can actually handle, or the build just won't go π«Ά
If the -cl-std build option isn't specified, then CL_DEVICE_OPENCL_C_VERSION is what picks which version of OpenCL C gets used when the program is compiled for each device. no cap, it just decides for you β¨
π Options for Querying Kernel Argument Information π
-cl-kernel-arg-infoβ this lets the compiler stash the tea about a kernel's (or multiple kernels'!) arguments inside the program executable. What tea exactly? The argument name, its type, and the address and access qualifiers that were used. Wanna go get that info back out? Go read the description ofclGetKernelArgInfo(3clc)for how to query it. π΅οΈβοΈ
π Linker Options π
okay so this spec defines a whole standard set of linker options that the OpenCL C compiler has to support when it's linking compiled programs β online or offline, both. These linker options come in two flavours: library linking options and program linking options. And yeah, vendors or platforms can absolutely add their own extra options on top. β¨
π Library Linking Options π
These are the ones you can specify when you're creating a library of compiled binaries:
-create-libraryβ makes a library out of the compiled binaries you passed in the input_programs argument toclLinkProgram(3clc). π-enable-link-optionsβ lets the linker change the library's behaviour based on one or more link options (the ones described down in Program Linking Options) when this library gets linked with a program executable. Heads up bestie: this option must be specified together with the-create-libraryoption. π«Ά
π§· Program Linking Options π§·
These are the ones you can specify when linking a program executable:
-cl-denorms-are-zero-cl-no-signed-zeroes-cl-unsafe-math-optimizations-cl-finite-math-only-cl-fast-relaxed-mat
The linker may apply these options to all compiled program objects specified to clLinkProgram(3clc). And it may apply these options only to libraries that were created with -enable-link-option. π
π§© Separate Compilation and Linking of Programs π§©
OpenCL 1.2 leveled up how programs get compiled and linked, and it supports the following:
- Separate compilation and link stages. Program sources can be compiled to generate a compiled binary object, and then linked in a separate stage together with other compiled program objects into the program executable. two steps, two vibes π
- Embedded headers. Back in OpenCL 1.0 and 1.1, the
-Ibuild option was the way to specify the list of directories to be searched for header files that get included by a program source (or sources). OpenCL 1.2 extends this by letting the header sources come from program objects instead of just header files. glow up fr fr πΈ - Libraries. The linker can be used to link compiled objects and libraries into a program executable, or to create a library of compiled binaries.
π¨ ERRORS π¨
clBuildProgram returns CL_SUCCESS if the function ran successfully. Otherwise, it returns one of these bad boys:
CL_INVALID_PROGRAMif program is not a valid program object. πCL_INVALID_VALUEif device_list isNULLand num_devices is greater than zero, or if device_list is notNULLand num_devices is zero.CL_INVALID_VALUEif pfn_notify isNULLbut user_data is notNULL.CL_INVALID_DEVICEif the OpenCL devices listed in device_list are not in the list of devices associated with program. πCL_INVALID_BINARYif program was created withclCreateProgramWithBinary(3clc)and the devices listed in device_list don't have a valid program binary loaded.CL_INVALID_BUILD_OPTIONSif the build options specified by options are invalid.CL_INVALID_OPERATIONif the build of a program executable for any of the devices listed in device_list, from a previous call toclBuildProgramfor program, hasn't completed yet. patience, bestie πCL_COMPILER_NOT_AVAILABLEif program was created withclCreateProgramWithSource(3clc)and a compiler isn't available β like,CL_DEVICE_COMPILER_AVAILABLEspecified in the table of OpenCL Device Queries forclGetDeviceInfo(3clc)is set toCL_FALSE.CL_BUILD_PROGRAM_FAILUREif there's a failure to build the program executable. This error gets returned ifclBuildProgramdoesn't return until the build has completed.CL_INVALID_OPERATIONif there are kernel objects attached to program.CL_INVALID_OPERATIONif program was not created withclCreateProgramWithSource(3clc)orclCreateProgramWithBinary(3clc).CL_OUT_OF_RESOURCESif there's a failure to allocate the resources required by the OpenCL implementation on the device.CL_OUT_OF_HOST_MEMORYif there's a failure to allocate the resources required by the OpenCL implementation on the host.
π SPECIFICATION π
OpenCL Specification[1]
π SEE ALSO π
βοΈ AUTHORS βοΈ
The Khronos Group
Β©οΈ COPYRIGHT Β©οΈ
Copyright Β© 2007-2011 The Khronos Group Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and/or associated documentation files (the "Materials"), to deal in the Materials without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, and to permit persons to whom the Materials are furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Materials.
π NOTES π
- OpenCL Specification page 137, section 5.6.2 - Building Program Executables
and that's a wrap, babe π this page is brought to you by The Khronos Group, dated 01/14/2021, and it lives at CLBUILDPROGRAM(3clc). go build something iconic, mwah π
π¦ where this page comes from
- ubuntu jammy: opencl-1.2-man-doc 1.0~svn33624-5 Β· other
- ubuntu noble: opencl-1.2-man-doc 1.0~svn33624-5 Β· other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms