adding parallel loops

This commit is contained in:
Andy Pack 2025-01-28 18:18:21 +00:00
parent c8205adb50
commit 9c9e1c9828
Signed by: sarsoo
GPG Key ID: A55BA3536A5E0ED7
3 changed files with 29 additions and 13 deletions

2
.gitignore vendored

@ -55,7 +55,7 @@ bld/
[Bb]uild/
[Oo]bj/
[Ll]og/
cmake-build-debug
cmake-build-debug*
# Visual Studio 2015 cache/options directory
.vs/

@ -5,12 +5,16 @@
#include "config.hpp"
#include "logging.hpp"
#if __cpp_lib_execution //checking to see if the <execution> header is there
#include <execution>
#endif
namespace kc {
AppContext::AppContext(): config_loaded(false) {
}
void AppContext::load_config(int argc, const char *argv[])
void AppContext::load_config(const int argc, const char *argv[])
{
config = init_config(argc, argv);
config_loaded = true;
@ -29,15 +33,24 @@ void AppContext::load_and_parse_cache(ParseOperations operations)
const auto env_paths = (*config)["path"].as<std::vector<std::string>>();
for (const auto& env_path : env_paths) {
#ifdef __cpp_lib_execution
std::mutex m;
std::for_each(std::execution::par, env_paths.begin(), env_paths.end(), [this, &operations, &m](const std::string &env_path)
#else
std::ranges::for_each(env_paths, [this, &operations](const std::string &env_path)
#endif
{
print_and_log("Loading knowledge base from " + env_path);
auto file_cache = std::make_shared<kc::FileContextCache>();
const auto file_cache = std::make_shared<kc::FileContextCache>();
file_cache->load(env_path);
file_cache->parse_all(operations);
#ifdef __cpp_lib_execution
std::lock_guard<std::mutex> lock{m};
#endif
file_caches.push_back(file_cache);
}
});
}
std::string AppContext::command() const {
@ -45,10 +58,7 @@ std::string AppContext::command() const {
{
return (*config)["command"].as<std::string>();
}
else
{
return "";
}
return "";
}
}

@ -1,7 +1,10 @@
#include "FileContextCache.hpp"
#include <algorithm>
#if __cpp_lib_execution //checking to see if the <execution> header is there
#include <execution>
#endif
#include "../fs/fs.hpp"
#include "../logging.hpp"
@ -36,12 +39,12 @@ void FileContextCache::parse_all()
void FileContextCache::parse_all(ParseOperations operations) {
tag_map.clear();
#if __APPLE__
std::for_each(file_contexts.begin(), file_contexts.end(), [this, &operations](std::shared_ptr<kc::FileContext> &context)
#ifdef __cpp_lib_execution
std::mutex m;
std::for_each(std::execution::par, file_contexts.begin(), file_contexts.end(), [this, &operations, &m](std::shared_ptr<kc::FileContext> &context)
#else
std::for_each(std::execution::par_unseq, file_contexts.begin(), file_contexts.end(), [this, &operations](std::shared_ptr<kc::FileContext> &context)
std::ranges::for_each(file_contexts, [this, &operations](const std::shared_ptr<kc::FileContext> &context)
#endif
{
if (context->file_entry->relative_path.extension() == ".md")
{
@ -49,6 +52,9 @@ void FileContextCache::parse_all(ParseOperations operations) {
if (!context->tags.empty())
{
#ifdef __cpp_lib_execution
std::lock_guard<std::mutex> lock{m};
#endif
for (const auto& tag : context->tags)
{
tag_map[tag].push_back(context->file_entry);