diff options
author | Allan MacKinnon <allanmac@google.com> | 2018-06-19 13:57:04 -0700 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2018-06-20 01:19:18 +0000 |
commit | 4359d529121fc1f39f882693d641c0133d138d41 (patch) | |
tree | d2c3239162e68d24d5c2cebc8a4f6659860cc2a0 /src/compute/common | |
parent | 47c29fa64b3ffc1eec7723d40e9862b2d2a8443f (diff) |
Skia Compute core files
Bug: skia:
Change-Id: I4bba49cf20eff013e581800a3f114c85acd8498c
Reviewed-on: https://skia-review.googlesource.com/135782
Reviewed-by: Mike Klein <mtklein@google.com>
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
Diffstat (limited to 'src/compute/common')
-rw-r--r-- | src/compute/common/cl/assert_cl.c | 222 | ||||
-rw-r--r-- | src/compute/common/cl/assert_cl.h | 56 | ||||
-rw-r--r-- | src/compute/common/cl/find_cl.c | 210 | ||||
-rw-r--r-- | src/compute/common/cl/find_cl.h | 35 | ||||
-rw-r--r-- | src/compute/common/macros.h | 44 | ||||
-rw-r--r-- | src/compute/common/util.c | 86 | ||||
-rw-r--r-- | src/compute/common/util.h | 31 |
7 files changed, 684 insertions, 0 deletions
diff --git a/src/compute/common/cl/assert_cl.c b/src/compute/common/cl/assert_cl.c new file mode 100644 index 0000000000..91e5620c5f --- /dev/null +++ b/src/compute/common/cl/assert_cl.c @@ -0,0 +1,222 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + */ + +// +// +// + +#include <stdlib.h> +#include <stdio.h> + +// +// +// + +#include "assert_cl.h" + +// +// +// + +#define CL_VAL_TO_STRING(err) case err: return #err + +// +// +// + +#define CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR -1000 + +// +// +// + +char const * +cl_get_error_string(cl_int const err) +{ + switch (err) + { + CL_VAL_TO_STRING(CL_SUCCESS); + CL_VAL_TO_STRING(CL_DEVICE_NOT_FOUND); + CL_VAL_TO_STRING(CL_DEVICE_NOT_AVAILABLE); + CL_VAL_TO_STRING(CL_COMPILER_NOT_AVAILABLE); + CL_VAL_TO_STRING(CL_MEM_OBJECT_ALLOCATION_FAILURE); + CL_VAL_TO_STRING(CL_OUT_OF_RESOURCES); + CL_VAL_TO_STRING(CL_OUT_OF_HOST_MEMORY); + CL_VAL_TO_STRING(CL_PROFILING_INFO_NOT_AVAILABLE); + CL_VAL_TO_STRING(CL_MEM_COPY_OVERLAP); + CL_VAL_TO_STRING(CL_IMAGE_FORMAT_MISMATCH); + CL_VAL_TO_STRING(CL_IMAGE_FORMAT_NOT_SUPPORTED); + CL_VAL_TO_STRING(CL_BUILD_PROGRAM_FAILURE); + CL_VAL_TO_STRING(CL_MAP_FAILURE); + CL_VAL_TO_STRING(CL_MISALIGNED_SUB_BUFFER_OFFSET); + CL_VAL_TO_STRING(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST); + CL_VAL_TO_STRING(CL_COMPILE_PROGRAM_FAILURE); + CL_VAL_TO_STRING(CL_LINKER_NOT_AVAILABLE); + CL_VAL_TO_STRING(CL_LINK_PROGRAM_FAILURE); + CL_VAL_TO_STRING(CL_DEVICE_PARTITION_FAILED); + CL_VAL_TO_STRING(CL_KERNEL_ARG_INFO_NOT_AVAILABLE); + CL_VAL_TO_STRING(CL_INVALID_VALUE); + CL_VAL_TO_STRING(CL_INVALID_DEVICE_TYPE); + CL_VAL_TO_STRING(CL_INVALID_PLATFORM); + CL_VAL_TO_STRING(CL_INVALID_DEVICE); + CL_VAL_TO_STRING(CL_INVALID_CONTEXT); + CL_VAL_TO_STRING(CL_INVALID_QUEUE_PROPERTIES); + CL_VAL_TO_STRING(CL_INVALID_COMMAND_QUEUE); + CL_VAL_TO_STRING(CL_INVALID_HOST_PTR); + CL_VAL_TO_STRING(CL_INVALID_MEM_OBJECT); + CL_VAL_TO_STRING(CL_INVALID_IMAGE_FORMAT_DESCRIPTOR); + CL_VAL_TO_STRING(CL_INVALID_IMAGE_SIZE); + CL_VAL_TO_STRING(CL_INVALID_SAMPLER); + CL_VAL_TO_STRING(CL_INVALID_BINARY); + CL_VAL_TO_STRING(CL_INVALID_BUILD_OPTIONS); + CL_VAL_TO_STRING(CL_INVALID_PROGRAM); + CL_VAL_TO_STRING(CL_INVALID_PROGRAM_EXECUTABLE); + CL_VAL_TO_STRING(CL_INVALID_KERNEL_NAME); + CL_VAL_TO_STRING(CL_INVALID_KERNEL_DEFINITION); + CL_VAL_TO_STRING(CL_INVALID_KERNEL); + CL_VAL_TO_STRING(CL_INVALID_ARG_INDEX); + CL_VAL_TO_STRING(CL_INVALID_ARG_VALUE); + CL_VAL_TO_STRING(CL_INVALID_ARG_SIZE); + CL_VAL_TO_STRING(CL_INVALID_KERNEL_ARGS); + CL_VAL_TO_STRING(CL_INVALID_WORK_DIMENSION); + CL_VAL_TO_STRING(CL_INVALID_WORK_GROUP_SIZE); + CL_VAL_TO_STRING(CL_INVALID_WORK_ITEM_SIZE); + CL_VAL_TO_STRING(CL_INVALID_GLOBAL_OFFSET); + CL_VAL_TO_STRING(CL_INVALID_EVENT_WAIT_LIST); + CL_VAL_TO_STRING(CL_INVALID_EVENT); + CL_VAL_TO_STRING(CL_INVALID_OPERATION); + CL_VAL_TO_STRING(CL_INVALID_GL_OBJECT); + CL_VAL_TO_STRING(CL_INVALID_BUFFER_SIZE); + CL_VAL_TO_STRING(CL_INVALID_MIP_LEVEL); + CL_VAL_TO_STRING(CL_INVALID_GLOBAL_WORK_SIZE); + CL_VAL_TO_STRING(CL_INVALID_PROPERTY); + CL_VAL_TO_STRING(CL_INVALID_IMAGE_DESCRIPTOR); + CL_VAL_TO_STRING(CL_INVALID_COMPILER_OPTIONS); + CL_VAL_TO_STRING(CL_INVALID_LINKER_OPTIONS); + CL_VAL_TO_STRING(CL_INVALID_DEVICE_PARTITION_COUNT); + // CL_VAL_TO_STRING(CL_INVALID_PIPE_SIZE); + // CL_VAL_TO_STRING(CL_INVALID_DEVICE_QUEUE); + + // + // Extensions: + // + // cl_khr_gl_sharing + // + CL_VAL_TO_STRING(CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR); + + // + // + // + default: + return "UNKNOWN ERROR CODE"; + } +} + +// +// +// + +cl_int +assert_cl(cl_int const code, char const * const file, int const line, bool const abort) +{ + if (code != CL_SUCCESS) + { + char const * const cl_err_str = cl_get_error_string(code); + + fprintf(stderr, + "\"%s\", line %d: cl_assert (%d) = \"%s\"", + file,line,code,cl_err_str); + + if (abort) + { + // stop profiling and reset device here if necessary + exit(code); + } + } + + return code; +} + +// +// +// + +void +cl_get_event_info(cl_event event, + cl_int * const status, + cl_command_type * const type) +{ + if (status != NULL) { + cl(GetEventInfo(event, + CL_EVENT_COMMAND_EXECUTION_STATUS, + sizeof(*status), + status, + NULL)); + } + + if (type != NULL) { + cl(GetEventInfo(event, + CL_EVENT_COMMAND_TYPE, + sizeof(*type), + type, + NULL)); + } +} + + +char const * +cl_get_event_command_status_string(cl_int const status) +{ + switch (status) + { + CL_VAL_TO_STRING(CL_QUEUED); + CL_VAL_TO_STRING(CL_SUBMITTED); + CL_VAL_TO_STRING(CL_RUNNING); + CL_VAL_TO_STRING(CL_COMPLETE); + + default: + return "UNKNOWN COMMAND STATUS"; + } +} + +char const * +cl_get_event_command_type_string(cl_command_type const type) +{ + switch (type) + { + CL_VAL_TO_STRING(CL_COMMAND_NDRANGE_KERNEL); + CL_VAL_TO_STRING(CL_COMMAND_TASK); + CL_VAL_TO_STRING(CL_COMMAND_NATIVE_KERNEL); + CL_VAL_TO_STRING(CL_COMMAND_READ_BUFFER); + CL_VAL_TO_STRING(CL_COMMAND_WRITE_BUFFER); + CL_VAL_TO_STRING(CL_COMMAND_COPY_BUFFER); + CL_VAL_TO_STRING(CL_COMMAND_READ_IMAGE); + CL_VAL_TO_STRING(CL_COMMAND_WRITE_IMAGE); + CL_VAL_TO_STRING(CL_COMMAND_COPY_IMAGE); + CL_VAL_TO_STRING(CL_COMMAND_COPY_BUFFER_TO_IMAGE); + CL_VAL_TO_STRING(CL_COMMAND_COPY_IMAGE_TO_BUFFER); + CL_VAL_TO_STRING(CL_COMMAND_MAP_BUFFER); + CL_VAL_TO_STRING(CL_COMMAND_MAP_IMAGE); + CL_VAL_TO_STRING(CL_COMMAND_UNMAP_MEM_OBJECT); + CL_VAL_TO_STRING(CL_COMMAND_MARKER); + CL_VAL_TO_STRING(CL_COMMAND_ACQUIRE_GL_OBJECTS); + CL_VAL_TO_STRING(CL_COMMAND_RELEASE_GL_OBJECTS); + CL_VAL_TO_STRING(CL_COMMAND_READ_BUFFER_RECT); + CL_VAL_TO_STRING(CL_COMMAND_WRITE_BUFFER_RECT); + CL_VAL_TO_STRING(CL_COMMAND_COPY_BUFFER_RECT); + CL_VAL_TO_STRING(CL_COMMAND_USER); + CL_VAL_TO_STRING(CL_COMMAND_BARRIER); + CL_VAL_TO_STRING(CL_COMMAND_MIGRATE_MEM_OBJECTS); + CL_VAL_TO_STRING(CL_COMMAND_FILL_BUFFER); + CL_VAL_TO_STRING(CL_COMMAND_FILL_IMAGE); + CL_VAL_TO_STRING(CL_COMMAND_GL_FENCE_SYNC_OBJECT_KHR); + + default: + return "UNKNOWN EVENT COMMAND TYPE"; + } +} diff --git a/src/compute/common/cl/assert_cl.h b/src/compute/common/cl/assert_cl.h new file mode 100644 index 0000000000..5481f3850f --- /dev/null +++ b/src/compute/common/cl/assert_cl.h @@ -0,0 +1,56 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + */ + +#pragma once + +// +// +// + +#include <CL/opencl.h> +#include <stdbool.h> + +// +// +// + +char const * +cl_get_error_string(cl_int const err); + +cl_int +assert_cl(cl_int const code, + char const * const file, + int const line, + bool const abort); + +// +// +// + +#define cl(...) assert_cl((cl##__VA_ARGS__), __FILE__, __LINE__, true); +#define cl_ok(err) assert_cl(err, __FILE__, __LINE__, true); + +// +// +// + +void +cl_get_event_info(cl_event event, + cl_int * const status, + cl_command_type * const type); + +char const * +cl_get_event_command_status_string(cl_int const status); + +char const * +cl_get_event_command_type_string(cl_command_type const type); + +// +// +// + diff --git a/src/compute/common/cl/find_cl.c b/src/compute/common/cl/find_cl.c new file mode 100644 index 0000000000..613d2b9903 --- /dev/null +++ b/src/compute/common/cl/find_cl.c @@ -0,0 +1,210 @@ +/* + * Copyright 2018 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + */ + +// +// +// + +#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +// +// +// + +#include "find_cl.h" +#include "assert_cl.h" +#include "macros.h" + +// +// search platforms and devices for a match +// + +cl_int +clFindIdsByName(char const * const target_platform_substring, + char const * const target_device_substring, + cl_platform_id * const platform_id, + cl_device_id * const device_id, + size_t const matched_device_name_size, + char * const matched_device_name, + size_t * const matched_device_name_size_ret, + bool const is_verbose) +{ + bool match_platform=false, match_device=false; + + // + // get number of platforms + // + cl_uint platform_count; + + cl(GetPlatformIDs(0,NULL,&platform_count)); + + cl_platform_id * const platform_ids = ALLOCA(sizeof(*platform_ids) * platform_count); + + cl(GetPlatformIDs(platform_count,platform_ids,NULL)); + + // + // search platforms + // + for (cl_uint ii=0; ii<platform_count; ii++) + { + size_t platform_name_size; + + cl(GetPlatformInfo(platform_ids[ii], + CL_PLATFORM_NAME, + 0, + NULL, + &platform_name_size)); + + char * const platform_name = ALLOCA(platform_name_size); + + cl(GetPlatformInfo(platform_ids[ii], + CL_PLATFORM_NAME, + platform_name_size, + platform_name, + NULL)); + + + if (!match_platform && (strstr(platform_name,target_platform_substring) != NULL)) + { + match_platform = true; + *platform_id = platform_ids[ii]; + } + + if (is_verbose) { + fprintf(stdout,"%2u: %s\n",ii,platform_name); + } + + // + // find devices for current platform + // + cl_uint device_count; + cl_int cl_err; + + cl_err = clGetDeviceIDs(platform_ids[ii], + CL_DEVICE_TYPE_ALL, + 0, + NULL, + &device_count); + + cl_device_id * const device_ids = ALLOCA(sizeof(*device_ids) * device_count); + + cl_err = clGetDeviceIDs(platform_ids[ii], + CL_DEVICE_TYPE_ALL, + device_count, + device_ids, + NULL); + + if (cl_err != CL_DEVICE_NOT_FOUND) + cl_ok(cl_err); + + for (cl_uint jj=0; jj<device_count; jj++) + { + size_t device_name_size; + size_t driver_version_size; + + cl(GetDeviceInfo(device_ids[jj], + CL_DEVICE_NAME, + 0, + NULL, + &device_name_size)); + + cl(GetDeviceInfo(device_ids[jj], + CL_DRIVER_VERSION, + 0, + NULL, + &driver_version_size)); + + char * const device_name = ALLOCA(device_name_size); + char * const driver_version = ALLOCA(driver_version_size); + + cl(GetDeviceInfo(device_ids[jj], + CL_DEVICE_NAME, + device_name_size, + device_name, + NULL)); + + cl(GetDeviceInfo(device_ids[jj], + CL_DRIVER_VERSION, + driver_version_size, + driver_version, + NULL)); + + if (!match_device && match_platform && (strstr(device_name,target_device_substring) != NULL)) + { + match_device = true; + *device_id = device_ids[jj]; + + if (matched_device_name != NULL) + { + size_t bytes = 0; + + if (matched_device_name_size >= 1) + matched_device_name[matched_device_name_size-1] = 0; + + if (matched_device_name_size > 1) + { + bytes = MIN_MACRO(device_name_size,matched_device_name_size-1); + + memcpy(matched_device_name,device_name,bytes); + } + + if (matched_device_name_size_ret != NULL) + *matched_device_name_size_ret = bytes; + } + + if (is_verbose) { + fprintf(stdout," >>>"); + } + } + else if (is_verbose) + { + fprintf(stdout," "); + } + + if (is_verbose) + { + fprintf(stdout, + " %1u: %s [ %s ]\n", + jj, + device_name, + driver_version); + } + } + } + + if (is_verbose) { + fprintf(stdout,"\n"); + } + + // + // get target platform and device + // + if (!match_platform) + { + if (is_verbose) + fprintf(stderr,"no match for target platform substring %s\n",target_platform_substring); + + return CL_INVALID_PLATFORM; + } + if (!match_device) + { + if (is_verbose) + fprintf(stderr,"no match for target device substring %s\n",target_device_substring); + + return CL_DEVICE_NOT_FOUND; + } + + return CL_SUCCESS; +} + +// +// +// + diff --git a/src/compute/common/cl/find_cl.h b/src/compute/common/cl/find_cl.h new file mode 100644 index 0000000000..5143e39f85 --- /dev/null +++ b/src/compute/common/cl/find_cl.h @@ -0,0 +1,35 @@ +/* + * Copyright 2018 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + */ + +#pragma once + +// +// +// + +#include <CL/opencl.h> +#include <stdbool.h> + +// +// +// + +cl_int +clFindIdsByName(char const * const target_platform_substring, + char const * const target_device_substring, + cl_platform_id * const platform_id, + cl_device_id * const device_id, + size_t const matched_device_name_size, + char * const matched_device_name, + size_t * const matched_device_name_size_ret, + bool const is_verbose); + +// +// +// + diff --git a/src/compute/common/macros.h b/src/compute/common/macros.h new file mode 100644 index 0000000000..35f658f315 --- /dev/null +++ b/src/compute/common/macros.h @@ -0,0 +1,44 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can + * be found in the LICENSE file. + * + */ + +#pragma once + +// +// +// + +#define ARRAY_LENGTH(x) (sizeof(x)/sizeof(x[0])) + +// +// +// + +#define MAX_MACRO(a,b) (((a) > (b)) ? (a) : (b)) +#define MIN_MACRO(a,b) (((a) < (b)) ? (a) : (b)) +#define GTE_MACRO(a,b) ((a) >= (b)) +#define LT_MACRO(a,b) ((a) < (b)) + +// +// +// + +#define BITS_TO_MASK(n) (((uint32_t)1<<(n))-1) +#define BITS_TO_MASK_64(n) (((uint64_t)1<<(n))-1) + +#define BITS_TO_MASK_AT(n,b) (BITS_TO_MASK(n)<<(b)) +#define BITS_TO_MASK_AT_64(n,b) (BITS_TO_MASK_64(n)<<(b)) + +// +// +// + +#define ALLOCA(n) _alloca(n) + +// +// +// diff --git a/src/compute/common/util.c b/src/compute/common/util.c new file mode 100644 index 0000000000..69710125a3 --- /dev/null +++ b/src/compute/common/util.c @@ -0,0 +1,86 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + */ + +#include <intrin.h> + +// +// +// + +#include "util.h" + +// +// +// + +bool +is_pow2_u32(uint32_t n) +{ + return (n & (n-1)) == 0; +} + +// +// +// + +uint32_t +pow2_ru_u32(uint32_t n) +{ + n--; + n |= n >> 1; + n |= n >> 2; + n |= n >> 4; + n |= n >> 8; + n |= n >> 16; + n++; + + return n; +} + +// +// +// + +uint32_t +pow2_rd_u32(uint32_t n) +{ + return 1u << msb_idx_u32(n); +} + +// +// ASSUMES NON-ZERO +// + +uint32_t +msb_idx_u32(uint32_t n) +{ + +#ifdef _MSC_VER + + uint32_t index; + + _BitScanReverse(&index,n); + + return index; + +#elif defined(__GNUC__) + +#error "BUSTED msb_index()" + return 31 - __builtin_clz(mask); + +#else + +#error "No msb_index()" + +#endif + +} + +// +// +// diff --git a/src/compute/common/util.h b/src/compute/common/util.h new file mode 100644 index 0000000000..7d5a7b4600 --- /dev/null +++ b/src/compute/common/util.h @@ -0,0 +1,31 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + */ + +#pragma once + +// +// +// + +#include <stdint.h> +#include <stdbool.h> + +// +// +// + +bool is_pow2_u32(uint32_t n); +uint32_t pow2_ru_u32(uint32_t n); +uint32_t pow2_rd_u32(uint32_t n); +uint32_t msb_idx_u32(uint32_t n); // 0-based bit position + +// +// +// + + |