All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cpu_ram.hpp
Go to the documentation of this file.
#ifndef VIENNACL_BACKEND_CPU_RAM_HPP_
#define VIENNACL_BACKEND_CPU_RAM_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 <cassert>
#include <vector>
#ifdef VIENNACL_WITH_AVX2
#include <stdlib.h>
#endif
#include "viennacl/forwards.h"
#include "viennacl/tools/shared_ptr.hpp"
namespace viennacl
{
namespace backend
{
namespace cpu_ram
{
typedef viennacl::tools::shared_ptr<char> handle_type;
// Requirements for backend:
// * memory_create(size, host_ptr)
// * memory_copy(src, dest, offset_src, offset_dest, size)
// * memory_write_from_main_memory(src, offset, size,
// dest, offset, size)
// * memory_read_to_main_memory(src, offset, size
// dest, offset, size)
// *
//
namespace detail
{
template<class U>
struct array_deleter
{
#ifdef VIENNACL_WITH_AVX2
void operator()(U* p) const { free(p); }
#else
void operator()(U* p) const { delete[] p; }
#endif
};
}
inline handle_type memory_create(vcl_size_t size_in_bytes, const void * host_ptr = NULL)
{
#ifdef VIENNACL_WITH_AVX2
// Note: aligned_alloc not available on all compilers. Consider platform-specific alternatives such as posix_memalign()
if (!host_ptr)
return handle_type(reinterpret_cast<char*>(aligned_alloc(32, size_in_bytes)), detail::array_deleter<char>());
handle_type new_handle(reinterpret_cast<char*>(aligned_alloc(32, size_in_bytes)), detail::array_deleter<char>());
#else
if (!host_ptr)
return handle_type(new char[size_in_bytes], detail::array_deleter<char>());
handle_type new_handle(new char[size_in_bytes], detail::array_deleter<char>());
#endif
// copy data:
char * raw_ptr = new_handle.get();
const char * data_ptr = static_cast<const char *>(host_ptr);
#ifdef VIENNACL_WITH_OPENMP
#pragma omp parallel for
#endif
for (long i=0; i<long(size_in_bytes); ++i)
raw_ptr[i] = data_ptr[i];
return new_handle;
}
inline 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)
{
assert( (dst_buffer.get() != NULL) && bool("Memory not initialized!"));
assert( (src_buffer.get() != NULL) && bool("Memory not initialized!"));
#ifdef VIENNACL_WITH_OPENMP
#pragma omp parallel for
#endif
for (long i=0; i<long(bytes_to_copy); ++i)
dst_buffer.get()[vcl_size_t(i)+dst_offset] = src_buffer.get()[vcl_size_t(i) + src_offset];
}
inline void memory_write(handle_type & dst_buffer,
vcl_size_t dst_offset,
vcl_size_t bytes_to_copy,
const void * ptr,
bool /*async*/)
{
assert( (dst_buffer.get() != NULL) && bool("Memory not initialized!"));
#ifdef VIENNACL_WITH_OPENMP
#pragma omp parallel for
#endif
for (long i=0; i<long(bytes_to_copy); ++i)
dst_buffer.get()[vcl_size_t(i)+dst_offset] = static_cast<const char *>(ptr)[i];
}
inline void memory_read(handle_type const & src_buffer,
vcl_size_t src_offset,
vcl_size_t bytes_to_copy,
void * ptr,
bool /*async*/)
{
assert( (src_buffer.get() != NULL) && bool("Memory not initialized!"));
#ifdef VIENNACL_WITH_OPENMP
#pragma omp parallel for
#endif
for (long i=0; i<long(bytes_to_copy); ++i)
static_cast<char *>(ptr)[i] = src_buffer.get()[vcl_size_t(i)+src_offset];
}
}
} //backend
} //viennacl
#endif
viennacl::backend::cpu_ram::handle_type
viennacl::tools::shared_ptr< char > handle_type
Definition: cpu_ram.hpp:40
viennacl::backend::cpu_ram::memory_read
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:153
viennacl::backend::cpu_ram::memory_create
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:73
viennacl::backend::cpu_ram::detail::array_deleter
Helper struct for deleting an pointer to an array.
Definition: cpu_ram.hpp:56
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::backend::cpu_ram::memory_write
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:131
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::tools::shared_ptr< char >
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::backend::cpu_ram::memory_copy
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:108
viennacl::tools::shared_ptr::get
T * get() const
Definition: shared_ptr.hpp:101
viennacl::backend::cpu_ram::detail::array_deleter::operator()
void operator()(U *p) const
Definition: cpu_ram.hpp:61