All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
common.hpp
Go to the documentation of this file.
#ifndef VIENNACL_DEVICE_SPECIFIC_BUILTIN_DATABASE_COMMON_HPP_
#define VIENNACL_DEVICE_SPECIFIC_BUILTIN_DATABASE_COMMON_HPP_
/* =========================================================================
Copyright (c) 2010-2016, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
#include "viennacl/ocl/device_utils.hpp"
#include "viennacl/scheduler/forwards.h"
#include "viennacl/device_specific/forwards.h"
namespace viennacl
{
namespace device_specific
{
namespace builtin_database
{
using scheduler::FLOAT_TYPE;
using scheduler::DOUBLE_TYPE;
using namespace viennacl::ocl;
template<class ParamT>
class database_type
{
public:
//Because it would be too easy to use nested maps directly.
//THANKS, VISUAL STUDIO.
struct expression_t{ typedef std::map<scheduler::statement_node_numeric_type, ParamT> map_t; map_t d; };
struct device_name_t{ typedef std::map<device_name_type, expression_t> map_t; map_t d; };
struct device_architecture_t{ typedef std::map<ocl::device_architecture_family, device_name_t> map_t; map_t d; };
struct device_type_t{ typedef std::map<device_type, device_architecture_t> map_t; map_t d; };
struct type{ typedef std::map<vendor_id_type, device_type_t> map_t; map_t d; };
type map;
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)
{
map.d[p0].d[p1].d[p2].d[p3].d.insert(std::make_pair(p4, p5));
return *this;
}
database_type<ParamT> & add_1B(vendor_id_type p0, device_type p1, ocl::device_architecture_family p2, device_name_type p3, ParamT const & p5)
{
return (*this)(p0, p1, p2, p3, scheduler::CHAR_TYPE, p5)
(p0, p1, p2, p3, scheduler::UCHAR_TYPE, p5);
}
database_type<ParamT> & add_2B(vendor_id_type p0, device_type p1, ocl::device_architecture_family p2, device_name_type p3, ParamT const & p5)
{
return (*this)(p0, p1, p2, p3, scheduler::SHORT_TYPE, p5)
(p0, p1, p2, p3, scheduler::USHORT_TYPE, p5)
(p0, p1, p2, p3, scheduler::HALF_TYPE, p5);
}
database_type<ParamT> & add_4B(vendor_id_type p0, device_type p1, ocl::device_architecture_family p2, device_name_type p3, ParamT const & p5)
{
return (*this)(p0, p1, p2, p3, scheduler::INT_TYPE, p5)
(p0, p1, p2, p3, scheduler::UINT_TYPE, p5)
(p0, p1, p2, p3, scheduler::FLOAT_TYPE, p5);
}
database_type<ParamT> & add_8B(vendor_id_type p0, device_type p1, ocl::device_architecture_family p2, device_name_type p3, ParamT const & p5)
{
return (*this)(p0, p1, p2, p3, scheduler::LONG_TYPE, p5)
(p0, p1, p2, p3, scheduler::ULONG_TYPE, p5)
(p0, p1, p2, p3, scheduler::DOUBLE_TYPE, p5);
}
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
{
return viennacl::device_specific::at(
viennacl::device_specific::at(
viennacl::device_specific::at(
viennacl::device_specific::at(
viennacl::device_specific::at(map.d, p0).d,
p1).d,
p2).d,
p3).d,
p4);
}
};
template<typename StringT>
StringT get_mapped_device_name(StringT const & device_name, vendor_id_type vendor_id)
{
if (vendor_id == viennacl::ocl::nvidia_id)
{
vcl_size_t found=0;
if ((found = device_name.find("GeForce",0)) != std::string::npos)
{
if ((found = device_name.find_first_of("123456789", found)) != std::string::npos)
{
switch (device_name[found]) // GeForce 400 series mapped to GTX 470, GeForce 500 series mapped to GTX 580:
{
case '4' : return "GeForce GTX 470";
case '5' : return "GeForce GTX 570";
default: break; // since there is only one Kepler and one Maxwell device in the database, fallback works properly
}
}
}
else if ((found = device_name.find("Tesla",0)) != std::string::npos) // map Kepler-based Teslas to K20m
{
if (device_name.find("Tesla C10",0) != std::string::npos)
return "Tesla C2050";
else if (device_name.find("Tesla S10",0) != std::string::npos)
return "Tesla C2050";
else if (device_name.find("Tesla M20",0) != std::string::npos)
return "Tesla C2050";
else if (device_name.find("Tesla S20",0) != std::string::npos)
return "Tesla C2050";
else if (device_name.find("Tesla K",0) != std::string::npos) // all Kepler-based Teslas
return "Tesla K20m";
}
}
return device_name;
}
template<class NumericT, class ParamT>
inline ParamT const & get_parameters(database_type<ParamT> const & database, viennacl::ocl::device const & device)
{
scheduler::statement_node_numeric_type numeric_type = scheduler::statement_node_numeric_type(scheduler::result_of::numeric_type_id<NumericT>::value);
device_type dev_type = device.type() & device_type(0xFE); // chop off 'default' characterization
vendor_id_type vendor_id = device.vendor_id();
ocl::device_architecture_family device_architecture = device.architecture_family();
std::string const & device_name = device.name();
/*-Vendor ID-*/
// std::cout << "Looking up vendor ID..." << std::endl;
typename database_type<ParamT>::type::map_t::const_iterator vendor_it = database.map.d.find(vendor_id);
//Vendor not recognized => device type default
if (vendor_it==database.map.d.end())
return database.at(ocl::unknown_id, dev_type, ocl::unknown, "", numeric_type);
/*-Device Type-*/
// std::cout << "Looking up device type..." << std::endl;
typename database_type<ParamT>::device_type_t::map_t::const_iterator device_type_it = vendor_it->second.d.find(dev_type);
//Device type not recognized for this vendor => device type default
if (device_type_it==vendor_it->second.d.end())
return database.at(ocl::unknown_id, dev_type, ocl::unknown, "", numeric_type);
/*-Device Architecture-*/
// std::cout << "Looking up device architecture..." << std::endl;
typename database_type<ParamT>::device_architecture_t::map_t::const_iterator architecture_it = device_type_it->second.d.find(device_architecture);
//Architecture not found. We try to find the closest architecture available.
if (architecture_it==device_type_it->second.d.end())
{
typename database_type<ParamT>::device_architecture_t::map_t::const_iterator current_it = device_type_it->second.d.begin();
architecture_it = current_it;
int closest_arch = current_it->first - device_architecture;
while (current_it!=device_type_it->second.d.end())
{
int arch_diff = std::abs(static_cast<int>(current_it->first) - static_cast<int>(device_architecture));
if (arch_diff < closest_arch)
{
architecture_it = current_it;
closest_arch = arch_diff;
}
current_it++;
}
}
/*-Device Name-*/
std::string mapped_device_name = get_mapped_device_name(device_name, device.vendor_id());
typename database_type<ParamT>::device_name_t::map_t::const_iterator device_name_it = architecture_it->second.d.find(mapped_device_name);
//Name not found. We just take the first device for the architecture
if (device_name_it==architecture_it->second.d.end())
{
device_name_it = architecture_it->second.d.begin();
}
// std::cout << "Looking up expression name.." << std::endl;
/*-Expression-*/
typename database_type<ParamT>::expression_t::map_t::const_iterator expression_it = device_name_it->second.d.find(numeric_type);
//Expression not found => Vendor default
if (expression_it==device_name_it->second.d.end())
return database.at(ocl::unknown_id, dev_type, ocl::unknown, "", numeric_type);
// std::cout << "Device found in the database! Getting profile..." << std::endl;
//Everything okay. Return specific profile//
return expression_it->second;
}
}
}
}
#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