gpuScheduler.ts 4.43 KB
Newer Older
Deshui Yu's avatar
Deshui Yu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * Copyright (c) Microsoft Corporation
 * All rights reserved.
 *
 * MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

'use strict';

22
import * as cpp from 'child-process-promise';
23
import * as cp from 'child_process';
24
import * as fs from 'fs';
25
26
import * as os from 'os';
import * as path from 'path';
27
import { String } from 'typescript-string-operations';
28
import { execMkdir, getScriptName, getgpuMetricsCollectorScriptContent, execScript, execTail, execRemove, execKill } from '../common/util'
29
30
import { getLogger, Logger } from '../../common/log';
import { delay } from '../../common/utils';
31
import { GPUInfo, GPUSummary } from '../common/gpuData';
Deshui Yu's avatar
Deshui Yu committed
32
33

/**
34
 * GPUScheduler for local training service
Deshui Yu's avatar
Deshui Yu committed
35
36
37
38
39
 */
class GPUScheduler {

    private gpuSummary!: GPUSummary;
    private stopping: boolean;
40
    private log: Logger;
41
    private gpuMetricCollectorScriptFolder: string;
Deshui Yu's avatar
Deshui Yu committed
42
43
44

    constructor() {
        this.stopping = false;
45
        this.log = getLogger();
46
        this.gpuMetricCollectorScriptFolder = `${os.tmpdir()}/nni/script`;
Deshui Yu's avatar
Deshui Yu committed
47
48
49
    }

    public async run(): Promise<void> {
50
        await this.runGpuMetricsCollectorScript();
Deshui Yu's avatar
Deshui Yu committed
51
52
        while (!this.stopping) {
            try {
53
                await this.updateGPUSummary();
Deshui Yu's avatar
Deshui Yu committed
54
            } catch (error) {
55
                this.log.error('Read GPU summary failed with error: ', error);
Deshui Yu's avatar
Deshui Yu committed
56
57
58
59
60
            }
            await delay(5000);
        }
    }

61
62
63
64
65
66
67
68
69
70
71
72
73
    /**
     * 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> {
        await execMkdir(this.gpuMetricCollectorScriptFolder);
        //generate gpu_metrics_collector script
        let gpuMetricsCollectorScriptPath: string = path.join(this.gpuMetricCollectorScriptFolder, getScriptName('gpu_metrics_collector'));
        const gpuMetricsCollectorScriptContent: string = getgpuMetricsCollectorScriptContent(this.gpuMetricCollectorScriptFolder);
        await fs.promises.writeFile(gpuMetricsCollectorScriptPath, gpuMetricsCollectorScriptContent, { encoding: 'utf8' });
        execScript(gpuMetricsCollectorScriptPath)
    }

Deshui Yu's avatar
Deshui Yu committed
74
75
    public getAvailableGPUIndices(): number[] {
        if (this.gpuSummary !== undefined) {
76
77
            return this.gpuSummary.gpuInfos.filter((info: GPUInfo) => info.activeProcessNum === 0)
                .map((info: GPUInfo) => info.index);
Deshui Yu's avatar
Deshui Yu committed
78
79
80
81
82
        }

        return [];
    }

83
84
85
86
87
88
89
90
91
    public getSystemGpuCount(): number {
        if (this.gpuSummary !== undefined) {
            return this.gpuSummary.gpuCount;
        }

        return 0;
    }

    public async stop(): Promise<void> {
Deshui Yu's avatar
Deshui Yu committed
92
        this.stopping = true;
93
94
        try {
            const pid: string = await fs.promises.readFile(path.join(this.gpuMetricCollectorScriptFolder, 'pid'), 'utf8');
95
96
            await execKill(pid);
            await execRemove(this.gpuMetricCollectorScriptFolder);
97
        } catch (error) {
98
99
            this.log.error(`GPU scheduler error: ${error}`);
        }
Deshui Yu's avatar
Deshui Yu committed
100
101
    }

102
103
    private async updateGPUSummary(): Promise<void> {
        const cmdresult: cpp.childProcessPromise.Result =
104
            await execTail(path.join(this.gpuMetricCollectorScriptFolder, 'gpu_metrics'));
105
        if (cmdresult && cmdresult.stdout) {
106
            this.gpuSummary = <GPUSummary>JSON.parse(cmdresult.stdout);
107
        } else {
108
            this.log.error('Could not get gpu metrics information!');
109
        }
Deshui Yu's avatar
Deshui Yu committed
110
111
112
113
    }
}

export { GPUScheduler };