1 /*===--------------------------------------------------------------------------
2  *              ATMI (Asynchronous Task and Memory Interface)
3  *
4  * This file is distributed under the MIT License. See LICENSE.txt for details.
5  *===------------------------------------------------------------------------*/
6 #include "atmi_runtime.h"
7 #include "internal.h"
8 #include "machine.h"
9 #include "rt.h"
10 #include <cassert>
11 #include <hsa.h>
12 #include <hsa_ext_amd.h>
13 #include <iostream>
14 #include <stdio.h>
15 #include <string.h>
16 #include <thread>
17 #include <vector>
18 
19 using core::TaskImpl;
20 extern ATLMachine g_atl_machine;
21 
22 namespace core {
23 
24 namespace {
25 ATLProcessor &get_processor_by_mem_place(int DeviceId,
26                                          atmi_devtype_t DeviceType) {
27   switch (DeviceType) {
28   case ATMI_DEVTYPE_CPU:
29     return g_atl_machine.processors<ATLCPUProcessor>()[DeviceId];
30   case ATMI_DEVTYPE_GPU:
31     return g_atl_machine.processors<ATLGPUProcessor>()[DeviceId];
32   }
33 }
34 
35 hsa_amd_memory_pool_t get_memory_pool_by_mem_place(int DeviceId,
36                                                    atmi_devtype_t DeviceType) {
37   ATLProcessor &proc = get_processor_by_mem_place(DeviceId, DeviceType);
38   return get_memory_pool(proc, 0 /*Memory Type (always zero) */);
39 }
40 } // namespace
41 
42 hsa_status_t register_allocation(void *ptr, size_t size,
43                                  atmi_devtype_t DeviceType) {
44   if (DeviceType == ATMI_DEVTYPE_CPU)
45     return allow_access_to_all_gpu_agents(ptr);
46   else
47     return HSA_STATUS_SUCCESS;
48 }
49 
50 hsa_status_t Runtime::DeviceMalloc(void **ptr, size_t size, int DeviceId) {
51   return Runtime::Malloc(ptr, size, DeviceId, ATMI_DEVTYPE_GPU);
52 }
53 
54 hsa_status_t Runtime::HostMalloc(void **ptr, size_t size) {
55   return Runtime::Malloc(ptr, size, 0, ATMI_DEVTYPE_CPU);
56 }
57 
58 hsa_status_t Runtime::Malloc(void **ptr, size_t size, int DeviceId,
59                              atmi_devtype_t DeviceType) {
60   hsa_amd_memory_pool_t pool =
61       get_memory_pool_by_mem_place(DeviceId, DeviceType);
62   hsa_status_t err = hsa_amd_memory_pool_allocate(pool, size, 0, ptr);
63   DEBUG_PRINT("Malloced [%s %d] %p\n",
64               DeviceType == ATMI_DEVTYPE_CPU ? "CPU" : "GPU", DeviceId, *ptr);
65 
66   if (err == HSA_STATUS_SUCCESS) {
67     err = register_allocation(*ptr, size, DeviceType);
68   }
69 
70   return (err == HSA_STATUS_SUCCESS) ? HSA_STATUS_SUCCESS : HSA_STATUS_ERROR;
71 }
72 
73 hsa_status_t Runtime::Memfree(void *ptr) {
74   hsa_status_t err = hsa_amd_memory_pool_free(ptr);
75   DEBUG_PRINT("Freed %p\n", ptr);
76 
77   return (err == HSA_STATUS_SUCCESS) ? HSA_STATUS_SUCCESS : HSA_STATUS_ERROR;
78 }
79 
80 } // namespace core
81