Skip to content

Commit

Permalink
Update the call paths of hybridLaunchKernel so that we can use the new
Browse files Browse the repository at this point in the history
argument synthesis instead of falling back on and failing with quake
synthesis.
  • Loading branch information
schweitzpgi committed Sep 18, 2024
1 parent 8ffa491 commit 92d772f
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 127 deletions.
15 changes: 10 additions & 5 deletions python/runtime/utils/PyRemoteSimulatorQPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ launchKernelImpl(cudaq::ExecutionContext *executionContextPtr,
std::unique_ptr<cudaq::RemoteRuntimeClient> &m_client,
const std::string &m_simName, const std::string &name,
void (*kernelFunc)(void *), void *args,
std::uint64_t voidStarSize, std::uint64_t resultOffset) {
std::uint64_t voidStarSize, std::uint64_t resultOffset,
const std::vector<void *> &rawArgs) {
auto *wrapper = reinterpret_cast<cudaq::ArgWrapper *>(args);
auto m_module = wrapper->mod;
auto callableNames = wrapper->callableNames;
Expand Down Expand Up @@ -131,12 +132,14 @@ class PyRemoteSimulatorQPU : public cudaq::BaseRemoteSimulatorQPU {

void launchKernel(const std::string &name, void (*kernelFunc)(void *),
void *args, std::uint64_t voidStarSize,
std::uint64_t resultOffset) override {
std::uint64_t resultOffset,
const std::vector<void *> &rawArgs) override {
cudaq::info("PyRemoteSimulatorQPU: Launch kernel named '{}' remote QPU {} "
"(simulator = {})",
name, qpu_id, m_simName);
::launchKernelImpl(getExecutionContextForMyThread(), m_client, m_simName,
name, kernelFunc, args, voidStarSize, resultOffset);
name, kernelFunc, args, voidStarSize, resultOffset,
rawArgs);
}

void launchKernel(const std::string &name,
Expand Down Expand Up @@ -177,12 +180,14 @@ class PyNvcfSimulatorQPU : public cudaq::BaseNvcfSimulatorQPU {

void launchKernel(const std::string &name, void (*kernelFunc)(void *),
void *args, std::uint64_t voidStarSize,
std::uint64_t resultOffset) override {
std::uint64_t resultOffset,
const std::vector<void *> &rawArgs) override {
cudaq::info("PyNvcfSimulatorQPU: Launch kernel named '{}' remote QPU {} "
"(simulator = {})",
name, qpu_id, m_simName);
::launchKernelImpl(getExecutionContextForMyThread(), m_client, m_simName,
name, kernelFunc, args, voidStarSize, resultOffset);
name, kernelFunc, args, voidStarSize, resultOffset,
rawArgs);
}

void launchKernel(const std::string &name,
Expand Down
5 changes: 3 additions & 2 deletions runtime/common/BaseRemoteRESTQPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,8 @@ class BaseRemoteRESTQPU : public cudaq::QPU {
/// synchronous invocation.
void launchKernel(const std::string &kernelName, void (*kernelFunc)(void *),
void *args, std::uint64_t voidStarSize,
std::uint64_t resultOffset) override {
std::uint64_t resultOffset,
const std::vector<void *> &rawArgs) override {
cudaq::info("launching remote rest kernel ({})", kernelName);

// TODO future iterations of this should support non-void return types.
Expand All @@ -590,7 +591,7 @@ class BaseRemoteRESTQPU : public cudaq::QPU {
"cudaq::observe(), or cudaq::draw().");

// Get the Quake code, lowered according to config file.
auto codes = lowerQuakeCode(kernelName, args);
auto codes = lowerQuakeCode(kernelName, rawArgs);
completeLaunchKernel(kernelName, std::move(codes));
}

Expand Down
4 changes: 3 additions & 1 deletion runtime/common/BaseRemoteSimulatorQPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ class BaseRemoteSimulatorQPU : public cudaq::QPU {

void launchKernel(const std::string &name, void (*kernelFunc)(void *),
void *args, std::uint64_t voidStarSize,
std::uint64_t resultOffset) override {
std::uint64_t resultOffset,
const std::vector<void *> &rawArgs) override {
// Remote simulation cannot deal with rawArgs. Drop them on the floor.
launchKernelImpl(name, kernelFunc, args, voidStarSize, resultOffset,
nullptr);
}
Expand Down
9 changes: 6 additions & 3 deletions runtime/common/KernelWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ std::invoke_result_t<QuantumKernel, Args...> invokeKernel(QuantumKernel &&fn,
auto serializedArgsBuffer = serializeArgs(std::forward<Args>(args)...);
cudaq::get_platform().launchKernel(fn.name(), nullptr,
(void *)serializedArgsBuffer.data(),
serializedArgsBuffer.size(), 0);
serializedArgsBuffer.size(), 0, {});
} else {
// In library mode, to use the remote simulator platform, we need to pack
// the argument and delegate to the platform's launchKernel rather than
Expand All @@ -538,13 +538,16 @@ std::invoke_result_t<QuantumKernel, Args...> invokeKernel(QuantumKernel &&fn,
// send on the function pointer to the platform to retrieve the symbol name
// since the typeid of a function only contains signature info.
if constexpr (std::is_class_v<std::decay_t<QuantumKernel>>)
// FIXME: this shouldn't use the serialization code any longer. It should
// build a vector of void* and pass that instead.
cudaq::get_platform().launchKernel(cudaq::getKernelName(fn), nullptr,
(void *)serializedArgsBuffer.data(),
serializedArgsBuffer.size(), 0);
serializedArgsBuffer.size(), 0, {});
else
cudaq::get_platform().launchKernel(
cudaq::getKernelName(fn), reinterpret_cast<void (*)(void *)>(&fn),
(void *)serializedArgsBuffer.data(), serializedArgsBuffer.size(), 0);
(void *)serializedArgsBuffer.data(), serializedArgsBuffer.size(), 0,
{});
}
#else
return fn(std::forward<Args>(args)...);
Expand Down
3 changes: 2 additions & 1 deletion runtime/cudaq/platform/default/DefaultQuantumPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class DefaultQPU : public cudaq::QPU {
}

void launchKernel(const std::string &name, void (*kernelFunc)(void *),
void *args, std::uint64_t, std::uint64_t) override {
void *args, std::uint64_t, std::uint64_t,
const std::vector<void *> &rawArgs) override {
ScopedTraceWithContext(cudaq::TIMING_LAUNCH, "QPU::launchKernel");
kernelFunc(args);
}
Expand Down
6 changes: 4 additions & 2 deletions runtime/cudaq/platform/orca/OrcaQPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ class OrcaRemoteRESTQPU : public cudaq::QPU {
/// modifications for the execution context.
void launchKernel(const std::string &kernelName, void (*kernelFunc)(void *),
void *args, std::uint64_t voidStarSize,
std::uint64_t resultOffset) override;
std::uint64_t resultOffset,
const std::vector<void *> &rawArgs) override;
void launchKernel(const std::string &kernelName,
const std::vector<void *> &rawArgs) override {
throw std::runtime_error("launch kernel on raw args not implemented");
Expand Down Expand Up @@ -227,7 +228,8 @@ void OrcaRemoteRESTQPU::setTargetBackend(const std::string &backend) {
void OrcaRemoteRESTQPU::launchKernel(const std::string &kernelName,
void (*kernelFunc)(void *), void *args,
std::uint64_t voidStarSize,
std::uint64_t resultOffset) {
std::uint64_t resultOffset,
const std::vector<void *> &rawArgs) {
cudaq::info("launching ORCA remote rest kernel ({})", kernelName);

// TODO future iterations of this should support non-void return types.
Expand Down
3 changes: 2 additions & 1 deletion runtime/cudaq/platform/qpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ class QPU : public registry::RegisteredType<QPU> {
/// The raw function pointer is also provided, as are the runtime arguments,
/// as a struct-packed void pointer and its corresponding size.
virtual void launchKernel(const std::string &name, void (*kernelFunc)(void *),
void *args, std::uint64_t, std::uint64_t) = 0;
void *args, std::uint64_t, std::uint64_t,
const std::vector<void *> &rawArgs) = 0;

/// Launch the kernel with given name and argument arrays.
// This is intended for remote QPUs whereby we need to JIT-compile the kernel
Expand Down
14 changes: 8 additions & 6 deletions runtime/cudaq/platform/quantum_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ quantum_platform::get_remote_capabilities(const std::size_t qpu_id) const {
void quantum_platform::launchKernel(std::string kernelName,
void (*kernelFunc)(void *), void *args,
std::uint64_t voidStarSize,
std::uint64_t resultOffset) {
std::uint64_t resultOffset,
const std::vector<void *> &rawArgs) {
std::size_t qpu_id = 0;

auto tid = std::hash<std::thread::id>{}(std::this_thread::get_id());
Expand All @@ -160,7 +161,8 @@ void quantum_platform::launchKernel(std::string kernelName,
qpu_id = iter->second;

auto &qpu = platformQPUs[qpu_id];
qpu->launchKernel(kernelName, kernelFunc, args, voidStarSize, resultOffset);
qpu->launchKernel(kernelName, kernelFunc, args, voidStarSize, resultOffset,
rawArgs);
}

void quantum_platform::launchKernel(std::string kernelName,
Expand Down Expand Up @@ -212,7 +214,7 @@ void cudaq::altLaunchKernel(const char *kernelName, void (*kernelFunc)(void *),
auto &platform = *cudaq::getQuantumPlatformInternal();
std::string kernName = kernelName;
platform.launchKernel(kernName, kernelFunc, kernelArgs, argsSize,
resultOffset);
resultOffset, {});
}

void cudaq::streamlinedLaunchKernel(const char *kernelName,
Expand All @@ -231,9 +233,9 @@ void cudaq::hybridLaunchKernel(const char *kernelName, void (*kernel)(void *),
ScopedTraceWithContext("hybridLaunchKernel", kernelName);
auto &platform = *cudaq::getQuantumPlatformInternal();
const std::string kernName = kernelName;
if (platform.is_remote(platform.get_current_qpu())
/*|| resultOffset == std::numeric_limits<std::int32_t>::max()*/)
if (platform.is_remote(platform.get_current_qpu()))
platform.launchKernel(kernName, rawArgs);
else
platform.launchKernel(kernName, kernel, args, argsSize, resultOffset);
platform.launchKernel(kernName, kernel, args, argsSize, resultOffset,
rawArgs);
}
3 changes: 2 additions & 1 deletion runtime/cudaq/platform/quantum_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ class quantum_platform {
// quantum kernels.
void launchKernel(std::string kernelName, void (*kernelFunc)(void *),
void *args, std::uint64_t voidStarSize,
std::uint64_t resultOffset);
std::uint64_t resultOffset,
const std::vector<void *> &rawArgs);
void launchKernel(std::string kernelName, const std::vector<void *> &);

// This method is the hook for executing SerializedCodeExecutionContext
Expand Down
2 changes: 1 addition & 1 deletion runtime/cudaq/qis/remote_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ RemoteSimulationState::overlap(const cudaq::SimulationState &other) {
std::make_pair(static_cast<const cudaq::SimulationState *>(this),
static_cast<const cudaq::SimulationState *>(&otherState));
platform.set_exec_ctx(&context);
platform.launchKernel(kernelName, nullptr, nullptr, 0, 0);
platform.launchKernel(kernelName, nullptr, nullptr, 0, 0, {});
platform.reset_exec_ctx();
assert(context.overlapResult.has_value());
return context.overlapResult.value();
Expand Down
1 change: 0 additions & 1 deletion targettests/SeparateCompilation/emulate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

// Catch-22. We need to use argument synthesis, but if I enable that in the
// quantum_platform.cpp code, there are a bunch of failures.
// XFAIL: *

//--- emulib.h

Expand Down
7 changes: 5 additions & 2 deletions targettests/execution/state_init_err_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@

// clang-format off
// Note: change |& to 2>&1| if running in bash
// RUN: nvq++ %cpp_std %s -o %t --target quantinuum --emulate && %t |& FileCheck %s
// RUN: nvq++ %cpp_std %s -o %t -target quantinuum -emulate && %t |& FileCheck %s
// Note: change |& to 2>&1| if running in bash
// RUN: nvq++ %cpp_std --enable-mlir --target remote-mqpu --remote-mqpu-url localhost:9999 %s -o %t && %t |& FileCheck %s
// RUN: nvq++ %cpp_std --enable-mlir -target remote-mqpu --remote-mqpu-url localhost:9999 %s -o %t && %t |& FileCheck %s
// clang-format on

// Requires init_state to be implemented via new argument synthesis
// XFAIL: *

#include <cudaq.h>
#include <iostream>

Expand Down
Loading

0 comments on commit 92d772f

Please sign in to comment.