IndexerThreadPool: Minor fixes
Build Packages / build:rpm (ubuntu2204) (push) Has been cancelled
Build Packages / build:rpm (ubuntu2404) (push) Has been cancelled
Build Packages / DIALS test (push) Has been cancelled
Build Packages / XDS test (durin plugin) (push) Has been cancelled
Build Packages / build:rpm (rocky8_nocuda) (push) Has been cancelled
Build Packages / XDS test (JFJoch plugin) (push) Has been cancelled
Build Packages / XDS test (neggia plugin) (push) Has been cancelled
Build Packages / Generate python client (push) Has been cancelled
Build Packages / build:rpm (rocky8_sls9) (push) Has been cancelled
Build Packages / Build documentation (push) Has been cancelled
Build Packages / Unit tests (push) Has been cancelled
Build Packages / Create release (push) Has been cancelled
Build Packages / build:rpm (rocky9_nocuda) (push) Has started running
Build Packages / build:rpm (rocky8) (push) Has been cancelled
Build Packages / build:rpm (rocky9_sls9) (push) Has been cancelled
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Has been cancelled
Build Packages / build:rpm (rocky9) (push) Has been cancelled
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Has been cancelled
Build Packages / build:rpm (ubuntu2204) (push) Has been cancelled
Build Packages / build:rpm (ubuntu2404) (push) Has been cancelled
Build Packages / DIALS test (push) Has been cancelled
Build Packages / XDS test (durin plugin) (push) Has been cancelled
Build Packages / build:rpm (rocky8_nocuda) (push) Has been cancelled
Build Packages / XDS test (JFJoch plugin) (push) Has been cancelled
Build Packages / XDS test (neggia plugin) (push) Has been cancelled
Build Packages / Generate python client (push) Has been cancelled
Build Packages / build:rpm (rocky8_sls9) (push) Has been cancelled
Build Packages / Build documentation (push) Has been cancelled
Build Packages / Unit tests (push) Has been cancelled
Build Packages / Create release (push) Has been cancelled
Build Packages / build:rpm (rocky9_nocuda) (push) Has started running
Build Packages / build:rpm (rocky8) (push) Has been cancelled
Build Packages / build:rpm (rocky9_sls9) (push) Has been cancelled
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Has been cancelled
Build Packages / build:rpm (rocky9) (push) Has been cancelled
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Has been cancelled
This commit is contained in:
@@ -180,13 +180,33 @@ IndexerThread::~IndexerThread() {
|
||||
Finalize();
|
||||
}
|
||||
|
||||
IndexerThreadPool::IndexerThreadPool(const IndexingSettings &settings)
|
||||
IndexerThreadPool::IndexerThreadPool(const IndexingSettings &settings, bool blocking)
|
||||
: worker_busy(settings.GetIndexingThreads(), 0),
|
||||
viable_cell_min_spots(settings.GetViableCellMinSpots()) {
|
||||
worker_free_count(settings.GetIndexingThreads()),
|
||||
viable_cell_min_spots(settings.GetViableCellMinSpots()),
|
||||
blocking(blocking) {
|
||||
for (size_t i = 0; i < settings.GetIndexingThreads(); ++i)
|
||||
tasks.emplace_back(std::make_unique<IndexerThread>(std::cref(settings), i));
|
||||
}
|
||||
|
||||
int IndexerThreadPool::GetFreeWorker() {
|
||||
std::unique_lock<std::mutex> lock(m);
|
||||
|
||||
if (tasks.size() == 0)
|
||||
return -1;
|
||||
|
||||
if (blocking)
|
||||
c.wait(lock, [this] { return worker_free_count > 0; });
|
||||
|
||||
for (int i = 0; i < tasks.size(); i++) {
|
||||
if (worker_busy[i] == 0) {
|
||||
worker_busy[i] = 1;
|
||||
worker_free_count--;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
IndexerResult IndexerThreadPool::Run(const DiffractionExperiment &experiment, const std::vector<Coord> &recip) {
|
||||
if (experiment.GetIndexingAlgorithm() == IndexingAlgorithmEnum::None)
|
||||
@@ -195,25 +215,22 @@ IndexerResult IndexerThreadPool::Run(const DiffractionExperiment &experiment, co
|
||||
return IndexerResult{.lattice = {}, .indexing_time_s = 0};
|
||||
|
||||
// Check if there is available worker
|
||||
int task = -1;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m);
|
||||
for (int i = 0; i < tasks.size(); i++) {
|
||||
if (worker_busy[i] == 0) {
|
||||
task = i;
|
||||
worker_busy[i] = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
const int task = GetFreeWorker();
|
||||
|
||||
std::unique_ptr<IndexerResult> result;
|
||||
if (task >= 0) {
|
||||
result = tasks[task]->Run(experiment, recip);
|
||||
try {
|
||||
result = tasks[task]->Run(experiment, recip);
|
||||
} catch (const std::exception &e) {
|
||||
spdlog::error("Indexer thread failed: {}", e.what());
|
||||
result = nullptr;
|
||||
}
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m);
|
||||
worker_busy[task] = 0;
|
||||
worker_free_count++;
|
||||
}
|
||||
c.notify_one();
|
||||
}
|
||||
if (result)
|
||||
return *result;
|
||||
|
||||
Reference in New Issue
Block a user