1

nvidia-smi の出力を含む非常に大きなテキスト文字列を Python で分析しようとしていますが、正規表現のスキルを磨くよりもデータの分析に多くの時間を費やしたいと思っています。次のように正規表現を取得しましたが、一部の行で永遠に時間がかかります(一部の行の入力データのバリエーションである可能性があります)が、おそらく私の正規表現パターンも非常に計算集約的であると思いました.

extracted_line1 = r'[=]*[+][=]*[+][=]*\|\n\|(\s+(.*?)\|)+\n\|(\s+(.*?)\|)(\s+(.*?)\|)(\s+(.*?)\|)\n\|'

このパターンは、表の 3 行目に一致します。

これは下にあります⬇️

 ===============================+======================+======================|
|   0  GeForce GTX 1080    On   | 00000000:04:00.0 Off |                  N/A |
| 27%   20C    P8     6W / 180W |      2MiB /  8119MiB |      0%   E. Process |
|                               |                      |                  N/A |

ほとんどの行で機能しますが、一部の行でランダムにハングします。この正規表現のより単純化されたバージョンは何でしょうか? または、より良い質問は、この表のすべての行 (各 GPU の対応するメトリック) の各値を取得するための最良の方法は何ですか?

切り詰められた入力文字列はこちら

... bunch of text
nvidia-smi:
Tue Jun  8 15:00:02 2021       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 460.80       Driver Version: 460.80       CUDA Version: 11.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  GeForce GTX 1080    On   | 00000000:04:00.0 Off |                  N/A |
| 27%   20C    P8     6W / 180W |      2MiB /  8119MiB |      0%   E. Process |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   1  GeForce GTX 1080    On   | 00000000:05:00.0 Off |                  N/A |
| 27%   23C    P8     6W / 180W |      2MiB /  8119MiB |      0%   E. Process |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

... bunch of text

PS次の値を抽出しようとしています

        gpu_index = [processed result of regex output here]
        gpu_model_name = [processed result of regex output here]
        persistance_mode = [processed result of regex output here]
        bus_id = [processed result of regex output here]
        display_active = [processed result of regex output here]
        volatile_ecc = [processed result of regex output here]
        fan = [processed result of regex output here]
        temperature = [processed result of regex output here]
        perf = [processed result of regex output here]
        power_usage =  [processed result of regex output here]
        max_power = [processed result of regex output here]
        memory_usage = [processed result of regex output here] 
        available_mem = [processed result of regex output here] 
        gpu_utilization = [processed result of regex output here]
        compute_mode = [processed result of regex output here]
        multiple_instance_gpu_mode = [processed result of regex output here]
4

1 に答える 1