Skip to content

Commit

Permalink
Merge branch 'main' into remove_python_2
Browse files Browse the repository at this point in the history
  • Loading branch information
jhaigh0 authored Oct 14, 2024
2 parents 3e8a9f4 + 10c753c commit 29428ba
Show file tree
Hide file tree
Showing 334 changed files with 4,300 additions and 4,123 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ repos:
exclude: conda/recipes/mantid/meta.yaml|conda/recipes/mantid-developer/meta.yaml|conda/recipes/mantidqt/meta.yaml|conda/recipes/mantiddocs/meta.yaml|conda/recipes/mantidworkbench/meta.yaml

- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v11.1.0 # updating to a new version should wait until maintenance
rev: v12.0.1 # updating to a new version should wait until maintenance
hooks:
- id: clang-format
exclude: Testing/Tools/cxxtest|tools

- repo: https://github.com/mantidproject/pre-commit-hooks.git
rev: 2f8a4f22629d0d23332f621df9de93751331161b
rev: 17c2842277953a4afcbca26bf84e8956f729e765
hooks:
- id: cmake-missing-pytest-files
# Exclude sphinx / template file
Expand Down Expand Up @@ -59,7 +59,7 @@ repos:
)$
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.4
rev: v0.6.8
# ruff must appear before black in the list of hooks
hooks:
- id: ruff
Expand Down
10 changes: 8 additions & 2 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@
"inherits": [
"ci-default",
"conda"
]
],
"cacheVariables": {
"PROFILE_ALGORITHM_LINUX": "ON"
}
},
{
"name": "win-64-ci",
Expand Down Expand Up @@ -120,7 +123,10 @@
"inherits": [
"unix-debug",
"conda"
]
],
"cacheVariables": {
"PROFILE_ALGORITHM_LINUX": "ON"
}
},
{
"name": "win-vs-2019",
Expand Down
17 changes: 9 additions & 8 deletions Framework/API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,6 @@ set(INC_FILES
inc/MantidAPI/Workspace_fwd.h
)

option(PROFILE_ALGORITHM_LINUX "Profile algorithm execution on Linux" OFF)
if(PROFILE_ALGORITHM_LINUX)
set(SRC_FILES "${SRC_FILES}" "src/AlgorithmExecuteProfile.cpp" "src/AlgoTimeRegister.cpp")
set(INC_FILES "${INC_FILES}" "inc/MantidAPI/AlgoTimeRegister.h")
else()
set(SRC_FILES "${SRC_FILES}" "src/AlgorithmExecute.cpp")
endif()

set(TEST_FILES
ADSValidatorTest.h
AlgorithmFactoryObserverTest.h
Expand Down Expand Up @@ -490,6 +482,15 @@ set(TEST_FILES
WorkspaceUnitValidatorTest.h
)

option(PROFILE_ALGORITHM_LINUX "Profile algorithm execution on Linux" OFF)
if(PROFILE_ALGORITHM_LINUX)
set(SRC_FILES "${SRC_FILES}" "src/AlgorithmExecuteProfile.cpp" "src/AlgoTimeRegister.cpp")
set(INC_FILES "${INC_FILES}" "inc/MantidAPI/AlgoTimeRegister.h")
set(TEST_FILES "${TEST_FILES}" "AlgoTimeRegisterTest.h")
else()
set(SRC_FILES "${SRC_FILES}" "src/AlgorithmExecute.cpp")
endif()

set(GMOCK_TEST_FILES ImplicitFunctionFactoryTest.h ImplicitFunctionParameterParserFactoryTest.h MatrixWorkspaceTest.h)

if(COVERAGE)
Expand Down
45 changes: 26 additions & 19 deletions Framework/API/inc/MantidAPI/AlgoTimeRegister.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once

#include "MantidKernel/Logger.h"
#include "MantidKernel/SingletonHolder.h"
#include "MantidKernel/Timer.h"
#include <mutex>
#include <thread>
Expand All @@ -19,42 +21,47 @@ namespace Instrumentation {
/** AlgoTimeRegister : simple class to dump information about executed
* algorithms
*/
class AlgoTimeRegister {
class MANTID_API_DLL AlgoTimeRegisterImpl {
public:
static AlgoTimeRegister globalAlgoTimeRegister;
struct Info {
std::string m_name;
std::thread::id m_threadId;
Kernel::time_point_ns m_begin;
Kernel::time_point_ns m_end;

Info(const std::string &nm, const std::thread::id &id, const Kernel::time_point_ns &be,
const Kernel::time_point_ns &en)
: m_name(nm), m_threadId(id), m_begin(be), m_end(en) {}
};
AlgoTimeRegisterImpl(const AlgoTimeRegisterImpl &) = delete;
AlgoTimeRegisterImpl &operator=(const AlgoTimeRegisterImpl &) = delete;

class Dump {
AlgoTimeRegister &m_algoTimeRegister;
Kernel::time_point_ns m_regStart_chrono;

const std::string m_name;

public:
Dump(AlgoTimeRegister &atr, const std::string &nm);
Dump(const std::string &nm);
~Dump();
};

void addTime(const std::string &name, const std::thread::id thread_id, const Kernel::time_point_ns &begin,
const Kernel::time_point_ns &end);
void addTime(const std::string &name, const Kernel::time_point_ns &begin, const Kernel::time_point_ns &end);
AlgoTimeRegister();
~AlgoTimeRegister();

private:
std::mutex m_mutex;
std::vector<Info> m_info;

private:
friend struct Mantid::Kernel::CreateUsingNew<AlgoTimeRegisterImpl>;

AlgoTimeRegisterImpl();
~AlgoTimeRegisterImpl();

bool writeToFile();

Kernel::time_point_ns m_start;
std::string m_filename;
bool m_hasWrittenToFile;
};

using AlgoTimeRegister = Mantid::Kernel::SingletonHolder<AlgoTimeRegisterImpl>;

} // namespace Instrumentation
} // namespace Mantid

namespace Mantid {
namespace Kernel {
EXTERN_MANTID_API template class MANTID_API_DLL
Mantid::Kernel::SingletonHolder<Mantid::Instrumentation::AlgoTimeRegisterImpl>;
} // namespace Kernel
} // namespace Mantid
8 changes: 6 additions & 2 deletions Framework/API/inc/MantidAPI/Column.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,13 @@ class MANTID_API_DLL Column {
return vec;
}

virtual bool equals(const Column &, double) const { throw std::runtime_error("equals not implemented"); };
virtual bool equals(const Column &, double, bool const = false) const {
throw std::runtime_error("equals not implemented");
};

virtual bool equalsRelErr(const Column &, double) const { throw std::runtime_error("equals not implemented"); };
virtual bool equalsRelErr(const Column &, double, bool const = false) const {
throw std::runtime_error("equals not implemented");
};

protected:
/// Sets the new column size.
Expand Down
2 changes: 1 addition & 1 deletion Framework/API/inc/MantidAPI/ISISInstrumentDataCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MANTID_API_DLL ISISInstrumentDataCache {
std::pair<Mantid::Kernel::InstrumentInfo, std::string> validateInstrumentAndNumber(const std::string &filename) const;
std::filesystem::path makeIndexFilePath(const std::string &instrumentName) const;
std::pair<std::string, std::string> splitIntoInstrumentAndNumber(const std::string &filename) const;
[[nodiscard]] std::pair<std::string, Json::Value>
[[nodiscard]] std::pair<std::filesystem::path, Json::Value>
openCacheJsonFile(const Mantid::Kernel::InstrumentInfo &instrument) const;
[[nodiscard]] Mantid::Kernel::InstrumentInfo getInstrumentFromName(const std::string &instName) const;
std::string m_dataCachePath;
Expand Down
90 changes: 65 additions & 25 deletions Framework/API/src/AlgoTimeRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,84 @@
namespace Mantid {
namespace Instrumentation {

namespace {
Kernel::Logger &LOGGER() {
static Kernel::Logger logger("AlgoTimeRegister");
return logger;
}
} // namespace

using Kernel::ConfigService;
using Kernel::time_point_ns;

AlgoTimeRegister::Dump::Dump(AlgoTimeRegister &atr, const std::string &nm)
: m_algoTimeRegister(atr), m_regStart_chrono(std::chrono::high_resolution_clock::now()), m_name(nm) {}
AlgoTimeRegisterImpl::Dump::Dump(const std::string &nm)
: m_regStart_chrono(std::chrono::high_resolution_clock::now()), m_name(nm) {}

AlgoTimeRegister::Dump::~Dump() {
AlgoTimeRegisterImpl::Dump::~Dump() {
const time_point_ns regFinish = std::chrono::high_resolution_clock::now();
{
std::lock_guard<std::mutex> lock(m_algoTimeRegister.m_mutex);
m_algoTimeRegister.addTime(m_name, std::this_thread::get_id(), m_regStart_chrono, regFinish);
}
{ AlgoTimeRegister::Instance().addTime(m_name, std::this_thread::get_id(), m_regStart_chrono, regFinish); }
}

void AlgoTimeRegister::addTime(const std::string &name, const std::thread::id thread_id,
const Kernel::time_point_ns &begin, const Kernel::time_point_ns &end) {
m_info.emplace_back(name, thread_id, begin, end);
void AlgoTimeRegisterImpl::addTime(const std::string &name, const Kernel::time_point_ns &begin,
const Kernel::time_point_ns &end) {
AlgoTimeRegister::Instance().addTime(name, std::this_thread::get_id(), begin, end);
}

void AlgoTimeRegister::addTime(const std::string &name, const Kernel::time_point_ns &begin,
const Kernel::time_point_ns &end) {
this->addTime(name, std::this_thread::get_id(), begin, end);
}
bool AlgoTimeRegisterImpl::writeToFile() {
auto writeEnable = Kernel::ConfigService::Instance().getValue<bool>("performancelog.write").value();
if (!writeEnable) {
LOGGER().debug() << "performancelog.write is disabled (off/0/false)\n";
return false;
}
auto filename = Kernel::ConfigService::Instance().getString("performancelog.filename");
if (filename.empty()) {
LOGGER().debug() << "performancelog.filename is empty, please provide valid filename\n";
return false;
}
if (m_filename == filename && m_hasWrittenToFile) {
return true;
}

m_filename = filename;

AlgoTimeRegister::AlgoTimeRegister() : m_start(std::chrono::high_resolution_clock::now()) {}
LOGGER().debug() << "Performance log file: " << m_filename << '\n';

AlgoTimeRegister::~AlgoTimeRegister() {
std::fstream fs;
fs.open("./algotimeregister.out", std::ios::out);
// c++20 has an implementation of operator<<
fs << "START_POINT: " << std::chrono::duration_cast<std::chrono::nanoseconds>(m_start.time_since_epoch()).count()
<< " MAX_THREAD: " << PARALLEL_GET_MAX_THREADS << "\n";
for (auto &elem : m_info) {
const std::chrono::nanoseconds st = elem.m_begin - m_start;
const std::chrono::nanoseconds fi = elem.m_end - m_start;
fs << "ThreadID=" << elem.m_threadId << ", AlgorithmName=" << elem.m_name << ", StartTime=" << st.count()
<< ", EndTime=" << fi.count() << "\n";

fs.open(m_filename, std::ios::out);
if (fs.is_open()) {
fs << "START_POINT: " << std::chrono::duration_cast<std::chrono::nanoseconds>(m_start.time_since_epoch()).count()
<< " MAX_THREAD: " << PARALLEL_GET_MAX_THREADS << "\n";
fs.close();
m_hasWrittenToFile = true;
} else {
LOGGER().notice() << "Failed to open the file, timing will not write to file.\n";
return false;
}
return true;
}

void AlgoTimeRegisterImpl::addTime(const std::string &name, const std::thread::id thread_id,
const Kernel::time_point_ns &begin, const Kernel::time_point_ns &end) {
std::lock_guard<std::mutex> lock(AlgoTimeRegister::Instance().m_mutex);
if (writeToFile()) {
std::fstream fs;
fs.open(m_filename, std::ios::out | std::ios::app);
if (fs.is_open()) {
const std::chrono::nanoseconds st = begin - m_start;
const std::chrono::nanoseconds fi = end - m_start;
fs << "ThreadID=" << thread_id << ", AlgorithmName=" << name << ", StartTime=" << st.count()
<< ", EndTime=" << fi.count() << "\n";

fs.close();
}
}
}

AlgoTimeRegisterImpl::AlgoTimeRegisterImpl()
: m_start(std::chrono::high_resolution_clock::now()), m_hasWrittenToFile(false) {}

AlgoTimeRegisterImpl::~AlgoTimeRegisterImpl() {}

} // namespace Instrumentation
} // namespace Mantid
7 changes: 3 additions & 4 deletions Framework/API/src/AlgorithmExecuteProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "MantidAPI/Algorithm.h"

namespace Mantid {
Instrumentation::AlgoTimeRegister Instrumentation::AlgoTimeRegister::globalAlgoTimeRegister;
namespace API {

//---------------------------------------------------------------------------------------------
Expand All @@ -23,13 +22,13 @@ namespace API {
* @return true if executed successfully.
*/
bool Algorithm::execute() {
Instrumentation::AlgoTimeRegister::AlgoTimeRegister::Dump dmp(
Instrumentation::AlgoTimeRegister::globalAlgoTimeRegister, name());
Instrumentation::AlgoTimeRegister::Instance();
Instrumentation::AlgoTimeRegisterImpl::Dump dmp(name());
return executeInternal();
}
void Algorithm::addTimer(const std::string &name, const Kernel::time_point_ns &begin,
const Kernel::time_point_ns &end) {
Instrumentation::AlgoTimeRegister::globalAlgoTimeRegister.addTime(name, begin, end);
Instrumentation::AlgoTimeRegister::Instance().addTime(name, begin, end);
}
} // namespace API
} // namespace Mantid
3 changes: 2 additions & 1 deletion Framework/API/src/FileFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,8 @@ const API::Result<std::string> FileFinderImpl::getPath(const std::vector<IArchiv
errors += cacheFilePath.errors();

} else {
errors += "Could not find data cache directory: " + cachePathToSearch.string();
g_log.debug() << "Data cache directory not found, proceeding with the search." << std::endl;
errors += "Could not find data cache directory: " + cachePathToSearch.string() + '\n';
}

// Search the archive
Expand Down
3 changes: 1 addition & 2 deletions Framework/API/src/GroupingLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ void GroupingLoader::loadGroupingFromXML(const std::string &filename, Grouping &
try // ... to convert value to double
{
grouping.pairAlphas[ip] = boost::lexical_cast<double>(aElement->getAttribute("val"));
}
catch (boost::bad_lexical_cast &) {
} catch (boost::bad_lexical_cast &) {
throw Mantid::Kernel::Exception::FileError("Pair alpha value is not a number", filename);
}
}
Expand Down
18 changes: 10 additions & 8 deletions Framework/API/src/ISISInstrumentDataCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ std::string ISISInstrumentDataCache::getFileParentDirectoryPath(const std::strin
g_log.debug() << "ISISInstrumentDataCache::getFileParentDirectoryPath(" << fileName << ")" << std::endl;

auto [instrumentInfo, runNumber] = validateInstrumentAndNumber(fileName);
std::string instrName = instrumentInfo.name();

auto const [jsonPath, json] = openCacheJsonFile(instrumentInfo);

std::string relativePath = json[runNumber].asString();

if (relativePath.empty()) {
throw std::invalid_argument("Run number " + runNumber + " not found for instrument " + instrName + ".");
throw std::invalid_argument("Run number " + runNumber + " not found in index file " + jsonPath.filename().string() +
".");
}

std::string dirPath = m_dataCachePath + "/" + instrName + "/" + relativePath;
std::filesystem::path dirPath = jsonPath.parent_path() / relativePath;
std::string dirPathString = dirPath.make_preferred().string();

g_log.debug() << "Opened instrument index file: " << jsonPath << ". Found path to search: " << dirPath << "."
<< std::endl;
return dirPath;
g_log.debug() << "Found path to search: " << dirPathString << std::endl;
return dirPathString;
}

/**
Expand All @@ -57,7 +57,8 @@ std::vector<std::string> ISISInstrumentDataCache::getRunNumbersInCache(const std
* @return A pair containing the path and the contents of the json file.
* @throws std::invalid_argument if the file could not be found.
*/
std::pair<std::string, Json::Value> ISISInstrumentDataCache::openCacheJsonFile(const InstrumentInfo &instrument) const {
std::pair<std::filesystem::path, Json::Value>
ISISInstrumentDataCache::openCacheJsonFile(const InstrumentInfo &instrument) const {
// Open index json file
std::filesystem::path jsonPath = makeIndexFilePath(instrument.name());
std::ifstream ifstrm{jsonPath};
Expand All @@ -71,7 +72,8 @@ std::pair<std::string, Json::Value> ISISInstrumentDataCache::openCacheJsonFile(c
// Read directory path from json file
Json::Value json;
ifstrm >> json;
return {jsonPath.string(), json};
g_log.debug() << "Opened instrument index file: " << jsonPath << std::endl;
return {jsonPath, json};
}

std::pair<InstrumentInfo, std::string>
Expand Down
Loading

0 comments on commit 29428ba

Please sign in to comment.