ViennaCL - The Vienna Computing Library: viennacl/device_specific/execution_handler.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
execution_handler.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_DEVICE_SPECIFIC_EXECUTION_HANDLER_HPP
2 #define VIENNACL_DEVICE_SPECIFIC_EXECUTION_HANDLER_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
21
26 #include <map>
27
28 #include "viennacl/tools/shared_ptr.hpp"
29
30 #include "viennacl/device_specific/lazy_program_compiler.hpp"
31 #include "viennacl/device_specific/templates/template_base.hpp"
32 #include "viennacl/device_specific/utils.hpp"
33
34 namespace viennacl
35 {
36 namespace device_specific
37 {
38
39 class execution_handler
40 {
41 public:
42 typedef std::map< std::string, tools::shared_ptr<template_base> > container_type;
43
44 private:
45 std::string append_prefix(std::string const & str)
46 {
47 return "_" + str;
48 }
49
50 std::string define_extension(std::string const & ext)
51 {
52 // Note: On devices without double precision support, 'ext' is an empty string.
53 return (ext.length() > 1) ? std::string("#pragma OPENCL EXTENSION " + ext + " : enable\n") : std::string("\n");
54 }
55
56 void init_program_compiler(std::string const & name, bool force_recompilation)
57 {
58 lazy_programs_.push_back(lazy_program_compiler(&ctx_, name, force_recompilation));
59 lazy_programs_.back().add(define_extension(device_.double_support_extension()));
60 }
61
62 public:
63 execution_handler(std::string const & program_name_base, viennacl::ocl::context & ctx, viennacl::ocl::device const & device, bool force_recompilation = false) : ctx_(ctx), device_(device), program_names_(2)
64 {
65 lazy_programs_.reserve(2);
66 init_program_compiler(program_name_base + "_0", force_recompilation);
67 init_program_compiler(program_name_base + "_1", force_recompilation);
68 }
69
70 void add(std::string const & key, template_base const & T, statements_container const & statements)
71 {
72 if (kernels_.insert(container_type::value_type(key, T.clone())).second)
73 {
74 std::vector<std::string> sources = at(kernels_, key)->generate(append_prefix(key), statements, device_);
75 assert(sources.size()<=2);
76 for (unsigned int i = 0; i < sources.size(); ++i)
77 lazy_programs_[i].add(sources[i]);
78 }
79 }
80
81 template_base * template_of(std::string const & key)
82 {
83 return at(kernels_, key).get();
84 }
85
86 void execute(container_type::key_type const & key, statements_container const & statements)
87 {
88 tools::shared_ptr<template_base> & template_pointer = at(kernels_, key);
89 template_pointer->enqueue(append_prefix(key), lazy_programs_, statements);
90 }
91
92 private:
93 viennacl::ocl::context & ctx_;
94 viennacl::ocl::device const & device_;
95 container_type kernels_;
96 std::vector<std::string> program_names_;
97 std::vector<lazy_program_compiler> lazy_programs_;
98 };
99
100 }
101 }
102 #endif
viennacl::device_specific::execution_handler
Definition: execution_handler.hpp:39
viennacl::ocl::context
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:55
viennacl::ocl::device
A class representing a compute device (e.g. a GPU)
Definition: device.hpp:49
viennacl::device_specific::execution_handler::add
void add(std::string const &key, template_base const &T, statements_container const &statements)
Definition: execution_handler.hpp:70
viennacl::device_specific::template_base
Definition: template_base.hpp:54
template_base.hpp
shared_ptr.hpp
Implementation of a shared pointer class (cf. std::shared_ptr, boost::shared_ptr). Will be used until C++11 is widely available.
viennacl::device_specific::template_base::clone
virtual tools::shared_ptr< template_base > clone() const =0
viennacl::device_specific::execution_handler::execution_handler
execution_handler(std::string const &program_name_base, viennacl::ocl::context &ctx, viennacl::ocl::device const &device, bool force_recompilation=false)
Definition: execution_handler.hpp:63
lazy_program_compiler.hpp
Helper for compiling a program lazily.
viennacl::device_specific::execution_handler::template_of
template_base * template_of(std::string const &key)
Definition: execution_handler.hpp:81
viennacl::tools::shared_ptr
A shared pointer class similar to boost::shared_ptr. Reimplemented in order to avoid a Boost-dependen...
Definition: shared_ptr.hpp:83
viennacl::ocl::device::double_support_extension
std::string double_support_extension() const
ViennaCL convenience function: Returns the device extension which enables double precision (usually c...
Definition: device.hpp:967
utils.hpp
Internal utils.
viennacl::device_specific::execution_handler::execute
void execute(container_type::key_type const &key, statements_container const &statements)
Definition: execution_handler.hpp:86
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::device_specific::execution_handler::container_type
std::map< std::string, tools::shared_ptr< template_base > > container_type
Definition: execution_handler.hpp:42
viennacl::device_specific::statements_container
Definition: forwards.h:262