File size: 1,654 Bytes
f0d6538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import argparse
import torch
import time


def main():
    parser = argparse.ArgumentParser()

    parser.add_argument('--device', type=int, required=True)
    parser.add_argument('--time', type=int, required=True)
    parser.add_argument('--gpu_mem', type=int, default=2, help='GB')
    parser.add_argument('--usage', type=int, default=50, help='soft gpu usage')

    args = parser.parse_args()

    device = f'cuda:{args.device}'
    torch.cuda.set_device(device)

    # ===============================
    # 1. 显存占位(只占,不频繁访问)
    # ===============================
    num_floats = args.gpu_mem * (1 << 30) // 4
    _gpu_mem_holder = torch.empty(num_floats, device=device)

    # ===============================
    # 2. 小 tensor:温和 GPU kernel
    # ===============================
    # 小、cache-friendly、不吃带宽
    x = torch.rand(512, 512, device=device)

    start_time = time.time()

    while True:
        # -------------------------------
        # GPU soft burn
        # -------------------------------
        for _ in range(args.usage):
            # 比 sin 更温和,不用特殊函数单元
            x = x * 1.0001 + 0.0001

        # 强制让调度器看到 GPU 在干活
        torch.cuda.synchronize()

        # -------------------------------
        # 防止 stdout idle(部分平台需要)
        # -------------------------------
        # if int(time.time() - start_time) % 60 == 0:
        #     print(f"[GPU {args.device}] alive", flush=True)

        if args.time > 0 and time.time() - start_time > args.time:
            break


if __name__ == '__main__':
    main()