Skip to content

Commit

Permalink
Merge pull request #17 from HPAC/dev-raw-data
Browse files Browse the repository at this point in the history
Dev raw data
  • Loading branch information
Chris Psarras authored May 15, 2019
2 parents 84693ab + 96e49b6 commit 5da4f4b
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 458 deletions.
95 changes: 12 additions & 83 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ project(linalg_tests)
#Question is: should we have two targets, one to install and one for examples/tests
cmake_policy(SET CMP0041 OLD)

option(WITH_EXAMPLES "Build examples" Off)
option(WITH_TESTS "Build tests" Off)
option(TESTS_FORCE_BUILD "Build Gtest" Off)
option(WITH_BLAS "Build with BLAS" Off)
option(WITH_LAPACK "Build with LAPACK" Off)

option(WITH_BLAZE "Use Blaze library")
option(WITH_EIGEN "Use Eigen3 library")
option(WITH_EIGEN_DIAGONAL_MATRIX "" Off)
option(WITH_ARMADILLO "Use Blaze library")
option(WITH_MTL4 "Use Blaze library")

option(WITH_MKL "Use Intel MKL" Off)
option(WITH_EXAMPLES "Build examples" On)
option(WITH_BLAS "Build with BLAS" On)
option(WITH_LAPACK "Build with LAPACK" On)

option(WITH_EIGEN "Use Eigen3 library" On)
option(WITH_EIGEN_DIAGONAL_MATRIX "" Off)
option(WITH_ARMADILLO "Use Armadillo library" On)

option(WITH_MKL "Use Intel MKL" On)
option(WITH_MULTITHREADING "Enable parallelization inside linked libraries" Off)

option(GENERATE_RELOCATABLE_TARGETS "" Off)
Expand All @@ -44,17 +40,9 @@ endif()

# Use cxx compile features for CMake >= 3.8
if(${CMAKE_VERSION} VERSION_LESS "3.8.0")
if(${WITH_BLAZE})
set(CMAKE_CXX_STANDARD 14)
else()
set(CMAKE_CXX_STANDARD 11)
endif()
set(CMAKE_CXX_STANDARD 14)
else()
if(${WITH_BLAZE})
target_compile_features(libtests INTERFACE cxx_std_14)
else()
target_compile_features(libtests INTERFACE cxx_std_11)
endif()
target_compile_features(libtests INTERFACE cxx_std_14)
endif()

# Setup BLAS and LAPACK finders to find MKL
Expand Down Expand Up @@ -108,7 +96,7 @@ if(${WITH_ARMADILLO})
find_package(Armadillo REQUIRED)
target_include_directories(libtests INTERFACE ${ARMADILLO_INCLUDE_DIRS})
target_link_libraries(libtests INTERFACE ${ARMADILLO_LIBRARIES})

target_compile_definitions(libtests INTERFACE HAVE_ARMADILLO)
target_compile_definitions(libtests INTERFACE $<$<CONFIG:RELEASE>:ARMA_NO_DEBUG>)
endif()
Expand All @@ -129,37 +117,6 @@ if(${WITH_EIGEN})
endif()
endif()

#MTL4
if(${WITH_MTL4})
find_package(MTL CONFIG REQUIRED)
include_directories(${MTL_INCLUDE_DIRS})
add_definitions(-DHAVE_MTL4)
endif()

#Blaze
if(${WITH_BLAZE})
find_package(blaze CONFIG REQUIRED)
target_compile_definitions(libtests INTERFACE HAVE_BLAZE)
# For future dependencies on imported targets
#target_include_directories(libtests INTERFACE $<TARGET_PROPERTY:blaze::blaze,INTERFACE_INCLUDE_DIRECTORIES>)
if(${GENERATE_RELOCATABLE_TARGETS})
target_link_libraries(libtests INTERFACE blaze::blaze)
else()
# one can't use generator expressions for exported targets - use has to find blaze target
get_target_property(BLAZE_INCLUDE blaze::blaze INTERFACE_INCLUDE_DIRECTORIES)
target_include_directories(libtests INTERFACE ${BLAZE_INCLUDE})
endif()

if(${WITH_MKL})
# Otherwise Blaze would not compile
target_compile_definitions(libtests INTERFACE BLAZE_BLAS_INCLUDE_FILE=<mkl_cblas.h>)
endif()

if(${WITH_TRACES})
target_compile_definitions(libtests INTERFACE BLAZE_USE_FUNCTION_TRACES=1)
endif()
endif()

#Warnings
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
#Warnins for Clang/AppleClang
Expand Down Expand Up @@ -197,31 +154,3 @@ install(EXPORT LibTestsConfig
if(${WITH_EXAMPLES})
add_subdirectory(examples)
endif(${WITH_EXAMPLES})

################################
# GTest
################################
if(${WITH_TESTS})

enable_testing()
find_package(GTest)
if(GTEST_FOUND AND ${TESTS_FORCE_BUILD})
include_directories(${GTEST_INCLUDE_DIRS})
set(GTEST_LINK_FLAGS ${GTEST_BOTH_LIBRARIES} -lpthread)
else()
# don't update automatically
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
CMAKE_ARGS -DBUILD_GTEST=On
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/gtest
UPDATE_DISCONNECTED 1
INSTALL_COMMAND "")
ExternalProject_Get_Property(googletest source_dir binary_dir)
include_directories(${source_dir}/googletest/include/)
set(GTEST_LINK_FLAGS ${binary_dir}/googlemock/gtest/libgtest.a ${binary_dir}/googlemock/gtest/libgtest_main.a -lpthread CACHE STRING "GTest link flags" FORCE)
# for local build one has to make tests dependent on the external project
set(GTEST_DEPENDENCY googletest)
endif(GTEST_FOUND AND ${TESTS_FORCE_BUILD})

add_subdirectory(tests)
endif(${WITH_TESTS})
151 changes: 35 additions & 116 deletions examples/benchmarker.cpp
Original file line number Diff line number Diff line change
@@ -1,140 +1,59 @@
//
// Created by mcopik on 07.12.16.
//

#include <array>
#include <string>
#include <iostream>
#include <random>
#include <string>

#include <benchmarker/benchmarker.hpp>
#include <libraries.hpp>

using linalg_tests::benchmarker;

#ifdef HAVE_BLAZE
/// Blaze kernel as a function
/// Random initialization takes more time due to:
/// a) lack of randomly initialized matrices
/// b) unnecessary copy caused by forEach being an expression
/// evaluated on assignment
/// \param b
/// \param rows
/// \param cols
/// \return
static void blaze_kernel(benchmarker & b, int rows, int cols)
{
int idx = b.add_clock();
b.start_clock( idx );
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(0, 1);
blaze::DynamicMatrix<double> A(rows, cols);
A = blaze::forEach(A, [&](double) { return dis(gen); });
blaze::DynamicMatrix<double> B(rows, cols);
B = blaze::forEach(B, [&](double) { return dis(gen); });
b.stop_clock(idx);

b.start_clock(b.add_clock());
// TODO: remove when better non-lazy evaluation is implemented
blaze::DynamicMatrix<double> C = blaze::eval(A * blaze::trans(B));
// Use the feature of stopping last clock
b.stop_clock();
}
#endif

#ifdef HAVE_EIGEN
/// Eigen kernel as a function object
struct eigen_kernel
{
benchmarker & b;

eigen_kernel(benchmarker & b_) :
b(b_) {}

void operator()(int rows, int cols)
{
// Note that we can rely on increasing indices, remembering is not necessary
b.start_clock( b.add_clock() );
Eigen::MatrixXd A = Eigen::MatrixXd::Random(rows, cols);
Eigen::MatrixXd B = Eigen::MatrixXd::Random(rows, cols);
// Use the feature of stopping last clock
b.stop_clock();

b.start_clock(b.add_clock());
auto C = (A * B.transpose()).eval();
b.stop_clock();
}

};
/// Eigen kernel as a function object
struct eigen_kernel {
benchmarker& b;

eigen_kernel(benchmarker& b_)
: b(b_)
{
}

void operator()(int rows, int cols)
{
Eigen::MatrixXd A = Eigen::MatrixXd::Random(rows, cols);
Eigen::MatrixXd B = Eigen::MatrixXd::Random(rows, cols);

auto C = (A * B.transpose()).eval();
}
};
#endif

int main()
{
benchmarker benchmark;
benchmark.set_cache_size(8 * 1024 * 1024);
int rows = 200, cols = 100;
int iters = 100;
benchmarker benchmark;
benchmark.set_cache_size(8 * 1024 * 1024);
int rows = 200, cols = 100;
int iters = 5;

// Run a function with additional args
#ifdef HAVE_BLAZE
benchmark.run(iters, blaze_kernel, rows, cols);
#endif
// Run a functor without benchmark arg

// Run a functor without benchmark arg
#ifdef HAVE_EIGEN
eigen_kernel k(benchmark);
benchmark.run(iters, k, rows, cols);
#endif

// Allocate clocks before run
// Run a lambda expression without args - capture everything
benchmark.reserve_clocks(2);
#ifdef HAVE_ARMADILLO
benchmark.run(iters,
[=, &benchmark]() {
benchmark.start_clock(0);
arma::mat A = arma::randu<arma::mat>(rows, cols);
arma::mat B = arma::randu<arma::mat>(rows, cols);
benchmark.stop_clock(0);

benchmark.start_clock(1);
auto C = (A * B.t() ).eval();
benchmark.stop_clock(1);
});
std::array<std::string, 2> labels_arma{ { "Armadillo", "1" } };
benchmark.run(std::cout, labels_arma, iters,
[=, &benchmark]() {
arma::mat A = arma::randu<arma::mat>(rows, cols);
arma::mat B = arma::randu<arma::mat>(rows, cols);

auto C = (A * B.t()).eval();
});
#endif

#ifdef HAVE_MTL4
benchmark.run(iters,
[=, &benchmark]() {
benchmark.start_clock(0);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(0, 1);
mtl::mat::dense2D<double> A(rows, cols);
std::for_each(A.address_data(), A.address_data() + rows*cols,
[&](double & val) {
val = dis(gen);
});
mtl::mat::dense2D<double> B(rows, cols);
std::for_each(B.address_data(), B.address_data() + rows*cols,
[&](double & val) {
val = dis(gen);
});
benchmark.stop_clock(0);

benchmark.start_clock(1);
auto C = mtl::traits::evaluate(A * mtl::mat::trans(B) );
benchmark.stop_clock(1);
});
#ifdef HAVE_EIGEN
std::array<std::string, 2> labels_eigen{ { "Eigen", "1" } };
eigen_kernel k(benchmark);
benchmark.run(std::cout, labels_eigen, iters, k, rows, cols);
#endif

std::array<std::string, 4> labels{{"Blaze", "Eigen", "Armadillo", "MTL4"}};
std::array<std::string, 2> clock_labels{{"Initialization", "Computation"}};

benchmark.print_results(std::cout, labels, clock_labels);
}




Loading

0 comments on commit 5da4f4b

Please sign in to comment.