Skip to content

Commit

Permalink
rework on win32
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson-numerical-software committed Oct 14, 2024
1 parent cf6fdc2 commit fe9e75b
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 114 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,6 @@ jobs:
- name: Tests
run: |
set OMP_NUM_THREADS=1
set NELSON_TERM_IS_UNICODE_SUPPORTED=TRUE
set PATH=C:\Program Files\Microsoft MPI\Bin;%PATH%
set TESTS_RESULT_DIR=%GITHUB_WORKSPACE%/artifacts
Expand Down
10 changes: 6 additions & 4 deletions modules/ipc/src/cpp/NelsonInterprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
//=============================================================================
namespace Nelson {
//=============================================================================
constexpr std::size_t KILOBYTE = 1024;
constexpr std::size_t MEGABYTE = 1024 * KILOBYTE;
#if (defined(_LP64) || defined(_WIN64))
constexpr auto OFF_MSG_SIZE = sizeof(double) * 16 * 1024;
constexpr auto MAX_MSG_SIZE = sizeof(double) * (4096 * 4096) + OFF_MSG_SIZE;
constexpr auto MAX_NB_MSG = 4;
constexpr std::size_t OFF_MSG_SIZE = sizeof(double) * 16 * KILOBYTE;
constexpr std::size_t MAX_MSG_SIZE = sizeof(double) * (4 * MEGABYTE) + OFF_MSG_SIZE;
constexpr std::size_t MAX_NB_MSG = 4;
#else
constexpr auto MAX_MSG_SIZE = sizeof(double) * (1024 * 1024);
constexpr auto MAX_MSG_SIZE = sizeof(double) * MEGABYTE;
constexpr auto MAX_NB_MSG = 2;
#endif
constexpr auto TIMEOUT_COUNT = 20;
Expand Down
58 changes: 34 additions & 24 deletions modules/os_functions/src/cpp/SystemCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#include <fcntl.h>
#include <csignal>
#endif

// #include <BS_thread_pool.hpp>
#include <ctime>
#include <thread>
#include <chrono>
Expand All @@ -34,6 +32,7 @@
#include "i18n.hpp"
#include "PredefinedErrorMessages.hpp"
#include "SystemCommandTask.hpp"
#include "nlsBuildConfig.h"
//=============================================================================
namespace Nelson {
//=============================================================================
Expand Down Expand Up @@ -69,60 +68,70 @@ ParallelSystemCommand(const wstringVector& commands, const std::vector<uint64>&
std::vector<std::tuple<int, std::wstring, uint64>> results;
size_t nbCommands = commands.size();
results.resize(nbCommands);
size_t nbThreadsMax = (size_t)NelsonConfiguration::getInstance()->getMaxNumCompThreads();
size_t nbThreads = std::min(nbCommands, nbThreadsMax);

std::vector<std::thread> threads;
std::vector<SystemCommandTask*> taskList;

#if defined(__APPLE__) or (defined(_WIN32) && not defined(_WIN64))
for (ompIndexType k = 0; k < (ompIndexType)nbCommands; k++) {
SystemCommandTask* task = new SystemCommandTask();
taskList.push_back(task);
}
#if not defined(__APPLE__)
#if WITH_OPENMP
#pragma omp parallel for
#endif
#endif
for (ompIndexType k = 0; k < (ompIndexType)nbCommands; k++) {
taskList[k]->evaluateCommand(commands[k], timeouts[k]);
}
#else
size_t nbThreadsMax = (size_t)NelsonConfiguration::getInstance()->getMaxNumCompThreads();
size_t nbThreads = std::min(nbCommands, nbThreadsMax);
std::vector<std::thread> threadList;
for (size_t k = 0; k < nbCommands; k++) {
try {
SystemCommandTask* task = new SystemCommandTask();
taskList.push_back(task);
threads.emplace_back([task, commands, timeouts, k]() {
threadList.emplace_back([task, commands, timeouts, k]() {
task->evaluateCommand(commands[k], timeouts[k]);
});
} catch (std::bad_alloc&) {
Error(ERROR_MEMORY_ALLOCATION);
}
}

while (true) {
bool allTasksFinished = false;
do {
if (NelsonConfiguration::getInstance()->getInterruptPending(evaluatorID)) {
for (SystemCommandTask* task : taskList) {
task->terminate();
for (size_t k = 0; k < nbCommands; k++) {
taskList[k]->terminate();
}
break;
}

if (withEventsLoop) {
ProcessEventsDynamicFunction();
}
std::this_thread::sleep_for(std::chrono::milliseconds(uint64(1)));

std::this_thread::sleep_for(std::chrono::milliseconds(1));
allTasksFinished = std::all_of(threadList.begin(), threadList.end(),
[](const auto& thread) { return thread.joinable(); });

bool allTasksFinished = true;
for (SystemCommandTask* task : taskList) {
if (task->isRunning()) {
allTasksFinished = false;
break;
}
}
allTasksFinished = allTasksFinished
&& std::all_of(taskList.begin(), taskList.end(),
[](const auto& task) { return !task->isRunning(); });

if (allTasksFinished) {
for (auto& thread : threads) {
for (auto& thread : threadList) {
thread.join();
}
break;
}
}
} while (!allTasksFinished);

#endif
for (size_t k = 0; k < nbCommands; k++) {
if (taskList[k]) {
results[k] = taskList[k]->getResult();
}
}

for (SystemCommandTask* task : taskList) {
if (task) {
delete task;
Expand All @@ -132,7 +141,8 @@ ParallelSystemCommand(const wstringVector& commands, const std::vector<uint64>&
taskList.clear();

return results;
} //=============================================================================
}
//=============================================================================
static void
initGuiDynamicLibrary()
{
Expand Down
Loading

0 comments on commit fe9e75b

Please sign in to comment.