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 "machine.h"
7 #include "atmi_runtime.h"
8 #include "internal.h"
9 #include <cassert>
10 #include <hsa.h>
11 #include <hsa_ext_amd.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <vector>
15 
16 extern ATLMachine g_atl_machine;
17 extern hsa_region_t atl_cpu_kernarg_region;
18 
addMemory(const ATLMemory & mem)19 void ATLProcessor::addMemory(const ATLMemory &mem) {
20   for (auto &mem_obj : memories_) {
21     // if the memory already exists, then just return
22     if (mem.memory().handle == mem_obj.memory().handle)
23       return;
24   }
25   memories_.push_back(mem);
26 }
27 
memories() const28 const std::vector<ATLMemory> &ATLProcessor::memories() const {
29   return memories_;
30 }
31 
processors()32 template <> std::vector<ATLCPUProcessor> &ATLMachine::processors() {
33   return cpu_processors_;
34 }
35 
processors()36 template <> std::vector<ATLGPUProcessor> &ATLMachine::processors() {
37   return gpu_processors_;
38 }
39 
get_memory_pool(const ATLProcessor & proc,const int mem_id)40 hsa_amd_memory_pool_t get_memory_pool(const ATLProcessor &proc,
41                                       const int mem_id) {
42   hsa_amd_memory_pool_t pool;
43   const std::vector<ATLMemory> &mems = proc.memories();
44   assert(mems.size() && mem_id >= 0 && mem_id < mems.size() &&
45          "Invalid memory pools for this processor");
46   pool = mems[mem_id].memory();
47   return pool;
48 }
49 
addProcessor(const ATLCPUProcessor & p)50 template <> void ATLMachine::addProcessor(const ATLCPUProcessor &p) {
51   cpu_processors_.push_back(p);
52 }
53 
addProcessor(const ATLGPUProcessor & p)54 template <> void ATLMachine::addProcessor(const ATLGPUProcessor &p) {
55   gpu_processors_.push_back(p);
56 }
57