Skip to content

Commit

Permalink
Refactor Kernel library management and update tests for library avail…
Browse files Browse the repository at this point in the history
…ability using Registry
  • Loading branch information
loumalouomega committed Dec 16, 2024
1 parent d8fb7e8 commit fd37f13
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 47 deletions.
15 changes: 8 additions & 7 deletions kratos/includes/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
// _|\_\_| \__,_|\__|\___/ ____/
// Multi-Physics
//
// License: BSD License
// Kratos default license: kratos/license.txt
// License: BSD License
// Kratos default license: kratos/license.txt
//
// Main authors: Pooyan Dadvand
//
//

#if !defined(KRATOS_KERNEL_H_INCLUDED)
#define KRATOS_KERNEL_H_INCLUDED
#pragma once

// System includes
#include <string>
Expand Down Expand Up @@ -137,7 +136,11 @@ class KRATOS_API(KRATOS_CORE) Kernel {

static std::unordered_set<std::string>& GetApplicationsList();

static std::unordered_set<std::string>& GetLibrayList();
/**
* @brief Get the list of libraries available in the system
* @return The list of libraries available in the system (stored in a set)
*/
static std::unordered_set<std::string> GetLibraryList();

static std::string Version();

Expand Down Expand Up @@ -202,5 +205,3 @@ inline std::ostream& operator<<(std::ostream& rOStream, const Kernel& rThis) {
///@}

} // namespace Kratos.

#endif // KRATOS_KERNEL_H_INCLUDED defined
70 changes: 43 additions & 27 deletions kratos/sources/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "includes/kratos_version.h"
#include "includes/data_communicator.h"
#include "includes/parallel_environment.h"
#include "includes/registry.h"
#include "input_output/logger.h"
#include "utilities/parallel_utilities.h"

Expand Down Expand Up @@ -57,8 +58,41 @@ void Kernel::PrintInfo() {
}

void Kernel::Initialize() {
// Print kernel info
this->PrintInfo();

// Boost is always available
Registry::AddItem<std::string>("libraries.boost");

// When using the nonfree version of TRIANGLE, add it to the list of libraries
#if USE_TRIANGLE_NONFREE_TPL
Registry::AddItem<std::string>("libraries.triangle");
#else
// Open-source version alternative to TRIANGLE
Registry::AddItem<std::string>("libraries.delaunator-cpp");
#endif

// When using the nonfree version of TETGEN, add it to the list of libraries
#if USE_TETGEN_NONFREE_TPL
Registry::AddItem<std::string>("libraries.tetgen");
#endif

// Add the libraries that are always available
Registry::AddItem<std::string>("libraries.amgcl");
Registry::AddItem<std::string>("libraries.benchmark");
Registry::AddItem<std::string>("libraries.clipper");
Registry::AddItem<std::string>("libraries.concurrentqueue");
Registry::AddItem<std::string>("libraries.ghc");
Registry::AddItem<std::string>("libraries.gidpost");
Registry::AddItem<std::string>("libraries.intrusive_ptr");
Registry::AddItem<std::string>("libraries.json");
Registry::AddItem<std::string>("libraries.pybind11");
Registry::AddItem<std::string>("libraries.span");
Registry::AddItem<std::string>("libraries.tinyexpr");
Registry::AddItem<std::string>("libraries.vexcl");
Registry::AddItem<std::string>("libraries.zlib");

// Import the Kratos core application (if not already imported)
if (!IsImported("KratosMultiphysics")) {
this->ImportApplication(mpKratosCoreApplication);
}
Expand All @@ -69,32 +103,14 @@ std::unordered_set<std::string>& Kernel::GetApplicationsList() {
return application_list;
}

std::unordered_set<std::string>& Kernel::GetLibrayList() {
// TODO: add more libraries if required
static std::unordered_set<std::string> library_list = {
#if USE_TRIANGLE_NONFREE_TPL
"triangle",
#else
"delaunator-cpp",
#endif
#if USE_TETGEN_NONFREE_TPL
"tetgen",
#endif
#if KRATOS_USE_AMATRIX
"a_matrix",
#endif
"amgcl",
"concurrentqueue",
"ghc",
"gidpost",
"intrusive_ptr",
"json",
"pybind11",
"span",
"tinyexpr",
"vexcl",
"zlib"
};
std::unordered_set<std::string> Kernel::GetLibraryList() {
std::unordered_set<std::string> library_list;

const auto& r_item = Registry::GetItem("libraries");
for (auto it_item = r_item.cbegin(); it_item != r_item.cend(); ++it_item) {
library_list.insert((it_item->second)->Name());
}

return library_list;
}

Expand All @@ -103,7 +119,7 @@ bool Kernel::IsImported(const std::string& rApplicationName) const {
}

bool Kernel::IsLibraryAvailable(const std::string& rLibraryName) const {
return GetLibrayList().find(rLibraryName) != GetLibrayList().end();
return GetLibraryList().find(rLibraryName) != GetLibraryList().end();
}

bool Kernel::IsDistributedRun() {
Expand Down
30 changes: 17 additions & 13 deletions kratos/tests/test_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ def setUp(self):
pass

def testIsLibraryAvailable(self):
self.assertFalse(KM.KratosGlobals.Kernel.IsLibraryAvailable("pikachu_lib"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("amgcl"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("concurrentqueue"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("ghc"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("gidpost"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("intrusive_ptr"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("json"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("pybind11"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("span"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("tinyexpr"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("vexcl"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("zlib"))
self.assertNotEqual(KM.KratosGlobals.Kernel.IsLibraryAvailable("triangle"), KM.KratosGlobals.Kernel.IsLibraryAvailable("delaunator-cpp"))
# Check if the libraries are available
self.assertFalse(KM.KratosGlobals.Kernel.IsLibraryAvailable("pikachu_lib"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("boost"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("amgcl"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("benchmark"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("clipper"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("concurrentqueue"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("ghc"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("gidpost"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("intrusive_ptr"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("json"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("pybind11"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("span"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("tinyexpr"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("vexcl"))
self.assertTrue(KM.KratosGlobals.Kernel.IsLibraryAvailable("zlib"))
self.assertNotEqual(KM.KratosGlobals.Kernel.IsLibraryAvailable("triangle"), KM.KratosGlobals.Kernel.IsLibraryAvailable("delaunator-cpp"))

if __name__ == "__main__":
KratosUnittest.main()

0 comments on commit fd37f13

Please sign in to comment.