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