device_wrapper.txx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /**
  2. * \file device_wrapper.txx
  3. * \author Dhairya Malhotra, dhairya.malhotra@gmail.com
  4. * \date 6-5-2013
  5. * \brief This file contains implementation of DeviceWrapper.
  6. *
  7. * Modified:
  8. * editor Chenhan D. Yu
  9. * date Juan-28-2014
  10. * Add Cuda support. Error handle is available if needed.
  11. */
  12. #include <vector.hpp>
  13. #include <device_wrapper.hpp>
  14. // CUDA Stream
  15. #if defined(PVFMM_HAVE_CUDA)
  16. #endif
  17. namespace pvfmm{
  18. namespace DeviceWrapper{
  19. // CUDA functions
  20. inline uintptr_t alloc_device_cuda(size_t len) {
  21. char *dev_ptr=NULL;
  22. #if defined(PVFMM_HAVE_CUDA)
  23. //std::cout << "cudaMalloc();" << '\n';
  24. cudaError_t error;
  25. error = cudaMalloc((void**)&dev_ptr, len);
  26. /*
  27. std::cout << cudaGetErrorString(error) << ", "
  28. << (uintptr_t) dev_ptr << " - "
  29. << (uintptr_t) dev_ptr + len
  30. << "(" << len << ")" << '\n';
  31. */
  32. #endif
  33. return (uintptr_t)dev_ptr;
  34. }
  35. inline void free_device_cuda(char *dev_ptr) {
  36. #if defined(PVFMM_HAVE_CUDA)
  37. //std::cout << "cudaFree();" << '\n';
  38. cudaFree(dev_ptr);
  39. #endif
  40. }
  41. inline int host2device_cuda(char *host_ptr, char *dev_ptr, size_t len) {
  42. #if defined(PVFMM_HAVE_CUDA)
  43. //std::cout << "cudaHostRegister(), cudaMemcpyAsync(HostToDevice);" << '\n';
  44. cudaError_t error;
  45. cudaStream_t *stream = CUDA_Lock::acquire_stream(0);
  46. //error = cudaHostRegister(host_ptr, len, cudaHostRegisterPortable);
  47. //if (error != cudaSuccess) std::cout << "cudaHostRegister(): " << cudaGetErrorString(error) << '\n';
  48. //error = cudaMemcpyAsync(dev_ptr, host_ptr, len, cudaMemcpyHostToDevice, *stream);
  49. error = cudaMemcpy(dev_ptr, host_ptr, len, cudaMemcpyHostToDevice);
  50. if (error != cudaSuccess) {
  51. std::cout << "cudaMemcpyAsync(HostToDevice): " << cudaGetErrorString(error) << ", "
  52. << (uintptr_t) dev_ptr << ", len: "
  53. << len << '\n';
  54. return -1;
  55. }
  56. else return (int)len;
  57. #endif
  58. return -1;
  59. }
  60. inline int device2host_cuda(char *dev_ptr, char *host_ptr, size_t len) {
  61. #if defined(PVFMM_HAVE_CUDA)
  62. //std::cout << "cudaHostRegister(), cudaMemcpyAsync(DeviceToHost);" << '\n';
  63. cudaError_t error;
  64. cudaStream_t *stream = CUDA_Lock::acquire_stream(0);
  65. //error = cudaHostRegister(host_ptr, len, cudaHostRegisterPortable);
  66. //if (error != cudaSuccess) std::cout << "cudaHostRegister(): " << cudaGetErrorString(error) << '\n';
  67. //error = cudaMemcpyAsync(host_ptr, dev_ptr, len, cudaMemcpyDeviceToHost, *stream);
  68. error = cudaMemcpy(host_ptr, dev_ptr, len, cudaMemcpyDeviceToHost);
  69. if (error != cudaSuccess) {
  70. std::cout << "cudaMemcpyAsnc(DeviceToHost): " << cudaGetErrorString(error) << ", "
  71. << (uintptr_t) dev_ptr << ", len: "
  72. << len << '\n';
  73. return -1;
  74. }
  75. else return (int)len;
  76. #endif
  77. return -1;
  78. }
  79. // MIC functions
  80. inline uintptr_t alloc_device_mic(char* dev_handle, size_t len){
  81. assert(dev_handle!=NULL);
  82. uintptr_t dev_ptr=(uintptr_t)NULL;
  83. #ifdef __INTEL_OFFLOAD
  84. #pragma offload target(mic:0) nocopy( dev_handle: length(len) ALLOC) out(dev_ptr)
  85. #endif
  86. {dev_ptr=(uintptr_t)dev_handle;}
  87. return dev_ptr;
  88. }
  89. inline void free_device_mic(char* dev_handle, uintptr_t dev_ptr){
  90. #ifdef __INTEL_OFFLOAD
  91. #pragma offload target(mic:0) in( dev_handle: length(0) FREE)
  92. {
  93. assert(dev_ptr==(uintptr_t)dev_handle);
  94. }
  95. #endif
  96. }
  97. inline int host2device_mic(char* host_ptr, char* dev_handle, uintptr_t dev_ptr, size_t len){
  98. #ifdef __INTEL_OFFLOAD
  99. int wait_lock_idx=MIC_Lock::curr_lock();
  100. int lock_idx=MIC_Lock::get_lock();
  101. if(dev_handle==host_ptr){
  102. #pragma offload target(mic:0) in( dev_handle : length(len) REUSE ) signal(&MIC_Lock::lock_vec[lock_idx])
  103. {
  104. assert(dev_ptr==(uintptr_t)dev_handle);
  105. MIC_Lock::wait_lock(wait_lock_idx);
  106. MIC_Lock::release_lock(lock_idx);
  107. }
  108. }else{
  109. #pragma offload target(mic:0) in(host_ptr [0:len] : into ( dev_handle[0:len]) REUSE ) signal(&MIC_Lock::lock_vec[lock_idx])
  110. {
  111. assert(dev_ptr==(uintptr_t)dev_handle);
  112. MIC_Lock::wait_lock(wait_lock_idx);
  113. MIC_Lock::release_lock(lock_idx);
  114. }
  115. }
  116. #ifndef __MIC_ASYNCH__ // Wait
  117. #pragma offload target(mic:0)
  118. {MIC_Lock::wait_lock(lock_idx);}
  119. #endif
  120. return lock_idx;
  121. #endif
  122. return -1;
  123. }
  124. inline int device2host_mic(char* dev_handle, uintptr_t dev_ptr, char* host_ptr, size_t len){
  125. #ifdef __INTEL_OFFLOAD
  126. int wait_lock_idx=MIC_Lock::curr_lock();
  127. int lock_idx=MIC_Lock::get_lock();
  128. if(dev_handle==host_ptr){
  129. #pragma offload target(mic:0) out( dev_handle : length(len) REUSE ) signal(&MIC_Lock::lock_vec[lock_idx])
  130. {
  131. assert(dev_ptr==(uintptr_t)dev_handle);
  132. MIC_Lock::wait_lock(wait_lock_idx);
  133. MIC_Lock::release_lock(lock_idx);
  134. }
  135. }else{
  136. #pragma offload target(mic:0) out( dev_handle[0:len] : into (host_ptr [0:len]) REUSE ) signal(&MIC_Lock::lock_vec[lock_idx])
  137. {
  138. assert(dev_ptr==(uintptr_t)dev_handle);
  139. MIC_Lock::wait_lock(wait_lock_idx);
  140. MIC_Lock::release_lock(lock_idx);
  141. }
  142. }
  143. #ifndef __MIC_ASYNCH__ // Wait
  144. MIC_Lock::wait_lock(lock_idx);
  145. #endif
  146. return lock_idx;
  147. #endif
  148. return -1;
  149. }
  150. inline void wait_mic(int lock_idx){
  151. #ifdef __INTEL_OFFLOAD
  152. MIC_Lock::wait_lock(lock_idx);
  153. #endif
  154. }
  155. // Wrapper functions
  156. inline uintptr_t alloc_device(char* dev_handle, size_t len){
  157. #ifdef __INTEL_OFFLOAD
  158. return alloc_device_mic(dev_handle,len);
  159. #elif defined(PVFMM_HAVE_CUDA)
  160. return alloc_device_cuda(len);
  161. #else
  162. uintptr_t dev_ptr=(uintptr_t)NULL;
  163. {dev_ptr=(uintptr_t)dev_handle;}
  164. return dev_ptr;
  165. #endif
  166. }
  167. inline void free_device(char* dev_handle, uintptr_t dev_ptr){
  168. #ifdef __INTEL_OFFLOAD
  169. free_device_mic(dev_handle,dev_ptr);
  170. #elif defined(PVFMM_HAVE_CUDA)
  171. free_device_cuda((char*)dev_ptr);
  172. #else
  173. ;
  174. #endif
  175. }
  176. inline int host2device(char* host_ptr, char* dev_handle, uintptr_t dev_ptr, size_t len){
  177. int lock_idx=-1;
  178. #ifdef __INTEL_OFFLOAD
  179. lock_idx=host2device_mic(host_ptr,dev_handle,dev_ptr,len);
  180. #elif defined(PVFMM_HAVE_CUDA)
  181. //lock_idx is len if success.
  182. lock_idx=host2device_cuda(host_ptr,(char*)dev_ptr,len);
  183. #else
  184. ;
  185. #endif
  186. return lock_idx;
  187. }
  188. inline int device2host(char* dev_handle, uintptr_t dev_ptr, char* host_ptr, size_t len){
  189. int lock_idx=-1;
  190. #ifdef __INTEL_OFFLOAD
  191. lock_idx=device2host_mic(dev_handle,dev_ptr, host_ptr, len);
  192. #elif defined(PVFMM_HAVE_CUDA)
  193. //lock_idx is len if success.
  194. lock_idx=device2host_cuda((char*)dev_ptr, host_ptr, len);
  195. #else
  196. ;
  197. #endif
  198. return lock_idx;
  199. }
  200. inline void wait(int lock_idx){
  201. #ifdef __INTEL_OFFLOAD
  202. wait_mic(lock_idx);
  203. #else
  204. ;
  205. #endif
  206. }
  207. }
  208. // Implementation of MIC_Lock
  209. #ifdef __MIC__
  210. #define have_mic 1
  211. #else
  212. #define have_mic 0
  213. #endif
  214. inline void MIC_Lock::init(){
  215. if(have_mic) abort();// Cannot be called from MIC.
  216. lock_idx=0;
  217. lock_vec.Resize(NUM_LOCKS);
  218. lock_vec.SetZero();
  219. lock_vec_=lock_vec.AllocDevice(false);
  220. {for(size_t i=0;i<NUM_LOCKS;i++) lock_vec [i]=1;}
  221. #ifdef __INTEL_OFFLOAD
  222. #pragma offload target(mic:0)
  223. {for(size_t i=0;i<NUM_LOCKS;i++) lock_vec_[i]=1;}
  224. #endif
  225. }
  226. inline int MIC_Lock::get_lock(){
  227. if(have_mic) abort();// Cannot be called from MIC.
  228. int idx;
  229. #pragma omp critical
  230. {
  231. if(lock_idx==NUM_LOCKS-1){
  232. int wait_lock_idx=-1;
  233. wait_lock_idx=MIC_Lock::curr_lock();
  234. MIC_Lock::wait_lock(wait_lock_idx);
  235. #ifdef __INTEL_OFFLOAD
  236. #pragma offload target(mic:0)
  237. {MIC_Lock::wait_lock(wait_lock_idx);}
  238. #endif
  239. MIC_Lock::init();
  240. }
  241. idx=lock_idx;
  242. lock_idx++;
  243. assert(lock_idx<NUM_LOCKS);
  244. }
  245. return idx;
  246. }
  247. inline int MIC_Lock::curr_lock(){
  248. if(have_mic) abort();// Cannot be called from MIC.
  249. return lock_idx-1;
  250. }
  251. inline void MIC_Lock::release_lock(int idx){ // Only call from inside an offload section
  252. #ifdef __MIC__
  253. if(idx>=0) lock_vec_[idx]=0;
  254. #endif
  255. }
  256. inline void MIC_Lock::wait_lock(int idx){
  257. #ifdef __MIC__
  258. if(idx>=0) while(lock_vec_[idx]==1){
  259. _mm_delay_32(8192);
  260. }
  261. #else
  262. if(idx<0 || lock_vec[idx]==0) return;
  263. if(lock_vec[idx]==2){
  264. while(lock_vec[idx]==2);
  265. return;
  266. }
  267. lock_vec[idx]=2;
  268. #ifdef __INTEL_OFFLOAD
  269. #pragma offload_wait target(mic:0) wait(&lock_vec[idx])
  270. #endif
  271. lock_vec[idx]=0;
  272. #endif
  273. }
  274. Vector<char> MIC_Lock::lock_vec;
  275. Vector<char>::Device MIC_Lock::lock_vec_;
  276. int MIC_Lock::lock_idx;
  277. // Implementation of Simple CUDA_Lock
  278. #if defined(PVFMM_HAVE_CUDA)
  279. CUDA_Lock::CUDA_Lock () {
  280. cuda_init = false;
  281. }
  282. inline void CUDA_Lock::init () {
  283. cudaError_t error;
  284. cublasStatus_t status;
  285. if (!cuda_init) {
  286. for (int i = 0; i < NUM_STREAM; i++) {
  287. error = cudaStreamCreate(&(stream[i]));
  288. }
  289. status = cublasCreate(&handle);
  290. //status = cublasSetStream(handle, stream[0]);
  291. cuda_init = true;
  292. }
  293. }
  294. inline void CUDA_Lock::terminate () {
  295. cudaError_t error;
  296. cublasStatus_t status;
  297. if (!cuda_init) init();
  298. for (int i = 0; i < NUM_STREAM; i++) {
  299. error = cudaStreamDestroy(stream[i]);
  300. }
  301. status = cublasDestroy(handle);
  302. cuda_init = false;
  303. }
  304. inline cudaStream_t *CUDA_Lock::acquire_stream (int idx) {
  305. if (!cuda_init) init();
  306. if (idx < NUM_STREAM) return &(stream[idx]);
  307. else return NULL;
  308. }
  309. inline cublasHandle_t *CUDA_Lock::acquire_handle () {
  310. if (!cuda_init) init();
  311. return &handle;
  312. }
  313. inline void CUDA_Lock::wait (int idx) {
  314. cudaError_t error;
  315. if (!cuda_init) init();
  316. if (idx < NUM_STREAM) error = cudaStreamSynchronize(stream[idx]);
  317. }
  318. cudaStream_t CUDA_Lock::stream[NUM_STREAM];
  319. cublasHandle_t CUDA_Lock::handle;
  320. bool CUDA_Lock::cuda_init;
  321. #endif
  322. }//end namespace