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_DEBUG_MODE 0
18 class Environment {
19 public:
20   Environment()
21       : max_queue_size_(DEFAULT_MAX_QUEUE_SIZE),
22         debug_mode_(DEFAULT_DEBUG_MODE) {
23     GetEnvAll();
24   }
25 
26   void GetEnvAll();
27 
28   int getMaxQueueSize() const { return max_queue_size_; }
29   int getDebugMode() const { return debug_mode_; }
30 
31 private:
32   std::string GetEnv(const char *name) {
33     char *env = getenv(name);
34     std::string ret;
35     if (env) {
36       ret = env;
37     }
38     return ret;
39   }
40 
41   int max_queue_size_;
42   int debug_mode_;
43 };
44 
45 class Runtime final {
46 public:
47   static Runtime &getInstance() {
48     static Runtime instance;
49     return instance;
50   }
51 
52   // machine info
53   static atmi_machine_t *GetMachineInfo();
54   // modules
55   static atmi_status_t RegisterModuleFromMemory(
56       void *, size_t, atmi_place_t,
57       atmi_status_t (*on_deserialized_data)(void *data, size_t size,
58                                             void *cb_state),
59       void *cb_state, std::vector<hsa_executable_t> &HSAExecutables);
60 
61   // data
62   static atmi_status_t Memcpy(hsa_signal_t, void *, const void *, size_t);
63   static atmi_status_t Memfree(void *);
64   static atmi_status_t Malloc(void **, size_t, atmi_mem_place_t);
65 
66   int getMaxQueueSize() const { return env_.getMaxQueueSize(); }
67   int getDebugMode() const { return env_.getDebugMode(); }
68 
69 protected:
70   Runtime() = default;
71   ~Runtime() = default;
72   Runtime(const Runtime &) = delete;
73   Runtime &operator=(const Runtime &) = delete;
74 
75 protected:
76   // variable to track environment variables
77   Environment env_;
78 };
79 
80 } // namespace core
81 
82 #endif // SRC_RUNTIME_INCLUDE_RT_H_
83