From 1a8ad6c2e3beb00d07ef109b984658e08c5729da Mon Sep 17 00:00:00 2001 From: Francisco Jerez Date: Sat, 6 Apr 2013 14:35:00 +0200 Subject: [PATCH] clover: Define platform class and merge with device_registry. Null platform IDs are OK according to the spec, but some applications have been reported to get paranoid and assume that our NULL platform is unusable. As it doesn't hurt to have device enumeration separate from the rest of the device code (quite the opposite, it makes the code cleaner), make the API use an actual platform object that keeps track of the available devices instead of the former NULL pointer. Reported-and-reviewed-by: Tom Stellard Signed-off-by: Francisco Jerez --- src/gallium/state_trackers/clover/Makefile.am | 2 + .../state_trackers/clover/api/context.cpp | 23 ++++--- .../state_trackers/clover/api/device.cpp | 16 ++--- .../state_trackers/clover/api/platform.cpp | 7 +- .../state_trackers/clover/core/device.cpp | 32 ++++----- .../state_trackers/clover/core/device.hpp | 39 ++--------- .../state_trackers/clover/core/platform.cpp | 38 +++++++++++ .../state_trackers/clover/core/platform.hpp | 66 +++++++++++++++++++ 8 files changed, 155 insertions(+), 68 deletions(-) create mode 100644 src/gallium/state_trackers/clover/core/platform.cpp create mode 100644 src/gallium/state_trackers/clover/core/platform.hpp diff --git a/src/gallium/state_trackers/clover/Makefile.am b/src/gallium/state_trackers/clover/Makefile.am index 33ff03d77ee..b4c197ab139 100644 --- a/src/gallium/state_trackers/clover/Makefile.am +++ b/src/gallium/state_trackers/clover/Makefile.am @@ -50,6 +50,8 @@ libclover_la_SOURCES = \ core/format.cpp \ core/memory.hpp \ core/memory.cpp \ + core/platform.hpp \ + core/platform.cpp \ core/resource.hpp \ core/resource.cpp \ core/sampler.hpp \ diff --git a/src/gallium/state_trackers/clover/api/context.cpp b/src/gallium/state_trackers/clover/api/context.cpp index 80afb6bf4ec..99b95666401 100644 --- a/src/gallium/state_trackers/clover/api/context.cpp +++ b/src/gallium/state_trackers/clover/api/context.cpp @@ -41,8 +41,7 @@ clCreateContext(const cl_context_properties *props, cl_uint num_devs, throw error(CL_INVALID_DEVICE); for (auto p : mprops) { - if (!(p.first == CL_CONTEXT_PLATFORM && - (cl_platform_id)p.second == NULL)) + if (p.first != CL_CONTEXT_PLATFORM) throw error(CL_INVALID_PROPERTY); } @@ -61,17 +60,25 @@ clCreateContextFromType(const cl_context_properties *props, cl_device_type type, void (CL_CALLBACK *pfn_notify)( const char *, const void *, size_t, void *), - void *user_data, cl_int *errcode_ret) { + void *user_data, cl_int *errcode_ret) try { + cl_platform_id platform; + cl_uint num_platforms; cl_device_id dev; cl_int ret; - ret = clGetDeviceIDs(0, type, 1, &dev, 0); - if (ret) { - ret_error(errcode_ret, ret); - return NULL; - } + ret = clGetPlatformIDs(1, &platform, &num_platforms); + if (ret || !num_platforms) + throw error(CL_INVALID_PLATFORM); + + ret = clGetDeviceIDs(platform, type, 1, &dev, 0); + if (ret) + throw error(CL_DEVICE_NOT_FOUND); return clCreateContext(props, 1, &dev, pfn_notify, user_data, errcode_ret); + +} catch(error &e) { + ret_error(errcode_ret, e); + return NULL; } PUBLIC cl_int diff --git a/src/gallium/state_trackers/clover/api/device.cpp b/src/gallium/state_trackers/clover/api/device.cpp index cf68d0fc18c..cd8095d3c7e 100644 --- a/src/gallium/state_trackers/clover/api/device.cpp +++ b/src/gallium/state_trackers/clover/api/device.cpp @@ -21,29 +21,25 @@ // #include "api/util.hpp" +#include "core/platform.hpp" #include "core/device.hpp" using namespace clover; -static device_registry registry; - PUBLIC cl_int clGetDeviceIDs(cl_platform_id platform, cl_device_type device_type, cl_uint num_entries, cl_device_id *devices, cl_uint *num_devices) { std::vector devs; - if (platform != NULL) - return CL_INVALID_PLATFORM; - if ((!num_entries && devices) || (!num_devices && !devices)) return CL_INVALID_VALUE; // Collect matching devices - for (device &dev : registry) { + for (device &dev : *platform) { if (((device_type & CL_DEVICE_TYPE_DEFAULT) && - &dev == ®istry.front()) || + &dev == &platform->front()) || (device_type & dev.type())) devs.push_back(&dev); } @@ -223,13 +219,15 @@ clGetDeviceInfo(cl_device_id dev, cl_device_info param, return string_property(buf, size, size_ret, "FULL_PROFILE"); case CL_DEVICE_VERSION: - return string_property(buf, size, size_ret, "OpenCL 1.1 MESA " PACKAGE_VERSION); + return string_property(buf, size, size_ret, + "OpenCL 1.1 MESA " PACKAGE_VERSION); case CL_DEVICE_EXTENSIONS: return string_property(buf, size, size_ret, ""); case CL_DEVICE_PLATFORM: - return scalar_property(buf, size, size_ret, NULL); + return scalar_property(buf, size, size_ret, + &dev->platform); case CL_DEVICE_HOST_UNIFIED_MEMORY: return scalar_property(buf, size, size_ret, CL_TRUE); diff --git a/src/gallium/state_trackers/clover/api/platform.cpp b/src/gallium/state_trackers/clover/api/platform.cpp index f99b6949356..90111c7e2dc 100644 --- a/src/gallium/state_trackers/clover/api/platform.cpp +++ b/src/gallium/state_trackers/clover/api/platform.cpp @@ -21,9 +21,12 @@ // #include "api/util.hpp" +#include "core/platform.hpp" using namespace clover; +static platform __platform; + PUBLIC cl_int clGetPlatformIDs(cl_uint num_entries, cl_platform_id *platforms, cl_uint *num_platforms) { @@ -34,7 +37,7 @@ clGetPlatformIDs(cl_uint num_entries, cl_platform_id *platforms, if (num_platforms) *num_platforms = 1; if (platforms) - *platforms = NULL; + *platforms = &__platform; return CL_SUCCESS; } @@ -42,7 +45,7 @@ clGetPlatformIDs(cl_uint num_entries, cl_platform_id *platforms, PUBLIC cl_int clGetPlatformInfo(cl_platform_id platform, cl_platform_info param_name, size_t size, void *buf, size_t *size_ret) { - if (platform != NULL) + if (platform != &__platform) return CL_INVALID_PLATFORM; switch (param_name) { diff --git a/src/gallium/state_trackers/clover/core/device.cpp b/src/gallium/state_trackers/clover/core/device.cpp index d93a1f6ac6c..fde8135cbeb 100644 --- a/src/gallium/state_trackers/clover/core/device.cpp +++ b/src/gallium/state_trackers/clover/core/device.cpp @@ -38,15 +38,18 @@ namespace { } } -_cl_device_id::_cl_device_id(pipe_loader_device *ldev) : ldev(ldev) { +_cl_device_id::_cl_device_id(clover::platform &platform, + pipe_loader_device *ldev) : + platform(platform), ldev(ldev) { pipe = pipe_loader_create_screen(ldev, PIPE_SEARCH_DIR); if (!pipe || !pipe->get_param(pipe, PIPE_CAP_COMPUTE)) throw error(CL_INVALID_DEVICE); } -_cl_device_id::_cl_device_id(_cl_device_id &&dev) : pipe(dev.pipe), ldev(dev.ldev) { - dev.ldev = NULL; +_cl_device_id::_cl_device_id(_cl_device_id &&dev) : + platform(dev.platform), pipe(dev.pipe), ldev(dev.ldev) { dev.pipe = NULL; + dev.ldev = NULL; } _cl_device_id::~_cl_device_id() { @@ -56,6 +59,16 @@ _cl_device_id::~_cl_device_id() { pipe_loader_release(&ldev, 1); } +_cl_device_id & +_cl_device_id::operator=(_cl_device_id dev) { + assert(&platform == &dev.platform); + + std::swap(pipe, dev.pipe); + std::swap(ldev, dev.ldev); + + return *this; +} + cl_device_type _cl_device_id::type() const { switch (ldev->type) { @@ -179,16 +192,3 @@ _cl_device_id::ir_target() const { PIPE_COMPUTE_CAP_IR_TARGET); return { target.data() }; } - -device_registry::device_registry() { - int n = pipe_loader_probe(NULL, 0); - std::vector ldevs(n); - - pipe_loader_probe(&ldevs.front(), n); - - for (pipe_loader_device *ldev : ldevs) { - try { - devs.emplace_back(ldev); - } catch (error &) {} - } -} diff --git a/src/gallium/state_trackers/clover/core/device.hpp b/src/gallium/state_trackers/clover/core/device.hpp index 5b92751cbcd..09c3a1cc4dd 100644 --- a/src/gallium/state_trackers/clover/core/device.hpp +++ b/src/gallium/state_trackers/clover/core/device.hpp @@ -32,17 +32,20 @@ namespace clover { typedef struct _cl_device_id device; + typedef struct _cl_platform_id platform; class root_resource; class hard_event; } struct _cl_device_id { public: - _cl_device_id(pipe_loader_device *ldev); + _cl_device_id(clover::platform &platform, pipe_loader_device *ldev); _cl_device_id(_cl_device_id &&dev); _cl_device_id(const _cl_device_id &dev) = delete; ~_cl_device_id(); + _cl_device_id &operator=(_cl_device_id dev); + cl_device_type type() const; cl_uint vendor_id() const; size_t max_images_read() const; @@ -70,41 +73,11 @@ public: friend std::set clover::supported_formats(cl_context, cl_mem_object_type); + clover::platform &platform; + private: pipe_screen *pipe; pipe_loader_device *ldev; }; -namespace clover { - /// - /// Container of all the compute devices that are available in the - /// system. - /// - class device_registry { - public: - typedef std::vector::iterator iterator; - - device_registry(); - - iterator begin() { - return devs.begin(); - } - - iterator end() { - return devs.end(); - } - - device &front() { - return devs.front(); - } - - device &back() { - return devs.back(); - } - - protected: - std::vector devs; - }; -} - #endif diff --git a/src/gallium/state_trackers/clover/core/platform.cpp b/src/gallium/state_trackers/clover/core/platform.cpp new file mode 100644 index 00000000000..a17b9925c77 --- /dev/null +++ b/src/gallium/state_trackers/clover/core/platform.cpp @@ -0,0 +1,38 @@ +// +// Copyright 2012 Francisco Jerez +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is 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 Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF +// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +#include "core/platform.hpp" + +using namespace clover; + +_cl_platform_id::_cl_platform_id() { + int n = pipe_loader_probe(NULL, 0); + std::vector ldevs(n); + + pipe_loader_probe(&ldevs.front(), n); + + for (pipe_loader_device *ldev : ldevs) { + try { + devs.emplace_back(*this, ldev); + } catch (error &) {} + } +} diff --git a/src/gallium/state_trackers/clover/core/platform.hpp b/src/gallium/state_trackers/clover/core/platform.hpp new file mode 100644 index 00000000000..eeb6d60a56f --- /dev/null +++ b/src/gallium/state_trackers/clover/core/platform.hpp @@ -0,0 +1,66 @@ +// +// Copyright 2013 Francisco Jerez +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is 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 Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF +// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +#ifndef __CORE_PLATFORM_HPP__ +#define __CORE_PLATFORM_HPP__ + +#include + +#include "core/base.hpp" +#include "core/device.hpp" + +namespace clover { + typedef struct _cl_platform_id platform; +} + +struct _cl_platform_id { +public: + typedef std::vector::iterator iterator; + + _cl_platform_id(); + + /// + /// Container of all compute devices that are available in the platform. + /// + /// @{ + iterator begin() { + return devs.begin(); + } + + iterator end() { + return devs.end(); + } + + clover::device &front() { + return devs.front(); + } + + clover::device &back() { + return devs.back(); + } + /// @} + +protected: + std::vector devs; +}; + +#endif