1 //===--- amdgpu/impl/rt.h ----------------------------------------- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #ifndef SRC_RUNTIME_INCLUDE_RT_H_
9 #define SRC_RUNTIME_INCLUDE_RT_H_
10 
11 #include "atmi_runtime.h"
12 #include "hsa_api.h"
13 #include <string>
14 
15 namespace core {
16 
17 #define DEFAULT_MAX_QUEUE_SIZE 4096
18 #define DEFAULT_DEBUG_MODE 0
19 class Environment {
20 public:
Environment()21   Environment()
22       : max_queue_size_(DEFAULT_MAX_QUEUE_SIZE),
23         debug_mode_(DEFAULT_DEBUG_MODE) {
24     GetEnvAll();
25   }
26 
27   void GetEnvAll();
28 
getMaxQueueSize()29   int getMaxQueueSize() const { return max_queue_size_; }
getDebugMode()30   int getDebugMode() const { return debug_mode_; }
31 
32 private:
GetEnv(const char * name)33   std::string GetEnv(const char *name) {
34     char *env = getenv(name);
35     std::string ret;
36     if (env) {
37       ret = env;
38     }
39     return ret;
40   }
41 
42   int max_queue_size_;
43   int debug_mode_;
44 };
45 
46 class Runtime final {
47 public:
getInstance()48   static Runtime &getInstance() {
49     static Runtime instance;
50     return instance;
51   }
52 
53   // modules
54   static hsa_status_t RegisterModuleFromMemory(
55       void *, size_t, hsa_agent_t agent,
56       hsa_status_t (*on_deserialized_data)(void *data, size_t size,
57                                            void *cb_state),
58       void *cb_state, std::vector<hsa_executable_t> &HSAExecutables);
59 
60   // data
61   static hsa_status_t Memcpy(hsa_signal_t, void *, const void *, size_t);
62   static hsa_status_t Memfree(void *);
63   static hsa_status_t HostMalloc(void **ptr, size_t size);
64   static hsa_status_t DeviceMalloc(void **ptr, size_t size, int DeviceId);
65 
getMaxQueueSize()66   int getMaxQueueSize() const { return env_.getMaxQueueSize(); }
getDebugMode()67   int getDebugMode() const { return env_.getDebugMode(); }
68 
69 private:
70   static hsa_status_t Malloc(void **ptr, size_t size, int DeviceId,
71                              atmi_devtype_t DeviceType);
72 
73 protected:
74   Runtime() = default;
75   ~Runtime() = default;
76   Runtime(const Runtime &) = delete;
77   Runtime &operator=(const Runtime &) = delete;
78 
79 protected:
80   // variable to track environment variables
81   Environment env_;
82 };
83 
84 } // namespace core
85 
86 #endif // SRC_RUNTIME_INCLUDE_RT_H_
87