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_RT_H_
7 #define SRC_RUNTIME_INCLUDE_RT_H_
8 
9 #include "atmi_runtime.h"
10 #include "hsa.h"
11 #include <cstdarg>
12 #include <string>
13 
14 namespace core {
15 
16 #define DEFAULT_MAX_QUEUE_SIZE 4096
17 #define DEFAULT_MAX_KERNEL_TYPES 32
18 #define DEFAULT_NUM_GPU_QUEUES -1 // computed in code
19 #define DEFAULT_NUM_CPU_QUEUES -1 // computed in code
20 #define DEFAULT_DEBUG_MODE 0
21 class Environment {
22 public:
23   Environment()
24       : max_queue_size_(DEFAULT_MAX_QUEUE_SIZE),
25         max_kernel_types_(DEFAULT_MAX_KERNEL_TYPES),
26         num_gpu_queues_(DEFAULT_NUM_GPU_QUEUES),
27         num_cpu_queues_(DEFAULT_NUM_CPU_QUEUES),
28         debug_mode_(DEFAULT_DEBUG_MODE) {
29     GetEnvAll();
30   }
31 
32   ~Environment() {}
33 
34   void GetEnvAll();
35 
36   int getMaxQueueSize() const { return max_queue_size_; }
37   int getMaxKernelTypes() const { return max_kernel_types_; }
38   int getNumGPUQueues() const { return num_gpu_queues_; }
39   int getNumCPUQueues() const { return num_cpu_queues_; }
40   // TODO(ashwinma): int may change to enum if we have more debug modes
41   int getDebugMode() const { return debug_mode_; }
42   // TODO(ashwinma): int may change to enum if we have more profile modes
43 
44 private:
45   std::string GetEnv(const char *name) {
46     char *env = getenv(name);
47     std::string ret;
48     if (env) {
49       ret = env;
50     }
51     return ret;
52   }
53 
54   int max_queue_size_;
55   int max_kernel_types_;
56   int num_gpu_queues_;
57   int num_cpu_queues_;
58   int debug_mode_;
59 };
60 
61 class Runtime final {
62 public:
63   static Runtime &getInstance() {
64     static Runtime instance;
65     return instance;
66   }
67 
68   // init/finalize
69   static atmi_status_t Initialize();
70   static atmi_status_t Finalize();
71 
72   // modules
73   static atmi_status_t RegisterModuleFromMemory(
74       void *, size_t, atmi_place_t,
75       atmi_status_t (*on_deserialized_data)(void *data, size_t size,
76                                             void *cb_state),
77       void *cb_state);
78 
79   // machine info
80   static atmi_machine_t *GetMachineInfo();
81 
82   // data
83   static atmi_status_t Memcpy(void *, const void *, size_t);
84   static atmi_status_t Memfree(void *);
85   static atmi_status_t Malloc(void **, size_t, atmi_mem_place_t);
86 
87   // environment variables
88   int getMaxQueueSize() const { return env_.getMaxQueueSize(); }
89   int getMaxKernelTypes() const { return env_.getMaxKernelTypes(); }
90   int getNumGPUQueues() const { return env_.getNumGPUQueues(); }
91   int getNumCPUQueues() const { return env_.getNumCPUQueues(); }
92   // TODO(ashwinma): int may change to enum if we have more debug modes
93   int getDebugMode() const { return env_.getDebugMode(); }
94 
95 protected:
96   Runtime() = default;
97   ~Runtime() = default;
98   Runtime(const Runtime &) = delete;
99   Runtime &operator=(const Runtime &) = delete;
100 
101 protected:
102   // variable to track environment variables
103   Environment env_;
104 };
105 
106 } // namespace core
107 
108 #endif // SRC_RUNTIME_INCLUDE_RT_H_
109