util_debug.cuh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /******************************************************************************
  2. * Copyright (c) 2011, Duane Merrill. All rights reserved.
  3. * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the NVIDIA CORPORATION nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. ******************************************************************************/
  28. /**
  29. * \file
  30. * Error and event logging routines.
  31. *
  32. * The following macros definitions are supported:
  33. * - \p CUB_LOG. Simple event messages are printed to \p stdout.
  34. */
  35. #pragma once
  36. #include <stdio.h>
  37. #include "util_namespace.cuh"
  38. #include "util_arch.cuh"
  39. /// Optional outer namespace(s)
  40. CUB_NS_PREFIX
  41. /// CUB namespace
  42. namespace cub {
  43. /**
  44. * \addtogroup UtilMgmt
  45. * @{
  46. */
  47. /// CUB error reporting macro (prints error messages to stderr)
  48. #if (defined(DEBUG) || defined(_DEBUG))
  49. #define CUB_STDERR
  50. #endif
  51. /**
  52. * \brief %If \p CUB_STDERR is defined and \p error is not \p cudaSuccess, the corresponding error message is printed to \p stderr (or \p stdout in device code) along with the supplied source context.
  53. *
  54. * \return The CUDA error.
  55. */
  56. __host__ __device__ __forceinline__ cudaError_t Debug(
  57. cudaError_t error,
  58. const char* filename,
  59. int line)
  60. {
  61. #ifdef CUB_STDERR
  62. if (error)
  63. {
  64. #if (CUB_PTX_VERSION == 0)
  65. fprintf(stderr, "CUDA error %d [%s, %d]: %s\n", error, filename, line, cudaGetErrorString(error));
  66. fflush(stderr);
  67. #elif (CUB_PTX_VERSION >= 200)
  68. printf("CUDA error %d [block %d, thread %d, %s, %d]\n", error, blockIdx.x, threadIdx.x, filename, line);
  69. #endif
  70. }
  71. #endif
  72. return error;
  73. }
  74. /**
  75. * \brief Debug macro
  76. */
  77. #define CubDebug(e) cub::Debug((e), __FILE__, __LINE__)
  78. /**
  79. * \brief Debug macro with exit
  80. */
  81. #define CubDebugExit(e) if (cub::Debug((e), __FILE__, __LINE__)) { exit(1); }
  82. /**
  83. * \brief Log macro for printf statements.
  84. */
  85. #if (CUB_PTX_VERSION == 0)
  86. #define CubLog(format, ...) printf(format,__VA_ARGS__);
  87. #elif (CUB_PTX_VERSION >= 200)
  88. #define CubLog(format, ...) printf("[block %d, thread %d]: " format, blockIdx.x, threadIdx.x, __VA_ARGS__);
  89. #endif
  90. /** @} */ // end group UtilMgmt
  91. } // CUB namespace
  92. CUB_NS_POSTFIX // Optional outer namespace(s)