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 if (err != HSA_STATUS_SUCCESS) { 52 printf("[%s:%d] %s failed: %s\n", __FILE__, __LINE__, "atmi_malloc", 53 get_error_string(err)); 54 exit(1); 55 } 56 DEBUG_PRINT("Malloced [%s %d] %p\n", 57 place.dev_type == ATMI_DEVTYPE_CPU ? "CPU" : "GPU", place.dev_id, 58 *ptr); 59 if (err != HSA_STATUS_SUCCESS) 60 ret = ATMI_STATUS_ERROR; 61 62 register_allocation(*ptr, size, place); 63 64 return ret; 65 } 66 67 atmi_status_t Runtime::Memfree(void *ptr) { 68 atmi_status_t ret = ATMI_STATUS_SUCCESS; 69 hsa_status_t err; 70 err = hsa_amd_memory_pool_free(ptr); 71 if (err != HSA_STATUS_SUCCESS) { 72 printf("[%s:%d] %s failed: %s\n", __FILE__, __LINE__, "atmi_free", 73 get_error_string(err)); 74 exit(1); 75 } 76 DEBUG_PRINT("Freed %p\n", ptr); 77 78 if (err != HSA_STATUS_SUCCESS) 79 ret = ATMI_STATUS_ERROR; 80 return ret; 81 } 82 83 } // namespace core 84