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 // Size of page in bytes.
31 #ifndef __HIP_SIZE_OF_PAGE
32 #define __HIP_SIZE_OF_PAGE 64
33 #endif
34 
35 // Total number of pages
36 #ifndef __HIP_NUM_PAGES
37 #define __HIP_NUM_PAGES (16 * 64 * 64)
38 #endif
39 
40 #define __HIP_SIZE_OF_HEAP (__HIP_NUM_PAGES * __HIP_SIZE_OF_PAGE)
41 
42 #if __HCC__ || __HIP__
43 
44 extern __attribute__((weak)) __device__ char __hip_device_heap[__HIP_SIZE_OF_HEAP];
45 extern __attribute__((weak)) __device__
46  uint32_t __hip_device_page_flag[__HIP_NUM_PAGES];
47 
48 extern "C" inline __device__ void* __hip_malloc(size_t size) {
49  char* heap = (char*)__hip_device_heap;
50  if (size > __HIP_SIZE_OF_HEAP) {
51  return (void*)nullptr;
52  }
53  uint32_t totalThreads =
54  hipBlockDim_x * hipGridDim_x * hipBlockDim_y
55  * hipGridDim_y * hipBlockDim_z * hipGridDim_z;
56  uint32_t currentWorkItem = hipThreadIdx_x + hipBlockDim_x * hipBlockIdx_x
57  + (hipThreadIdx_y + hipBlockDim_y * hipBlockIdx_y) * hipBlockDim_x
58  + (hipThreadIdx_z + hipBlockDim_z * hipBlockIdx_z) * hipBlockDim_x
59  * hipBlockDim_y;
60 
61  uint32_t numHeapsPerWorkItem = __HIP_NUM_PAGES / totalThreads;
62  uint32_t heapSizePerWorkItem = __HIP_SIZE_OF_HEAP / totalThreads;
63 
64  uint32_t stride = size / __HIP_SIZE_OF_PAGE;
65  uint32_t start = numHeapsPerWorkItem * currentWorkItem;
66 
67  uint32_t k = 0;
68 
69  while (__hip_device_page_flag[k] > 0) {
70  k++;
71  }
72 
73  for (uint32_t i = 0; i < stride - 1; i++) {
74  __hip_device_page_flag[i + start + k] = 1;
75  }
76 
77  __hip_device_page_flag[start + stride - 1 + k] = 2;
78 
79  void* ptr = (void*)(heap
80  + heapSizePerWorkItem * currentWorkItem + k * __HIP_SIZE_OF_PAGE);
81 
82  return ptr;
83 }
84 
85 extern "C" inline __device__ void* __hip_free(void* ptr) {
86  if (ptr == nullptr) {
87  return nullptr;
88  }
89 
90  uint32_t offsetByte = (uint64_t)ptr - (uint64_t)__hip_device_heap;
91  uint32_t offsetPage = offsetByte / __HIP_SIZE_OF_PAGE;
92 
93  while (__hip_device_page_flag[offsetPage] != 0) {
94  if (__hip_device_page_flag[offsetPage] == 2) {
95  __hip_device_page_flag[offsetPage] = 0;
96  offsetPage++;
97  break;
98  } else {
99  __hip_device_page_flag[offsetPage] = 0;
100  offsetPage++;
101  }
102  }
103 
104  return nullptr;
105 }
106 
107 #endif
108 
109 #endif // HIP_INCLUDE_HIP_HCC_DETAIL_HIP_MEMORY_H