Skip to content

Commit

Permalink
Merge pull request #165 from yungyuc/yyc-buffer
Browse files Browse the repository at this point in the history
Get rid of the intermediate wrapper class "Table" in the pybind11 module
  • Loading branch information
yungyuc committed Jul 2, 2016
2 parents 364f21b + 707cf66 commit e394a23
Show file tree
Hide file tree
Showing 11 changed files with 589 additions and 197 deletions.
6 changes: 6 additions & 0 deletions libmarch/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ project(libmarch)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")

option(MARCH_RELAX_ERROR "Relax compiler error" OFF)

include_directories("include")

set(NOWARN_MARCH "-Wno-unused-private-field")

if(MARCH_RELAX_ERROR)
set(NOWARN_MARCH "${NOWARN_MARCH} -Wno-error=unused-variable")
endif()

set(CMAKE_CXX_FLAGS "-std=c++14 -fPIC -Werror -Wall ${NOWARN_MARCH}")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
Expand Down
42 changes: 22 additions & 20 deletions libmarch/include/march/core/Buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,61 @@
*/

#include <stdexcept>
#include <memory>

namespace march
{

namespace mesh { class LookupTableCore; }

/**
* Untyped and unresizeable memory buffer for data storage.
*/
class Buffer {
class Buffer: public std::enable_shared_from_this<Buffer> {

private:

size_t m_length = 0;
char * m_data = nullptr;
bool m_own_data = true;

struct ctor_passkey {};

public:

Buffer() {
static_assert(sizeof(Buffer) == 24, "Buffer size changes");
Buffer(const ctor_passkey &) { }

static std::shared_ptr<Buffer> construct() {
return std::make_shared<Buffer>(ctor_passkey());
}

/**
* \param[in] length Memory buffer length.
*/
Buffer(size_t length) : m_length(length) { m_data = new char[length](); }
Buffer(size_t length, const ctor_passkey &) : m_length(length) { m_data = new char[length](); }

/**
* When given an allocated memory block (\p data) from outside, the
* constructed Buffer object doesn't manage its own memory.
*
* This constructor allows the ownership of the memory block can be
* transferred to an outside system, like NumPy.
*
* \param[in] length Memory buffer length.
* \param[in] data The memory block.
*/
Buffer(size_t length, char * data) : m_length(length), m_data(data), m_own_data(false) {}
static std::shared_ptr<Buffer> construct(size_t length) {
return std::make_shared<Buffer>(length, ctor_passkey());
}

~Buffer() {
if (m_own_data && nullptr != m_data) { delete m_data; }
delete[] m_data;
m_data = nullptr;
}

Buffer(const Buffer &) = delete;

Buffer(Buffer &&) = delete;

Buffer & operator=(const Buffer &) = delete;
Buffer & operator=(Buffer && other) {

Buffer & operator=(Buffer &&) = delete;
/*Buffer & operator=(Buffer && other) {
m_data = other.m_data;
other.m_data = nullptr;
m_data = nullptr;
m_length = other.m_length;
m_own_data = other.m_own_data;
return *this;
}
}*/

explicit operator bool() const { return nullptr == m_data; }

Expand Down
1 change: 1 addition & 0 deletions libmarch/include/march/core/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include "march/core/types.hpp"
#include "march/core/utility.hpp"
#include "march/core/Buffer.hpp"

// vim: set ff=unix fenc=utf8 nobomb et sw=4 ts=4:
96 changes: 96 additions & 0 deletions libmarch/include/march/core/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,106 @@
*/

#include <cstdint>
#include <complex>

#include "march/core/utility.hpp"

namespace march
{

/**
* The enum is compatible to numpy NPY_TYPES.
*
* MH_LONG, MH_ULONG, MH_LONGDOUBLE, MH_CLONGDOUBLE aren't used.
*/
enum DataTypeId {
MH_BOOL=0,
MH_INT8, MH_UINT8,
MH_INT16, MH_UINT16,
MH_INT32, MH_UINT32,
MH_LONG, MH_ULONG,
MH_INT64, MH_UINT64,
MH_FLOAT, MH_DOUBLE, MH_LONGDOUBLE,
MH_CFLOAT, MH_CDOUBLE, MH_CLONGDOUBLE
};

inline size_t data_type_size(const DataTypeId dtid) {
size_t ret = 0;
switch (dtid) {
case MH_BOOL:
case MH_INT8:
case MH_UINT8:
ret = 1;
break;
case MH_INT16:
case MH_UINT16:
ret = 2;
break;
case MH_INT32:
case MH_UINT32:
ret = 4;
break;
case MH_INT64:
case MH_UINT64:
ret = 4;
break;
case MH_FLOAT:
ret = 4;
break;
case MH_DOUBLE:
ret = 8;
break;
default: // error.
ret = 0;
}
return ret;
}

/**
* Convert type to ID.
*/
template <typename type, typename SFINAE = void> struct type_to { };

template <typename T> struct type_to<T, typename std::enable_if<std::is_integral<T>::value>::type> {
private:
constexpr static DataTypeId ids[8] = {
MH_INT8, MH_UINT8, MH_INT16, MH_UINT16,
MH_INT32, MH_UINT32, MH_INT64, MH_UINT64
};
public:
constexpr static DataTypeId id = ids[detail::log2(sizeof(T)) * 2 + (std::is_unsigned<T>::value ? 1 : 0)];
};

#define DECL_TYPEID(Type, ID) \
template <> struct type_to<Type> { constexpr static DataTypeId id = ID; };
DECL_TYPEID(bool, MH_BOOL)
DECL_TYPEID(float, MH_FLOAT)
DECL_TYPEID(double, MH_DOUBLE)
DECL_TYPEID(std::complex<float>, MH_CFLOAT)
DECL_TYPEID(std::complex<double>, MH_CDOUBLE)
#undef DECL_TYPEID

/**
* Convert ID to type.
*/
template <int ID> struct id_to { };
template <> struct id_to<MH_BOOL> { typedef bool type; };
template <> struct id_to<MH_INT8> { typedef int8_t type; };
template <> struct id_to<MH_UINT8> { typedef uint8_t type; };
template <> struct id_to<MH_INT16> { typedef int16_t type; };
template <> struct id_to<MH_UINT16> { typedef uint16_t type; };
template <> struct id_to<MH_INT32> { typedef int32_t type; };
template <> struct id_to<MH_UINT32> { typedef uint32_t type; };
template <> struct id_to<MH_INT64> { typedef int64_t type; };
template <> struct id_to<MH_UINT64> { typedef uint64_t type; };
template <> struct id_to<MH_FLOAT> { typedef float type; };
template <> struct id_to<MH_DOUBLE> { typedef double type; };
template <> struct id_to<MH_CFLOAT> { typedef std::complex<float> type; };
template <> struct id_to<MH_CDOUBLE> { typedef std::complex<double> type; };

/**
* The primitive data type for lookup-table indices.
*/
typedef int32_t index_type;
static constexpr index_type INVALID_INDEX = INT32_MAX;

Expand Down
25 changes: 25 additions & 0 deletions libmarch/include/march/core/utility.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

/*
* Copyright (c) 2016, Yung-Yu Chen <yyc@solvcon.net>
* BSD 3-Clause License, see COPYING
*/

/**
* \file
* Utilities.
*/

#include <cstring>

namespace march {

namespace detail {

inline static constexpr size_t log2(size_t n, int k = 0) { return (n <= 1) ? k : log2(n >> 1, k + 1); }

} /* end namespace detail */

} /* end namespace march */

// vim: set ff=unix fenc=utf8 nobomb et sw=4 ts=4:

0 comments on commit e394a23

Please sign in to comment.