Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
multiphaseCFD committed Apr 12, 2024
1 parent 2028317 commit 7a7e60d
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 137 deletions.
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.

// 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
*/

#pragma once
#include <string>

#include <custatevec.h>

#include "Error.hpp"
#include "Util.hpp"
// LCOV_EXCL_START
namespace {
using namespace Pennylane::Util;
}

#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;
}
} // 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2022-2023 Xanadu Quantum Technologies Inc.

// 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{
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}};

static constexpr std::size_t num_char = 1;

std::vector<custatevecPauli_t> 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;
}

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{
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
7 changes: 4 additions & 3 deletions pennylane_lightning/core/src/utils/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

/**
* @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
#define SCIPY_LIBS_PATH \
"/home/shuli/env/lib/python3.10/site-packages/scipy.libs"
#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

0 comments on commit 7a7e60d

Please sign in to comment.