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