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