ViennaCL - The Vienna Computing Library: viennacl/device_specific/builtin_database/common.hpp Source File

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
26 #include "viennacl/ocl/device_utils.hpp"
27
28 #include "viennacl/scheduler/forwards.h"
29
30 #include "viennacl/device_specific/forwards.h"
31
32 namespace viennacl
33 {
34 namespace device_specific
35 {
36 namespace builtin_database
37 {
38
39 using scheduler::FLOAT_TYPE;
40 using scheduler::DOUBLE_TYPE;
41 using namespace viennacl::ocl;
42
43 template<class ParamT>
44 class database_type
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; };
55 type map;
56
57 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)
58 {
59 map.d[p0].d[p1].d[p2].d[p3].d.insert(std::make_pair(p4, p5));
60 return *this;
61 }
62
63 database_type<ParamT> & add_1B(vendor_id_type p0, device_type p1, ocl::device_architecture_family p2, device_name_type p3, ParamT const & p5)
64 {
65 return (*this)(p0, p1, p2, p3, scheduler::CHAR_TYPE, p5)
66 (p0, p1, p2, p3, scheduler::UCHAR_TYPE, p5);
67 }
68
69 database_type<ParamT> & add_2B(vendor_id_type p0, device_type p1, ocl::device_architecture_family p2, device_name_type p3, ParamT const & p5)
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
76 database_type<ParamT> & add_4B(vendor_id_type p0, device_type p1, ocl::device_architecture_family p2, device_name_type p3, ParamT const & p5)
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
83 database_type<ParamT> & add_8B(vendor_id_type p0, device_type p1, ocl::device_architecture_family p2, device_name_type p3, ParamT const & p5)
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
90 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
91 {
92 return viennacl::device_specific::at(
93 viennacl::device_specific::at(
94 viennacl::device_specific::at(
95 viennacl::device_specific::at(
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 {
150 scheduler::statement_node_numeric_type numeric_type = scheduler::statement_node_numeric_type(scheduler::result_of::numeric_type_id<NumericT>::value);
151
152 device_type dev_type = device.type() & device_type(0xFE); // chop off 'default' characterization
153 vendor_id_type vendor_id = device.vendor_id();
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
viennacl::device_specific::vendor_id_type
cl_uint vendor_id_type
Definition: forwards.h:212
viennacl::scheduler::INT_TYPE
Definition: forwards.h:294
viennacl::scheduler::HALF_TYPE
Definition: forwards.h:298
viennacl::device_specific::device_name_type
std::string device_name_type
Definition: forwards.h:214
viennacl::scheduler::UCHAR_TYPE
Definition: forwards.h:291
viennacl::device_specific::builtin_database::database_type::device_type_t::map_t
std::map< device_type, device_architecture_t > map_t
Definition: common.hpp:53
viennacl::device_specific::builtin_database::get_mapped_device_name
StringT get_mapped_device_name(StringT const &device_name, vendor_id_type vendor_id)
Definition: common.hpp:108
viennacl::ocl::nvidia_id
Definition: device_utils.hpp:49
viennacl::scheduler::DOUBLE_TYPE
Definition: forwards.h:300
viennacl::device_specific::builtin_database::database_type::expression_t
Definition: common.hpp:50
viennacl::ocl::device
A class representing a compute device (e.g. a GPU)
Definition: device.hpp:49
viennacl::device_specific::builtin_database::get_parameters
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
viennacl::device_specific::builtin_database::database_type::device_name_t
Definition: common.hpp:51
viennacl::ocl::device::type
cl_device_type type() const
The OpenCL device type.
Definition: device.hpp:893
viennacl::device_specific::builtin_database::database_type::type::map_t
std::map< vendor_id_type, device_type_t > map_t
Definition: common.hpp:54
viennacl::device_specific::builtin_database::database_type::at
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
viennacl::device_specific::builtin_database::database_type
Definition: common.hpp:44
viennacl::ocl::device::vendor_id
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
viennacl::scheduler::statement_node_numeric_type
statement_node_numeric_type
Encodes the type of a node in the statement tree.
Definition: forwards.h:286
viennacl::ocl::unknown_id
Definition: device_utils.hpp:51
viennacl::device_specific::builtin_database::database_type::device_architecture_t::map_t
std::map< ocl::device_architecture_family, device_name_t > map_t
Definition: common.hpp:52
viennacl::device_specific::builtin_database::database_type::type
Definition: common.hpp:54
viennacl::device_specific::builtin_database::database_type::add_8B
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
viennacl::scheduler::SHORT_TYPE
Definition: forwards.h:292
viennacl::scheduler::LONG_TYPE
Definition: forwards.h:296
viennacl::device_specific::builtin_database::database_type::map
type map
Definition: common.hpp:55
viennacl::scheduler::CHAR_TYPE
Definition: forwards.h:290
viennacl::device_specific::builtin_database::database_type::add_2B
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
viennacl::device_specific::builtin_database::database_type::add_4B
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
device_utils.hpp
Various utility implementations for dispatching with respect to the different devices available on th...
viennacl::device_specific::builtin_database::database_type::device_name_t::map_t
std::map< device_name_type, expression_t > map_t
Definition: common.hpp:51
forwards.h
Forwards declaration.
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::ocl::vendor_id
vendor_id
Definition: device_utils.hpp:45
forwards.h
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
viennacl::ocl::device::name
std::string name() const
Device name string.
Definition: device.hpp:566
viennacl::scheduler::USHORT_TYPE
Definition: forwards.h:293
viennacl::ocl::device::architecture_family
device_architecture_family architecture_family() const
Device architecture family.
Definition: device.hpp:578
viennacl::scheduler::result_of::numeric_type_id
Helper metafunction for obtaining the runtime type ID for a numerical type.
Definition: forwards.h:310
viennacl::device_specific::builtin_database::database_type::device_architecture_t
Definition: common.hpp:52
viennacl::device_specific::builtin_database::database_type::add_1B
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
viennacl::device_specific::device_type
cl_device_type device_type
Definition: forwards.h:213
viennacl::device_specific::builtin_database::database_type::operator()
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
viennacl::scheduler::UINT_TYPE
Definition: forwards.h:295
viennacl::scheduler::FLOAT_TYPE
Definition: forwards.h:299
viennacl::ocl::device_architecture_family
device_architecture_family
Definition: device_utils.hpp:55
viennacl::device_specific::at
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
viennacl::scheduler::ULONG_TYPE
Definition: forwards.h:297
viennacl::device_specific::builtin_database::database_type::expression_t::map_t
std::map< scheduler::statement_node_numeric_type, ParamT > map_t
Definition: common.hpp:50
viennacl::device_specific::builtin_database::database_type::device_type_t
Definition: common.hpp:53
viennacl::ocl::unknown
Definition: device_utils.hpp:69