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
30#include <string>
31
33public:
34 cuda_runtime_error_cuda(cudaError_t error)
35 : cuda_runtime_error(error_message(error)) {}
36
37private:
38 std::string error_message(cudaError_t error) {
39 const char *cuda_error = cudaGetErrorString(error);
40 return std::string("CUDA error: ") + cuda_error;
41 }
42};
43
44/** Convert CUDA error codes into runtime errors. */
45#define CUDA_CHECK(statement) \
46 { \
47 cudaError_t const error_code = (statement); \
48 if (error_code != cudaSuccess) { \
49 throw cuda_runtime_error_cuda(error_code); \
50 } \
51 }
52
53/** CUDA streams for parallel computing on CPU and GPU */
54extern cudaStream_t stream[1];
55
56/** In case of error during CUDA memory allocation and memory copy, print
57 * the error message and exit.
58 * @param CU_err cuda error code
59 * @param file .cu file were the error took place
60 * @param line line of the file were the error took place
61 */
62void cuda_safe_mem_exit(cudaError_t CU_err, const char *file,
63 unsigned int line);
64
65/** In case of error during a CUDA operation, print the error message and exit.
66 */
67void cuda_check_errors_exit(const dim3 &block, const dim3 &grid,
68 const char *function, const char *file,
69 unsigned int line);
70
71#define cuda_safe_mem(a) cuda_safe_mem_exit((a), __FILE__, __LINE__)
72
73#define KERNELCALL_shared(_function, _grid, _block, _stream, ...) \
74 _function<<<_grid, _block, _stream, stream[0]>>>(__VA_ARGS__); \
75 cuda_check_errors_exit(_grid, _block, #_function, __FILE__, __LINE__);
76
77#define KERNELCALL(_function, _grid, _block, ...) \
78 KERNELCALL_shared(_function, _grid, _block, 0, ##__VA_ARGS__)
cuda_runtime_error_cuda(cudaError_t error)
Definition utils.cuh:34
static double * block(double *p, std::size_t index, std::size_t size)
Definition elc.cpp:174
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.