Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Thread-Safe Implementation #455

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ message(STATUS "C++ version ${CXX_STANDARD} configured.")

option(XACC_BUILD_TESTS "Build test programs" OFF)
option(XACC_BUILD_EXAMPLES "Build example programs" OFF)
option(XACC_MULTI_THREADED "Enable multi-thread support" OFF)
option(XACC_ENSMALLEN_INCLUDE_DIR "Path to ensmallen.hpp for mlpack optimizer" "")
option(XACC_ARMADILLO_INCLUDE_DIR "Path to armadillo header for mlpack optimizer" "")

Expand Down Expand Up @@ -101,6 +102,15 @@ endif()

message(STATUS "${BoldGreen}Installing XACC to ${CMAKE_INSTALL_PREFIX}. Override with -DCMAKE_INSTALL_PREFIX=...${ColorReset}")

if (XACC_MULTI_THREADED)
message(STATUS "${BoldGreen}Multi-thread support is enabled ${ColorReset}")
add_compile_definitions(_XACC_MUTEX)
set(_XACC_MUTEX True)
else()
message(STATUS "${BoldGreen}Multi-thread support is disabled (default)${ColorReset}")
set(_XACC_MUTEX False)
endif()

if (XACC_BUILD_TESTS)
enable_testing()
macro(add_xacc_test _TEST_NAME)
Expand Down
3 changes: 3 additions & 0 deletions cmake/xacc-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,6 @@ set_target_properties(xacc::fermion PROPERTIES
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set (CMAKE_INSTALL_PREFIX "${XACC_ROOT}" CACHE PATH "default install path" FORCE )
endif()

# If xacc is built with -DXACC_MULTI_THREADED
set(XACC_MULTI_THREADED @XACC_MULTI_THREADED@)
1 change: 1 addition & 0 deletions cmake/xacc_config.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
#define ROERROR_TEST_FILE_DIR "${CMAKE_SOURCE_DIR}/quantum/plugins/decorators/tests/files"
#define GATEIR_TEST_FILE_DIR "${CMAKE_SOURCE_DIR}/quantum/gate/ir/tests/files"
#define XACC_IS_APPLE ${XACC_IS_APPLE}
#cmakedefine _XACC_MUTEX

#endif
11 changes: 11 additions & 0 deletions quantum/plugins/qpp/accelerator/QppAccelerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ namespace quantum {

void QppAccelerator::execute(std::shared_ptr<AcceleratorBuffer> buffer, const std::shared_ptr<CompositeInstruction> compositeInstruction)
{
#ifdef _XACC_MUTEX
std::lock_guard<std::recursive_mutex> lock(getMutex());
#endif
const auto runCircuit = [&](bool shotsMode){
m_visitor->initialize(buffer, shotsMode);

Expand Down Expand Up @@ -325,6 +328,10 @@ namespace quantum {

void QppAccelerator::execute(std::shared_ptr<AcceleratorBuffer> buffer, const std::vector<std::shared_ptr<CompositeInstruction>> compositeInstructions)
{
#ifdef _XACC_MUTEX
std::lock_guard<std::recursive_mutex> lock(getMutex());
#endif

if (!m_vqeMode || compositeInstructions.size() <= 1)
{
for (auto& f : compositeInstructions)
Expand Down Expand Up @@ -370,6 +377,10 @@ namespace quantum {

void QppAccelerator::apply(std::shared_ptr<AcceleratorBuffer> buffer, std::shared_ptr<Instruction> inst)
{
#ifdef _XACC_MUTEX
std::lock_guard<std::recursive_mutex> lock(getMutex());
#endif

if (!m_visitor->isInitialized()) {
m_visitor->initialize(buffer);
m_currentBuffer = std::make_pair(buffer.get(), buffer->size());
Expand Down
10 changes: 9 additions & 1 deletion quantum/plugins/qpp/accelerator/QppAccelerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#include "xacc.hpp"
#include "QppVisitor.hpp"
#include "NoiseModel.hpp"

#ifdef _XACC_MUTEX
#include <mutex>
#endif
namespace xacc {
namespace quantum {

Expand Down Expand Up @@ -50,6 +52,12 @@ class QppAccelerator : public Accelerator {
std::vector<std::pair<int,int>> m_connectivity;
xacc::HeterogeneousMap m_executionInfo;
std::pair<AcceleratorBuffer*, size_t> m_currentBuffer;
#ifdef _XACC_MUTEX
static std::recursive_mutex& getMutex() {
static std::recursive_mutex m;;
return m;
}
#endif
};

class DefaultNoiseModelUtils : public NoiseModelUtils
Expand Down
3 changes: 3 additions & 0 deletions xacc/optimizer/nlopt-optimizers/nlopt_optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ const bool NLOptimizer::isGradientBased() const {
}

OptResult NLOptimizer::optimize(OptFunction &function) {
#ifdef _XACC_MUTEX
std::lock_guard<std::mutex> lock(getMutex());
#endif

auto dim = function.dimensions();
nlopt::algorithm algo = nlopt::algorithm::LN_COBYLA;
Expand Down
13 changes: 12 additions & 1 deletion xacc/optimizer/nlopt-optimizers/nlopt_optimizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,32 @@
#include <utility>

#include "Optimizer.hpp"
#include "xacc.hpp"

namespace xacc {

struct ExtraNLOptData {
std::function<double(const std::vector<double>&, std::vector<double>&)> f;
};

class NLOptimizer : public Optimizer {
class NLOptimizer : public Optimizer, public xacc::Cloneable<Optimizer> {
#ifdef _XACC_MUTEX
static std::mutex& getMutex() {
static std::mutex lock;
return lock;
}
#endif
public:
OptResult optimize(OptFunction &function) override;
const bool isGradientBased() const override;
virtual const std::string get_algorithm() const;

const std::string name() const override { return "nlopt"; }
const std::string description() const override { return ""; }

std::shared_ptr<Optimizer> clone() override {
return std::make_shared<NLOptimizer>();
}
};
} // namespace xacc
#endif
10 changes: 10 additions & 0 deletions xacc/xacc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,14 @@ void error(const std::string &msg, MessagePredicate predicate) {
}
}

#ifdef _XACC_MUTEX
std::mutex qalloc_lock;
#endif

qbit qalloc(const int n) {
#ifdef _XACC_MUTEX
std::lock_guard<std::mutex> lock(qalloc_lock);
#endif
qbit q(n);
std::stringstream ss;
ss << "qreg_" << q;
Expand All @@ -165,6 +172,9 @@ qbit qalloc(const int n) {
return q;
}
qbit qalloc() {
#ifdef _XACC_MUTEX
std::lock_guard<std::mutex> lock(qalloc_lock);
#endif
qbit q;
std::stringstream ss;
ss << "qreg_" << q;
Expand Down
5 changes: 5 additions & 0 deletions xacc/xacc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
#include <sys/stat.h>
#include <iosfwd>

#ifdef _XACC_MUTEX
#pragma message("_XACC_MUTEX is defined")
#include <mutex>
#endif

namespace xacc {

namespace constants {
Expand Down