vSMC
vSMC: Scalable Monte Carlo
cl_setup.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/opencl/cl_setup.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013,2014, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_OPENCL_CL_SETUP_HPP
33 #define VSMC_OPENCL_CL_SETUP_HPP
34 
35 #include <vsmc/internal/common.hpp>
37 
38 namespace vsmc {
39 
42 template <typename ID = CLDefault>
43 class CLSetup
44 {
45  public :
46 
47  typedef ID id;
48 
49  static CLSetup<ID> &instance ()
50  {
51  static CLSetup<ID> config;
52 
53  return config;
54  }
55 
57  const std::string &default_name () const {return default_;}
58 
64  bool device_type (const std::string &name)
65  {
66  if (name == std::string("CPU"))
67  device_type(CL_DEVICE_TYPE_CPU);
68  else if (name == std::string("GPU"))
69  device_type(CL_DEVICE_TYPE_GPU);
70  else if (name == std::string("Accelerator"))
71  device_type(CL_DEVICE_TYPE_ACCELERATOR);
72  else {
73  device_type(CL_DEVICE_TYPE_DEFAULT);
74  return false;
75  }
76 
77  return true;
78  }
79 
80  void device_type (::cl_device_type type) {device_type_ = type;}
81  void device (const std::string &name) {device_ = name;}
82  void device_vendor (const std::string &name) {device_vendor_ = name;}
83  void platform (const std::string &name) {platform_ = name;}
84 
85  ::cl_device_type device_type () const {return device_type_;}
86  const std::string &device () const {return device_;}
87  const std::string &device_vendor () const {return device_vendor_;}
88  const std::string &platform () const {return platform_;}
89 
90  bool default_device_type () const
91  {return device_type_ == CL_DEVICE_TYPE_DEFAULT;}
92 
93  bool default_device () const {return default_ == device_;}
94  bool default_device_vendor () const {return default_ == device_vendor_;}
95  bool default_platform () const {return default_ == platform_;}
96 
97  bool check_device (const std::string &name) const
98  {return check_name(name, device_);}
99 
100  bool check_device_vendor (const std::string &name) const
101  {return check_name(name, device_vendor_);}
102 
103  bool check_platform (const std::string &name) const
104  {return check_name(name, platform_);}
105 
106  private :
107 
108  ::cl_device_type device_type_;
109  std::string default_;
110  std::string device_;
111  std::string device_vendor_;
112  std::string platform_;
113 
114  CLSetup () :
115  device_type_(CL_DEVICE_TYPE_DEFAULT), default_("vSMCOpenCLDefault"),
116  device_(default_), device_vendor_(default_), platform_(default_) {}
117 
118  CLSetup (const CLSetup<ID> &);
119  CLSetup<ID> &operator= (const CLSetup<ID> &);
120 
121  bool check_name (const std::string &name, const std::string &stored) const
122  {
123  if (stored == default_)
124  return true;
125  return name.find(stored) != std::string::npos;
126  }
127 }; // class CLSetup
128 
129 } // namespace vsmc
130 
131 #endif // VSMC_OPENCL_CL_SETUP_HPP
Definition: adapter.hpp:37
const std::string & default_name() const
Default string value of the device, vendor, platform names.
Definition: cl_setup.hpp:57
bool check_platform(const std::string &name) const
Definition: cl_setup.hpp:103
::cl_device_type device_type() const
Definition: cl_setup.hpp:85
bool check_device(const std::string &name) const
Definition: cl_setup.hpp:97
void platform(const std::string &name)
Definition: cl_setup.hpp:83
void device_type(::cl_device_type type)
Definition: cl_setup.hpp:80
void device(const std::string &name)
Definition: cl_setup.hpp:81
const std::string & device_vendor() const
Definition: cl_setup.hpp:87
bool default_device_vendor() const
Definition: cl_setup.hpp:94
bool device_type(const std::string &name)
Set the device type using a string value.
Definition: cl_setup.hpp:64
bool default_device() const
Definition: cl_setup.hpp:93
const std::string & platform() const
Definition: cl_setup.hpp:88
static CLSetup< ID > & instance()
Definition: cl_setup.hpp:49
bool check_device_vendor(const std::string &name) const
Definition: cl_setup.hpp:100
const std::string & device() const
Definition: cl_setup.hpp:86
Configure the default behavior of CLManager.
Definition: cl_setup.hpp:43
bool default_device_type() const
Definition: cl_setup.hpp:90
bool default_platform() const
Definition: cl_setup.hpp:95
void device_vendor(const std::string &name)
Definition: cl_setup.hpp:82