gpuScheduler.ts 3.7 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
Deshui Yu's avatar
Deshui Yu committed
3

4
5
6
7
8
9
import cpp from 'child-process-promise';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { getLogger, Logger } from 'common/log';
import { delay } from 'common/utils';
10
import { GPUInfo, GPUSummary } from '../common/gpuData';
11
import { execKill, execMkdir, execRemove, execTail, runGpuMetricsCollector } from '../common/util';
Deshui Yu's avatar
Deshui Yu committed
12
13

/**
14
 * GPUScheduler for local training service
Deshui Yu's avatar
Deshui Yu committed
15
16
17
18
19
 */
class GPUScheduler {

    private gpuSummary!: GPUSummary;
    private stopping: boolean;
20
21
    private readonly log: Logger;
    private readonly gpuMetricCollectorScriptFolder: string;
Deshui Yu's avatar
Deshui Yu committed
22
23
24

    constructor() {
        this.stopping = false;
liuzhe-lz's avatar
liuzhe-lz committed
25
        this.log = getLogger('GPUScheduler');
26
        this.gpuMetricCollectorScriptFolder = `${os.tmpdir()}/${os.userInfo().username}/nni/script`;
Deshui Yu's avatar
Deshui Yu committed
27
28
29
    }

    public async run(): Promise<void> {
30
        await this.runGpuMetricsCollectorScript();
Deshui Yu's avatar
Deshui Yu committed
31
32
        while (!this.stopping) {
            try {
33
                await this.updateGPUSummary();
Deshui Yu's avatar
Deshui Yu committed
34
            } catch (error) {
35
                this.log.error('Read GPU summary failed with error: ', error);
Deshui Yu's avatar
Deshui Yu committed
36
            }
37
38
39
            if (this.gpuSummary !== undefined && this.gpuSummary.gpuCount === 0) {
                throw new Error('GPU not available. Please check your CUDA configuration');
            }
Deshui Yu's avatar
Deshui Yu committed
40
41
42
43
            await delay(5000);
        }
    }

44
    public getAvailableGPUIndices(useActiveGpu: boolean | undefined, occupiedGpuIndexNumMap: Map<number, number>): number[] {
Deshui Yu's avatar
Deshui Yu committed
45
        if (this.gpuSummary !== undefined) {
46
            if (process.platform === 'win32' || useActiveGpu) {
demianzhang's avatar
demianzhang committed
47
                return this.gpuSummary.gpuInfos.map((info: GPUInfo) => info.index);
48
49
50
51
52
            } else {
                return this.gpuSummary.gpuInfos.filter((info: GPUInfo) =>
                         occupiedGpuIndexNumMap.get(info.index) === undefined && info.activeProcessNum === 0 ||
                         occupiedGpuIndexNumMap.get(info.index) !== undefined)
                       .map((info: GPUInfo) => info.index);
demianzhang's avatar
demianzhang committed
53
            }
Deshui Yu's avatar
Deshui Yu committed
54
55
56
57
58
        }

        return [];
    }

59
    public getSystemGpuCount(): number | undefined{
60
61
62
63
        if (this.gpuSummary !== undefined) {
            return this.gpuSummary.gpuCount;
        }

64
        return undefined;
65
66
67
    }

    public async stop(): Promise<void> {
Deshui Yu's avatar
Deshui Yu committed
68
        this.stopping = true;
69
70
        try {
            const pid: string = await fs.promises.readFile(path.join(this.gpuMetricCollectorScriptFolder, 'pid'), 'utf8');
71
72
            await execKill(pid);
            await execRemove(this.gpuMetricCollectorScriptFolder);
73
        } catch (error) {
74
75
            this.log.error(`GPU scheduler error: ${error}`);
        }
Deshui Yu's avatar
Deshui Yu committed
76
77
    }

78
79
80
81
82
    /**
     * Generate gpu metric collector shell script in local machine,
     * used to run in remote machine, and will be deleted after uploaded from local.
     */
    private async runGpuMetricsCollectorScript(): Promise<void> {
83
        await execMkdir(this.gpuMetricCollectorScriptFolder, true);
84
        runGpuMetricsCollector(this.gpuMetricCollectorScriptFolder);
85
86
    }

87
    private async updateGPUSummary(): Promise<void> {
88
        const gpuMetricPath: string = path.join(this.gpuMetricCollectorScriptFolder, 'gpu_metrics');
89
90
        if (fs.existsSync(gpuMetricPath)) {
            const cmdresult: cpp.childProcessPromise.Result = await execTail(gpuMetricPath);
91
            if (cmdresult !== undefined && cmdresult.stdout !== undefined) {
92
93
94
95
                this.gpuSummary = <GPUSummary>JSON.parse(cmdresult.stdout);
            } else {
                this.log.error('Could not get gpu metrics information!');
            }
96
97
        } else {
            this.log.warning('gpu_metrics file does not exist!');
98
        }
Deshui Yu's avatar
Deshui Yu committed
99
100
101
102
    }
}

export { GPUScheduler };