text: /*
  All modification made by Cambricon Corporation: © 2018 Cambricon Corporation
  All rights reserved.
  All other contributions:
  Copyright (c) 2014--2018, the respective contributors
  All rights reserved.
  For the list of contributors go to https://github.com/BVLC/caffe/blob/master/CONTRIBUTORS.md
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
      * Redistributions of source code must retain the above copyright notice,
        this list of conditions and the following disclaimer.
      * Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
      * Neither the name of Intel Corporation nor the names of its contributors
        may be used to endorse or promote products derived from this software
        without specific prior written permission.
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
  
  #ifndef INCLUDE_CAFFE_UTIL_DEVICE_ALTERNATE_HPP_
  #define INCLUDE_CAFFE_UTIL_DEVICE_ALTERNATE_HPP_
  
  #include <vector>
  
  #ifdef USE_MLU
  #include "cnml.h" // NOLINT
  #endif
  
  #ifdef USE_CUDA
  #include <cublas_v2.h>
  #include <cuda.h>
  #include <cuda_runtime.h>
  #include <curand.h>
  #include <driver_types.h>  // cuda driver types
  #endif
  
  #ifdef USE_CUDNN  // cuDNN acceleration library.
  #include "caffe/util/cudnn.hpp"
  #endif
  
  
  // Stub out GPU calls as unavailable.
  
  #define NO_GPU LOG(FATAL) << "Cannot use GPU in CPU-only Caffe: check mode."
  
  #define STUB_GPU(classname) \
  template <typename Dtype> \
  void classname<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom, \
      const vector<Blob<Dtype>*>& top) { NO_GPU; } \
  template <typename Dtype> \
  void classname<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top, \
      const vector<bool>& propagate_down, \
      const vector<Blob<Dtype>*>& bottom) { NO_GPU; } \
  
  #define STUB_GPU_FORWARD(classname, funcname) \
  template <typename Dtype> \
  void classname<Dtype>::funcname##_##gpu(const vector<Blob<Dtype>*>& bottom, \
      const vector<Blob<Dtype>*>& top) { NO_GPU; } \
  
  #define STUB_GPU_BACKWARD(classname, funcname) \
  template <typename Dtype> \
  void classname<Dtype>::funcname##_##gpu(const vector<Blob<Dtype>*>& top, \
      const vector<bool>& propagate_down, \
      const vector<Blob<Dtype>*>& bottom) { NO_GPU; } \
  
  #ifdef USE_CUDA
  
  //
  // CUDA macros
  //
  
  // CUDA: various checks for different function calls.
  #define CUDA_CHECK(condition) \
    /* Code block avoids redefinition of cudaError_t error */ \
    do { \
      cudaError_t error = condition; \
      CHECK_EQ(error, cudaSuccess) << " " << cudaGetErrorString(error); \
    } while (0)
  
  #define CUBLAS_CHECK(condition) \
    do { \
      cublasStatus_t status = condition; \
      CHECK_EQ(status, CUBLAS_STATUS_SUCCESS) << " " \
        << caffe::cublasGetErrorString(status); \
    } while (0)
  
  #define CURAND_CHECK(condition) \
    do { \
      curandStatus_t status = condition; \
      CHECK_EQ(status, CURAND_STATUS_SUCCESS) << " " \
        << caffe::curandGetErrorString(status); \
    } while (0)
  
  // CUDA: grid stride looping
  #define CUDA_KERNEL_LOOP(i, n) \
    for (int i = blockIdx.x * blockDim.x + threadIdx.x; \
         i < (n); \
         i += blockDim.x * gridDim.x)
  
  // CUDA: check for error after kernel execution and exit loudly if there is one.
  #define CUDA_POST_KERNEL_CHECK CUDA_CHECK(cudaPeekAtLastError())
  #endif
  
  #ifdef USE_MLU
  
  #define MLU_CHECK(condition) \
    do { \
      cnmlStatus_t status = condition; \
      CHECK_EQ(status, CNML_STATUS_SUCCESS) << " " << caffe::mluGetErrorString(status); \
    } while (0)
  
  #endif
  
  namespace caffe {
  
  #ifdef USE_MLU
  const char* mluGetErrorString(cnmlStatus_t status);
  const char* cnrtGetErrorString(cnrtRet_t status);
  #endif
  
  #ifdef USE_CUDA
  // CUDA: library error reporting.
  const char* cublasGetErrorString(cublasStatus_t error);
  const char* curandGetErrorString(curandStatus_t error);
  
  // CUDA: use 512 threads per block
  const int CAFFE_CUDA_NUM_THREADS = 512;
  
  // CUDA: number of blocks for threads.
  inline int CAFFE_GET_BLOCKS(const int N) {
    return (N + CAFFE_CUDA_NUM_THREADS - 1) / CAFFE_CUDA_NUM_THREADS;
  }
  #endif
  
  
  }  // namespace caffe
  #endif  // INCLUDE_CAFFE_UTIL_DEVICE_ALTERNATE_HPP_
  