device_wrapper.hpp 2.4 KB

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