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::Malloc(void **ptr, size_t size, int DeviceId,
51                              atmi_devtype_t DeviceType) {
52   hsa_amd_memory_pool_t pool =
53       get_memory_pool_by_mem_place(DeviceId, DeviceType);
54   hsa_status_t err = hsa_amd_memory_pool_allocate(pool, size, 0, ptr);
55   DEBUG_PRINT("Malloced [%s %d] %p\n",
56               DeviceType == ATMI_DEVTYPE_CPU ? "CPU" : "GPU", DeviceId, *ptr);
57 
58   if (err == HSA_STATUS_SUCCESS) {
59     err = register_allocation(*ptr, size, DeviceType);
60   }
61 
62   return (err == HSA_STATUS_SUCCESS) ? HSA_STATUS_SUCCESS : HSA_STATUS_ERROR;
63 }
64 
65 hsa_status_t Runtime::Memfree(void *ptr) {
66   hsa_status_t err = hsa_amd_memory_pool_free(ptr);
67   DEBUG_PRINT("Freed %p\n", ptr);
68 
69   return (err == HSA_STATUS_SUCCESS) ? HSA_STATUS_SUCCESS : HSA_STATUS_ERROR;
70 }
71 
72 } // namespace core
73