Gromacs
5.1.4
|
#include "gmxpre.h"
#include "ocl_compiler.h"
#include "config.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include "gromacs/utility/path.h"
#include "gromacs/utility/programcontext.h"
#include "gromacs/utility/stringutil.h"
Define infrastructure for OpenCL JIT compilation for Gromacs.
TODO Currently this file handles compilation of NBNXN kernels, but e.g. organizing the defines for various physics models is leaking in here a bit.
Macros | |
#define | SEPARATOR '/' |
Path separator. | |
Enumerations | |
enum | build_options_index_t { b_invalid_option = 0, b_amd_cpp, b_nvidia_verbose, b_generic_cl11, b_generic_cl12, b_generic_fast_relaxed_math, b_generic_noopt_compilation, b_generic_debug_symbols, b_amd_dump_temp_files, b_include_install_opencl_dir, b_include_source_opencl_dirs, b_num_build_options } |
Compiler options index. | |
Functions | |
static const char * | get_ocl_build_option (build_options_index_t build_option_id) |
Get the string of a build option of the specific id. More... | |
static size_t | get_ocl_build_option_length (build_options_index_t build_option_id) |
Get the size of the string (without null termination) required for the build option of the specific id. More... | |
static size_t | create_ocl_build_options_length (ocl_vendor_id_t build_device_vendor_id, const char *custom_build_options_prepend, const char *custom_build_options_append) |
Get the size of final composed build options literal. More... | |
static char * | create_ocl_build_options (char *build_options_string, size_t build_options_length, ocl_vendor_id_t build_device_vendor_id, const char *custom_build_options_prepend, const char *custom_build_options_append) |
Get the size of final composed build options literal. More... | |
static std::string | get_ocl_root_path () |
Get the path to the main folder storing OpenCL kernels. More... | |
static size_t | get_ocl_kernel_source_file_info (kernel_source_index_t kernel_src_id) |
Get the size of the full kernel source file path and name. More... | |
static char * | get_ocl_kernel_source_path (char *ocl_kernel_filename, kernel_source_index_t kernel_src_id, size_t kernel_filename_len) |
Compose and the full path and name of the kernel src to be used. More... | |
static char * | load_ocl_source (const char *filename, size_t *p_source_length) |
Loads the src inside the file filename onto a string in memory. More... | |
static void | handle_ocl_build_log (const char *build_log, const char *build_options_string, cl_int build_status, kernel_source_index_t kernel_src_id) |
Handles the dumping of the OpenCL JIT compilation log. More... | |
static cl_int | ocl_get_warp_size (cl_context context, cl_device_id device_id) |
Get the warp size reported by device. More... | |
static kernel_vendor_spec_t | ocl_autoselect_kernel_from_vendor (ocl_vendor_id_t vendor_id) |
Automatically select vendor-specific kernel from vendor id. More... | |
static const char * | ocl_get_vendor_specific_define (kernel_vendor_spec_t kernel_spec) |
Returns the compiler define string needed to activate vendor-specific kernels. More... | |
bool | check_ocl_cache (char *ocl_binary_filename, char *build_options_string, char *ocl_source, size_t *ocl_binary_size, unsigned char **ocl_binary) |
Check if there's a valid cache available, and return it if so. More... | |
char * | ocl_get_build_options_string (cl_context context, cl_device_id device_id, kernel_vendor_spec_t kernel_vendor_spec, ocl_vendor_id_t ocl_device_vendor, const char *defines_for_kernel_types, const char *runtime_consts) |
Builds a string with build options for the OpenCL kernels. More... | |
void | print_ocl_binaries_to_file (cl_program program, char *file_name) |
Implement caching of OpenCL binaries. More... | |
cl_int | ocl_compile_program (kernel_source_index_t kernel_source_file, kernel_vendor_spec_t kernel_vendor_spec, const char *defines_for_kernel_types, char *result_str, cl_context context, cl_device_id device_id, ocl_vendor_id_t ocl_device_vendor, cl_program *p_program, const char *runtime_consts) |
Compile the kernels as described by kernel src id and vendor spec. More... | |
Variables | |
static const char * | build_options_list [] |
List of available OpenCL compiler options. More... | |
static const char * | kernel_filenames [] = {"nbnxn_ocl_kernels.cl"} |
Available sources. | |
static const char * | kernel_vendor_spec_definitions [] |
Defines to enable specific kernels based on vendor. More... | |
bool check_ocl_cache | ( | char * | ocl_binary_filename, |
char * | build_options_string, | ||
char * | ocl_source, | ||
size_t * | ocl_binary_size, | ||
unsigned char ** | ocl_binary | ||
) |
Check if there's a valid cache available, and return it if so.
[in] | ocl_binary_filename | Name of file containing the binary cache |
[in] | build_options_string | Compiler command-line options to use (currently unused) |
[in] | ocl_source | NULL-terminated string of OpenCL source code (currently unused) |
[out] | ocl_binary_size | Size of the binary file once loaded in memory |
[out] | ocl_binary | Pointer to the binary file bytes (valid only if return is true) |
|
static |
Get the size of final composed build options literal.
build_options_string | The string where to save the resulting build options in |
build_options_length | The size of the build options |
build_device_vendor_id | Device vendor id. Used to automatically enable some vendor specific options |
custom_build_options_prepend | Prepend options string |
custom_build_options_append | Append options string |
|
static |
Get the size of final composed build options literal.
build_device_vendor_id | Device vendor id. Used to automatically enable some vendor specific options |
custom_build_options_prepend | Prepend options string |
custom_build_options_append | Append options string |
|
static |
Get the string of a build option of the specific id.
build_option_id | The option id as defines in the header |
|
static |
Get the size of the string (without null termination) required for the build option of the specific id.
build_option_id | The option id as defines in the header |
|
static |
Get the size of the full kernel source file path and name.
The following full path size is computed: strlen(ocl_root_path) + strlen(kernel_id.cl) + separator + null term
kernel_src_id | Id of the kernel source (auto,nvidia,amd,nowarp) |
std::bad_alloc | if out of memory |
|
static |
Compose and the full path and name of the kernel src to be used.
ocl_kernel_filename | String where the full path and name will be saved |
kernel_src_id | Id of the kernel source (default) |
kernel_filename_len | Size of the full path and name string, as computed by get_ocl_kernel_source_file_info() |
std::bad_alloc | if out of memory |
|
static |
Get the path to the main folder storing OpenCL kernels.
By default, this function constructs the full path to the OpenCL from the known location of the binary that is running, so that we handle both in-source and installed builds. The user can override this behavior by defining GMX_OCL_FILE_PATH environment variable.
std::bad_alloc | if out of memory. |
|
static |
Handles the dumping of the OpenCL JIT compilation log.
In a debug build: -Success: Save to file kernel_id.SUCCEEDED in the run folder. -Fail : Save to file kernel_id.FAILED in the run folder. Dump to stderr In a release build: -Success: Nothing is logged. -Fail : Save to a file kernel_id.FAILED in the run folder. If GMX_OCL_DUMP_LOG is set, log is always dumped to file If OCL_JIT_DUMP_STDERR is set, log is always dumped to stderr
build_log | String containing the OpenCL JIT compilation log |
build_options_string | String containing the options used for the build |
build_status | The OpenCL type status of the build (CL_SUCCESS etc) |
kernel_src_id | The id of the kernel src used for the build (default) |
std::bad_alloc | if out of memory |
|
static |
Loads the src inside the file filename onto a string in memory.
filename | The name of the file to be read |
p_source_length | Pointer to the size of the source in bytes (without null termination) |
|
static |
Automatically select vendor-specific kernel from vendor id.
vendor_id | Vendor id enumerator (amd,nvidia,intel,unknown) |
cl_int ocl_compile_program | ( | kernel_source_index_t | kernel_source_file, |
kernel_vendor_spec_t | kernel_vendor_spec, | ||
const char * | defines_for_kernel_types, | ||
char * | result_str, | ||
cl_context | context, | ||
cl_device_id | device_id, | ||
ocl_vendor_id_t | ocl_device_vendor, | ||
cl_program * | p_program, | ||
const char * | runtime_consts | ||
) |
Compile the kernels as described by kernel src id and vendor spec.
[in] | kernel_source_file | Index of the kernel src to be used (default) |
[in] | kernel_vendor_spec | Vendor-specific compilation (auto,nvidia,amd,nowarp) |
[in] | defines_for_kernel_types | Preprocessor defines that trigger the compilation of the kernels |
[out] | result_str | Gromacs error string |
[in] | context | Current context on the device to compile for |
[in] | device_id | OpenCL device id of the device to compile for |
[in] | ocl_device_vendor | Enumerator of the device vendor to compile for |
[out] | p_program | Pointer to the cl_program where the compiled cl_program will be stored |
[in] | runtime_consts | Optional string with runtime constants. Each constant is given according to the following format: "-Dname=value". Multiple defines are separated by blanks. |
std::bad_alloc | if out of memory |
char* ocl_get_build_options_string | ( | cl_context | context, |
cl_device_id | device_id, | ||
kernel_vendor_spec_t | kernel_vendor_spec, | ||
ocl_vendor_id_t | ocl_device_vendor, | ||
const char * | defines_for_kernel_types, | ||
const char * | runtime_consts | ||
) |
Builds a string with build options for the OpenCL kernels.
std::bad_alloc | if out of memory |
|
static |
Returns the compiler define string needed to activate vendor-specific kernels.
kernel_spec | Kernel vendor specification |
|
static |
Get the warp size reported by device.
This is platform implementation dependant and seems to only work on the Nvidia and Amd platforms! Nvidia reports 32, Amd for GPU 64. Ignore the rest
context | Current OpenCL context |
device_id | OpenCL device with the context |
void print_ocl_binaries_to_file | ( | cl_program | program, |
char * | file_name | ||
) |
Implement caching of OpenCL binaries.
[in] | program | Index of program to cache |
[in] | file_name | Name of file to use for the cache |
|
static |
List of available OpenCL compiler options.
|
static |
Defines to enable specific kernels based on vendor.