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 14 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 "utils/trace_base.h"
  24. #include "include/curand.h"
  25. namespace mindspore {
  26. namespace device {
  27. namespace gpu {
  28. #define CHECK_OP_RET_WITH_EXCEPT(expression, message) \
  29. { \
  30. bool success = (expression); \
  31. if (!success) { \
  32. MS_LOG(EXCEPTION) << "Op Error: " << message << " | Error Number: " << success; \
  33. } \
  34. }
  35. #define CHECK_OP_RET_WITH_ERROR(expression, message) \
  36. { \
  37. bool success = (expression); \
  38. if (!success) { \
  39. MS_LOG(ERROR) << "Op Error: " << message << " | Error Number: " << success; \
  40. } \
  41. }
  42. #define CHECK_RET_WITH_RETURN_ERROR(expression, message) \
  43. { \
  44. bool success = (expression); \
  45. if (!success) { \
  46. MS_LOG(ERROR) << message; \
  47. return false; \
  48. } \
  49. }
  50. #define CHECK_CUDA_RET_WITH_ERROR(node, expression, message) \
  51. { \
  52. cudaError_t status = (expression); \
  53. if (status != cudaSuccess) { \
  54. MS_LOG(ERROR) << "CUDA Error: " << message << " | Error Number: " << status << " " << cudaGetErrorString(status) \
  55. << trace::DumpSourceLines(node.lock()); \
  56. } \
  57. }
  58. #define CHECK_CUDA_RET_WITH_ERROR_NOTRACE(expression, message) \
  59. { \
  60. cudaError_t status = (expression); \
  61. if (status != cudaSuccess) { \
  62. MS_LOG(ERROR) << "CUDA Error: " << message << " | Error Number: " << status << " " \
  63. << cudaGetErrorString(status); \
  64. } \
  65. }
  66. #define CHECK_CUDA_RET_WITH_RETURN_ERROR_NOTRACE(expression, message) \
  67. { \
  68. cudaError_t status = (expression); \
  69. if (status != cudaSuccess) { \
  70. MS_LOG(ERROR) << "CUDA Error: " << message << " | Error Number: " << status << " " \
  71. << cudaGetErrorString(status); \
  72. return false; \
  73. } \
  74. }
  75. #define CHECK_CUDA_RET_WITH_EXCEPT(node, expression, message) \
  76. { \
  77. cudaError_t status = (expression); \
  78. if (status != cudaSuccess) { \
  79. MS_LOG(EXCEPTION) << "CUDA Error: " << message << " | Error Number: " << status << " " \
  80. << cudaGetErrorString(status) << trace::DumpSourceLines(node.lock()); \
  81. } \
  82. }
  83. #define CHECK_CUDA_RET_WITH_EXCEPT_NOTRACE(expression, message) \
  84. { \
  85. cudaError_t status = (expression); \
  86. if (status != cudaSuccess) { \
  87. MS_LOG(EXCEPTION) << "CUDA Error: " << message << " | Error Number: " << status << " " \
  88. << cudaGetErrorString(status); \
  89. } \
  90. }
  91. #define CHECK_CUDNN_RET_WITH_EXCEPT(node, expression, message) \
  92. { \
  93. cudnnStatus_t status = (expression); \
  94. if (status != CUDNN_STATUS_SUCCESS) { \
  95. MS_LOG(EXCEPTION) << "cuDNN Error: " << message << " | Error Number: " << status << " " \
  96. << cudnnGetErrorString(status) << trace::DumpSourceLines(node.lock()); \
  97. } \
  98. }
  99. #define CHECK_CUDNN_RET_WITH_EXCEPT_NOTRACE(expression, message) \
  100. { \
  101. cudnnStatus_t status = (expression); \
  102. if (status != CUDNN_STATUS_SUCCESS) { \
  103. MS_LOG(EXCEPTION) << "cuDNN Error: " << message << " | Error Number: " << status << " " \
  104. << cudnnGetErrorString(status); \
  105. } \
  106. }
  107. #define CHECK_CUDNN_RET_WITH_ERROR_NOTRACE(expression, message) \
  108. { \
  109. cudnnStatus_t status = (expression); \
  110. if (status != CUDNN_STATUS_SUCCESS) { \
  111. MS_LOG(ERROR) << "cuDNN Error: " << message << " | Error Number: " << status << " " \
  112. << cudnnGetErrorString(status); \
  113. } \
  114. }
  115. #define CHECK_CUDNN_RET_WITH_ERROR(node, expression, message) \
  116. { \
  117. cudnnStatus_t status = (expression); \
  118. if (status != CUDNN_STATUS_SUCCESS) { \
  119. MS_LOG(ERROR) << "cuDNN Error: " << message << " | Error Number: " << status << " " \
  120. << cudnnGetErrorString(status) << trace::DumpSourceLines(node.lock()); \
  121. } \
  122. }
  123. #define CHECK_CUBLAS_RET_WITH_EXCEPT_NOTRACE(expression, message) \
  124. { \
  125. cublasStatus_t status = (expression); \
  126. if (status != CUBLAS_STATUS_SUCCESS) { \
  127. MS_LOG(EXCEPTION) << "cuBLAS Error: " << message << " | Error Number: " << status; \
  128. } \
  129. }
  130. #define CHECK_CUBLAS_RET_WITH_EXCEPT(node, expression, message) \
  131. { \
  132. cublasStatus_t status = (expression); \
  133. if (status != CUBLAS_STATUS_SUCCESS) { \
  134. MS_LOG(EXCEPTION) << "cuBLAS Error: " << message << " | Error Number: " << status \
  135. << trace::DumpSourceLines(node.lock()); \
  136. } \
  137. }
  138. #define CHECK_CUBLAS_RET_WITH_ERROR(expression, message) \
  139. { \
  140. cublasStatus_t status = (expression); \
  141. if (status != CUBLAS_STATUS_SUCCESS) { \
  142. MS_LOG(ERROR) << "cuBLAS Error: " << message << " | Error Number: " << status; \
  143. } \
  144. }
  145. #define CHECK_CUSOLVER_RET_WITH_EXCEPT_NOTRACE(expression, message) \
  146. { \
  147. cusolverStatus_t status = (expression); \
  148. if (status != CUSOLVER_STATUS_SUCCESS) { \
  149. MS_LOG(EXCEPTION) << "cusolver Error: " << message << " | Error Number: " << status; \
  150. } \
  151. }
  152. #define CHECK_CUSOLVER_RET_WITH_EXCEPT(node, expression, message) \
  153. { \
  154. cusolverStatus_t status = (expression); \
  155. if (status != CUSOLVER_STATUS_SUCCESS) { \
  156. MS_LOG(EXCEPTION) << "cusolver Error: " << message << " | Error Number: " << status \
  157. << trace::DumpSourceLines(node.lock()); \
  158. ; \
  159. } \
  160. }
  161. #define CHECK_CUSOLVER_RET_WITH_ERROR(expression, message) \
  162. { \
  163. cusolverStatus_t status = (expression); \
  164. if (status != CUSOLVER_STATUS_SUCCESS) { \
  165. MS_LOG(ERROR) << "cusolver Error: " << message << " | Error Number: " << status; \
  166. } \
  167. }
  168. #define CHECK_NCCL_RET_WITH_EXCEPT(node, expression, message) \
  169. { \
  170. int result = (expression); \
  171. if (result != ncclSuccess) { \
  172. MS_LOG(EXCEPTION) << "NCCL Error: " << message << " | Error Number: " << result \
  173. << trace::DumpSourceLines(node.lock()); \
  174. } \
  175. }
  176. #define VARIABLE_NOT_USED(var) \
  177. { (void)(var); }
  178. inline bool CheckNullInput(const std::vector<size_t> &input_shape) {
  179. // If input_shape.size() == 0, it means a scalar input; If input_shape.size() != 0 and input_shape contains 0,
  180. // it means a null input. Just return a null output.
  181. if (input_shape.size() != 0) {
  182. if (std::any_of(input_shape.begin(), input_shape.end(), [](size_t i) { return i == 0; })) {
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. #define CHECK_NULL_INPUT(input_shape) mindspore::device::gpu::CheckNullInput(input_shape)
  189. #define CHECK_CURAND_RET_WITH_EXCEPT(expression, message) \
  190. { \
  191. curandStatus_t status = (expression); \
  192. if (status != CURAND_STATUS_SUCCESS) { \
  193. MS_LOG(EXCEPTION) << "CUDA curand Error: " << message << " | curandStatus: " << status; \
  194. } \
  195. }
  196. } // namespace gpu
  197. } // namespace device
  198. } // namespace mindspore
  199. #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_GPU_COMMON_H_