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 #ifndef SRC_RUNTIME_INCLUDE_MACHINE_H_
7 #define SRC_RUNTIME_INCLUDE_MACHINE_H_
8 #include "atmi.h"
9 #include "internal.h"
10 #include <hsa.h>
11 #include <hsa_ext_amd.h>
12 #include <vector>
13
14 class ATLMemory;
15
16 class ATLProcessor {
17 public:
18 explicit ATLProcessor(hsa_agent_t agent,
19 atmi_devtype_t type = ATMI_DEVTYPE_ALL)
agent_(agent)20 : agent_(agent), type_(type) {
21 memories_.clear();
22 }
23 void addMemory(const ATLMemory &p);
agent()24 hsa_agent_t agent() const { return agent_; }
25 // TODO(ashwinma): Do we need this or are we building the machine structure
26 // just once in the program?
27 // void removeMemory(ATLMemory &p);
28 const std::vector<ATLMemory> &memories() const;
type()29 atmi_devtype_t type() const { return type_; }
30
31 protected:
32 hsa_agent_t agent_;
33 atmi_devtype_t type_;
34 std::vector<ATLMemory> memories_;
35 };
36
37 class ATLCPUProcessor : public ATLProcessor {
38 public:
ATLCPUProcessor(hsa_agent_t agent)39 explicit ATLCPUProcessor(hsa_agent_t agent)
40 : ATLProcessor(agent, ATMI_DEVTYPE_CPU) {}
41 };
42
43 class ATLGPUProcessor : public ATLProcessor {
44 public:
45 explicit ATLGPUProcessor(hsa_agent_t agent,
46 atmi_devtype_t type = ATMI_DEVTYPE_dGPU)
ATLProcessor(agent,type)47 : ATLProcessor(agent, type) {}
48 };
49
50 class ATLMemory {
51 public:
ATLMemory(hsa_amd_memory_pool_t pool,ATLProcessor p,atmi_memtype_t t)52 ATLMemory(hsa_amd_memory_pool_t pool, ATLProcessor p, atmi_memtype_t t)
53 : memory_pool_(pool), processor_(p), type_(t) {}
memory()54 hsa_amd_memory_pool_t memory() const { return memory_pool_; }
55
type()56 atmi_memtype_t type() const { return type_; }
57
58 private:
59 hsa_amd_memory_pool_t memory_pool_;
60 ATLProcessor processor_;
61 atmi_memtype_t type_;
62 };
63
64 class ATLMachine {
65 public:
ATLMachine()66 ATLMachine() {
67 cpu_processors_.clear();
68 gpu_processors_.clear();
69 }
70 template <typename T> void addProcessor(const T &p);
71 template <typename T> std::vector<T> &processors();
processorCount()72 template <typename T> size_t processorCount() {
73 return processors<T>().size();
74 }
75
76 private:
77 std::vector<ATLCPUProcessor> cpu_processors_;
78 std::vector<ATLGPUProcessor> gpu_processors_;
79 };
80
81 hsa_amd_memory_pool_t get_memory_pool(const ATLProcessor &proc,
82 const int mem_id);
83
84 extern ATLMachine g_atl_machine;
get_processor(atmi_place_t place)85 template <typename T> T &get_processor(atmi_place_t place) {
86 int dev_id = place.device_id;
87 if (dev_id == -1) {
88 // user is asking runtime to pick a device
89 // TODO(ashwinma): best device of this type? pick 0 for now
90 dev_id = 0;
91 }
92 return g_atl_machine.processors<T>()[dev_id];
93 }
94
95 #endif // SRC_RUNTIME_INCLUDE_MACHINE_H_
96