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 void allow_access_to_all_gpu_agents(void *ptr);
24 
25 namespace {
26 ATLProcessor &get_processor_by_mem_place(atmi_mem_place_t place) {
27   int dev_id = place.dev_id;
28   switch (place.dev_type) {
29   case ATMI_DEVTYPE_CPU:
30     return g_atl_machine.processors<ATLCPUProcessor>()[dev_id];
31   case ATMI_DEVTYPE_GPU:
32     return g_atl_machine.processors<ATLGPUProcessor>()[dev_id];
33   }
34 }
35 
36 hsa_amd_memory_pool_t get_memory_pool_by_mem_place(atmi_mem_place_t place) {
37   ATLProcessor &proc = get_processor_by_mem_place(place);
38   return get_memory_pool(proc, place.mem_id);
39 }
40 } // namespace
41 
42 void register_allocation(void *ptr, size_t size, atmi_mem_place_t place) {
43   if (place.dev_type == ATMI_DEVTYPE_CPU)
44     allow_access_to_all_gpu_agents(ptr);
45 }
46 
47 atmi_status_t Runtime::Malloc(void **ptr, size_t size, atmi_mem_place_t place) {
48   atmi_status_t ret = ATMI_STATUS_SUCCESS;
49   hsa_amd_memory_pool_t pool = get_memory_pool_by_mem_place(place);
50   hsa_status_t err = hsa_amd_memory_pool_allocate(pool, size, 0, ptr);
51   ErrorCheck("atmi_malloc", err);
52   DEBUG_PRINT("Malloced [%s %d] %p\n",
53               place.dev_type == ATMI_DEVTYPE_CPU ? "CPU" : "GPU", place.dev_id,
54               *ptr);
55   if (err != HSA_STATUS_SUCCESS)
56     ret = ATMI_STATUS_ERROR;
57 
58   register_allocation(*ptr, size, place);
59 
60   return ret;
61 }
62 
63 atmi_status_t Runtime::Memfree(void *ptr) {
64   atmi_status_t ret = ATMI_STATUS_SUCCESS;
65   hsa_status_t err;
66   err = hsa_amd_memory_pool_free(ptr);
67   ErrorCheck("atmi_free", err);
68   DEBUG_PRINT("Freed %p\n", ptr);
69 
70   if (err != HSA_STATUS_SUCCESS)
71     ret = ATMI_STATUS_ERROR;
72   return ret;
73 }
74 
75 } // namespace core
76