YAKL
YAKL_error.h
Go to the documentation of this file.
1 
7 #pragma once
8 // Included by YAKL.h
9 
11 namespace yakl {
12 
17  YAKL_INLINE void yakl_throw(const char * msg) {
18  // If we're on the host, then let's throw a real exception
20  fence();
21  std::cerr << "YAKL FATAL ERROR:\n";
22  std::cerr << msg << std::endl;
23  throw std::runtime_error(msg);
24  )
25  // Otherwise, we need to be more careful with printf and intentionally segfaulting to stop the program
27  #ifdef YAKL_ARCH_SYCL
28  // SYCL cannot printf like the other backends quite yet
29  const CL_CONSTANT char format[] = "KERNEL CHECK FAILED:\n %s\n";
30  sycl::ext::oneapi::experimental::printf(format,msg);
31  #elif defined(YAKL_ARCH_CUDA) || defined(YAKL_ARCH_HIP)
32  printf("%s\n",msg);
33  #endif
34  // Intentionally cause a segfault to kill the run
35  int *segfault = nullptr;
36  *segfault = 10;
37  )
38  }
39 
40 
45  inline void check_last_error() {
46  #ifdef YAKL_DEBUG
47  fence();
48  #ifdef YAKL_ARCH_CUDA
49  auto ierr = cudaGetLastError();
50  if (ierr != cudaSuccess) { yakl_throw( cudaGetErrorString( ierr ) ); }
51  #elif defined(YAKL_ARCH_HIP)
52  auto ierr = hipGetLastError();
53  if (ierr != hipSuccess) { yakl_throw( hipGetErrorString( ierr ) ); }
54  #elif defined(YAKL_ARCH_SYCL)
55  #endif
56  #endif
57  }
58 
59 
64  inline bool yakl_mainproc() {
65  // Only actually check if the user says MPI is available. Otherwise, always return true
66  #ifdef HAVE_MPI
67  int is_initialized;
68  MPI_Initialized(&is_initialized);
69  if (!is_initialized) {
70  return true;
71  } else {
72  int myrank;
73  MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
74  return myrank == 0;
75  }
76  #else
77  return true;
78  #endif
79  }
80 
81 }
83 
YAKL_EXECUTE_ON_DEVICE_ONLY
#define YAKL_EXECUTE_ON_DEVICE_ONLY(...)
[NOT COMMONLY USED] Macro function used to determine if the current code is compiling for the device.
Definition: YAKL_defines.h:158
__YAKL_NAMESPACE_WRAPPER_END__
#define __YAKL_NAMESPACE_WRAPPER_END__
Definition: YAKL.h:20
__YAKL_NAMESPACE_WRAPPER_BEGIN__
#define __YAKL_NAMESPACE_WRAPPER_BEGIN__
Definition: YAKL.h:19
YAKL_INLINE
#define YAKL_INLINE
Used to decorate functions called from kernels (parallel_for and parallel_outer) or from CPU function...
Definition: YAKL_defines.h:140
yakl::fence
void fence()
Block the host code until all device code has completed.
Definition: YAKL_fence.h:16
YAKL_EXECUTE_ON_HOST_ONLY
#define YAKL_EXECUTE_ON_HOST_ONLY(...)
[NOT COMMONLY USED] Macro function used to determine if the current code is compiling for the host.
Definition: YAKL_defines.h:153
yakl::yakl_throw
YAKL_INLINE void yakl_throw(const char *msg)
Throw an error message. Works from the host or device.
Definition: YAKL_error.h:17
yakl::yakl_mainproc
bool yakl_mainproc()
If true, this is the main MPI process (task number == 0)
Definition: YAKL_error.h:64
yakl
yakl::check_last_error
void check_last_error()
Checks to see if an error has occurred on the device.
Definition: YAKL_error.h:45