ViennaCL - The Vienna Computing Library  1.7.1
Free open-source GPU-accelerated linear algebra and solver library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
common.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_DEVICE_SPECIFIC_BUILTIN_DATABASE_COMMON_HPP_
2 #define VIENNACL_DEVICE_SPECIFIC_BUILTIN_DATABASE_COMMON_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2016, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8  Portions of this software are copyright by UChicago Argonne, LLC.
9 
10  -----------------
11  ViennaCL - The Vienna Computing Library
12  -----------------
13 
14  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15 
16  (A list of authors and contributors can be found in the manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
27 
29 
31 
32 namespace viennacl
33 {
34 namespace device_specific
35 {
36 namespace builtin_database
37 {
38 
41 using namespace viennacl::ocl;
42 
43 template<class ParamT>
45 {
46 public:
47 
48  //Because it would be too easy to use nested maps directly.
49  //THANKS, VISUAL STUDIO.
50  struct expression_t{ typedef std::map<scheduler::statement_node_numeric_type, ParamT> map_t; map_t d; };
51  struct device_name_t{ typedef std::map<device_name_type, expression_t> map_t; map_t d; };
52  struct device_architecture_t{ typedef std::map<ocl::device_architecture_family, device_name_t> map_t; map_t d; };
53  struct device_type_t{ typedef std::map<device_type, device_architecture_t> map_t; map_t d; };
54  struct type{ typedef std::map<vendor_id_type, device_type_t> map_t; map_t d; };
56 
58  {
59  map.d[p0].d[p1].d[p2].d[p3].d.insert(std::make_pair(p4, p5));
60  return *this;
61  }
62 
64  {
65  return (*this)(p0, p1, p2, p3, scheduler::CHAR_TYPE, p5)
66  (p0, p1, p2, p3, scheduler::UCHAR_TYPE, p5);
67  }
68 
70  {
71  return (*this)(p0, p1, p2, p3, scheduler::SHORT_TYPE, p5)
72  (p0, p1, p2, p3, scheduler::USHORT_TYPE, p5)
73  (p0, p1, p2, p3, scheduler::HALF_TYPE, p5);
74  }
75 
77  {
78  return (*this)(p0, p1, p2, p3, scheduler::INT_TYPE, p5)
79  (p0, p1, p2, p3, scheduler::UINT_TYPE, p5)
80  (p0, p1, p2, p3, scheduler::FLOAT_TYPE, p5);
81  }
82 
84  {
85  return (*this)(p0, p1, p2, p3, scheduler::LONG_TYPE, p5)
86  (p0, p1, p2, p3, scheduler::ULONG_TYPE, p5)
87  (p0, p1, p2, p3, scheduler::DOUBLE_TYPE, p5);
88  }
89 
91  {
96  viennacl::device_specific::at(map.d, p0).d,
97  p1).d,
98  p2).d,
99  p3).d,
100  p4);
101  }
102 
103 
104 };
105 
106 
107 template<typename StringT>
108 StringT get_mapped_device_name(StringT const & device_name, vendor_id_type vendor_id)
109 {
110  if (vendor_id == viennacl::ocl::nvidia_id)
111  {
112  vcl_size_t found=0;
113  if ((found = device_name.find("GeForce",0)) != std::string::npos)
114  {
115  if ((found = device_name.find_first_of("123456789", found)) != std::string::npos)
116  {
117  switch (device_name[found]) // GeForce 400 series mapped to GTX 470, GeForce 500 series mapped to GTX 580:
118  {
119  case '4' : return "GeForce GTX 470";
120  case '5' : return "GeForce GTX 570";
121  default: break; // since there is only one Kepler and one Maxwell device in the database, fallback works properly
122  }
123  }
124  }
125  else if ((found = device_name.find("Tesla",0)) != std::string::npos) // map Kepler-based Teslas to K20m
126  {
127  if (device_name.find("Tesla C10",0) != std::string::npos)
128  return "Tesla C2050";
129  else if (device_name.find("Tesla S10",0) != std::string::npos)
130  return "Tesla C2050";
131  else if (device_name.find("Tesla M20",0) != std::string::npos)
132  return "Tesla C2050";
133  else if (device_name.find("Tesla S20",0) != std::string::npos)
134  return "Tesla C2050";
135  else if (device_name.find("Tesla K",0) != std::string::npos) // all Kepler-based Teslas
136  return "Tesla K20m";
137  }
138  }
139 
140  return device_name;
141 }
142 
147 template<class NumericT, class ParamT>
148 inline ParamT const & get_parameters(database_type<ParamT> const & database, viennacl::ocl::device const & device)
149 {
151 
152  device_type dev_type = device.type() & device_type(0xFE); // chop off 'default' characterization
154  ocl::device_architecture_family device_architecture = device.architecture_family();
155  std::string const & device_name = device.name();
156 
157 
158  /*-Vendor ID-*/
159  // std::cout << "Looking up vendor ID..." << std::endl;
160  typename database_type<ParamT>::type::map_t::const_iterator vendor_it = database.map.d.find(vendor_id);
161  //Vendor not recognized => device type default
162  if (vendor_it==database.map.d.end())
163  return database.at(ocl::unknown_id, dev_type, ocl::unknown, "", numeric_type);
164 
165  /*-Device Type-*/
166  // std::cout << "Looking up device type..." << std::endl;
167  typename database_type<ParamT>::device_type_t::map_t::const_iterator device_type_it = vendor_it->second.d.find(dev_type);
168  //Device type not recognized for this vendor => device type default
169  if (device_type_it==vendor_it->second.d.end())
170  return database.at(ocl::unknown_id, dev_type, ocl::unknown, "", numeric_type);
171 
172  /*-Device Architecture-*/
173  // std::cout << "Looking up device architecture..." << std::endl;
174  typename database_type<ParamT>::device_architecture_t::map_t::const_iterator architecture_it = device_type_it->second.d.find(device_architecture);
175  //Architecture not found. We try to find the closest architecture available.
176  if (architecture_it==device_type_it->second.d.end())
177  {
178  typename database_type<ParamT>::device_architecture_t::map_t::const_iterator current_it = device_type_it->second.d.begin();
179  architecture_it = current_it;
180  int closest_arch = current_it->first - device_architecture;
181  while (current_it!=device_type_it->second.d.end())
182  {
183  int arch_diff = std::abs(static_cast<int>(current_it->first) - static_cast<int>(device_architecture));
184  if (arch_diff < closest_arch)
185  {
186  architecture_it = current_it;
187  closest_arch = arch_diff;
188  }
189  current_it++;
190  }
191  }
192 
193  /*-Device Name-*/
194  std::string mapped_device_name = get_mapped_device_name(device_name, device.vendor_id());
195 
196  typename database_type<ParamT>::device_name_t::map_t::const_iterator device_name_it = architecture_it->second.d.find(mapped_device_name);
197  //Name not found. We just take the first device for the architecture
198  if (device_name_it==architecture_it->second.d.end())
199  {
200  device_name_it = architecture_it->second.d.begin();
201  }
202 
203  // std::cout << "Looking up expression name.." << std::endl;
204  /*-Expression-*/
205  typename database_type<ParamT>::expression_t::map_t::const_iterator expression_it = device_name_it->second.d.find(numeric_type);
206  //Expression not found => Vendor default
207  if (expression_it==device_name_it->second.d.end())
208  return database.at(ocl::unknown_id, dev_type, ocl::unknown, "", numeric_type);
209 
210  // std::cout << "Device found in the database! Getting profile..." << std::endl;
211  //Everything okay. Return specific profile//
212  return expression_it->second;
213 }
214 
215 
216 }
217 }
218 }
219 #endif
std::string device_name_type
Definition: forwards.h:214
std::map< device_type, device_architecture_t > map_t
Definition: common.hpp:53
StringT get_mapped_device_name(StringT const &device_name, vendor_id_type vendor_id)
Definition: common.hpp:108
A class representing a compute device (e.g. a GPU)
Definition: device.hpp:49
ParamT const & get_parameters(database_type< ParamT > const &database, viennacl::ocl::device const &device)
Get the profile for a device and a descriptor.
Definition: common.hpp:148
cl_device_type type() const
The OpenCL device type.
Definition: device.hpp:893
std::map< vendor_id_type, device_type_t > map_t
Definition: common.hpp:54
ParamT const & at(vendor_id_type p0, device_type p1, ocl::device_architecture_family p2, device_name_type p3, scheduler::statement_node_numeric_type p4) const
Definition: common.hpp:90
cl_uint vendor_id() const
A unique device vendor identifier. An example of a unique device identifier could be the PCIe ID...
Definition: device.hpp:917
statement_node_numeric_type
Encodes the type of a node in the statement tree.
Definition: forwards.h:286
std::map< ocl::device_architecture_family, device_name_t > map_t
Definition: common.hpp:52
database_type< ParamT > & add_8B(vendor_id_type p0, device_type p1, ocl::device_architecture_family p2, device_name_type p3, ParamT const &p5)
Definition: common.hpp:83
database_type< ParamT > & add_2B(vendor_id_type p0, device_type p1, ocl::device_architecture_family p2, device_name_type p3, ParamT const &p5)
Definition: common.hpp:69
database_type< ParamT > & add_4B(vendor_id_type p0, device_type p1, ocl::device_architecture_family p2, device_name_type p3, ParamT const &p5)
Definition: common.hpp:76
Various utility implementations for dispatching with respect to the different devices available on th...
Forwards declaration.
std::size_t vcl_size_t
Definition: forwards.h:75
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
std::string name() const
Device name string.
Definition: device.hpp:566
device_architecture_family architecture_family() const
Device architecture family.
Definition: device.hpp:578
Helper metafunction for obtaining the runtime type ID for a numerical type.
Definition: forwards.h:310
database_type< ParamT > & add_1B(vendor_id_type p0, device_type p1, ocl::device_architecture_family p2, device_name_type p3, ParamT const &p5)
Definition: common.hpp:63
cl_device_type device_type
Definition: forwards.h:213
database_type< ParamT > & operator()(vendor_id_type p0, device_type p1, ocl::device_architecture_family p2, device_name_type p3, scheduler::statement_node_numeric_type p4, ParamT const &p5)
Definition: common.hpp:57
ValueT const & at(std::map< KeyT, ValueT > const &map, KeyT const &key)
Emulation of C++11's .at() member for std::map<>, const-version.
Definition: forwards.h:142
std::map< scheduler::statement_node_numeric_type, ParamT > map_t
Definition: common.hpp:50