ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
common_cuda.cu
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-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#include "errorhandling.hpp"
21
22#include "utils.cuh"
23#include "utils.hpp"
24
25#include <cuda.h>
26#include <cuda_runtime.h>
27
28#include <cstdio>
29#include <cstdlib>
30#include <sstream>
31#include <string>
32#include <utility>
33
34cudaStream_t stream[1];
35
36static std::basic_ostream<char> &operator<<(std::basic_ostream<char> &os,
37 const dim3 &dim) {
38 os << "<" << dim.x << "," << dim.y << "," << dim.z << ">";
39 return os;
40}
41
42static std::basic_ostream<char> &operator<<(std::basic_ostream<char> &os,
43 cudaError_t CU_err) {
44 os << "CUDA error: \"" << cudaGetErrorString(CU_err) << "\"";
45 return os;
46}
47
48void cuda_check_errors_exit(const dim3 &block, const dim3 &grid,
49 const char *function, const char *file,
50 unsigned int line) {
51 cudaError_t CU_err = cudaGetLastError();
52 if (CU_err != cudaSuccess) {
53 std::stringstream message;
54 message << CU_err << " while calling " << function
55 << " with block: " << block << ", grid: " << grid << " in " << file
56 << ":" << line;
57 throw cuda_fatal_error(message.str());
58 }
59}
60
61void cuda_safe_mem_exit(cudaError_t CU_err, const char *file,
62 unsigned int line) {
63 if (CU_err != cudaSuccess) {
64 std::stringstream message;
65 message << CU_err << " during memory operation in " << file << ":" << line;
66 if (CU_err == cudaErrorInvalidValue)
67 message << ". You may have tried to allocate zero memory";
68 throw cuda_fatal_error(message.str());
69 }
70 {
71 CU_err = cudaGetLastError();
72 if (CU_err != cudaSuccess) {
73 std::stringstream message;
74 message << CU_err << " in " << file << ":" << line << ". Error found "
75 << "during memory operation. Possibly however from a failed "
76 "operation before the memory operation";
77 throw cuda_fatal_error(message.str());
78 }
79 }
80}
81
83 : m_msg(std::move(msg)), m_terminate_handler(&errexit) {}
84
86 if (m_terminate_handler == nullptr or m_terminate_handler == errexit) {
87 fprintf(stderr, "%s\n", what());
88 }
89 ((m_terminate_handler == nullptr) ? &std::abort : m_terminate_handler)();
90}
Fatal CUDA exception.
char const * what() const noexcept
cuda_fatal_error(std::string msg)
void terminate() noexcept
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.
static std::basic_ostream< char > & operator<<(std::basic_ostream< char > &os, const dim3 &dim)
static double * block(double *p, std::size_t index, std::size_t size)
Definition elc.cpp:172
void errexit()
exit ungracefully, core dump if switched on.
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...