Skip to content

Commit

Permalink
Auto-format code changes (#570)
Browse files Browse the repository at this point in the history
Auto-format code using Clang-Format

Co-authored-by: GitHub Actions <[email protected]>
  • Loading branch information
github-actions[bot] and actions-user authored Jun 13, 2024
1 parent 1fa0bac commit e076450
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
6 changes: 1 addition & 5 deletions include/micm/solver/cuda_rosenbrock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,7 @@ namespace micm
const requires(CudaMatrix<DenseMatrixPolicy>&& VectorizableDense<DenseMatrixPolicy>)
{
return micm::cuda::NormalizedErrorDriver(
y_old.AsDeviceParam(),
y_new.AsDeviceParam(),
errors.AsDeviceParam(),
this->parameters_,
this->devstruct_);
y_old.AsDeviceParam(), y_new.AsDeviceParam(), errors.AsDeviceParam(), this->parameters_, this->devstruct_);
}

/// @brief Computes the scaled norm of the vector errors on the CPU
Expand Down
11 changes: 9 additions & 2 deletions include/micm/util/cuda_dense_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <micm/util/cuda_util.cuh>
#include <micm/util/error.hpp>
#include <micm/util/vector_matrix.hpp>

#include <cublas_v2.h>
#include <cuda_runtime.h>

Expand Down Expand Up @@ -203,8 +204,14 @@ namespace micm
static_assert(std::is_same_v<T, double>);
CHECK_CUBLAS_ERROR(
cublasDaxpy(
micm::cuda::GetCublasHandle(), x.param_.number_of_elements_, &alpha, x.param_.d_data_, incx, this->param_.d_data_, incy),
"CUBLAS Daxpy operation failed...");
micm::cuda::GetCublasHandle(),
x.param_.number_of_elements_,
&alpha,
x.param_.d_data_,
incx,
this->param_.d_data_,
incy),
"CUBLAS Daxpy operation failed...");
}

// Copy the device data from the other Cuda dense matrix into this one
Expand Down
1 change: 1 addition & 0 deletions include/micm/util/cuda_util.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <cublas_v2.h>
#include <cuda_runtime.h>

#include <string>

#define CHECK_CUDA_ERROR(err, msg) micm::cuda::CheckCudaError(err, __FILE__, __LINE__, msg)
Expand Down
9 changes: 6 additions & 3 deletions src/solver/rosenbrock.cu
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,9 @@ namespace micm
" but got: " + std::to_string(errors_param.number_of_elements_);
INTERNAL_ERROR(msg.c_str());
}
CHECK_CUDA_ERROR(cudaMemcpy(
devstruct.errors_input_, errors_param.d_data_, sizeof(double) * number_of_elements, cudaMemcpyDeviceToDevice),
CHECK_CUDA_ERROR(
cudaMemcpy(
devstruct.errors_input_, errors_param.d_data_, sizeof(double) * number_of_elements, cudaMemcpyDeviceToDevice),
"cudaMemcpy");

if (number_of_elements > 1000000)
Expand All @@ -273,7 +274,9 @@ namespace micm
ScaledErrorKernel<<<number_of_blocks, BLOCK_SIZE>>>(y_old_param, y_new_param, ros_param, devstruct);
// call cublas function to perform the norm:
// https://docs.nvidia.com/cuda/cublas/index.html?highlight=dnrm2#cublas-t-nrm2
CHECK_CUBLAS_ERROR(cublasDnrm2(micm::cuda::GetCublasHandle(), number_of_elements, devstruct.errors_input_, 1, &normalized_error), "cublasDnrm2");
CHECK_CUBLAS_ERROR(
cublasDnrm2(micm::cuda::GetCublasHandle(), number_of_elements, devstruct.errors_input_, 1, &normalized_error),
"cublasDnrm2");
normalized_error = normalized_error * std::sqrt(1.0 / number_of_elements);
}
else
Expand Down
21 changes: 13 additions & 8 deletions src/util/cuda_util.cu
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#include <micm/util/cuda_util.cuh>
#include <micm/util/internal_error.hpp>

#include <cublas_v2.h>
#include <cuda_runtime.h>

#include <map>
#include <memory>
#include <mutex>
#include <map>
#include <cublas_v2.h>

namespace micm
{
Expand All @@ -32,9 +33,12 @@ namespace micm
}

// Define a functor for the cublasHandle_t unique pointer deleter
struct CublasHandleDeleter {
void operator()(cublasHandle_t* handle) const {
if (handle != nullptr) {
struct CublasHandleDeleter
{
void operator()(cublasHandle_t* handle) const
{
if (handle != nullptr)
{
CHECK_CUBLAS_ERROR(cublasDestroy(*handle), "CUBLAS finalization failed");
delete handle;
}
Expand All @@ -45,19 +49,20 @@ namespace micm
using CublasHandlePtr = std::unique_ptr<cublasHandle_t, CublasHandleDeleter>;

// Create a cublas handle and return a unique pointer to it
CublasHandlePtr CreateCublasHandle() {
CublasHandlePtr CreateCublasHandle()
{
cublasHandle_t* handle = new cublasHandle_t;
CHECK_CUBLAS_ERROR(cublasCreate(handle), "CUBLAS initialization failed...");
return CublasHandlePtr(handle, CublasHandleDeleter());
}

cublasHandle_t& GetCublasHandle()
{
static std::map<int, CublasHandlePtr> cublas_handles_map;
static std::mutex mutex;
int device_id;
CHECK_CUDA_ERROR(cudaGetDevice(&device_id), "Failed to get device ID...");
std::lock_guard<std::mutex> lock(mutex); // lock the mutex and generate a new cublas handle below
std::lock_guard<std::mutex> lock(mutex); // lock the mutex and generate a new cublas handle below
if (auto search = cublas_handles_map.find(device_id); search == cublas_handles_map.end())
{
cublas_handles_map[device_id] = std::move(CreateCublasHandle());
Expand Down

0 comments on commit e076450

Please sign in to comment.