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 hsa_status_t RegisterModuleFromMemory(
56       void *, size_t, atmi_place_t,
57       hsa_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 hsa_status_t Memcpy(hsa_signal_t, void *, const void *, size_t);
63   static hsa_status_t Memfree(void *);
64   static hsa_status_t HostMalloc(void **ptr, size_t size);
65   static hsa_status_t DeviceMalloc(void **ptr, size_t size, int DeviceId);
66 
67   int getMaxQueueSize() const { return env_.getMaxQueueSize(); }
68   int getDebugMode() const { return env_.getDebugMode(); }
69 
70 private:
71   static hsa_status_t Malloc(void **ptr, size_t size, int DeviceId,
72                              atmi_devtype_t DeviceType);
73 
74 protected:
75   Runtime() = default;
76   ~Runtime() = default;
77   Runtime(const Runtime &) = delete;
78   Runtime &operator=(const Runtime &) = delete;
79 
80 protected:
81   // variable to track environment variables
82   Environment env_;
83 };
84 
85 } // namespace core
86 
87 #endif // SRC_RUNTIME_INCLUDE_RT_H_
88