Skip to content

Commit

Permalink
Updated implementation to not be build based on Morpheus. Added gener…
Browse files Browse the repository at this point in the history
…ic functors with a predifined interface so that users can perform tuning not only to Sparse Matrices
  • Loading branch information
cstyl committed Dec 16, 2023
1 parent 839baea commit 39df791
Show file tree
Hide file tree
Showing 44 changed files with 1,066 additions and 784 deletions.
7 changes: 0 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,6 @@ if(MorpheusOracle_INSTALL_TESTING)
morpheusoracle_add_test_directories(tests)
morpheusoracle_add_example_directories(examples)
else()
# Regular build, not install testing Do all the regular option processing
if(NOT MORPHEUSORACLE_HAS_PARENT)
# This is a standalone build
find_package(Morpheus REQUIRED)
message(STATUS "Found Morpheus at ${Morpheus_DIR}")
endif()

add_subdirectory(src)

if(MorpheusOracle_ENABLE_TESTS OR MorpheusOracle_ENABLE_EXAMPLES)
Expand Down
2 changes: 0 additions & 2 deletions cmake/MorpheusOracleConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ include(CMakeFindDependencyMacro)

@MORPHEUSORACLE_TPL_EXPORTS@

find_dependency(Morpheus HINTS @Morpheus_DIR@)

INCLUDE("${MorpheusOracle_CMAKE_DIR}/MorpheusOracleTargets.cmake")
58 changes: 0 additions & 58 deletions cmake/morpheusoracle_utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -178,64 +178,6 @@ function(MORPHEUSORACLE_ADD_LIBRARY LIBRARY_NAME)
morpheusoracle_internal_add_library(${LIBRARY_NAME} ${ARGN})
endfunction()

# function(MORPHEUSORACLE_ADD_EXECUTABLE EXE_NAME) cmake_parse_arguments(PARSE
# "" "" "SOURCES;COMPONENTS;TESTONLYLIBS" ${ARGN})
# verify_empty(MORPHEUSORACLE_ADD_EXECUTABLE ${PARSE_UNPARSED_ARGUMENTS})

# morpheusoracle_is_enabled(COMPONENTS ${PARSE_COMPONENTS} OUTPUT_VARIABLE
# IS_ENABLED)

# if(IS_ENABLED) add_executable(${EXE_NAME} ${PARSE_SOURCES}) # AJP, BMK
# altered: if(MORPHEUSORACLE_ENABLE_TESTS_AND_PERFSUITE)
# target_link_libraries(${EXE_NAME} PRIVATE common ${PARSE_TESTONLYLIBS})
# endif()

# if(PARSE_TESTONLYLIBS) target_link_libraries(${EXE_NAME} PRIVATE
# Morpheus::morpheus-oracle ${PARSE_TESTONLYLIBS}) else()
# target_link_libraries(${EXE_NAME} PRIVATE Morpheus::morpheus-oracle) endif()
# else() message( STATUS "Skipping executable ${EXE_NAME} because not all
# necessary components enabled" ) endif() endfunction()

# function(MORPHEUSORACLE_ADD_EXECUTABLE_AND_TEST ROOT_NAME)

# cmake_parse_arguments(PARSE "" "" "SOURCES;CATEGORIES;COMPONENTS;TESTONLYLIBS"
# ${ARGN}) verify_empty(MORPHEUSORACLE_ADD_EXECUTABLE_AND_RUN_VERIFY
# ${PARSE_UNPARSED_ARGUMENTS})

# morpheusoracle_is_enabled(COMPONENTS ${PARSE_COMPONENTS} OUTPUT_VARIABLE
# IS_ENABLED)

# if(IS_ENABLED) set(EXE_NAME ${PACKAGE_NAME}_${ROOT_NAME})
# morpheusoracle_add_executable(${EXE_NAME} SOURCES ${PARSE_SOURCES})
# if(PARSE_TESTONLYLIBS) target_link_libraries(${EXE_NAME} PRIVATE
# ${PARSE_TESTONLYLIBS}) endif() morpheusoracle_add_test(NAME ${ROOT_NAME} EXE
# ${EXE_NAME}) else() message( STATUS "Skipping executable/test ${ROOT_NAME}
# because not all necessary components enabled" ) endif()

# endfunction()

# function(MORPHEUSORACLE_ADD_TEST)

# cmake_parse_arguments(PARSE "" "" "COMPONENTS" ${ARGN})

# morpheusoracle_is_enabled(COMPONENTS ${PARSE_COMPONENTS} OUTPUT_VARIABLE
# IS_ENABLED)

# if(IS_ENABLED) cmake_parse_arguments( TEST "WILL_FAIL"
# "FAIL_REGULAR_EXPRESSION;PASS_REGULAR_EXPRESSION;EXE;NAME" "CATEGORIES"
# ${PARSE_UNPARSED_ARGUMENTS}) if(TEST_EXE) set(EXE ${TEST_EXE}) else() set(EXE
# ${TEST_NAME}) endif() if(WIN32) add_test( NAME ${TEST_NAME} WORKING_DIRECTORY
# ${LIBRARY_OUTPUT_PATH} COMMAND ${EXE}${CMAKE_EXECUTABLE_SUFFIX}) else()
# add_test(NAME ${TEST_NAME} COMMAND ${EXE}) endif() if(TEST_WILL_FAIL)
# set_tests_properties(${TEST_NAME} PROPERTIES WILL_FAIL ${TEST_WILL_FAIL})
# endif() if(TEST_FAIL_REGULAR_EXPRESSION) set_tests_properties( ${TEST_NAME}
# PROPERTIES FAIL_REGULAR_EXPRESSION ${TEST_FAIL_REGULAR_EXPRESSION}) endif()
# if(TEST_PASS_REGULAR_EXPRESSION) set_tests_properties( ${TEST_NAME} PROPERTIES
# PASS_REGULAR_EXPRESSION ${TEST_PASS_REGULAR_EXPRESSION}) endif()
# verify_empty(MORPHEUSORACLE_ADD_TEST ${TEST_UNPARSED_ARGUMENTS}) else()
# message( STATUS "Skipping test ${TEST_NAME} because not all necessary
# components enabled" ) endif() endfunction()

function(MORPHEUSORACLE_ADD_EXECUTABLE_AND_TEST ROOT_NAME)
cmake_parse_arguments(PARSE "" "" "SOURCES;CATEGORIES;ARGS" ${ARGN})
verify_empty(MORPHEUSORACLE_ADD_EXECUTABLE_AND_TEST
Expand Down
4 changes: 4 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
morpheusoracle_include_directories(${CMAKE_CURRENT_BINARY_DIR})
morpheusoracle_include_directories(${CMAKE_CURRENT_SOURCE_DIR})


find_package(Morpheus REQUIRED)
message(STATUS "Found Morpheus at ${Morpheus_DIR}")

add_subdirectory(run-first-tuner)
add_subdirectory(decision-tree-tuner)
add_subdirectory(random-forest-tuner)
4 changes: 4 additions & 0 deletions examples/decision-tree-tuner/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ if(Morpheus_ENABLE_CUDA)
morpheusoracle_add_test_executable(decision_tree_tuner_CUDA SOURCES
decision_tree_tuner.cpp)
target_compile_definitions(MorpheusOracle_decision_tree_tuner_CUDA PRIVATE EXAMPLE_ENABLE_CUDA)
target_link_libraries(MorpheusOracle_decision_tree_tuner_CUDA PUBLIC Morpheus::morpheus)
elseif(Morpheus_ENABLE_HIP)
morpheusoracle_add_test_executable(decision_tree_tuner_HIP SOURCES decision_tree_tuner.cpp)
target_compile_definitions(MorpheusOracle_decision_tree_tuner_HIP PRIVATE EXAMPLE_ENABLE_HIP)
target_link_libraries(MorpheusOracle_decision_tree_tuner_HIP PUBLIC Morpheus::morpheus)
elseif(Morpheus_ENABLE_OPENMP)
morpheusoracle_add_test_executable(decision_tree_tuner_OPENMP SOURCES
decision_tree_tuner.cpp)
target_compile_definitions(MorpheusOracle_decision_tree_tuner_OPENMP
PRIVATE EXAMPLE_ENABLE_OPENMP)
target_link_libraries(MorpheusOracle_decision_tree_tuner_OPENMP PUBLIC Morpheus::morpheus)
else()
morpheusoracle_add_test_executable(decision_tree_tuner_SERIAL SOURCES
decision_tree_tuner.cpp)
target_compile_definitions(MorpheusOracle_decision_tree_tuner_SERIAL
PRIVATE EXAMPLE_ENABLE_SERIAL)
target_link_libraries(MorpheusOracle_decision_tree_tuner_SERIAL PUBLIC Morpheus::morpheus)
endif()
62 changes: 60 additions & 2 deletions examples/decision-tree-tuner/decision_tree_tuner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

#include <Morpheus_Oracle.hpp>
#include <Morpheus_Core.hpp>

#if defined(EXAMPLE_ENABLE_SERIAL)
using Space = Morpheus::Serial;
Expand All @@ -36,6 +37,61 @@ using Space = Morpheus::HIP;
using backend = typename Space::backend;
using DynamicMatrix = Morpheus::DynamicMatrix<double, backend>;

template <typename ExecSpace>
struct ExtractFeaturesFunctor : public Morpheus::Oracle::MLFunctorBase<
ExtractFeaturesFunctor<ExecSpace>> {
using features_vector_t = Morpheus::DenseVector<double, Morpheus::HostSpace>;

ExtractFeaturesFunctor(size_t nfeatures)
: _nfeatures(nfeatures), _features(nfeatures, 0) {}

template <typename Data>
void extract_features(
const Data& data,
typename std::enable_if_t<Morpheus::is_dynamic_matrix_container_v<Data> &&
Morpheus::has_access_v<ExecSpace, Data>>* =
nullptr) {
using size_type = typename Data::size_type;
using index_type = typename Data::index_type;
using memory_space = typename Data::memory_space;
using IndexVector =
Morpheus::DenseVector<index_type, size_type, memory_space>;

if (_features.size() != _nfeatures) {
_features.resize(_nfeatures, 0);
}

IndexVector nnz_per_row(data.nrows(), 0);
IndexVector nnz_per_diag(data.nrows() + data.ncols() - 1, 0);
Morpheus::count_nnz_per_row<ExecSpace>(data, nnz_per_row, false);
Morpheus::count_nnz_per_diagonal<ExecSpace>(data, nnz_per_diag, false);

_features[0] = Morpheus::number_of_rows(data);
_features[1] = Morpheus::number_of_columns(data);
_features[2] = Morpheus::number_of_nnz(data);
_features[3] = Morpheus::average_nnnz(data);
_features[4] = Morpheus::density(data);
_features[5] = Morpheus::max<ExecSpace>(nnz_per_row, nnz_per_row.size());
_features[6] = Morpheus::min<ExecSpace>(nnz_per_row, nnz_per_row.size());
_features[7] = Morpheus::std<ExecSpace>(nnz_per_row, nnz_per_row.size(),
Morpheus::average_nnnz(data));
_features[8] = Morpheus::count_nnz<ExecSpace>(nnz_per_diag);
_features[9] =
Morpheus::count_nnz<ExecSpace>(nnz_per_diag, data.nrows() / 5);
}

features_vector_t& features() { return _features; }

template <typename Tuner>
void inference(Tuner& tuner) {
tuner.run(features());
}

private:
size_t _nfeatures;
Morpheus::DenseVector<double, Morpheus::HostSpace> _features;
};

int main(int argc, char* argv[]) {
Morpheus::initialize(argc, argv);
{
Expand Down Expand Up @@ -67,8 +123,10 @@ int main(int argc, char* argv[]) {
DynamicMatrix A = Morpheus::create_mirror<Space>(Ah);
Morpheus::copy(Ah, A);

Morpheus::Oracle::DecisionTreeTuner tuner(ftree, true);
Morpheus::Oracle::tune_multiply<backend>(A, tuner);
Morpheus::Oracle::DecisionTreeTuner tuner(ftree, true, true, true);
ExtractFeaturesFunctor<Space> f(10);

Morpheus::Oracle::tune(A, f, tuner);
tuner.print();
}
Morpheus::finalize();
Expand Down
54 changes: 0 additions & 54 deletions examples/decision-tree-tuner/tree.txt

This file was deleted.

Binary file added examples/decision-tree-tuner/tree_bin/tree.bin
Binary file not shown.
30 changes: 30 additions & 0 deletions examples/decision-tree-tuner/tree_bin/tree.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Sizes (NFeatures, Nclasses, NodeCount, MaxDepth)
10 3 13 5
# Classes
0 1 3
# Feature Names Sizes
5 5 4 7 7 10 10 10 6 10
# Feature Names
Nrows Ncols Nnnz AvgNnnz Density MaxRowNnnz MinRowNnnz StdRowNnnz NDiags NTrueDiags
# Left
1 2 3 4 -1 -1 7 -1 9 -1 -1 -1 -1
# Right
12 11 6 5 -1 -1 8 -1 10 -1 -1 -1 -1
# Threshold
44.85799980163574 13.5 22.0 0.004978629993274808 -2.0 -2.0 0.0034212350146844983 -2.0 0.003925944911316037 -2.0 -2.0 -2.0 -2.0
# Feature
7 6 5 4 -2 -2 4 -2 4 -2 -2 -2 -2
# Values
3.0 53.0 3.0
1.0 53.0 3.0
1.0 53.0 2.0
0.0 2.0 1.0
0.0 0.0 1.0
0.0 2.0 0.0
1.0 51.0 1.0
0.0 18.0 0.0
1.0 33.0 1.0
1.0 31.0 1.0
0.0 2.0 0.0
0.0 0.0 1.0
2.0 0.0 0.0
4 changes: 4 additions & 0 deletions examples/random-forest-tuner/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ if(Morpheus_ENABLE_CUDA)
morpheusoracle_add_test_executable(random_forest_tuner_CUDA SOURCES
random_forest_tuner.cpp)
target_compile_definitions(MorpheusOracle_random_forest_tuner_CUDA PRIVATE EXAMPLE_ENABLE_CUDA)
target_link_libraries(MorpheusOracle_random_forest_tuner_CUDA PUBLIC Morpheus::morpheus)
elseif(Morpheus_ENABLE_HIP)
morpheusoracle_add_test_executable(random_forest_tuner_HIP SOURCES random_forest_tuner.cpp)
target_compile_definitions(MorpheusOracle_random_forest_tuner_HIP PRIVATE EXAMPLE_ENABLE_HIP)
target_link_libraries(MorpheusOracle_random_forest_tuner_HIP PUBLIC Morpheus::morpheus)
elseif(Morpheus_ENABLE_OPENMP)
morpheusoracle_add_test_executable(random_forest_tuner_OPENMP SOURCES
random_forest_tuner.cpp)
target_compile_definitions(MorpheusOracle_random_forest_tuner_OPENMP
PRIVATE EXAMPLE_ENABLE_OPENMP)
target_link_libraries(MorpheusOracle_random_forest_tuner_OPENMP PUBLIC Morpheus::morpheus)
else()
morpheusoracle_add_test_executable(random_forest_tuner_SERIAL SOURCES
random_forest_tuner.cpp)
target_compile_definitions(MorpheusOracle_random_forest_tuner_SERIAL
PRIVATE EXAMPLE_ENABLE_SERIAL)
target_link_libraries(MorpheusOracle_random_forest_tuner_SERIAL PUBLIC Morpheus::morpheus)
endif()
6 changes: 0 additions & 6 deletions examples/random-forest-tuner/forest/metadata.txt

This file was deleted.

46 changes: 0 additions & 46 deletions examples/random-forest-tuner/forest/tree_0.txt

This file was deleted.

Loading

0 comments on commit 39df791

Please sign in to comment.