device_wrapper.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * \file device_wrapper.hpp
  3. * \author Dhairya Malhotra, dhairya.malhotra@gmail.com
  4. * \date 6-5-2013
  5. * \brief This file contains definition of DeviceWrapper.
  6. */
  7. #include <cstdlib>
  8. #include <stdint.h>
  9. // Cuda Headers
  10. #if defined(PVFMM_HAVE_CUDA)
  11. #include <cuda_runtime_api.h>
  12. #include <cublas_v2.h>
  13. #endif
  14. #include <pvfmm_common.hpp>
  15. #include <vector.hpp>
  16. #ifndef _PVFMM_DEVICE_WRAPPER_HPP_
  17. #define _PVFMM_DEVICE_WRAPPER_HPP_
  18. #ifdef __INTEL_OFFLOAD
  19. #pragma offload_attribute(push,target(mic))
  20. #endif
  21. namespace pvfmm{
  22. namespace DeviceWrapper{
  23. uintptr_t alloc_device(char* dev_handle, size_t len);
  24. void free_device(char* dev_handle, uintptr_t dev_ptr);
  25. template <int SYNC=__DEVICE_SYNC__>
  26. int host2device(char* host_ptr, char* dev_handle, uintptr_t dev_ptr, size_t len);
  27. template <int SYNC=__DEVICE_SYNC__>
  28. int device2host(char* dev_handle, uintptr_t dev_ptr, char* host_ptr, size_t len);
  29. void wait(int lock_idx);
  30. }//end namespace
  31. /*
  32. Usage of 'MIC_Lock' in Asynchronous Offloads
  33. --------------------------------------------
  34. Note: Any MIC offload section should look like this:
  35. int wait_lock_idx=MIC_Lock::curr_lock();
  36. int lock_idx=MIC_Lock::get_lock();
  37. #pragma offload target(mic:0) signal(&MIC_Lock::lock_vec[lock_idx])
  38. {
  39. MIC_Lock::wait_lock(wait_lock_idx);
  40. // Offload code here...
  41. MIC_Lock::release_lock(lock_idx);
  42. }
  43. #ifdef __DEVICE_SYNC__
  44. MIC_Lock::wait_lock(lock_idx);
  45. #endif
  46. This ensures the execution of offloaded code does not overlap with other
  47. asynchronous offloaded code and that data transfers from host to mic have
  48. completed before the data is accessed. You will however, need to be careful
  49. not to overwrite data on mic which may be transferring to the host, or data on
  50. the host which may be transferring to the mic.
  51. On the host, to wait for the last asynchronous offload section or data
  52. transfer, use:
  53. int wait_lock_idx=MIC_Lock::curr_lock();
  54. MIC_Lock::wait_lock(wait_lock_idx);
  55. */
  56. class MIC_Lock{
  57. public:
  58. static void init();
  59. static int get_lock();
  60. static void release_lock(int idx);
  61. static void wait_lock(int idx);
  62. static int curr_lock();
  63. static Vector<char> lock_vec;
  64. static Vector<char>::Device lock_vec_;
  65. private:
  66. MIC_Lock(){}; // private constructor for static class.
  67. static int lock_idx;
  68. };
  69. #if defined(PVFMM_HAVE_CUDA)
  70. class CUDA_Lock {
  71. public:
  72. static void init(size_t num_stream=1);
  73. static void finalize();
  74. static cudaStream_t *acquire_stream(int idx=0);
  75. static cublasHandle_t *acquire_handle();
  76. static void wait(int idx=0);
  77. private:
  78. CUDA_Lock();
  79. static std::vector<cudaStream_t> stream;
  80. static cublasHandle_t handle;
  81. };
  82. #endif
  83. }//end namespace
  84. #ifdef __INTEL_OFFLOAD
  85. #pragma offload_attribute(pop)
  86. #endif
  87. #include <device_wrapper.txx>
  88. #endif //_PVFMM_DEVICE_WRAPPER_HPP_