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

Refactor cuda_utils : move custatevec to lightning_gpu_utils #681

Merged
merged 27 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7a7e60d
Init commit
multiphaseCFD Apr 12, 2024
b76db37
Auto update version
github-actions[bot] Apr 12, 2024
d3b8bb0
Add MidMeasureMP Kokkos (#658)
tomlqc Apr 12, 2024
e64ea7a
Merge branch 'master' into tidyup_cuda_utils
multiphaseCFD Apr 12, 2024
82eff86
Trigger CI
multiphaseCFD Apr 12, 2024
4a406f6
make format
multiphaseCFD Apr 12, 2024
a4e8416
Auto update version
github-actions[bot] Apr 12, 2024
8c9a958
Trigger CIs
multiphaseCFD Apr 15, 2024
ee21a65
Merge branch 'master' into tidyup_cuda_utils
multiphaseCFD Apr 17, 2024
b888ba0
Auto update version
github-actions[bot] Apr 17, 2024
e40fe2a
Trigger CI
multiphaseCFD Apr 17, 2024
e6f0f9c
Merge branch 'master' into tidyup_cuda_utils
multiphaseCFD Apr 18, 2024
abeb7bb
Auto update version
github-actions[bot] Apr 18, 2024
0d6b24f
add more docstring
multiphaseCFD Apr 18, 2024
caae334
add changelog
multiphaseCFD Apr 18, 2024
b663855
Update pennylane_lightning/core/src/simulators/lightning_gpu/utils/cu…
multiphaseCFD Apr 18, 2024
ddbe2b2
Update pennylane_lightning/core/src/simulators/lightning_gpu/utils/cu…
multiphaseCFD Apr 18, 2024
f66ced7
Update pennylane_lightning/core/src/simulators/lightning_gpu/utils/cu…
multiphaseCFD Apr 18, 2024
6e6011d
Update pennylane_lightning/core/src/simulators/lightning_gpu/utils/cu…
multiphaseCFD Apr 18, 2024
c7c110e
Update pennylane_lightning/core/src/simulators/lightning_gpu/utils/cu…
multiphaseCFD Apr 18, 2024
749434b
Update pennylane_lightning/core/src/simulators/lightning_gpu/utils/cu…
multiphaseCFD Apr 18, 2024
3f871cc
Update pennylane_lightning/core/src/utils/cuda_utils/cuError.hpp
multiphaseCFD Apr 18, 2024
1152fde
Update pennylane_lightning/core/src/simulators/lightning_gpu/utils/cu…
multiphaseCFD Apr 18, 2024
8f11fba
Trigger CI
multiphaseCFD Apr 18, 2024
cba005e
make format
multiphaseCFD Apr 18, 2024
d188286
quick fix
multiphaseCFD Apr 18, 2024
4fdd50e
fix
multiphaseCFD Apr 18, 2024
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
2 changes: 1 addition & 1 deletion pennylane_lightning/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
Version number (major.minor.patch[-label])
"""

__version__ = "0.36.0-dev29"
__version__ = "0.36.0-dev30"
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "MPIManager.hpp"
#include "MPI_helpers.hpp"
#include "cuError.hpp"
#include "cuStateVecError.hpp"
#include <cuda.h>
#include <cuda_runtime.h>
#include <custatevec.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "DevTag.hpp"
#include "Error.hpp"
#include "StateVectorBase.hpp"
#include "cuStateVecError.hpp"
#include "cuda_helpers.hpp"

/// @cond DEV
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "StateVectorCudaBase.hpp"
#include "cuGateCache.hpp"
#include "cuGates_host.hpp"
#include "cuStateVecError.hpp"
#include "cuStateVec_helpers.hpp"
#include "cuda_helpers.hpp"

#include "CPUMemoryModel.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include "CPUMemoryModel.hpp"

#include "cuError.hpp"
#include "cuStateVecError.hpp"
#include "cuStateVec_helpers.hpp"

#include "LinearAlg.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "Observables.hpp"
#include "ObservablesGPU.hpp"
#include "StateVectorCudaManaged.hpp"
#include "cuStateVecError.hpp"
#include "cuStateVec_helpers.hpp"
#include "cuda_helpers.hpp"

/// @cond DEV
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "ObservablesGPUMPI.hpp"
#include "StateVectorCudaMPI.hpp"
#include "StateVectorCudaManaged.hpp"
#include "cuStateVecError.hpp"
#include "cuda_helpers.hpp"

/// @cond DEV
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2022-2023 Xanadu Quantum Technologies Inc.
multiphaseCFD marked this conversation as resolved.
Show resolved Hide resolved

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Adapted from JET: https://github.com/XanaduAI/jet.git
// and from Lightning: https://github.com/PennylaneAI/pennylane-lightning.git
/**
* @file cuStateVecError.hpp
*/
multiphaseCFD marked this conversation as resolved.
Show resolved Hide resolved

#pragma once
#include <string>
multiphaseCFD marked this conversation as resolved.
Show resolved Hide resolved

#include <custatevec.h>

#include "Error.hpp"
#include "Util.hpp"
// LCOV_EXCL_START
namespace {
using namespace Pennylane::Util;
}
multiphaseCFD marked this conversation as resolved.
Show resolved Hide resolved

#ifndef CUDA_UNSAFE

/**
* @brief Macro that throws Exception from cuStateVec failure error codes.
*
* @param err cuStateVec function error-code.
*/
#define PL_CUSTATEVEC_IS_SUCCESS(err) \
PL_ABORT_IF_NOT( \
err == CUSTATEVEC_STATUS_SUCCESS, \
Pennylane::LightningGPU::Util::GetCuStateVecErrorString(err).c_str())

#else
#define PL_CUSTATEVEC_IS_SUCCESS(err) \
{ static_cast<void>(err); }
#endif

namespace Pennylane::LightningGPU::Util {
static const std::string
GetCuStateVecErrorString(const custatevecStatus_t &err) {
std::string result;
switch (err) {
case CUSTATEVEC_STATUS_SUCCESS:
result = "No errors";
break;
case CUSTATEVEC_STATUS_NOT_INITIALIZED:
result = "custatevec not initialized";
break;
case CUSTATEVEC_STATUS_ALLOC_FAILED:
result = "custatevec memory allocation failed";
break;
case CUSTATEVEC_STATUS_INVALID_VALUE:
result = "custatevec invalid value";
break;
case CUSTATEVEC_STATUS_ARCH_MISMATCH:
result = "custatevec CUDA device architecture mismatch";
break;
case CUSTATEVEC_STATUS_EXECUTION_FAILED:
result = "custatevec execution failed";
break;
case CUSTATEVEC_STATUS_INTERNAL_ERROR:
result = "custatevec internal error";
break;
case CUSTATEVEC_STATUS_NOT_SUPPORTED:
result = "custatevec unsupported operation/device";
break;
case CUSTATEVEC_STATUS_INSUFFICIENT_WORKSPACE:
result =
"custatevec insufficient memory for gate-application workspace";
break;
case CUSTATEVEC_STATUS_SAMPLER_NOT_PREPROCESSED:
result = "custatevec sampler not preprocessed";
break;
case CUSTATEVEC_STATUS_NO_DEVICE_ALLOCATOR:
result = "custatevec no device allocator";
break;
case CUSTATEVEC_STATUS_DEVICE_ALLOCATOR_ERROR:
result = "custatevec device allocator error";
break;
case CUSTATEVEC_STATUS_COMMUNICATOR_ERROR:
result = "custatevec communicator failure";
break;
case CUSTATEVEC_STATUS_LOADING_LIBRARY_FAILED:
result = "custatevec dynamic library load failure";
break;
default:
result =
"custatevec status not found. Error code=" + std::to_string(err);
}
return result;
}
multiphaseCFD marked this conversation as resolved.
Show resolved Hide resolved
} // namespace Pennylane::LightningGPU::Util

Check notice on line 104 in pennylane_lightning/core/src/simulators/lightning_gpu/utils/cuStateVecError.hpp

View check run for this annotation

codefactor.io / CodeFactor

pennylane_lightning/core/src/simulators/lightning_gpu/utils/cuStateVecError.hpp#L52-L104

Complex Method
// LCOV_EXCL_STOP
multiphaseCFD marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2022-2023 Xanadu Quantum Technologies Inc.
multiphaseCFD marked this conversation as resolved.
Show resolved Hide resolved

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Adapted from JET: https://github.com/XanaduAI/jet.git

/**
* @file cudaStateVec_helpers.hpp
*/

#pragma once
#include <custatevec.h>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include "cuStateVecError.hpp"
#include "cuStateVec_helpers.hpp"

namespace Pennylane::LightningGPU::Util {

inline static auto pauliStringToEnum(const std::string &pauli_word)
-> std::vector<custatevecPauli_t> {
// Map string rep to Pauli enums
const std::unordered_map<std::string, custatevecPauli_t> pauli_map{
multiphaseCFD marked this conversation as resolved.
Show resolved Hide resolved
multiphaseCFD marked this conversation as resolved.
Show resolved Hide resolved
std::pair<const std::string, custatevecPauli_t>{std::string("X"),
CUSTATEVEC_PAULI_X},
std::pair<const std::string, custatevecPauli_t>{std::string("Y"),
CUSTATEVEC_PAULI_Y},
std::pair<const std::string, custatevecPauli_t>{std::string("Z"),
CUSTATEVEC_PAULI_Z},
std::pair<const std::string, custatevecPauli_t>{std::string("I"),
CUSTATEVEC_PAULI_I}};
multiphaseCFD marked this conversation as resolved.
Show resolved Hide resolved

static constexpr std::size_t num_char = 1;
multiphaseCFD marked this conversation as resolved.
Show resolved Hide resolved

std::vector<custatevecPauli_t> output;
output.reserve(pauli_word.size());

for (const auto ch : pauli_word) {
multiphaseCFD marked this conversation as resolved.
Show resolved Hide resolved
auto out = pauli_map.at(std::string(num_char, ch));
output.push_back(out);
}
return output;
}

inline static auto pauliStringToOpNames(const std::string &pauli_word)
-> std::vector<std::string> {
// Map string rep to Pauli
const std::unordered_map<std::string, std::string> pauli_map{
multiphaseCFD marked this conversation as resolved.
Show resolved Hide resolved
std::pair<const std::string, std::string>{std::string("X"),
std::string("PauliX")},
std::pair<const std::string, std::string>{std::string("Y"),
std::string("PauliY")},
std::pair<const std::string, std::string>{std::string("Z"),
std::string("PauliZ")},
std::pair<const std::string, std::string>{std::string("I"),
std::string("Identity")}};

static constexpr std::size_t num_char = 1;

std::vector<std::string> output;
output.reserve(pauli_word.size());

for (const auto ch : pauli_word) {
auto out = pauli_map.at(std::string(num_char, ch));
output.push_back(out);
}
return output;
}

/**
* Utility function object to tell std::shared_ptr how to
* release/destroy cuStateVec objects.
*/
struct handleDeleter {
void operator()(custatevecHandle_t handle) const {
PL_CUSTATEVEC_IS_SUCCESS(custatevecDestroy(handle));
}
};

using SharedCusvHandle =
std::shared_ptr<std::remove_pointer<custatevecHandle_t>::type>;

/**
* @brief Creates a SharedCusvHandle (a shared pointer to a custatevecHandle)
*/
inline SharedCusvHandle make_shared_cusv_handle() {
custatevecHandle_t h;
PL_CUSTATEVEC_IS_SUCCESS(custatevecCreate(&h));
return {h, handleDeleter()};
}
} // namespace Pennylane::LightningGPU::Util
4 changes: 2 additions & 2 deletions pennylane_lightning/core/src/utils/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

/**
* @file
* Record the path to scipy.libs at compile time.
* Config file for the path to scipy.libs at compile time.
*/

#ifndef CONFIG_H
#define CONFIG_H
#define SCIPY_LIBS_PATH ""
#endif
#endif
15 changes: 0 additions & 15 deletions pennylane_lightning/core/src/utils/cuda_utils/LinearAlg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <cublas_v2.h>
#include <cuda.h>
#include <cusparse_v2.h>
#include <custatevec.h>

#include "DataBuffer.hpp"
#include "cuError.hpp"
Expand Down Expand Up @@ -247,17 +246,12 @@ struct HandleDeleter {
void operator()(cublasHandle_t handle) const {
PL_CUBLAS_IS_SUCCESS(cublasDestroy(handle));
}
void operator()(custatevecHandle_t handle) const {
PL_CUSTATEVEC_IS_SUCCESS(custatevecDestroy(handle));
}
void operator()(cusparseHandle_t handle) const {
PL_CUSPARSE_IS_SUCCESS(cusparseDestroy(handle));
}
};

using SharedCublasCaller = std::shared_ptr<CublasCaller>;
using SharedCusvHandle =
std::shared_ptr<std::remove_pointer<custatevecHandle_t>::type>;
using SharedCusparseHandle =
std::shared_ptr<std::remove_pointer<cusparseHandle_t>::type>;

Expand All @@ -268,15 +262,6 @@ inline SharedCublasCaller make_shared_cublas_caller() {
return std::make_shared<CublasCaller>();
}

/**
* @brief Creates a SharedCusvHandle (a shared pointer to a custatevecHandle)
*/
inline SharedCusvHandle make_shared_cusv_handle() {
custatevecHandle_t h;
PL_CUSTATEVEC_IS_SUCCESS(custatevecCreate(&h));
return {h, HandleDeleter()};
}

/**
* @brief Creates a SharedCusparseHandle (a shared pointer to a cusparseHandle)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <cublas_v2.h>
#include <cuda.h>
#include <cusparse_v2.h>
#include <custatevec.h>

#include "DataBuffer.hpp"
#include "cuError.hpp"
Expand Down
7 changes: 6 additions & 1 deletion pennylane_lightning/core/src/utils/cuda_utils/MPIManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <cuComplex.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <custatevec.h>
#include <mpi.h>
#include <stdexcept>
#include <string>
Expand All @@ -30,6 +29,10 @@
#include <unordered_map>
#include <vector>

#ifdef _ENABLE_PLGPU
#include <custatevec.h>
#endif

#include "DataBuffer.hpp"
#include "Error.hpp"

Expand Down Expand Up @@ -155,7 +158,9 @@ class MPIManager final {
{cppTypeToString<cuFloatComplex>(), MPI_C_FLOAT_COMPLEX},
{cppTypeToString<double2>(), MPI_C_DOUBLE_COMPLEX},
{cppTypeToString<cuDoubleComplex>(), MPI_C_DOUBLE_COMPLEX},
#ifdef _ENABLE_PLGPU
{cppTypeToString<custatevecIndex_t>(), MPI_INT64_T},
#endif
// cuda related types
{cppTypeToString<cudaIpcMemHandle_t>(), MPI_UINT8_T},
{cppTypeToString<cudaIpcEventHandle_t>(), MPI_UINT8_T}};
Expand Down
Loading
Loading