Commit 44b0f664 authored by Stephen's avatar Stephen
Browse files

Create RAII structure to encapsulate omp_set_num_threads in Loader

This commit creates a RAII structure to encapsulate the omp_set_num_threads_call. This is necessary to restore the default number of threads once the algorithm has finished. This also adds a unit test to ensure the max threads is restored after the algorithm is executed.
parent f2889ad4
......@@ -179,10 +179,25 @@ void LoadMuonNexusV2::isEntryMultiPeriod(const NXEntry &entry) {
*/
void LoadMuonNexusV2::runLoadISISNexus() {
// Here we explicit set the number of OpenMP threads, as by default
// LoadISISNexus spawns up a large number of threads, this was found
// when the algorithm was profiled, which showed 100% CPU usage. This is
// unnecessary as the Muon file has a small number of spectra (typically ~100)
omp_set_num_threads(1);
// LoadISISNexus spawns up a large number of threads,
// which is unnecessary for the size (~100 spectra) of workspaces seen here.
// Through profiling it was found that a single threaded call to LoadISISNexus
// was quicker due to the overhead of setting up the threads, which outweighs
// the cost of the resulting operations.
// To prevent the omp_set_num_threads call having side effects, we use a RAII
// pattern to restore the default behavior once runLoadISISNexus is complete.
struct ScopedNumThreadsSetter {
ScopedNumThreadsSetter(const int numThreads) {
(void)numThreads; // Treat compiler warning in OSX
globalNumberOfThreads = PARALLEL_GET_MAX_THREADS;
PARALLEL_SET_NUM_THREADS(numThreads);
}
~ScopedNumThreadsSetter() {
PARALLEL_SET_NUM_THREADS(globalNumberOfThreads);
}
int globalNumberOfThreads;
};
ScopedNumThreadsSetter restoreDefaultThreadsOnExit(1);
IAlgorithm_sptr childAlg =
createChildAlgorithm("LoadISISNexus", 0, 1, true, 2);
declareProperty("LoadMonitors", "Exclude"); // we need to set this property
......
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source
// & Institut Laue - Langevin
// Copyright © 2020 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
......@@ -156,7 +156,8 @@ public:
TS_ASSERT_EQUALS(groupingTable->cell<std::vector<int>>(0, 0),
testGroupingVec);
testGroupingVec.clear();
for (int i = 49; i < output2D->getNumberHistograms() + 1; ++i)
for (int i = 49; i < static_cast<int>(output2D->getNumberHistograms() + 1);
++i)
testGroupingVec.emplace_back(i);
TS_ASSERT_EQUALS(groupingTable->cell<std::vector<int>>(1, 0),
testGroupingVec);
......@@ -262,6 +263,18 @@ public:
TS_ASSERT_THROWS(ld.execute(), const std::invalid_argument &)
}
void testMaxThreadsRestoredWhenAlgorithmFinished() {
int maxThreads = PARALLEL_GET_MAX_THREADS;
LoadMuonNexusV2 ld;
ld.initialize();
ld.setPropertyValue("Filename", "EMU00103638.nxs_v2");
ld.setPropertyValue("OutputWorkspace", "outWS");
ld.execute();
TS_ASSERT_EQUALS(maxThreads, PARALLEL_GET_MAX_THREADS)
}
};
//------------------------------------------------------------------------------
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment