All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
execution_handler.hpp
Go to the documentation of this file.
#ifndef VIENNACL_DEVICE_SPECIFIC_EXECUTION_HANDLER_HPP
#define VIENNACL_DEVICE_SPECIFIC_EXECUTION_HANDLER_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 <map>
#include "viennacl/tools/shared_ptr.hpp"
#include "viennacl/device_specific/lazy_program_compiler.hpp"
#include "viennacl/device_specific/templates/template_base.hpp"
#include "viennacl/device_specific/utils.hpp"
namespace viennacl
{
namespace device_specific
{
class execution_handler
{
public:
typedef std::map< std::string, tools::shared_ptr<template_base> > container_type;
private:
std::string append_prefix(std::string const & str)
{
return "_" + str;
}
std::string define_extension(std::string const & ext)
{
// Note: On devices without double precision support, 'ext' is an empty string.
return (ext.length() > 1) ? std::string("#pragma OPENCL EXTENSION " + ext + " : enable\n") : std::string("\n");
}
void init_program_compiler(std::string const & name, bool force_recompilation)
{
lazy_programs_.push_back(lazy_program_compiler(&ctx_, name, force_recompilation));
lazy_programs_.back().add(define_extension(device_.double_support_extension()));
}
public:
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)
{
lazy_programs_.reserve(2);
init_program_compiler(program_name_base + "_0", force_recompilation);
init_program_compiler(program_name_base + "_1", force_recompilation);
}
void add(std::string const & key, template_base const & T, statements_container const & statements)
{
if (kernels_.insert(container_type::value_type(key, T.clone())).second)
{
std::vector<std::string> sources = at(kernels_, key)->generate(append_prefix(key), statements, device_);
assert(sources.size()<=2);
for (unsigned int i = 0; i < sources.size(); ++i)
lazy_programs_[i].add(sources[i]);
}
}
template_base * template_of(std::string const & key)
{
return at(kernels_, key).get();
}
void execute(container_type::key_type const & key, statements_container const & statements)
{
tools::shared_ptr<template_base> & template_pointer = at(kernels_, key);
template_pointer->enqueue(append_prefix(key), lazy_programs_, statements);
}
private:
viennacl::ocl::context & ctx_;
viennacl::ocl::device const & device_;
container_type kernels_;
std::vector<std::string> program_names_;
std::vector<lazy_program_compiler> lazy_programs_;
};
}
}
#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