ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
utils.cuh
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#if !defined(__CUDACC__)
23#error Do not include CUDA headers in normal C++-code!!!
24#endif
25
26#include "utils.hpp"
27
28#include <cuda.h>
29#include <cuda_runtime.h>
30
31#include <string>
32
34public:
35 cuda_runtime_error_cuda(cudaError_t error)
36 : cuda_runtime_error(error_message(error)) {}
37
38private:
39 std::string error_message(cudaError_t error) {
40 const char *cuda_error = cudaGetErrorString(error);
41 return std::string("CUDA error: ") + cuda_error;
42 }
43};
44
45/** Convert CUDA error codes into runtime errors. */
46#define CUDA_CHECK(statement) \
47 { \
48 cudaError_t const error_code = (statement); \
49 if (error_code != cudaSuccess) { \
50 static_cast<void>(cudaGetLastError()); /* clear non-sticky errors */ \
51 throw cuda_runtime_error_cuda(error_code); \
52 } \
53 }
54
55/** CUDA streams for parallel computing on CPU and GPU */
56extern cudaStream_t stream[1];
57
58/** In case of error during CUDA memory allocation and memory copy, print
59 * the error message and exit.
60 * @param CU_err cuda error code
61 * @param file .cu file were the error took place
62 * @param line line of the file were the error took place
63 */
64void cuda_safe_mem_exit(cudaError_t CU_err, const char *file,
65 unsigned int line);
66
67/** In case of error during a CUDA operation, print the error message and exit.
68 */
69void cuda_check_errors_exit(const dim3 &block, const dim3 &grid,
70 const char *function, const char *file,
71 unsigned int line);
72
73#define cuda_safe_mem(a) cuda_safe_mem_exit((a), __FILE__, __LINE__)
74
75#define KERNELCALL_shared(_function, _grid, _block, _stream, ...) \
76 _function<<<_grid, _block, _stream, stream[0]>>>(__VA_ARGS__); \
77 cuda_check_errors_exit(_grid, _block, #_function, __FILE__, __LINE__);
78
79#define KERNELCALL(_function, _grid, _block, ...) \
80 KERNELCALL_shared(_function, _grid, _block, 0, ##__VA_ARGS__)
cuda_runtime_error_cuda(cudaError_t error)
Definition utils.cuh:35
Wrapper for CUDA runtime exceptions.
static double * block(double *p, std::size_t index, std::size_t size)
Definition elc.cpp:172
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
void cuda_check_errors_exit(const dim3 &block, const dim3 &grid, const char *function, const char *file, unsigned int line)
In case of error during a CUDA operation, print the error message and exit.
void cuda_safe_mem_exit(cudaError_t CU_err, const char *file, unsigned int line)
In case of error during CUDA memory allocation and memory copy, print the error message and exit.