Path: blob/21.2-virgl/src/gallium/frontends/clover/api/device.cpp
4572 views
//1// Copyright 2012 Francisco Jerez2//3// Permission is hereby granted, free of charge, to any person obtaining a4// copy of this software and associated documentation files (the "Software"),5// to deal in the Software without restriction, including without limitation6// the rights to use, copy, modify, merge, publish, distribute, sublicense,7// and/or sell copies of the Software, and to permit persons to whom the8// Software is furnished to do so, subject to the following conditions:9//10// The above copyright notice and this permission notice shall be included in11// all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR17// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19// OTHER DEALINGS IN THE SOFTWARE.20//2122#include "api/util.hpp"23#include "core/platform.hpp"24#include "core/device.hpp"25#include "git_sha1.h"2627using namespace clover;2829namespace {30std::string31supported_il_versions_as_string(const device &dev) {32std::string il_versions_string;3334for (const auto &il_version : dev.supported_il_versions()) {35if (!il_versions_string.empty())36il_versions_string += " ";3738il_versions_string += std::string(il_version.name) + "_" +39std::to_string(CL_VERSION_MAJOR(il_version.version)) + "." +40std::to_string(CL_VERSION_MINOR(il_version.version));41}42return il_versions_string;43}44}4546CLOVER_API cl_int47clGetDeviceIDs(cl_platform_id d_platform, cl_device_type device_type,48cl_uint num_entries, cl_device_id *rd_devices,49cl_uint *rnum_devices) try {50auto &platform = obj(d_platform);51std::vector<cl_device_id> d_devs;5253if ((!num_entries && rd_devices) ||54(!rnum_devices && !rd_devices))55throw error(CL_INVALID_VALUE);5657// Collect matching devices58for (device &dev : platform) {59if (((device_type & CL_DEVICE_TYPE_DEFAULT) &&60dev == platform.front()) ||61(device_type & dev.type()))62d_devs.push_back(desc(dev));63}6465if (d_devs.empty())66throw error(CL_DEVICE_NOT_FOUND);6768// ...and return the requested data.69if (rnum_devices)70*rnum_devices = d_devs.size();71if (rd_devices)72copy(range(d_devs.begin(),73std::min((unsigned)d_devs.size(), num_entries)),74rd_devices);7576return CL_SUCCESS;7778} catch (error &e) {79return e.get();80}8182CLOVER_API cl_int83clCreateSubDevices(cl_device_id d_dev,84const cl_device_partition_property *props,85cl_uint num_devs, cl_device_id *rd_devs,86cl_uint *rnum_devs) {87// There are no currently supported partitioning schemes.88return CL_INVALID_VALUE;89}9091CLOVER_API cl_int92clRetainDevice(cl_device_id d_dev) try {93obj(d_dev);9495// The reference count doesn't change for root devices.96return CL_SUCCESS;9798} catch (error &e) {99return e.get();100}101102CLOVER_API cl_int103clReleaseDevice(cl_device_id d_dev) try {104obj(d_dev);105106// The reference count doesn't change for root devices.107return CL_SUCCESS;108109} catch (error &e) {110return e.get();111}112113CLOVER_API cl_int114clGetDeviceInfo(cl_device_id d_dev, cl_device_info param,115size_t size, void *r_buf, size_t *r_size) try {116property_buffer buf { r_buf, size, r_size };117auto &dev = obj(d_dev);118119switch (param) {120case CL_DEVICE_TYPE:121buf.as_scalar<cl_device_type>() = dev.type();122break;123124case CL_DEVICE_VENDOR_ID:125buf.as_scalar<cl_uint>() = dev.vendor_id();126break;127128case CL_DEVICE_MAX_COMPUTE_UNITS:129buf.as_scalar<cl_uint>() = dev.max_compute_units();130break;131132case CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS:133buf.as_scalar<cl_uint>() = dev.max_block_size().size();134break;135136case CL_DEVICE_MAX_WORK_ITEM_SIZES:137buf.as_vector<size_t>() = dev.max_block_size();138break;139140case CL_DEVICE_MAX_WORK_GROUP_SIZE:141buf.as_scalar<size_t>() = dev.max_threads_per_block();142break;143144case CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR:145buf.as_scalar<cl_uint>() = 16;146break;147148case CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT:149buf.as_scalar<cl_uint>() = 8;150break;151152case CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT:153buf.as_scalar<cl_uint>() = 4;154break;155156case CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG:157buf.as_scalar<cl_uint>() = 2;158break;159160case CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT:161buf.as_scalar<cl_uint>() = 4;162break;163164case CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE:165buf.as_scalar<cl_uint>() = dev.has_doubles() ? 2 : 0;166break;167168case CL_DEVICE_PREFERRED_VECTOR_WIDTH_HALF:169buf.as_scalar<cl_uint>() = dev.has_halves() ? 8 : 0;170break;171172case CL_DEVICE_MAX_CLOCK_FREQUENCY:173buf.as_scalar<cl_uint>() = dev.max_clock_frequency();174break;175176case CL_DEVICE_ADDRESS_BITS:177buf.as_scalar<cl_uint>() = dev.address_bits();178break;179180case CL_DEVICE_MAX_READ_IMAGE_ARGS:181buf.as_scalar<cl_uint>() = dev.max_images_read();182break;183184case CL_DEVICE_MAX_WRITE_IMAGE_ARGS:185buf.as_scalar<cl_uint>() = dev.max_images_write();186break;187188case CL_DEVICE_MAX_MEM_ALLOC_SIZE:189buf.as_scalar<cl_ulong>() = dev.max_mem_alloc_size();190break;191192case CL_DEVICE_IMAGE2D_MAX_WIDTH:193case CL_DEVICE_IMAGE2D_MAX_HEIGHT:194buf.as_scalar<size_t>() = dev.max_image_size();195break;196197case CL_DEVICE_IMAGE3D_MAX_WIDTH:198case CL_DEVICE_IMAGE3D_MAX_HEIGHT:199case CL_DEVICE_IMAGE3D_MAX_DEPTH:200buf.as_scalar<size_t>() = dev.max_image_size_3d();201break;202203case CL_DEVICE_IMAGE_MAX_BUFFER_SIZE:204buf.as_scalar<size_t>() = dev.max_image_buffer_size();205break;206207case CL_DEVICE_IMAGE_MAX_ARRAY_SIZE:208buf.as_scalar<size_t>() = dev.max_image_array_number();209break;210211case CL_DEVICE_IMAGE_SUPPORT:212buf.as_scalar<cl_bool>() = dev.image_support();213break;214215case CL_DEVICE_MAX_PARAMETER_SIZE:216buf.as_scalar<size_t>() = dev.max_mem_input();217break;218219case CL_DEVICE_MAX_SAMPLERS:220buf.as_scalar<cl_uint>() = dev.max_samplers();221break;222223case CL_DEVICE_MEM_BASE_ADDR_ALIGN:224buf.as_scalar<cl_uint>() = 8 * dev.mem_base_addr_align();225break;226227case CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE:228buf.as_scalar<cl_uint>() = 128;229break;230231case CL_DEVICE_HALF_FP_CONFIG:232// This is the "mandated minimum half precision floating-point233// capability" for OpenCL 1.x.234buf.as_scalar<cl_device_fp_config>() =235CL_FP_INF_NAN | CL_FP_ROUND_TO_NEAREST;236break;237238case CL_DEVICE_SINGLE_FP_CONFIG:239// This is the "mandated minimum single precision floating-point240// capability" for OpenCL 1.1. In OpenCL 1.2, nothing is required for241// custom devices.242buf.as_scalar<cl_device_fp_config>() =243CL_FP_INF_NAN | CL_FP_ROUND_TO_NEAREST;244break;245246case CL_DEVICE_DOUBLE_FP_CONFIG:247if (dev.has_doubles())248// This is the "mandated minimum double precision floating-point249// capability"250buf.as_scalar<cl_device_fp_config>() =251CL_FP_FMA252| CL_FP_ROUND_TO_NEAREST253| CL_FP_ROUND_TO_ZERO254| CL_FP_ROUND_TO_INF255| CL_FP_INF_NAN256| CL_FP_DENORM;257else258buf.as_scalar<cl_device_fp_config>() = 0;259break;260261case CL_DEVICE_GLOBAL_MEM_CACHE_TYPE:262buf.as_scalar<cl_device_mem_cache_type>() = CL_NONE;263break;264265case CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE:266buf.as_scalar<cl_uint>() = 0;267break;268269case CL_DEVICE_GLOBAL_MEM_CACHE_SIZE:270buf.as_scalar<cl_ulong>() = 0;271break;272273case CL_DEVICE_GLOBAL_MEM_SIZE:274buf.as_scalar<cl_ulong>() = dev.max_mem_global();275break;276277case CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE:278buf.as_scalar<cl_ulong>() = dev.max_const_buffer_size();279break;280281case CL_DEVICE_MAX_CONSTANT_ARGS:282buf.as_scalar<cl_uint>() = dev.max_const_buffers();283break;284285case CL_DEVICE_LOCAL_MEM_TYPE:286buf.as_scalar<cl_device_local_mem_type>() = CL_LOCAL;287break;288289case CL_DEVICE_LOCAL_MEM_SIZE:290buf.as_scalar<cl_ulong>() = dev.max_mem_local();291break;292293case CL_DEVICE_ERROR_CORRECTION_SUPPORT:294buf.as_scalar<cl_bool>() = CL_FALSE;295break;296297case CL_DEVICE_PROFILING_TIMER_RESOLUTION:298buf.as_scalar<size_t>() = 0;299break;300301case CL_DEVICE_ENDIAN_LITTLE:302buf.as_scalar<cl_bool>() = (dev.endianness() == PIPE_ENDIAN_LITTLE);303break;304305case CL_DEVICE_AVAILABLE:306case CL_DEVICE_COMPILER_AVAILABLE:307case CL_DEVICE_LINKER_AVAILABLE:308buf.as_scalar<cl_bool>() = CL_TRUE;309break;310311case CL_DEVICE_EXECUTION_CAPABILITIES:312buf.as_scalar<cl_device_exec_capabilities>() = CL_EXEC_KERNEL;313break;314315case CL_DEVICE_QUEUE_PROPERTIES:316buf.as_scalar<cl_command_queue_properties>() = CL_QUEUE_PROFILING_ENABLE;317break;318319case CL_DEVICE_BUILT_IN_KERNELS:320buf.as_string() = "";321break;322323case CL_DEVICE_NAME:324buf.as_string() = dev.device_name();325break;326327case CL_DEVICE_VENDOR:328buf.as_string() = dev.vendor_name();329break;330331case CL_DRIVER_VERSION:332buf.as_string() = PACKAGE_VERSION;333break;334335case CL_DEVICE_PROFILE:336buf.as_string() = "FULL_PROFILE";337break;338339case CL_DEVICE_VERSION:340buf.as_string() = "OpenCL " + dev.device_version_as_string() + " Mesa " PACKAGE_VERSION MESA_GIT_SHA1;341break;342343case CL_DEVICE_EXTENSIONS:344buf.as_string() = dev.supported_extensions_as_string();345break;346347case CL_DEVICE_PLATFORM:348buf.as_scalar<cl_platform_id>() = desc(dev.platform);349break;350351case CL_DEVICE_HOST_UNIFIED_MEMORY:352buf.as_scalar<cl_bool>() = dev.has_unified_memory();353break;354355case CL_DEVICE_NATIVE_VECTOR_WIDTH_CHAR:356buf.as_scalar<cl_uint>() = 16;357break;358359case CL_DEVICE_NATIVE_VECTOR_WIDTH_SHORT:360buf.as_scalar<cl_uint>() = 8;361break;362363case CL_DEVICE_NATIVE_VECTOR_WIDTH_INT:364buf.as_scalar<cl_uint>() = 4;365break;366367case CL_DEVICE_NATIVE_VECTOR_WIDTH_LONG:368buf.as_scalar<cl_uint>() = 2;369break;370371case CL_DEVICE_NATIVE_VECTOR_WIDTH_FLOAT:372buf.as_scalar<cl_uint>() = 4;373break;374375case CL_DEVICE_NATIVE_VECTOR_WIDTH_DOUBLE:376buf.as_scalar<cl_uint>() = dev.has_doubles() ? 2 : 0;377break;378379case CL_DEVICE_NATIVE_VECTOR_WIDTH_HALF:380buf.as_scalar<cl_uint>() = dev.has_halves() ? 8 : 0;381break;382383case CL_DEVICE_OPENCL_C_VERSION:384buf.as_string() = "OpenCL C " + dev.device_clc_version_as_string() + " ";385break;386387case CL_DEVICE_PRINTF_BUFFER_SIZE:388buf.as_scalar<size_t>() = dev.max_printf_buffer_size();389break;390391case CL_DEVICE_PREFERRED_INTEROP_USER_SYNC:392buf.as_scalar<cl_bool>() = CL_TRUE;393break;394395case CL_DEVICE_PARENT_DEVICE:396buf.as_scalar<cl_device_id>() = NULL;397break;398399case CL_DEVICE_PARTITION_MAX_SUB_DEVICES:400buf.as_scalar<cl_uint>() = 0;401break;402403case CL_DEVICE_PARTITION_PROPERTIES:404buf.as_vector<cl_device_partition_property>() =405desc(property_list<cl_device_partition_property>());406break;407408case CL_DEVICE_PARTITION_AFFINITY_DOMAIN:409buf.as_scalar<cl_device_affinity_domain>() = 0;410break;411412case CL_DEVICE_PARTITION_TYPE:413buf.as_vector<cl_device_partition_property>() =414desc(property_list<cl_device_partition_property>());415break;416417case CL_DEVICE_REFERENCE_COUNT:418buf.as_scalar<cl_uint>() = 1;419break;420421case CL_DEVICE_SVM_CAPABILITIES:422case CL_DEVICE_SVM_CAPABILITIES_ARM:423buf.as_scalar<cl_device_svm_capabilities>() = dev.svm_support();424break;425426case CL_DEVICE_NUMERIC_VERSION:427buf.as_scalar<cl_version>() = dev.device_version();428break;429430case CL_DEVICE_OPENCL_C_NUMERIC_VERSION_KHR:431buf.as_scalar<cl_version>() = dev.device_clc_version();432break;433434case CL_DEVICE_OPENCL_C_ALL_VERSIONS:435buf.as_vector<cl_name_version>() = dev.opencl_c_all_versions();436break;437438case CL_DEVICE_EXTENSIONS_WITH_VERSION:439buf.as_vector<cl_name_version>() = dev.supported_extensions();440break;441442case CL_DEVICE_OPENCL_C_FEATURES:443buf.as_vector<cl_name_version>() = dev.opencl_c_features();444break;445446case CL_DEVICE_IL_VERSION:447if (dev.supported_extensions_as_string().find("cl_khr_il_program") == std::string::npos)448throw error(CL_INVALID_VALUE);449buf.as_string() = supported_il_versions_as_string(dev);450break;451452case CL_DEVICE_ILS_WITH_VERSION:453buf.as_vector<cl_name_version>() = dev.supported_il_versions();454break;455456case CL_DEVICE_BUILT_IN_KERNELS_WITH_VERSION:457buf.as_vector<cl_name_version>() = std::vector<cl_name_version>{};458break;459460case CL_DEVICE_MAX_READ_WRITE_IMAGE_ARGS:461case CL_DEVICE_IMAGE_PITCH_ALIGNMENT:462case CL_DEVICE_IMAGE_BASE_ADDRESS_ALIGNMENT:463case CL_DEVICE_PREFERRED_PLATFORM_ATOMIC_ALIGNMENT:464case CL_DEVICE_PREFERRED_GLOBAL_ATOMIC_ALIGNMENT:465case CL_DEVICE_PREFERRED_LOCAL_ATOMIC_ALIGNMENT:466case CL_DEVICE_MAX_NUM_SUB_GROUPS:467case CL_DEVICE_QUEUE_ON_DEVICE_PREFERRED_SIZE:468case CL_DEVICE_QUEUE_ON_DEVICE_MAX_SIZE:469case CL_DEVICE_MAX_ON_DEVICE_QUEUES:470case CL_DEVICE_MAX_ON_DEVICE_EVENTS:471case CL_DEVICE_MAX_PIPE_ARGS:472case CL_DEVICE_PIPE_MAX_ACTIVE_RESERVATIONS:473case CL_DEVICE_PIPE_MAX_PACKET_SIZE:474buf.as_scalar<cl_uint>() = 0;475break;476477case CL_DEVICE_MAX_GLOBAL_VARIABLE_SIZE:478case CL_DEVICE_GLOBAL_VARIABLE_PREFERRED_TOTAL_SIZE:479buf.as_scalar<size_t>() = 0;480break;481482case CL_DEVICE_SUB_GROUP_INDEPENDENT_FORWARD_PROGRESS:483case CL_DEVICE_NON_UNIFORM_WORK_GROUP_SUPPORT:484case CL_DEVICE_WORK_GROUP_COLLECTIVE_FUNCTIONS_SUPPORT:485case CL_DEVICE_GENERIC_ADDRESS_SPACE_SUPPORT:486case CL_DEVICE_PIPE_SUPPORT:487buf.as_scalar<cl_bool>() = CL_FALSE;488break;489490case CL_DEVICE_QUEUE_ON_DEVICE_PROPERTIES:491buf.as_scalar<cl_command_queue_properties>() = 0;492break;493494case CL_DEVICE_ATOMIC_MEMORY_CAPABILITIES:495buf.as_scalar<cl_device_atomic_capabilities>() = (CL_DEVICE_ATOMIC_ORDER_RELAXED |496CL_DEVICE_ATOMIC_SCOPE_WORK_GROUP);497break;498case CL_DEVICE_ATOMIC_FENCE_CAPABILITIES:499buf.as_scalar<cl_device_atomic_capabilities>() = (CL_DEVICE_ATOMIC_ORDER_RELAXED |500CL_DEVICE_ATOMIC_ORDER_ACQ_REL |501CL_DEVICE_ATOMIC_SCOPE_WORK_GROUP);502break;503504case CL_DEVICE_DEVICE_ENQUEUE_CAPABILITIES:505buf.as_scalar<cl_device_device_enqueue_capabilities>() = 0;506break;507508case CL_DEVICE_PREFERRED_WORK_GROUP_SIZE_MULTIPLE:509buf.as_scalar<size_t>() = 1;510break;511512case CL_DEVICE_LATEST_CONFORMANCE_VERSION_PASSED:513buf.as_string() = "";514break;515516default:517throw error(CL_INVALID_VALUE);518}519520return CL_SUCCESS;521522} catch (error &e) {523return e.get();524}525526527