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 
29 
33 
34 namespace viennacl
35 {
36 namespace device_specific
37 {
38 
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:
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
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:55
A class representing a compute device (e.g. a GPU)
Definition: device.hpp:49
void add(std::string const &key, template_base const &T, statements_container const &statements)
Implementation of a shared pointer class (cf. std::shared_ptr, boost::shared_ptr). Will be used until C++11 is widely available.
virtual tools::shared_ptr< template_base > clone() const =0
execution_handler(std::string const &program_name_base, viennacl::ocl::context &ctx, viennacl::ocl::device const &device, bool force_recompilation=false)
Helper for compiling a program lazily.
template_base * template_of(std::string const &key)
A shared pointer class similar to boost::shared_ptr. Reimplemented in order to avoid a Boost-dependen...
Definition: shared_ptr.hpp:83
std::string double_support_extension() const
ViennaCL convenience function: Returns the device extension which enables double precision (usually c...
Definition: device.hpp:967
Internal utils.
void execute(container_type::key_type const &key, statements_container const &statements)
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< std::string, tools::shared_ptr< template_base > > container_type