You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

gpu_common.h 8.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_GPU_COMMON_H_
  17. #define MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_GPU_COMMON_H_
  18. #include <iostream>
  19. #include <vector>
  20. #include <algorithm>
  21. #include <map>
  22. #include "utils/log_adapter.h"
  23. #include "include/curand.h"
  24. namespace mindspore {
  25. namespace device {
  26. namespace gpu {
  27. #define CHECK_OP_RET_WITH_EXCEPT(expression, message) \
  28. { \
  29. bool success = (expression); \
  30. if (!success) { \
  31. MS_LOG(EXCEPTION) << "Op Error: " << message << " | Error Number: " << success; \
  32. } \
  33. }
  34. #define CHECK_OP_RET_WITH_ERROR(expression, message) \
  35. { \
  36. bool success = (expression); \
  37. if (!success) { \
  38. MS_LOG(ERROR) << "Op Error: " << message << " | Error Number: " << success; \
  39. } \
  40. }
  41. #define CHECK_CUDA_RET_WITH_ERROR(expression, message) \
  42. { \
  43. cudaError_t status = (expression); \
  44. if (status != cudaSuccess) { \
  45. MS_LOG(ERROR) << "CUDA Error: " << message << " | Error Number: " << status << " " \
  46. << cudaGetErrorString(status); \
  47. } \
  48. }
  49. #define CHECK_CUDA_RET_WITH_EXCEPT(expression, message) \
  50. { \
  51. cudaError_t status = (expression); \
  52. if (status != cudaSuccess) { \
  53. MS_LOG(EXCEPTION) << "CUDA Error: " << message << " | Error Number: " << status << " " \
  54. << cudaGetErrorString(status); \
  55. } \
  56. }
  57. #define CHECK_CUDNN_RET_WITH_EXCEPT(expression, message) \
  58. { \
  59. cudnnStatus_t status = (expression); \
  60. if (status != CUDNN_STATUS_SUCCESS) { \
  61. MS_LOG(EXCEPTION) << "cuDNN Error: " << message << " | Error Number: " << status << " " \
  62. << cudnnGetErrorString(status); \
  63. } \
  64. }
  65. #define CHECK_CUDNN_RET_WITH_ERROR(expression, message) \
  66. { \
  67. cudnnStatus_t status = (expression); \
  68. if (status != CUDNN_STATUS_SUCCESS) { \
  69. MS_LOG(ERROR) << "cuDNN Error: " << message << " | Error Number: " << status << " " \
  70. << cudnnGetErrorString(status); \
  71. } \
  72. }
  73. #define CHECK_CUBLAS_RET_WITH_EXCEPT(expression, message) \
  74. { \
  75. cublasStatus_t status = (expression); \
  76. if (status != CUBLAS_STATUS_SUCCESS) { \
  77. MS_LOG(EXCEPTION) << "cuBLAS Error: " << message << " | Error Number: " << status; \
  78. } \
  79. }
  80. #define CHECK_CUBLAS_RET_WITH_ERROR(expression, message) \
  81. { \
  82. cublasStatus_t status = (expression); \
  83. if (status != CUBLAS_STATUS_SUCCESS) { \
  84. MS_LOG(ERROR) << "cuBLAS Error: " << message << " | Error Number: " << status; \
  85. } \
  86. }
  87. #define CHECK_CUSOLVER_RET_WITH_EXCEPT(expression, message) \
  88. { \
  89. cusolverStatus_t status = (expression); \
  90. if (status != CUSOLVER_STATUS_SUCCESS) { \
  91. MS_LOG(EXCEPTION) << "cusolver Error: " << message << " | Error Number: " << status; \
  92. } \
  93. }
  94. #define CHECK_CUSOLVER_RET_WITH_ERROR(expression, message) \
  95. { \
  96. cusolverStatus_t status = (expression); \
  97. if (status != CUSOLVER_STATUS_SUCCESS) { \
  98. MS_LOG(ERROR) << "cusolver Error: " << message << " | Error Number: " << status; \
  99. } \
  100. }
  101. #define CHECK_NCCL_RET_WITH_EXCEPT(expression, message) \
  102. { \
  103. int result = (expression); \
  104. if (result != ncclSuccess) { \
  105. MS_LOG(EXCEPTION) << "NCCL Error: " << message << " | Error Number: " << result; \
  106. } \
  107. }
  108. #define VARIABLE_NOT_USED(var) \
  109. { (void)(var); }
  110. inline bool CheckNullInput(std::vector<size_t> input_shape) {
  111. // If input_shape.size() == 0, it means a scalar input; If input_shape.size() != 0 and input_shape contains 0,
  112. // it means a null input. Just return a null output.
  113. if (input_shape.size() != 0) {
  114. if (std::any_of(input_shape.begin(), input_shape.end(), [](size_t i) { return i == 0; })) {
  115. return true;
  116. }
  117. }
  118. return false;
  119. }
  120. #define CHECK_NULL_INPUT(input_shape) mindspore::device::gpu::CheckNullInput(input_shape)
  121. #define CHECK_CURAND_RET_WITH_EXCEPT(expression, message) \
  122. { \
  123. curandStatus_t status = (expression); \
  124. if (status != CURAND_STATUS_SUCCESS) { \
  125. MS_LOG(EXCEPTION) << "CUAD curand Error: " << message << " | curandStatus: " << status; \
  126. } \
  127. }
  128. } // namespace gpu
  129. } // namespace device
  130. } // namespace mindspore
  131. #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_GPU_COMMON_H_