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   // init/finalize
53   static atmi_status_t Initialize();
54   static atmi_status_t Finalize();
55   // machine info
56   static atmi_machine_t *GetMachineInfo();
57   // modules
58   static atmi_status_t RegisterModuleFromMemory(
59       void *, size_t, atmi_place_t,
60       atmi_status_t (*on_deserialized_data)(void *data, size_t size,
61                                             void *cb_state),
62       void *cb_state);
63 
64   // data
65   static atmi_status_t Memcpy(hsa_signal_t, void *, const void *, size_t);
66   static atmi_status_t Memfree(void *);
67   static atmi_status_t Malloc(void **, size_t, atmi_mem_place_t);
68 
69   int getMaxQueueSize() const { return env_.getMaxQueueSize(); }
70   int getDebugMode() const { return env_.getDebugMode(); }
71 
72 protected:
73   Runtime() = default;
74   ~Runtime() = default;
75   Runtime(const Runtime &) = delete;
76   Runtime &operator=(const Runtime &) = delete;
77 
78 protected:
79   // variable to track environment variables
80   Environment env_;
81 };
82 
83 } // namespace core
84 
85 #endif // SRC_RUNTIME_INCLUDE_RT_H_
86