Skip to content

Commit

Permalink
Memory fixes, and large refactor. Replaces many raw ptrs with shared_…
Browse files Browse the repository at this point in the history
…ptr. Uses std::variant.
  • Loading branch information
universenox authored and sdavtaker committed Jun 23, 2019
1 parent 527f196 commit 8d8b26d
Show file tree
Hide file tree
Showing 38 changed files with 1,907 additions and 1,962 deletions.
1 change: 1 addition & 0 deletions .codecov.yml
Expand Up @@ -4,3 +4,4 @@ ignore:
- "doc/**"
- "external/**"
- "test/**"
"bench/**"
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -9,3 +9,4 @@ CMakeFiles/
CMakeScripts/
CTestTestfile.cmake
cmake_install.cmake
.vscode/
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -58,4 +58,4 @@ matrix:
- os: osx

script:
- cmake -DBENCH=OFF -DCMAKE_BUILD_TYPE=Debug -G "Unix Makefiles" . && make && make test
- cmake -DCMAKE_BUILD_TYPE=Debug -DREAL_BENCH=OFF -G "Unix Makefiles" . && make -j`nproc` && make -j`nproc` test
37 changes: 17 additions & 20 deletions CMakeLists.txt
Expand Up @@ -8,7 +8,7 @@ IF(CMAKE_BUILD_TYPE MATCHES Debug)
message("debug mode adding coverage report tools and flags")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake-modules")
include(CodeCoverage)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_DEBUG} -Wall -g -O0 -fprofile-arcs -ftest-coverage")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_DEBUG} -Wall -g -O0 -fsanitize=address -fno-omit-frame-pointer -fprofile-arcs -ftest-coverage")
ENDIF(CMAKE_BUILD_TYPE MATCHES Debug)

# Check for standard to use
Expand All @@ -20,34 +20,31 @@ else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic --std=c++1z")
endif()

#Detect Boost.Test framework
set(Boost_USE_MULTITHREADED OFF)
find_package(Boost COMPONENTS unit_test_framework REQUIRED)

include_directories(include external/include test/include ${Boost_INCLUDE_DIRS})
include_directories(include external/include ${Boost_INCLUDE_DIRS})

# add Boost.Real as a 'linkable' target
add_library(Boost.Real INTERFACE)

# Unit tests
enable_testing()
FILE(GLOB TestSources RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} test/*_test.cpp)
foreach(testSrc ${TestSources})
get_filename_component(testName ${testSrc} NAME_WE)
add_executable(${testName} test/main-test.cpp ${testSrc})
add_test(${testName} ${testName})
endforeach(testSrc)

#Library Headers
add_executable(Boost.Real_headers include)
set_target_properties(Boost.Real_headers PROPERTIES
EXCLUDE_FROM_ALL TRUE
EXCLUDE_FROM_DEFAULT_BUILD TRUE
LINKER_LANGUAGE CXX)

#Google Benchmark, turn on/off with -DBENCH=ON or OFF
find_package(Threads REQUIRED)
option(BENCH "Build benchmarks" ON)
if(BENCH)
LINKER_LANGUAGE CXX
)
# Unit tests
option(REAL_TEST "Build tests" ON)
if(REAL_TEST)
enable_testing()
#Detect Boost.Test framework
set(Boost_USE_MULTITHREADED OFF)
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
add_subdirectory(test)
endif()

#Google Benchmark, turn on/off with -DREAL_BENCH=ON or OFF
option(REAL_BENCH "Build benchmarks" OFF)
if(REAL_BENCH)
add_subdirectory(bench)
endif()
25 changes: 13 additions & 12 deletions bench/CMakeLists.txt
@@ -1,18 +1,19 @@
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Suppressing benchmark's tests" FORCE)

add_subdirectory(benchmark)
include_directories(benchmark/include
include)

set(bench_list
addition_bench.cpp
# remember to add src's here
set(benchSrcs
operation_tree_bench.cpp
main_bench.cpp
)

add_executable(main_bench ${bench_list})


target_link_libraries(main_bench
benchmark
Boost.Real
${CMAKE_THREAD_LIBS_INIT}
# to run a subset of benchmarks, use:
# ./main_bench --benchmark_filter=<regex>
add_executable(main_bench ${benchSrcs})
target_include_directories(main_bench
PRIVATE ./include
)
target_link_libraries(main_bench
PUBLIC Boost.Real
PUBLIC benchmark
)
19 changes: 0 additions & 19 deletions bench/addition_bench.cpp

This file was deleted.

17 changes: 17 additions & 0 deletions bench/include/benchmark_helpers.hpp
@@ -0,0 +1,17 @@
#include <iostream>


class NullBuffer : public std::streambuf
{
public:
int overflow(int c) { return c; }
};

class NullStream: public std::ostream
{
public:
NullStream() : std::ostream(&sb) {}

private:
NullBuffer sb;
};
7 changes: 2 additions & 5 deletions bench/include/bench_helpers.hpp → bench/main_bench.cpp
@@ -1,10 +1,7 @@
#ifndef BOOST_REAL_BENCH_HELPERS_HPP
#define BOOST_REAL_BENCH_HELPERS_HPP


#include <benchmark/benchmark.h>
#include <real/real.hpp>

unsigned int boost::real::real::maximum_precision = 10;
unsigned int boost::real::real_algorithm::maximum_precision = 10;

#endif //BOOST_REAL_BENCH_HELPERS_HPP
BENCHMARK_MAIN();
52 changes: 52 additions & 0 deletions bench/operation_tree_bench.cpp
@@ -0,0 +1,52 @@
#include <benchmark/benchmark.h>
#include <real/real.hpp>
#include <benchmark_helpers.hpp>

/** @file benchmark the operation trees,
* with different kinds of leaves, and/or a large number of nodes.
*/

// constants used in the benchmarks
const int MIN_TREE_SIZE = 8;
const int MAX_TREE_SIZE = 16;

// used to force evaluation, without console output
NullStream null_stream;

// trees between MIN_TREE_SIZE and MAX_TREE_SIZE of single operations
static void BM_RealAdditionTree(benchmark::State& state) {
boost::real::real a ("1.2");
boost::real::real c ("0");

for (auto i : state)
for (int i = 0; i < state.range(0); i++)
c += a;
null_stream << c << '\n';
}
BENCHMARK(BM_RealAdditionTree)->Range(MIN_TREE_SIZE,MAX_TREE_SIZE)->Unit(benchmark::kMillisecond);

static void BM_RealSubtractionTree(benchmark::State& state) {
boost::real::real a ("1");
boost::real::real c ("9999");

for (auto i : state)
{
for (int i = 0; i < state.range(0); i++)
c -= a;
null_stream << c << '\n';
}
}
BENCHMARK(BM_RealSubtractionTree)->Range(MIN_TREE_SIZE,MAX_TREE_SIZE)->Unit(benchmark::kMillisecond);

static void BM_RealMultiplicationTree(benchmark::State& state) {
boost::real::real a ("1");
boost::real::real c ("9");

for (auto i : state)
{
for (int i = 0; i < state.range(0); i++)
c *= a;
null_stream << c << '\n';
}
}
BENCHMARK(BM_RealMultiplicationTree)->Range(MIN_TREE_SIZE,MAX_TREE_SIZE)->Unit(benchmark::kMillisecond);
14 changes: 7 additions & 7 deletions include/real/boundary.hpp
Expand Up @@ -52,15 +52,15 @@ namespace boost {

if (this->positive) {
if (this->exponent == other.exponent) {
return boost::real::helper::aligned_vectors_is_lower(this->digits,
return boost::real::boundary_helper::aligned_vectors_is_lower(this->digits,
other.digits);
}

return this->exponent < other.exponent;
}

if (this->exponent == other.exponent) {
return boost::real::helper::aligned_vectors_is_lower(other.digits,
return boost::real::boundary_helper::aligned_vectors_is_lower(other.digits,
this->digits);
}

Expand All @@ -82,14 +82,14 @@ namespace boost {

if (this->positive) {
if (this->exponent == other.exponent) {
return boost::real::helper::aligned_vectors_is_lower(other.digits, this->digits);
return boost::real::boundary_helper::aligned_vectors_is_lower(other.digits, this->digits);
}

return this->exponent > other.exponent;
}

if (this->exponent == other.exponent) {
return boost::real::helper::aligned_vectors_is_lower(this->digits, other.digits);
return boost::real::boundary_helper::aligned_vectors_is_lower(this->digits, other.digits);
}

return other.exponent > this->exponent;
Expand Down Expand Up @@ -194,7 +194,7 @@ namespace boost {
* @param digit - The new digit to add.
*/
void push_front(int digit) {
this->digits.insert(this->digits.begin(), digit);
this->digits.insert(this->digits.cbegin(), digit);
}

/**
Expand All @@ -203,7 +203,7 @@ namespace boost {
*/
void normalize() {
while (this->digits.size() > 1 && this->digits.front() == 0) {
this->digits.erase(this->digits.begin());
this->digits.erase(this->digits.cbegin());
this->exponent--;
}

Expand All @@ -224,7 +224,7 @@ namespace boost {
*/
void normalize_left() {
while (this->digits.size() > 1 && this->digits.front() == 0) {
this->digits.erase(this->digits.begin());
this->digits.erase(this->digits.cbegin());
this->exponent--;
}
}
Expand Down
9 changes: 5 additions & 4 deletions include/real/boundary_helper.hpp
Expand Up @@ -2,11 +2,12 @@
#define BOOST_REAL_BOUNDARY_HELPER_HPP

#include <algorithm>
#include "interval.hpp"
#include <real/interval.hpp>

namespace boost {
namespace real {
namespace helper {
class boundary_helper {
public:

/**
* @author Laouen Mayal Louan Belloli
Expand All @@ -22,7 +23,7 @@ namespace boost {
* @return a bool that is true if and only if, the number represented by lsh is lower than
* the represented number by rhs.
*/
bool aligned_vectors_is_lower(const std::vector<int> &lhs, const std::vector<int> &rhs) {
static bool aligned_vectors_is_lower(const std::vector<int> &lhs, const std::vector<int> &rhs) {

// Check if lhs is lower than rhs
auto lhs_it = lhs.cbegin();
Expand All @@ -42,7 +43,7 @@ namespace boost {
return lhs_all_zero && !rhs_all_zero;
}

}
};
}
}

Expand Down

0 comments on commit 8d8b26d

Please sign in to comment.