diff --git a/.ccls b/.ccls index da94613..a6ca40c 100644 --- a/.ccls +++ b/.ccls @@ -14,4 +14,5 @@ -DKOMPUTE_INCLUDE_FOR_SYNTAX -I./external/ +-I./src/include/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 6749925..567c247 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,31 +5,14 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -find_package(fmt REQUIRED) -find_package(spdlog REQUIRED) -find_package(Vulkan REQUIRED) +# Custom commands +option(KOMPUTE_BUILD_TESTS "Enable if you want to build tests" ON) -file(GLOB kompute_SRC - "${CMAKE_CURRENT_LIST_DIR}/src/*.hpp" - "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp" - "${CMAKE_CURRENT_LIST_DIR}/src/shaders/*.hpp" - "${CMAKE_CURRENT_LIST_DIR}/src/shaders/*.cpp" -) +add_subdirectory(src) -add_executable(kompute - ${kompute_SRC}) - -target_include_directories( - kompute PUBLIC - fmt - spdlog - Vulkan -) - -target_link_libraries( - kompute - fmt::fmt - spdlog::spdlog - Vulkan::Vulkan -) +if(KOMPUTE_BUILD_TESTS) + include(CTest) + enable_testing() + add_subdirectory(test) +endif() diff --git a/Makefile b/Makefile index 24d0e45..0554e5e 100755 --- a/Makefile +++ b/Makefile @@ -22,6 +22,9 @@ run_cmake: -DCMAKE_EXPORT_COMPILE_COMMANDS=1 \ -G "Visual Studio 16 2019" +clean_cmake: + rm -rf build/ + build: clean build_shaders $(CC) \ src/* \ diff --git a/src/Algorithm.cpp b/src/Algorithm.cpp index d788ffe..da183f3 100644 --- a/src/Algorithm.cpp +++ b/src/Algorithm.cpp @@ -1,6 +1,6 @@ #include -#include "Algorithm.hpp" +#include "kompute/Algorithm.hpp" namespace kp { diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..e69b0c1 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,39 @@ + +find_package(fmt REQUIRED) +find_package(spdlog REQUIRED) +find_package(Vulkan REQUIRED) + +file(GLOB kompute_CPP + "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" +) + +add_library(kompute + ${kompute_CPP}) + +target_include_directories( + kompute PUBLIC + $ + $ +) + +target_link_libraries( + kompute + fmt::fmt + spdlog::spdlog + Vulkan::Vulkan +) + +add_library(kompute::kompute ALIAS kompute) + +install(TARGETS kompute EXPORT KomputeTargets + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib + INCLUDES DESTINATION include) + +install(DIRECTORY include/ DESTINATION include) + +install(EXPORT KomputeTargets + FILE KomputeTargets.cmake + NAMESPACE kp:: + DESTINATION lib/cmake/kompute) + diff --git a/src/Manager.cpp b/src/Manager.cpp index 0e85e65..a44f9d6 100644 --- a/src/Manager.cpp +++ b/src/Manager.cpp @@ -2,7 +2,7 @@ #include #include -#include "Manager.hpp" +#include "kompute/Manager.hpp" namespace kp { diff --git a/src/OpCreateTensor.cpp b/src/OpCreateTensor.cpp index 4362ffb..5638e87 100644 --- a/src/OpCreateTensor.cpp +++ b/src/OpCreateTensor.cpp @@ -1,7 +1,7 @@ -#include "Tensor.hpp" +#include "kompute/Tensor.hpp" -#include "OpCreateTensor.hpp" +#include "kompute/OpCreateTensor.hpp" namespace kp { diff --git a/src/OpMult.hpp b/src/OpMult.hpp deleted file mode 100644 index 96a2fdb..0000000 --- a/src/OpMult.hpp +++ /dev/null @@ -1,62 +0,0 @@ -// Defining OPMULT_H to ensure cpp class doesn't reimport -#define OPMULT_HPP -#pragma once - -#include -#include - -// SPDLOG_ACTIVE_LEVEL must be defined before spdlog.h import -#if DEBUG -#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG -#endif - -#include - -#include "Algorithm.hpp" -#include "Tensor.hpp" - -#include "OpBase.hpp" - -namespace kp { - -template -class OpMult : public OpBase -{ - public: - OpMult(); - - OpMult(std::shared_ptr physicalDevice, - std::shared_ptr device, - std::shared_ptr commandBuffer); - - ~OpMult(); - - void init(std::vector> tensors) override; - - void record() override; - - void postSubmit() override; - - private: - // Always owned resources - std::shared_ptr mTensorOutputStaging; - - // Optionally owned resources - std::shared_ptr mAlgorithm; - bool mFreeAlgorithm = false; - - // Never owned resources - std::shared_ptr mTensorLHS; - std::shared_ptr mTensorRHS; - std::shared_ptr mTensorOutput; - - uint32_t mX; - uint32_t mY; - uint32_t mZ; -}; - -} // End namespace kp - -// Including implemenation for template class -#include "OpMult.cpp" - diff --git a/src/Sequence.cpp b/src/Sequence.cpp index 6cd06c9..ab24e19 100644 --- a/src/Sequence.cpp +++ b/src/Sequence.cpp @@ -1,5 +1,5 @@ -#include "Sequence.hpp" +#include "kompute/Sequence.hpp" namespace kp { diff --git a/src/Tensor.cpp b/src/Tensor.cpp index 1f77116..141ea57 100755 --- a/src/Tensor.cpp +++ b/src/Tensor.cpp @@ -3,7 +3,7 @@ #include #endif -#include "Tensor.hpp" +#include "kompute/Tensor.hpp" namespace kp { diff --git a/src/Algorithm.hpp b/src/include/kompute/Algorithm.hpp similarity index 98% rename from src/Algorithm.hpp rename to src/include/kompute/Algorithm.hpp index e2228e7..1dd50a2 100644 --- a/src/Algorithm.hpp +++ b/src/include/kompute/Algorithm.hpp @@ -10,7 +10,7 @@ #include -#include "Tensor.hpp" +#include "kompute/Tensor.hpp" namespace kp { diff --git a/src/Manager.hpp b/src/include/kompute/Manager.hpp similarity index 98% rename from src/Manager.hpp rename to src/include/kompute/Manager.hpp index 4d2acf5..4ae61af 100644 --- a/src/Manager.hpp +++ b/src/include/kompute/Manager.hpp @@ -10,7 +10,7 @@ #include -#include "Sequence.hpp" +#include "kompute/Sequence.hpp" namespace kp { diff --git a/src/OpBase.hpp b/src/include/kompute/OpBase.hpp similarity index 97% rename from src/OpBase.hpp rename to src/include/kompute/OpBase.hpp index 381e808..599b430 100644 --- a/src/OpBase.hpp +++ b/src/include/kompute/OpBase.hpp @@ -12,7 +12,7 @@ #include -#include "Tensor.hpp" +#include "kompute/Tensor.hpp" namespace kp { diff --git a/src/OpCreateTensor.hpp b/src/include/kompute/OpCreateTensor.hpp similarity index 92% rename from src/OpCreateTensor.hpp rename to src/include/kompute/OpCreateTensor.hpp index 3ca15e6..16d7979 100644 --- a/src/OpCreateTensor.hpp +++ b/src/include/kompute/OpCreateTensor.hpp @@ -10,9 +10,9 @@ #include -#include "Tensor.hpp" +#include "kompute/Tensor.hpp" -#include "OpBase.hpp" +#include "kompute/OpBase.hpp" namespace kp { diff --git a/src/OpMult.cpp b/src/include/kompute/OpMult.hpp similarity index 83% rename from src/OpMult.cpp rename to src/include/kompute/OpMult.hpp index 8cec0d5..ccdce5a 100644 --- a/src/OpMult.cpp +++ b/src/include/kompute/OpMult.hpp @@ -1,19 +1,65 @@ -#ifndef OPMULT_CPP -#define OPMULT_CPP +#pragma once #include -#include "Tensor.hpp" +#include +#include -#if RELEASE -#include +// SPDLOG_ACTIVE_LEVEL must be defined before spdlog.h import +#if DEBUG +#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG #endif -// Only defining hpp file for syntax validation in editors -#ifndef OPMULT_HPP -#include "OpMult.hpp" -#endif +#include +#include "kompute/Algorithm.hpp" +#include "kompute/Tensor.hpp" + +#include "kompute/OpBase.hpp" + +namespace kp { + +template +class OpMult : public OpBase +{ + public: + OpMult(); + + OpMult(std::shared_ptr physicalDevice, + std::shared_ptr device, + std::shared_ptr commandBuffer); + + ~OpMult(); + + void init(std::vector> tensors) override; + + void record() override; + + void postSubmit() override; + + private: + // Always owned resources + std::shared_ptr mTensorOutputStaging; + + // Optionally owned resources + std::shared_ptr mAlgorithm; + bool mFreeAlgorithm = false; + + // Never owned resources + std::shared_ptr mTensorLHS; + std::shared_ptr mTensorRHS; + std::shared_ptr mTensorOutput; + + uint32_t mX; + uint32_t mY; + uint32_t mZ; +}; + +} // End namespace kp + +// Including implemenation for template class +#ifndef OPMULT_CPP +#define OPMULT_CPP namespace kp { @@ -187,3 +233,4 @@ OpMult::postSubmit() #endif // #ifndef OPMULT_CPP + diff --git a/src/Parameter.hpp b/src/include/kompute/Parameter.hpp similarity index 98% rename from src/Parameter.hpp rename to src/include/kompute/Parameter.hpp index 4ad4170..5e93931 100644 --- a/src/Parameter.hpp +++ b/src/include/kompute/Parameter.hpp @@ -10,7 +10,7 @@ #include -#include "Tensor.hpp" +#include "kompute/Tensor.hpp" namespace kp { diff --git a/src/Sequence.hpp b/src/include/kompute/Sequence.hpp similarity index 98% rename from src/Sequence.hpp rename to src/include/kompute/Sequence.hpp index 3d46eb9..e426987 100644 --- a/src/Sequence.hpp +++ b/src/include/kompute/Sequence.hpp @@ -10,7 +10,7 @@ #include -#include "OpBase.hpp" +#include "kompute/OpBase.hpp" namespace kp { diff --git a/src/Tensor.hpp b/src/include/kompute/Tensor.hpp similarity index 100% rename from src/Tensor.hpp rename to src/include/kompute/Tensor.hpp diff --git a/src/shaders/computeheadless.hpp b/src/include/kompute/shaders/computeheadless.hpp similarity index 100% rename from src/shaders/computeheadless.hpp rename to src/include/kompute/shaders/computeheadless.hpp diff --git a/src/shaders/machinelearning.hpp b/src/include/kompute/shaders/machinelearning.hpp similarity index 100% rename from src/shaders/machinelearning.hpp rename to src/include/kompute/shaders/machinelearning.hpp diff --git a/src/shaders/opmult.hpp b/src/include/kompute/shaders/opmult.hpp similarity index 100% rename from src/shaders/opmult.hpp rename to src/include/kompute/shaders/opmult.hpp diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100755 index e5f74d6..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,117 +0,0 @@ -#if defined(_WIN32) -#pragma comment(linker, "/subsystem:console") -#endif - -// SPDLOG_ACTIVE_LEVEL must be defined before spdlog.h import -#if DEBUG -#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG -#endif - -#include - -#include -// ranges.h must come after spdlog.h -#include - -#include -#include - -#include "Manager.hpp" -#include "OpCreateTensor.hpp" -#include "OpMult.hpp" -#include "Tensor.hpp" - -int -main() -{ -#if DEBUG - spdlog::set_level(spdlog::level::debug); -#else - spdlog::set_level(spdlog::level::info); -#endif - - try { - - // Run Kompute - - { - spdlog::info("Creating manager"); - kp::Manager mgr; - - spdlog::info("Creating first tensor"); - std::shared_ptr tensorLHS{ new kp::Tensor( { 0, 1, 2 }) }; - mgr.evalOp({ tensorLHS }); - - spdlog::info("Creating second tensor"); - std::shared_ptr tensorRHS{ new kp::Tensor( - { 2, 4, 6 }) }; - mgr.evalOp({ tensorRHS }); - - // TODO: Add capabilities for just output tensor types - spdlog::info("Creating output tensor"); - std::shared_ptr tensorOutput{ new kp::Tensor( - { 0, 0, 0 }) }; - mgr.evalOp({ tensorOutput }); - - spdlog::info("OpCreateTensor success for tensors"); - spdlog::info("Tensor one: {}", tensorLHS->data()); - spdlog::info("Tensor two: {}", tensorRHS->data()); - spdlog::info("Tensor output: {}", tensorOutput->data()); - - spdlog::info("Calling op mult"); - mgr.evalOp>({ tensorLHS, tensorRHS, tensorOutput }); - - spdlog::info("OpMult call success"); - spdlog::info("Tensor output: {}", tensorOutput->data()); - - spdlog::info("Called manager eval success END PROGRAM"); - } - { - spdlog::info("Creating manager"); - kp::Manager mgr; - kp::Sequence sq = mgr.constructSequence(); - sq.begin(); - - spdlog::info("Creating first tensor"); - std::shared_ptr tensorLHS{ new kp::Tensor( - { 0, 1, 2 }) }; - - spdlog::info("Creating second tensor"); - std::shared_ptr tensorRHS{ new kp::Tensor( - { 2, 4, 6 }) }; - - // TODO: Add capabilities for just output tensor types - spdlog::info("Creating output tensor"); - std::shared_ptr tensorOutput{ new kp::Tensor( - { 0, 0, 0 }) }; - - sq.record({ tensorLHS }); - sq.record({ tensorRHS }); - sq.record({ tensorOutput }); - - spdlog::info("OpCreateTensor success for tensors"); - spdlog::info("Tensor one: {}", tensorLHS->data()); - spdlog::info("Tensor two: {}", tensorRHS->data()); - spdlog::info("Tensor output: {}", tensorOutput->data()); - - spdlog::info("Calling op mult"); - sq.record>({ tensorLHS, tensorRHS, tensorOutput }); - - sq.end(); - sq.eval(); - - spdlog::info("OpMult call success"); - spdlog::info("Tensor output: {}", tensorOutput->data()); - - spdlog::info("Called manager eval success END PROGRAM"); - } - - return 0; - } catch (const std::exception& exc) { - spdlog::error("Exception caught: {}", exc.what()); - return 1; - } catch (...) { - spdlog::error("Uncaught exception"); - return 1; - } -} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..dc2b0a2 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,11 @@ + +find_package(Catch2 REQUIRED) + +add_executable(test_kompute Main.cpp) + +target_link_libraries(test_kompute PRIVATE Catch2::Catch2) +target_link_libraries(test_kompute PRIVATE kompute) + +add_test(NAME test_kompute COMMAND test_kompute) + + diff --git a/test/Main.cpp b/test/Main.cpp old mode 100644 new mode 100755 index 6ba5b1e..7252872 --- a/test/Main.cpp +++ b/test/Main.cpp @@ -1,4 +1,117 @@ -#define CATCH_CONFIG_MAIN +#if defined(_WIN32) +#pragma comment(linker, "/subsystem:console") +#endif -#include "catch2/catch.hpp" +// SPDLOG_ACTIVE_LEVEL must be defined before spdlog.h import +#if DEBUG +#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG +#endif +#include + +#include +// ranges.h must come after spdlog.h +#include + +#include +#include + +#include "kompute/Manager.hpp" +#include "kompute/OpCreateTensor.hpp" +#include "kompute/OpMult.hpp" +#include "kompute/Tensor.hpp" + +int +main() +{ +#if DEBUG + spdlog::set_level(spdlog::level::debug); +#else + spdlog::set_level(spdlog::level::info); +#endif + + try { + + // Run Kompute + + { + spdlog::info("Creating manager"); + kp::Manager mgr; + + spdlog::info("Creating first tensor"); + std::shared_ptr tensorLHS{ new kp::Tensor( { 0, 1, 2 }) }; + mgr.evalOp({ tensorLHS }); + + spdlog::info("Creating second tensor"); + std::shared_ptr tensorRHS{ new kp::Tensor( + { 2, 4, 6 }) }; + mgr.evalOp({ tensorRHS }); + + // TODO: Add capabilities for just output tensor types + spdlog::info("Creating output tensor"); + std::shared_ptr tensorOutput{ new kp::Tensor( + { 0, 0, 0 }) }; + mgr.evalOp({ tensorOutput }); + + spdlog::info("OpCreateTensor success for tensors"); + spdlog::info("Tensor one: {}", tensorLHS->data()); + spdlog::info("Tensor two: {}", tensorRHS->data()); + spdlog::info("Tensor output: {}", tensorOutput->data()); + + spdlog::info("Calling op mult"); + mgr.evalOp>({ tensorLHS, tensorRHS, tensorOutput }); + + spdlog::info("OpMult call success"); + spdlog::info("Tensor output: {}", tensorOutput->data()); + + spdlog::info("Called manager eval success END PROGRAM"); + } + { + spdlog::info("Creating manager"); + kp::Manager mgr; + kp::Sequence sq = mgr.constructSequence(); + sq.begin(); + + spdlog::info("Creating first tensor"); + std::shared_ptr tensorLHS{ new kp::Tensor( + { 0, 1, 2 }) }; + + spdlog::info("Creating second tensor"); + std::shared_ptr tensorRHS{ new kp::Tensor( + { 2, 4, 6 }) }; + + // TODO: Add capabilities for just output tensor types + spdlog::info("Creating output tensor"); + std::shared_ptr tensorOutput{ new kp::Tensor( + { 0, 0, 0 }) }; + + sq.record({ tensorLHS }); + sq.record({ tensorRHS }); + sq.record({ tensorOutput }); + + spdlog::info("OpCreateTensor success for tensors"); + spdlog::info("Tensor one: {}", tensorLHS->data()); + spdlog::info("Tensor two: {}", tensorRHS->data()); + spdlog::info("Tensor output: {}", tensorOutput->data()); + + spdlog::info("Calling op mult"); + sq.record>({ tensorLHS, tensorRHS, tensorOutput }); + + sq.end(); + sq.eval(); + + spdlog::info("OpMult call success"); + spdlog::info("Tensor output: {}", tensorOutput->data()); + + spdlog::info("Called manager eval success END PROGRAM"); + } + + return 0; + } catch (const std::exception& exc) { + spdlog::error("Exception caught: {}", exc.what()); + return 1; + } catch (...) { + spdlog::error("Uncaught exception"); + return 1; + } +} diff --git a/test/TestMain.cpp b/test/TestMain.cpp new file mode 100644 index 0000000..6ba5b1e --- /dev/null +++ b/test/TestMain.cpp @@ -0,0 +1,4 @@ +#define CATCH_CONFIG_MAIN + +#include "catch2/catch.hpp" +