ViennaCL - The Vienna Computing Library  1.6.0
Free open-source GPU-accelerated linear algebra and solver library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cpu_ram.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_BACKEND_CPU_RAM_HPP_
2 #define VIENNACL_BACKEND_CPU_RAM_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2014, 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 PDF manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
25 #include <cassert>
26 #include <vector>
28 
29 namespace viennacl
30 {
31 namespace backend
32 {
33 namespace cpu_ram
34 {
36 // Requirements for backend:
37 
38 // * memory_create(size, host_ptr)
39 // * memory_copy(src, dest, offset_src, offset_dest, size)
40 // * memory_write_from_main_memory(src, offset, size,
41 // dest, offset, size)
42 // * memory_read_to_main_memory(src, offset, size
43 // dest, offset, size)
44 // *
45 //
46 
47 namespace detail
48 {
50  template<class U>
52  {
53  void operator()(U* p) const { delete[] p; }
54  };
55 
56 }
57 
64 inline handle_type memory_create(vcl_size_t size_in_bytes, const void * host_ptr = NULL)
65 {
66  if (!host_ptr)
67  return handle_type(new char[size_in_bytes], detail::array_deleter<char>());
68 
69  handle_type new_handle(new char[size_in_bytes], detail::array_deleter<char>());
70 
71  // copy data:
72  char * raw_ptr = new_handle.get();
73  const char * data_ptr = static_cast<const char *>(host_ptr);
74  for (vcl_size_t i=0; i<size_in_bytes; ++i)
75  raw_ptr[i] = data_ptr[i];
76 
77  return new_handle;
78 }
79 
88 inline void memory_copy(handle_type const & src_buffer,
89  handle_type & dst_buffer,
90  vcl_size_t src_offset,
91  vcl_size_t dst_offset,
92  vcl_size_t bytes_to_copy)
93 {
94  assert( (dst_buffer.get() != NULL) && bool("Memory not initialized!"));
95  assert( (src_buffer.get() != NULL) && bool("Memory not initialized!"));
96 
97  for (vcl_size_t i=0; i<bytes_to_copy; ++i)
98  dst_buffer.get()[i+dst_offset] = src_buffer.get()[i + src_offset];
99 }
100 
108 inline void memory_write(handle_type & dst_buffer,
109  vcl_size_t dst_offset,
110  vcl_size_t bytes_to_copy,
111  const void * ptr,
112  bool /*async*/)
113 {
114  assert( (dst_buffer.get() != NULL) && bool("Memory not initialized!"));
115 
116  for (vcl_size_t i=0; i<bytes_to_copy; ++i)
117  dst_buffer.get()[i+dst_offset] = static_cast<const char *>(ptr)[i];
118 }
119 
127 inline void memory_read(handle_type const & src_buffer,
128  vcl_size_t src_offset,
129  vcl_size_t bytes_to_copy,
130  void * ptr,
131  bool /*async*/)
132 {
133  assert( (src_buffer.get() != NULL) && bool("Memory not initialized!"));
134 
135  for (vcl_size_t i=0; i<bytes_to_copy; ++i)
136  static_cast<char *>(ptr)[i] = src_buffer.get()[i+src_offset];
137 }
138 
139 }
140 } //backend
141 } //viennacl
142 #endif
viennacl::tools::shared_ptr< char > handle_type
Definition: cpu_ram.hpp:35
void memory_read(handle_type const &src_buffer, vcl_size_t src_offset, vcl_size_t bytes_to_copy, void *ptr, bool)
Reads data from a buffer back to main RAM.
Definition: cpu_ram.hpp:127
handle_type memory_create(vcl_size_t size_in_bytes, const void *host_ptr=NULL)
Creates an array of the specified size in main RAM. If the second argument is provided, the buffer is initialized with data from that pointer.
Definition: cpu_ram.hpp:64
Helper struct for deleting an pointer to an array.
Definition: cpu_ram.hpp:51
void memory_write(handle_type &dst_buffer, vcl_size_t dst_offset, vcl_size_t bytes_to_copy, const void *ptr, bool)
Writes data from main RAM identified by 'ptr' to the buffer identified by 'dst_buffer'.
Definition: cpu_ram.hpp:108
Implementation of a shared pointer class (cf. std::shared_ptr, boost::shared_ptr). Will be used until C++11 is widely available.
std::size_t vcl_size_t
Definition: forwards.h:74
void memory_copy(handle_type const &src_buffer, handle_type &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: cpu_ram.hpp:88