device_wrapper.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <stdint.h>
  8. #include <pvfmm_common.hpp>
  9. #include <vector.hpp>
  10. #ifndef _PVFMM_DEVICE_WRAPPER_HPP_
  11. #define _PVFMM_DEVICE_WRAPPER_HPP_
  12. #ifdef __INTEL_OFFLOAD
  13. #pragma offload_attribute(push,target(mic))
  14. #endif
  15. namespace pvfmm{
  16. namespace DeviceWrapper{
  17. uintptr_t alloc_device(char* dev_handle, size_t len);
  18. void free_device(char* dev_handle, uintptr_t dev_ptr);
  19. template <int SYNC=__DEVICE_SYNC__>
  20. int host2device(char* host_ptr, char* dev_handle, uintptr_t dev_ptr, size_t len);
  21. template <int SYNC=__DEVICE_SYNC__>
  22. int device2host(char* dev_handle, uintptr_t dev_ptr, char* host_ptr, size_t len);
  23. void wait(int lock_idx);
  24. }//end namespace
  25. /*
  26. Usage of 'MIC_Lock' in Asynchronous Offloads
  27. --------------------------------------------
  28. Note: Any MIC offload section should look like this:
  29. int wait_lock_idx=MIC_Lock::curr_lock();
  30. int lock_idx=MIC_Lock::get_lock();
  31. #pragma offload target(mic:0) signal(&MIC_Lock::lock_vec[lock_idx])
  32. {
  33. MIC_Lock::wait_lock(wait_lock_idx);
  34. // Offload code here...
  35. MIC_Lock::release_lock(lock_idx);
  36. }
  37. #ifdef __DEVICE_SYNC__
  38. MIC_Lock::wait_lock(lock_idx);
  39. #endif
  40. This ensures the execution of offloaded code does not overlap with other
  41. asynchronous offloaded code and that data transfers from host to mic have
  42. completed before the data is accessed. You will however, need to be careful
  43. not to overwrite data on mic which may be transferring to the host, or data on
  44. the host which may be transferring to the mic.
  45. On the host, to wait for the last asynchronous offload section or data
  46. transfer, use:
  47. int wait_lock_idx=MIC_Lock::curr_lock();
  48. MIC_Lock::wait_lock(wait_lock_idx);
  49. */
  50. #define NUM_LOCKS 1000000
  51. class MIC_Lock{
  52. public:
  53. static void init();
  54. static int get_lock();
  55. static void release_lock(int idx);
  56. static void wait_lock(int idx);
  57. static int curr_lock();
  58. static Vector<char> lock_vec;
  59. static Vector<char>::Device lock_vec_;
  60. private:
  61. MIC_Lock(){}; // private constructor for static class.
  62. static int lock_idx;
  63. };
  64. }//end namespace
  65. #ifdef __INTEL_OFFLOAD
  66. #pragma offload_attribute(pop)
  67. #endif
  68. #include <device_wrapper.txx>
  69. #endif //_PVFMM_DEVICE_WRAPPER_HPP_