HIP: Heterogenous-computing Interface for Portability
hip_memory.h
1 /*
2 Copyright (c) 2015 - present Advanced Micro Devices, Inc. All rights reserved.
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 
23 #ifndef HIP_INCLUDE_HIP_HCC_DETAIL_HIP_MEMORY_H
24 #define HIP_INCLUDE_HIP_HCC_DETAIL_HIP_MEMORY_H
25 
26 // Implementation of malloc and free device functions.
27 // HIP heap is implemented as a global array with fixed size. Users may define
28 // __HIP_SIZE_OF_PAGE and __HIP_NUM_PAGES to have a larger heap.
29 
30 #if __HCC__ || __HIP__
31 
32 // Size of page in bytes.
33 #ifndef __HIP_SIZE_OF_PAGE
34 #define __HIP_SIZE_OF_PAGE 64
35 #endif
36 
37 // Total number of pages
38 #ifndef __HIP_NUM_PAGES
39 #define __HIP_NUM_PAGES (16 * 64 * 64)
40 #endif
41 
42 #define __HIP_SIZE_OF_HEAP (__HIP_NUM_PAGES * __HIP_SIZE_OF_PAGE)
43 
44 #if __HIP__ && __HIP_DEVICE_COMPILE__
45 __attribute__((weak)) __device__ char __hip_device_heap[__HIP_SIZE_OF_HEAP];
46 __attribute__((weak)) __device__
47  uint32_t __hip_device_page_flag[__HIP_NUM_PAGES];
48 #else
49 extern __device__ char __hip_device_heap[];
50 extern __device__ uint32_t __hip_device_page_flag[];
51 #endif
52 
53 extern "C" inline __device__ void* __hip_malloc(size_t size) {
54  char* heap = (char*)__hip_device_heap;
55  if (size > __HIP_SIZE_OF_HEAP) {
56  return (void*)nullptr;
57  }
58  uint32_t totalThreads =
59  hipBlockDim_x * hipGridDim_x * hipBlockDim_y
60  * hipGridDim_y * hipBlockDim_z * hipGridDim_z;
61  uint32_t currentWorkItem = hipThreadIdx_x + hipBlockDim_x * hipBlockIdx_x
62  + (hipThreadIdx_y + hipBlockDim_y * hipBlockIdx_y) * hipBlockDim_x
63  + (hipThreadIdx_z + hipBlockDim_z * hipBlockIdx_z) * hipBlockDim_x
64  * hipBlockDim_y;
65 
66  uint32_t numHeapsPerWorkItem = __HIP_NUM_PAGES / totalThreads;
67  uint32_t heapSizePerWorkItem = __HIP_SIZE_OF_HEAP / totalThreads;
68 
69  uint32_t stride = size / __HIP_SIZE_OF_PAGE;
70  uint32_t start = numHeapsPerWorkItem * currentWorkItem;
71 
72  uint32_t k = 0;
73 
74  while (__hip_device_page_flag[k] > 0) {
75  k++;
76  }
77 
78  for (uint32_t i = 0; i < stride - 1; i++) {
79  __hip_device_page_flag[i + start + k] = 1;
80  }
81 
82  __hip_device_page_flag[start + stride - 1 + k] = 2;
83 
84  void* ptr = (void*)(heap
85  + heapSizePerWorkItem * currentWorkItem + k * __HIP_SIZE_OF_PAGE);
86 
87  return ptr;
88 }
89 
90 extern "C" inline __device__ void* __hip_free(void* ptr) {
91  if (ptr == nullptr) {
92  return nullptr;
93  }
94 
95  uint32_t offsetByte = (uint64_t)ptr - (uint64_t)__hip_device_heap;
96  uint32_t offsetPage = offsetByte / __HIP_SIZE_OF_PAGE;
97 
98  while (__hip_device_page_flag[offsetPage] != 0) {
99  if (__hip_device_page_flag[offsetPage] == 2) {
100  __hip_device_page_flag[offsetPage] = 0;
101  offsetPage++;
102  break;
103  } else {
104  __hip_device_page_flag[offsetPage] = 0;
105  offsetPage++;
106  }
107  }
108 
109  return nullptr;
110 }
111 
112 #endif
113 
114 #endif // HIP_INCLUDE_HIP_HCC_DETAIL_HIP_MEMORY_H