---
# System prepended metadata

title: CPE資源調用

---

# CPE測試說明

CPE密碼:111111

## 資源調用步驟
README裡寫的步驟是基於CentOS，但CPE是灌ubuntu所以指令會有所不同

先pip install protobuf(一定要是**3.20.0** 太新的不行)和grpcio(1.64.0)
```
pip install protobuf grpcio
```

接下來要啟用GRPC server:
跟README寫的不同 我是輸入
```
systemctl start qmonitor-proxy 
```
```
systemctl status qmonitor-proxy
```
![Screenshot from 2024-05-28 15-08-53](https://hackmd.io/_uploads/BJzQX-Q4R.png)
有顯示active(running)就有成功跑起來了
```
關掉server: systemctl stop qmonitor-proxy
```

接下來要開防火牆
在ubuntu裡面要用ufw開啟防火牆 預設端口62472
```
sudo ufw enable
sudo ufw allow 62472/tcp
sudo ufw status
```
![image](https://hackmd.io/_uploads/B1QTvWmER.png)

接著在examples/python底下輸入

```
python3 QAicMonitorClient.py --device_info --sync_mode --print_json
```
就可以成功調用各種資源

![Screenshot from 2024-05-28 14-40-00](https://hackmd.io/_uploads/BJj83emV0.png)
## CPE測試
我開了兩個視窗，右邊執行政憲之前寫的多線程執行，左邊寫了一個無限迴圈，持續監視系統資源
持續調用資源程式碼參考:

```
import multiprocessing
import subprocess
from concurrent.futures import ThreadPoolExecutor


def monitor_resources():
    while True:
        result = subprocess.run(
            ['python3', '/opt/qti-aic/examples/python/QAicMonitorClient.py', '--device_info', '-d', '-1', '--sync_mode', '--print_json'],
            capture_output=True,
            text=True
        )
        # 如果返回碼不是0，表示有錯誤
        if result.returncode != 0:
            print("Error:", result.stderr)
            continue

        # 解析輸出並提取資源參數
        output_lines = result.stdout.strip().split('\n')
        for line in output_lines:
            if "neural_processors_free" in line:
                neural_processors_free_str = line.split(":")[-1].strip().rstrip(',')
                neural_processors_free = int(neural_processors_free_str)
                print("Neural Processors Free:", neural_processors_free)

            elif "neural_processors_total" in line:
                neural_processors_total_str = line.split(":")[-1].strip().rstrip(',')
                neural_processors_total = int(neural_processors_total_str)
                print("Neural Processors total:", neural_processors_total)
            elif "dram_bw" in line:
                dram_bw_str = line.split(":")[-1].strip().rstrip(',')
                dram_bw = float(dram_bw_str)
                print("DRAM Bandwidth:", dram_bw)
            elif "num_networks_active" in line:
                num_networks_active_str = line.split(":")[-1].strip().rstrip(',')
                num_networks_active = int(num_networks_active_str)
                print("Number of Active Networks:", num_networks_active)
            elif "num_networks_loaded" in line:
                num_networks_loaded_str = line.split(":")[-1].strip().rstrip(',')
                num_networks_loaded = int(num_networks_loaded_str)
                print("Number of Loaded Networks:", num_networks_loaded)
        print('\n')

# 資源監控進程的啟動和終止
if __name__ == "__main__":
    monitor_process = multiprocessing.Process(target=monitor_resources)
    monitor_process.start()

    # 等待資源監控進程完成
    monitor_process.join()


```

![Screenshot from 2024-05-28 15-43-59](https://hackmd.io/_uploads/rkY8iZX4R.png)

結果發現的確有調用到AI-chip，可以看到閒置的Neural Processors從2變為0，而我同時要求三次模型推論，但只有兩個推論成功，所以推測多線程的處理跟Neural Processors的個數有很大的關係，但不知道為甚麼處理器總共有9個，但實際上閒置可調用的只有2個，所以導致多線程最多只能同時跑兩個推論。
![Screenshot from 2024-05-28 14-53-22](https://hackmd.io/_uploads/HJIY1-XV0.png)
![Screenshot from 2024-05-28 14-54-12](https://hackmd.io/_uploads/rkRskZXER.png)
