Skip to content

Commit

Permalink
Module search paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ohhmm committed Mar 13, 2024
1 parent 67e9b64 commit 9ba5d9a
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@
/CMakeLists.txt.user
CMakeSettings.json
/enc_temp_folder/
*Cache.solutions
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ option(OPENMIND_DEBUG_CHECKS "Additional debug checks" OFF)
find_package(OpenCL)
option(OPENMIND_USE_OPENCL "OpenCL GPU calculations" ${OpenCL_FOUND})
option(OPENMIND_MATH_USE_LEVELDB_CACHE "DB cache solutions" ON)
set(BOOST_ADDITIONAL_COMPONENTS program_options CACHE VERSION "Boost components" FORCE)
set(BOOST_ADDITIONAL_COMPONENTS program_options;dll CACHE VERSION "Boost components" FORCE)
# Tell OpenMind to use any boost version installed in the system if any
if(Boost_FOUND)
set(OPENMIND_REQUIRED_BOOST_VERSION ${Boost_VERSION_STRING} CACHE VERSION "Boost library version to use" FORCE)
Expand Down
4 changes: 3 additions & 1 deletion libskrypt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@

lib(rt math)
lib(rt math
Boost::dll
)
60 changes: 59 additions & 1 deletion libskrypt/skrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <boost/filesystem/path.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/dll/runtime_symbol_info.hpp>
#include <boost/dll/shared_library.hpp>

#include <iostream>
#include <fstream>
Expand Down Expand Up @@ -88,6 +90,7 @@ void Skrypt::PrintVarKnowns(const Variable& v)
auto module = Module(v);
auto mappedModuleVariable = MappedModuleVariable(v, module);
if (mappedModuleVariable.getVaHost() != v.getVaHost() || mappedModuleVariable != v) {
std::cout << GetModuleName(v) << '.';
module->PrintVarKnowns(mappedModuleVariable);
return;
}
Expand Down Expand Up @@ -334,7 +337,10 @@ const omnn::math::Valuable::va_names_t& Skrypt::Load(std::istream& in)
const omnn::math::Valuable::va_names_t& Skrypt::Load(const boost::filesystem::path & path)
{
std::cout << "Loading " << path << '\n' << std::endl;
boost::filesystem::ifstream stream(path);
auto filepath = boost::filesystem::exists(path)
? path
: FindModulePath({path.string()});
boost::filesystem::ifstream stream(filepath);
return Load(stream);
}

Expand All @@ -357,6 +363,36 @@ boost::filesystem::path Skrypt::FindModulePath(std::string_view name) const {
if (!boost::filesystem::exists(path)) {
path = path.filename();
}
auto base = boost::dll::program_location();
if (!boost::filesystem::exists(path)) {
path = base.parent_path() / name;
if (!path.has_extension())
path.replace_extension(".skrypt");
}

if (!boost::filesystem::exists(path)) {
auto loc = boost::dll::this_line_location();
if (loc != base) {
path = loc.parent_path() / name;
if (!path.has_extension())
path.replace_extension(".skrypt");
}
}

if (!boost::filesystem::exists(path)) {
for (auto& searchPath : moduleFileSearchAdditionalPaths) {
path = searchPath / name;
if (!path.has_extension())
path.replace_extension(".skrypt");
if (boost::filesystem::exists(path)) {
break;
}
}
}

if (!boost::filesystem::exists(path)) {
std::cerr << "Module not found: " << path << std::endl;
}
return path;
}

Expand Down Expand Up @@ -511,6 +547,28 @@ Skrypt::module_t Skrypt::Module(const ::omnn::math::Variable& v) {
return Module(moduleName);
}

Skrypt::module_t Skrypt::WaitTillModuleLoadingComplete(std::string_view name) {
bool isModuleLoading = IsModuleLoading(name);
if (isModuleLoading) {
do {
if (isModuleLoading) {
auto loaded = modulesLoadingQueue.PeekNextResult();
auto it = loaded.find(name);
if (it != loaded.end()) {
return it->second.get();
}
isModuleLoading = IsModuleLoading(name);
if (isModuleLoading) {
std::this_thread::yield();
} else {
break;
}
}
} while (isModuleLoading);
}
return Module(name);
}

const ::omnn::math::Valuable::solutions_t& Skrypt::Known(const ::omnn::math::Variable& v)
{
auto known = std::cref(base::Known(v));
Expand Down
22 changes: 18 additions & 4 deletions libskrypt/skrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace skrypt {
class Skrypt
: public omnn::math::System
{
using base = omnn::math::System;
using base = ::omnn::math::System;
using path = ::boost::filesystem::path;

::omnn::math::VarHost::ptr varHost = ::omnn::math::VarHost::make<std::string>();

Expand All @@ -36,7 +37,8 @@ class Skrypt
mutable std::shared_mutex modulesLoadingMutex;
modules_t modulesLoading;
::omnn::rt::StoringTasksQueue<loading_modules_t> modulesLoadingQueue;
::boost::filesystem::path sourceFilePath;
path sourceFilePath;
std::vector<path> moduleFileSearchAdditionalPaths;

protected:
void SetVarhost(decltype(varHost));
Expand Down Expand Up @@ -81,19 +83,31 @@ class Skrypt
/// <param name="name">The module name</param>
module_t Module(std::string_view name);
module_t Module(const ::omnn::math::Variable&);
module_t GetLoadedModule(std::string_view fileName) const;
module_t GetLoadedModule(std::string_view name) const;

boost::filesystem::path FindModulePath(std::string_view name) const;
template <class T>
void AddModuleSearchDirPath(T&& p) {
moduleFileSearchAdditionalPaths.emplace_back(std::forward<T>(p));
}
bool IsModuleLoading(std::string_view name) const;
loading_module_t StartLoadingModule(std::string_view name);
loading_modules_t LoadModules(const ::omnn::math::Valuable& v);
loading_modules_future_t StartLoadingModules(const ::omnn::math::Valuable& v);

module_t WaitTillModuleLoadingComplete(std::string_view name);
void BackgroudLoadingModules(const ::omnn::math::Valuable& v);
std::string_view GetVariableName(const ::omnn::math::Variable&) const;
std::string_view GetModuleName(std::string_view variableName) const;
std::string_view GetModuleName(const ::omnn::math::Variable&) const;
const solutions_t& Known(const ::omnn::math::Variable& v);

/// <summary>
/// Obtainse the <variable> from the <module> by its local representation variable <module>.<variable>
/// If module object providen, the variable is obtained from it, otherwise the module deduced from the variable name
/// </summary>
/// <param name="variable">The variable</param>
/// <param name="module">The module (optional)</param>
/// <returns>Variable of submodule skrypt object</returns>
const ::omnn::math::Variable& MappedModuleVariable(const ::omnn::math::Variable&, module_t module = {});

void Echo(bool e) { echo = e; }
Expand Down
3 changes: 2 additions & 1 deletion libskrypt/tests/Modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ BOOST_AUTO_TEST_CASE(LoadingModulesNoExceptionsTest) {

BOOST_AUTO_TEST_CASE(ModulePropertyTest) {
Skrypt skrypt;
skrypt.Load(TEST_SRC_DIR "Module.skrypt");
skrypt.AddModuleSearchDirPath(TEST_SRC_DIR);
skrypt.Load("Module.skrypt");

// get the variable
auto variableName = "x"s;
Expand Down

0 comments on commit 9ba5d9a

Please sign in to comment.