ViennaCL - The Vienna Computing Library  1.5.1
viennacl/backend/cpu_ram.hpp
Go to the documentation of this file.
00001 #ifndef VIENNACL_BACKEND_CPU_RAM_HPP_
00002 #define VIENNACL_BACKEND_CPU_RAM_HPP_
00003 
00004 /* =========================================================================
00005    Copyright (c) 2010-2014, Institute for Microelectronics,
00006                             Institute for Analysis and Scientific Computing,
00007                             TU Wien.
00008    Portions of this software are copyright by UChicago Argonne, LLC.
00009 
00010                             -----------------
00011                   ViennaCL - The Vienna Computing Library
00012                             -----------------
00013 
00014    Project Head:    Karl Rupp                   rupp@iue.tuwien.ac.at
00015 
00016    (A list of authors and contributors can be found in the PDF manual)
00017 
00018    License:         MIT (X11), see file LICENSE in the base directory
00019 ============================================================================= */
00020 
00025 #include <cassert>
00026 #include <vector>
00027 #include "viennacl/tools/shared_ptr.hpp"
00028 
00029 namespace viennacl
00030 {
00031   namespace backend
00032   {
00033     namespace cpu_ram
00034     {
00035       typedef viennacl::tools::shared_ptr<char>  handle_type;
00036       // Requirements for backend:
00037 
00038       // * memory_create(size, host_ptr)
00039       // * memory_copy(src, dest, offset_src, offset_dest, size)
00040       // * memory_write_from_main_memory(src, offset, size,
00041       //                                 dest, offset, size)
00042       // * memory_read_to_main_memory(src, offset, size
00043       //                              dest, offset, size)
00044       // *
00045       //
00046 
00047       namespace detail
00048       {
00050         template<class U>
00051         struct array_deleter
00052         {
00053           void operator()(U* p) const { delete[] p; }
00054         };
00055 
00056       }
00057 
00064       inline handle_type  memory_create(vcl_size_t size_in_bytes, const void * host_ptr = NULL)
00065       {
00066         if (!host_ptr)
00067           return handle_type(new char[size_in_bytes], detail::array_deleter<char>());
00068 
00069         handle_type new_handle(new char[size_in_bytes], detail::array_deleter<char>());
00070 
00071         // copy data:
00072         char * raw_ptr = new_handle.get();
00073         const char * data_ptr = static_cast<const char *>(host_ptr);
00074         for (vcl_size_t i=0; i<size_in_bytes; ++i)
00075           raw_ptr[i] = data_ptr[i];
00076 
00077         return new_handle;
00078       }
00079 
00088       inline void memory_copy(handle_type const & src_buffer,
00089                               handle_type & dst_buffer,
00090                               vcl_size_t src_offset,
00091                               vcl_size_t dst_offset,
00092                               vcl_size_t bytes_to_copy)
00093       {
00094         assert( (dst_buffer.get() != NULL) && bool("Memory not initialized!"));
00095         assert( (src_buffer.get() != NULL) && bool("Memory not initialized!"));
00096 
00097         for (vcl_size_t i=0; i<bytes_to_copy; ++i)
00098           dst_buffer.get()[i+dst_offset] = src_buffer.get()[i + src_offset];
00099       }
00100 
00108       inline void memory_write(handle_type & dst_buffer,
00109                                vcl_size_t dst_offset,
00110                                vcl_size_t bytes_to_copy,
00111                                const void * ptr,
00112                                bool /*async*/)
00113       {
00114         assert( (dst_buffer.get() != NULL) && bool("Memory not initialized!"));
00115 
00116         for (vcl_size_t i=0; i<bytes_to_copy; ++i)
00117           dst_buffer.get()[i+dst_offset] = static_cast<const char *>(ptr)[i];
00118       }
00119 
00127       inline void memory_read(handle_type const & src_buffer,
00128                               vcl_size_t src_offset,
00129                               vcl_size_t bytes_to_copy,
00130                               void * ptr,
00131                               bool /*async*/)
00132       {
00133         assert( (src_buffer.get() != NULL) && bool("Memory not initialized!"));
00134 
00135         for (vcl_size_t i=0; i<bytes_to_copy; ++i)
00136           static_cast<char *>(ptr)[i] = src_buffer.get()[i+src_offset];
00137       }
00138 
00139 
00140     }
00141   } //backend
00142 } //viennacl
00143 #endif