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