• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
getPlaceStr(atmi_devtype_t type)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 
get_processor_by_mem_place(atmi_mem_place_t place)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 
get_memory_pool_by_mem_place(atmi_mem_place_t place)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 
register_allocation(void * ptr,size_t size,atmi_mem_place_t place)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   // TODO(ashwinma): what if one GPU wants to access another GPU?
55 }
56 
Malloc(void ** ptr,size_t size,atmi_mem_place_t place)57 atmi_status_t Runtime::Malloc(void **ptr, size_t size, atmi_mem_place_t place) {
58   atmi_status_t ret = ATMI_STATUS_SUCCESS;
59   hsa_amd_memory_pool_t pool = get_memory_pool_by_mem_place(place);
60   hsa_status_t err = hsa_amd_memory_pool_allocate(pool, size, 0, ptr);
61   ErrorCheck(atmi_malloc, err);
62   DEBUG_PRINT("Malloced [%s %d] %p\n",
63               place.dev_type == ATMI_DEVTYPE_CPU ? "CPU" : "GPU", place.dev_id,
64               *ptr);
65   if (err != HSA_STATUS_SUCCESS)
66     ret = ATMI_STATUS_ERROR;
67 
68   register_allocation(*ptr, size, place);
69 
70   return ret;
71 }
72 
Memfree(void * ptr)73 atmi_status_t Runtime::Memfree(void *ptr) {
74   atmi_status_t ret = ATMI_STATUS_SUCCESS;
75   hsa_status_t err;
76   err = hsa_amd_memory_pool_free(ptr);
77   ErrorCheck(atmi_free, err);
78   DEBUG_PRINT("Freed %p\n", ptr);
79 
80   if (err != HSA_STATUS_SUCCESS)
81     ret = ATMI_STATUS_ERROR;
82   return ret;
83 }
84 
85 } // namespace core
86