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 <stdio.h>
14 #include <string.h>
15 #include <thread>
16 #include <vector>
17 
18 using core::TaskImpl;
19 extern ATLMachine g_atl_machine;
20 
21 namespace core {
22 
23 namespace {
24 ATLProcessor &get_processor_by_mem_place(int DeviceId,
25                                          atmi_devtype_t DeviceType) {
26   switch (DeviceType) {
27   case ATMI_DEVTYPE_CPU:
28     return g_atl_machine.processors<ATLCPUProcessor>()[DeviceId];
29   case ATMI_DEVTYPE_GPU:
30     return g_atl_machine.processors<ATLGPUProcessor>()[DeviceId];
31   }
32 }
33 
34 hsa_amd_memory_pool_t get_memory_pool_by_mem_place(int DeviceId,
35                                                    atmi_devtype_t DeviceType) {
36   ATLProcessor &proc = get_processor_by_mem_place(DeviceId, DeviceType);
37   return get_memory_pool(proc, 0 /*Memory Type (always zero) */);
38 }
39 } // namespace
40 
41 hsa_status_t Runtime::DeviceMalloc(void **ptr, size_t size, int DeviceId) {
42   return Runtime::Malloc(ptr, size, DeviceId, ATMI_DEVTYPE_GPU);
43 }
44 
45 hsa_status_t Runtime::HostMalloc(void **ptr, size_t size) {
46   hsa_status_t Err = Runtime::Malloc(ptr, size, 0, ATMI_DEVTYPE_CPU);
47   if (Err == HSA_STATUS_SUCCESS) {
48     Err = core::allow_access_to_all_gpu_agents(*ptr);
49   }
50   return Err;
51 }
52 
53 hsa_status_t Runtime::Malloc(void **ptr, size_t size, int DeviceId,
54                              atmi_devtype_t DeviceType) {
55   hsa_amd_memory_pool_t pool =
56       get_memory_pool_by_mem_place(DeviceId, DeviceType);
57   hsa_status_t err = hsa_amd_memory_pool_allocate(pool, size, 0, ptr);
58   DEBUG_PRINT("Malloced [%s %d] %p\n",
59               DeviceType == ATMI_DEVTYPE_CPU ? "CPU" : "GPU", DeviceId, *ptr);
60   return (err == HSA_STATUS_SUCCESS) ? HSA_STATUS_SUCCESS : HSA_STATUS_ERROR;
61 }
62 
63 hsa_status_t Runtime::Memfree(void *ptr) {
64   hsa_status_t err = hsa_amd_memory_pool_free(ptr);
65   DEBUG_PRINT("Freed %p\n", ptr);
66 
67   return (err == HSA_STATUS_SUCCESS) ? HSA_STATUS_SUCCESS : HSA_STATUS_ERROR;
68 }
69 
70 } // namespace core
71