All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
entry_proxy.hpp
Go to the documentation of this file.
#ifndef VIENNACL_TOOLS_ENTRY_PROXY_HPP_
#define VIENNACL_TOOLS_ENTRY_PROXY_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/forwards.h"
#include "viennacl/scalar.hpp"
namespace viennacl
{
//proxy class for single vector entries (this is a slow operation!!)
template<typename NumericT>
class entry_proxy
{
public:
typedef viennacl::backend::mem_handle handle_type;
explicit entry_proxy(vcl_size_t mem_offset,
handle_type & mem_handle)
: index_(mem_offset), mem_handle_(mem_handle) {}
//operators:
entry_proxy & operator+=(NumericT value)
{
NumericT temp = read();
temp += value;
write(temp);
return *this;
}
entry_proxy & operator-=(NumericT value)
{
NumericT temp = read();
temp -= value;
write(temp);
return *this;
}
entry_proxy & operator*=(NumericT value)
{
NumericT temp = read();
temp *= value;
write(temp);
return *this;
}
entry_proxy & operator/=(NumericT value)
{
NumericT temp = read();
temp /= value;
write(temp);
return *this;
}
entry_proxy & operator=(NumericT value)
{
write(value);
return *this;
}
entry_proxy & operator=(scalar<NumericT> const & value)
{
viennacl::backend::memory_copy(value.handle(), mem_handle_, 0, sizeof(NumericT)*index_, sizeof(NumericT));
return *this;
}
entry_proxy & operator=(entry_proxy const & other)
{
viennacl::backend::memory_copy(other.handle(), mem_handle_, sizeof(NumericT) * other.index_, sizeof(NumericT)*index_, sizeof(NumericT));
return *this;
}
//type conversion:
// allows to write something like:
// double test = vector(4);
operator NumericT () const
{
NumericT temp = read();
return temp;
}
vcl_size_t index() const { return index_; }
handle_type const & handle() const { return mem_handle_; }
private:
NumericT read() const
{
NumericT temp;
viennacl::backend::memory_read(mem_handle_, sizeof(NumericT)*index_, sizeof(NumericT), &temp);
return temp;
}
void write(NumericT value)
{
viennacl::backend::memory_write(mem_handle_, sizeof(NumericT)*index_, sizeof(NumericT), &value);
}
vcl_size_t index_;
viennacl::backend::mem_handle & mem_handle_;
}; //entry_proxy
template<typename NumericT>
class const_entry_proxy
{
typedef const_entry_proxy<NumericT> self_type;
public:
typedef viennacl::backend::mem_handle handle_type;
explicit const_entry_proxy(vcl_size_t mem_offset,
handle_type const & mem_handle)
: index_(mem_offset), mem_handle_(mem_handle) {}
//type conversion:
// allows to write something like:
// double test = vector(4);
operator NumericT () const
{
NumericT temp = read();
return temp;
}
unsigned int index() const { return index_; }
handle_type const & handle() const { return mem_handle_; }
private:
NumericT read() const
{
NumericT temp;
viennacl::backend::memory_read(mem_handle_, sizeof(NumericT)*index_, sizeof(NumericT), &temp);
return temp;
}
vcl_size_t index_;
viennacl::backend::mem_handle const & mem_handle_;
}; //entry_proxy
}
#endif
viennacl::const_entry_proxy::const_entry_proxy
const_entry_proxy(vcl_size_t mem_offset, handle_type const &mem_handle)
The constructor for the proxy class. Declared explicit to avoid any surprises created by the compiler...
Definition: entry_proxy.hpp:190
viennacl::backend::memory_write
void memory_write(mem_handle &dst_buffer, vcl_size_t dst_offset, vcl_size_t bytes_to_write, const void *ptr, bool async=false)
Writes data from main RAM identified by 'ptr' to the buffer identified by 'dst_buffer'.
Definition: memory.hpp:220
viennacl::scalar
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:227
viennacl::scalar::handle
handle_type & handle()
Returns the memory handle, non-const version.
Definition: scalar.hpp:833
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::backend::memory_read
void memory_read(mem_handle const &src_buffer, vcl_size_t src_offset, vcl_size_t bytes_to_read, void *ptr, bool async=false)
Reads data from a buffer back to main RAM.
Definition: memory.hpp:261
viennacl::entry_proxy::operator-=
entry_proxy & operator-=(NumericT value)
Inplace subtraction of a CPU floating point value.
Definition: entry_proxy.hpp:69
viennacl::entry_proxy::handle_type
viennacl::backend::mem_handle handle_type
Definition: entry_proxy.hpp:44
viennacl::entry_proxy::operator/=
entry_proxy & operator/=(NumericT value)
Inplace division by a CPU floating point value.
Definition: entry_proxy.hpp:89
NumericT
float NumericT
Definition: bisect.cpp:40
viennacl::entry_proxy::operator+=
entry_proxy & operator+=(NumericT value)
Inplace addition of a CPU floating point value.
Definition: entry_proxy.hpp:59
viennacl::entry_proxy::index
vcl_size_t index() const
Returns the index of the represented element.
Definition: entry_proxy.hpp:138
viennacl::entry_proxy::operator=
entry_proxy & operator=(entry_proxy const &other)
Assignment of another GPU value.
Definition: entry_proxy.hpp:115
viennacl::entry_proxy::operator=
entry_proxy & operator=(NumericT value)
Assignment of a CPU floating point value.
Definition: entry_proxy.hpp:99
viennacl::entry_proxy::operator*=
entry_proxy & operator*=(NumericT value)
Inplace multiplication with a CPU floating point value.
Definition: entry_proxy.hpp:79
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::entry_proxy::entry_proxy
entry_proxy(vcl_size_t mem_offset, handle_type &mem_handle)
The constructor for the proxy class. Declared explicit to avoid any surprises created by the compiler...
Definition: entry_proxy.hpp:51
viennacl::entry_proxy::handle
handle_type const & handle() const
Returns the memory viennacl::ocl::handle.
Definition: entry_proxy.hpp:142
viennacl::backend::memory_copy
void memory_copy(mem_handle const &src_buffer, mem_handle &dst_buffer, vcl_size_t src_offset, vcl_size_t dst_offset, vcl_size_t bytes_to_copy)
Copies 'bytes_to_copy' bytes from address 'src_buffer + src_offset' to memory starting at address 'ds...
Definition: memory.hpp:140
viennacl::backend::mem_handle
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
viennacl::const_entry_proxy::handle_type
viennacl::backend::mem_handle handle_type
Definition: entry_proxy.hpp:183
viennacl::const_entry_proxy::index
unsigned int index() const
Returns the index of the represented element.
Definition: entry_proxy.hpp:212
viennacl::entry_proxy
A proxy class for a single element of a vector or matrix. This proxy should not be noticed by end-use...
Definition: forwards.h:233
scalar.hpp
Implementation of the ViennaCL scalar class.
viennacl::const_entry_proxy::handle
handle_type const & handle() const
Returns the memory handle.
Definition: entry_proxy.hpp:216
viennacl::entry_proxy::operator=
entry_proxy & operator=(scalar< NumericT > const &value)
Assignment of a GPU floating point value. Avoids unnecessary GPU->CPU->GPU transfers.
Definition: entry_proxy.hpp:107