xref: /aosp_15_r20/external/executorch/backends/vulkan/tools/gpuinfo/include/app.h (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #pragma once
10 
11 #include <executorch/backends/vulkan/runtime/api/api.h>
12 #include <folly/json.h>
13 #include <fstream>
14 #include <iostream>
15 
16 #include "utils.h"
17 
18 namespace gpuinfo {
19 
20 class App {
21  private:
22   folly::dynamic config_;
23 
24  public:
25   size_t buf_cache_size;
26   uint32_t max_shared_mem_size;
27   uint32_t sm_count;
28   uint32_t nthread_logic;
29   uint32_t subgroup_size;
30   uint32_t max_tex_width;
31   uint32_t max_tex_height;
32   uint32_t max_tex_depth;
33 
App()34   App() {
35     context()->initialize_querypool();
36 
37     std::cout << context()->adapter_ptr()->stringize() << std::endl
38               << std::endl;
39 
40     auto cl_device = get_cl_device();
41 
42     sm_count = cl_device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>();
43     nthread_logic = cl_device.getInfo<CL_DEVICE_MAX_WORK_GROUP_SIZE>();
44     buf_cache_size = cl_device.getInfo<CL_DEVICE_GLOBAL_MEM_CACHE_SIZE>();
45     max_shared_mem_size = cl_device.getInfo<CL_DEVICE_LOCAL_MEM_SIZE>();
46     max_tex_width = cl_device.getInfo<CL_DEVICE_IMAGE3D_MAX_WIDTH>();
47     max_tex_height = cl_device.getInfo<CL_DEVICE_IMAGE3D_MAX_HEIGHT>();
48     max_tex_depth = cl_device.getInfo<CL_DEVICE_IMAGE3D_MAX_DEPTH>();
49 
50     VkPhysicalDeviceSubgroupProperties subgroup_props{};
51     VkPhysicalDeviceProperties2 props2{};
52 
53     props2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
54     props2.pNext = &subgroup_props;
55     subgroup_props.sType =
56         VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES;
57     vkGetPhysicalDeviceProperties2(
58         context()->adapter_ptr()->physical_handle(), &props2);
59     subgroup_size = subgroup_props.subgroupSize;
60 
61     std::cout << std::endl;
62     std::cout << "SM count," << sm_count << std::endl;
63     std::cout << "Logic Thread Count," << nthread_logic << std::endl;
64     std::cout << "Cache Size," << buf_cache_size << std::endl;
65     std::cout << "Shared Memory Size," << max_shared_mem_size << std::endl;
66     std::cout << "SubGroup Size," << subgroup_size << std::endl;
67     std::cout << "MaxTexWidth," << max_tex_width << std::endl;
68     std::cout << "MaxTexHeight," << max_tex_height << std::endl;
69     std::cout << "MaxTexDepth," << max_tex_depth << std::endl;
70   }
71 
get_config(const std::string & test,const std::string & key)72   float get_config(const std::string& test, const std::string& key) const {
73     if (config_[test].empty()) {
74       throw std::runtime_error("Missing config for " + test);
75     }
76 
77     if (!config_[test][key].isNumber()) {
78       throw std::runtime_error(
79           "Config for " + test + "." + key + " is not a number");
80     }
81 
82     float value;
83     if (config_[test][key].isDouble()) {
84       value = config_[test][key].getDouble();
85     } else {
86       value = config_[test][key].getInt();
87     }
88 
89     std::cout << "Read value for " << test << "." << key << " = " << value
90               << std::endl;
91     return value;
92   }
93 
enabled(const std::string & test)94   bool enabled(const std::string& test) const {
95     if (config_.empty() || config_[test].empty() ||
96         !config_[test]["enabled"].isBool()) {
97       return true;
98     }
99     return config_[test]["enabled"].getBool();
100   }
101 
load_config(std::string file_path)102   void load_config(std::string file_path) {
103     std::ifstream file(file_path);
104     std::stringstream buffer;
105     buffer << file.rdbuf();
106     const std::string json_str = buffer.str();
107     if (json_str.empty()) {
108       throw std::runtime_error(
109           "Failed to read config file from " + file_path + ".");
110     }
111     config_ = folly::parseJson(json_str);
112   }
113 };
114 } // namespace gpuinfo
115