feat(parallelization): enable OpenMP/loop parallelism as a compilation target independently of HPX/DFR.

This commit is contained in:
Antoniu Pop
2022-03-28 09:52:45 +01:00
committed by Antoniu Pop
parent 9d754e947c
commit 0e38e0a48c
3 changed files with 14 additions and 49 deletions

View File

@@ -8,7 +8,7 @@
#include <map>
#include <mutex>
#include <string>
#include <pthread.h>
extern "C" {
#include "concrete-ffi.h"
@@ -20,53 +20,23 @@ namespace concretelang {
typedef struct RuntimeContext {
LweKeyswitchKey_u64 *ksk;
LweBootstrapKey_u64 *bsk;
#ifdef CONCRETELANG_PARALLEL_EXECUTION_ENABLED
std::map<std::string, Engine *> engines;
std::map<pthread_t, Engine *> engines;
std::mutex engines_map_guard;
#else
Engine *engine;
#endif
RuntimeContext()
#ifndef CONCRETELANG_PARALLEL_EXECUTION_ENABLED
: engine(nullptr)
#endif
{
}
RuntimeContext() {}
// Ensure that the engines map is not copied
RuntimeContext(const RuntimeContext &ctx)
: ksk(ctx.ksk), bsk(ctx.bsk)
#ifndef CONCRETELANG_PARALLEL_EXECUTION_ENABLED
,
engine(nullptr)
#endif
{
}
RuntimeContext(const RuntimeContext &ctx) : ksk(ctx.ksk), bsk(ctx.bsk) {}
RuntimeContext(const RuntimeContext &&other)
: ksk(other.ksk), bsk(other.bsk)
#ifndef CONCRETELANG_PARALLEL_EXECUTION_ENABLED
,
engine(nullptr)
#endif
{
}
: ksk(other.ksk), bsk(other.bsk) {}
~RuntimeContext() {
#ifdef CONCRETELANG_PARALLEL_EXECUTION_ENABLED
for (const auto &key : engines) {
free_engine(key.second);
}
#else
if (engine != nullptr)
free_engine(engine);
#endif
}
RuntimeContext &operator=(const RuntimeContext &rhs) {
ksk = rhs.ksk;
bsk = rhs.bsk;
#ifndef CONCRETELANG_PARALLEL_EXECUTION_ENABLED
engine = nullptr;
#endif
return *this;
}
} RuntimeContext;

View File

@@ -4,6 +4,10 @@ add_library(ConcretelangRuntime SHARED
dfr_terminate.cpp
)
if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
link_libraries(-Wl,--no-as-needed omp)
endif()
if(CONCRETELANG_PARALLEL_EXECUTION_ENABLED)
add_library(DFRuntime SHARED DFRuntime.cpp)
target_link_libraries(DFRuntime PUBLIC pthread m dl HPX::hpx HPX::iostreams_component -rdynamic)
@@ -21,9 +25,9 @@ if(CONCRETELANG_PARALLEL_EXECUTION_ENABLED)
-Wl,--no-as-needed
DFRuntime
omp
)
)
else()
target_link_libraries(ConcretelangRuntime Concrete pthread m dl $<TARGET_OBJECTS:mlir_c_runner_utils>)
target_link_libraries(ConcretelangRuntime Concrete pthread m dl $<TARGET_OBJECTS:mlir_c_runner_utils> omp)
endif()
install(TARGETS ConcretelangRuntime omp EXPORT ConcretelangRuntime)

View File

@@ -7,10 +7,6 @@
#include <concretelang/Runtime/context.h>
#include <stdio.h>
#ifdef CONCRETELANG_PARALLEL_EXECUTION_ENABLED
#include <hpx/include/runtime.hpp>
#endif
LweKeyswitchKey_u64 *
get_keyswitch_key_u64(mlir::concretelang::RuntimeContext *context) {
return context->ksk;
@@ -23,20 +19,15 @@ get_bootstrap_key_u64(mlir::concretelang::RuntimeContext *context) {
// Instantiate one engine per thread on demand
Engine *get_engine(mlir::concretelang::RuntimeContext *context) {
#ifdef CONCRETELANG_PARALLEL_EXECUTION_ENABLED
std::string threadName = hpx::get_thread_name();
pthread_t threadId = pthread_self();
std::lock_guard<std::mutex> guard(context->engines_map_guard);
auto engineIt = context->engines.find(threadName);
auto engineIt = context->engines.find(threadId);
if (engineIt == context->engines.end()) {
engineIt =
context->engines
.insert(std::pair<std::string, Engine *>(threadName, new_engine()))
.insert(std::pair<pthread_t, Engine *>(threadId, new_engine()))
.first;
}
assert(engineIt->second && "No engine available in context");
return engineIt->second;
#else
return (context->engine == nullptr) ? context->engine = new_engine()
: context->engine;
#endif
}