remoteMachineData.ts 4.62 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
import { TrialJobApplicationForm, TrialJobDetail, TrialJobStatus } from 'common/trainingService';
import { RemoteMachineConfig } from 'common/experimentConfig';
6
import { GPUInfo, GPUSummary, ScheduleResultType } from '../common/gpuData';
7
import { ShellExecutor } from './shellExecutor';
Deshui Yu's avatar
Deshui Yu committed
8
9
10
11
12

/**
 * Metadata of remote machine for configuration and statuc query
 */
export class RemoteMachineMeta {
13
    public readonly config: RemoteMachineConfig;
chicm-ms's avatar
chicm-ms committed
14
    public gpuSummary: GPUSummary | undefined;
15
16
17
18
19
20
    public occupiedGpuIndexMap: Map<number, number>;

    constructor(config: RemoteMachineConfig) {
        this.config = config;
        this.occupiedGpuIndexMap = new Map<number, number>();
    }
21
22
}

Deshui Yu's avatar
Deshui Yu committed
23
24
25
26
/**
 * The execution result for command executed on remote machine
 */
export class RemoteCommandResult {
chicm-ms's avatar
chicm-ms committed
27
28
29
    public readonly stdout: string;
    public readonly stderr: string;
    public readonly exitCode: number;
Deshui Yu's avatar
Deshui Yu committed
30

chicm-ms's avatar
chicm-ms committed
31
    constructor(stdout: string, stderr: string, exitCode: number) {
Deshui Yu's avatar
Deshui Yu committed
32
33
34
35
36
37
38
39
40
41
42
43
        this.stdout = stdout;
        this.stderr = stderr;
        this.exitCode = exitCode;
    }
}

/**
 * RemoteMachineTrialJobDetail
 */
export class RemoteMachineTrialJobDetail implements TrialJobDetail {
    public id: string;
    public status: TrialJobStatus;
44
45
46
    public submitTime: number;
    public startTime?: number;
    public endTime?: number;
Deshui Yu's avatar
Deshui Yu committed
47
48
49
    public tags?: string[];
    public url?: string;
    public workingDirectory: string;
50
    public form: TrialJobApplicationForm;
Deshui Yu's avatar
Deshui Yu committed
51
    public rmMeta?: RemoteMachineMeta;
52
    public isEarlyStopped?: boolean;
53
    public gpuIndices: GPUInfo[];
Deshui Yu's avatar
Deshui Yu committed
54

55
    constructor(id: string, status: TrialJobStatus, submitTime: number,
56
        workingDirectory: string, form: TrialJobApplicationForm) {
Deshui Yu's avatar
Deshui Yu committed
57
58
59
60
61
62
        this.id = id;
        this.status = status;
        this.submitTime = submitTime;
        this.workingDirectory = workingDirectory;
        this.form = form;
        this.tags = [];
63
        this.gpuIndices = [];
Deshui Yu's avatar
Deshui Yu committed
64
65
66
    }
}

SparkSnail's avatar
SparkSnail committed
67
/**
68
 * The remote machine executor manager
SparkSnail's avatar
SparkSnail committed
69
 */
70
export class ExecutorManager {
71
    public readonly rmMeta: RemoteMachineMeta;
72
73
74
75
    private readonly executorMap: Map<string, ShellExecutor> = new Map<string, ShellExecutor>();

    private executors: ShellExecutor[] = [];

76
77
    constructor(config: RemoteMachineConfig) {
        this.rmMeta = new RemoteMachineMeta(config);
SparkSnail's avatar
SparkSnail committed
78
79
    }

80
81
82
    public async getExecutor(id: string): Promise<ShellExecutor> {
        let isFound = false;
        let executor: ShellExecutor | undefined;
83

84
85
86
87
88
        // already assigned
        if (this.executorMap.has(id)) {
            executor = this.executorMap.get(id);
            if (executor === undefined) {
                throw new Error("executor shouldn't be undefined before return!");
SparkSnail's avatar
SparkSnail committed
89
            }
90
            return executor;
91
92
        }

93
94
95
96
97
98
99
100
101
102
103
        for (const candidateExecutor of this.executors) {
            if (candidateExecutor.addUsage()) {
                isFound = true;
                executor = candidateExecutor;
                break;
            }
        }
        // init a new executor if no free one.
        if (!isFound) {
            executor = await this.createShellExecutor();
        }
104

105
106
107
108
        if (executor === undefined) {
            throw new Error("executor shouldn't be undefined before set!");
        }
        this.executorMap.set(id, executor);
109

110
        return executor;
SparkSnail's avatar
SparkSnail committed
111
    }
112

SparkSnail's avatar
SparkSnail committed
113
    /**
114
     * close all of executor
SparkSnail's avatar
SparkSnail committed
115
     */
116
117
118
    public releaseAllExecutor(): void {
        this.executorMap.clear();
        for (const executor of this.executors) {
119
            executor.close();
SparkSnail's avatar
SparkSnail committed
120
        }
121
        this.executors = [];
SparkSnail's avatar
SparkSnail committed
122
    }
123

SparkSnail's avatar
SparkSnail committed
124
    /**
125
126
     * retrieve resource, minus a number for given executor
     * @param executor executor
SparkSnail's avatar
SparkSnail committed
127
     */
128
129
    public releaseExecutor(id: string): void {
        const executor = this.executorMap.get(id);
130
        if (executor === undefined) {
131
            throw new Error(`executor for ${id} is not found`);
SparkSnail's avatar
SparkSnail committed
132
        }
133
134
        executor.releaseUsage();
        this.executorMap.delete(id);
SparkSnail's avatar
SparkSnail committed
135
136
    }

137
    /**
138
     * Create a new connection executor and initialize it
139
     */
140
    private async createShellExecutor(): Promise<ShellExecutor> {
141
142
        const executor = new ShellExecutor();
        await executor.initialize(this.rmMeta);
143
144
145
146
        if (!executor.addUsage()) {
            throw new Error("failed to add usage on new created Executor! It's a wired bug!");
        }
        this.executors.push(executor);
147
        return executor;
148
149
    }
}
SparkSnail's avatar
SparkSnail committed
150

151
export type RemoteMachineScheduleResult = { scheduleInfo: RemoteMachineScheduleInfo | undefined; resultType: ScheduleResultType };
Deshui Yu's avatar
Deshui Yu committed
152

153
export type RemoteMachineScheduleInfo = { rmMeta: RemoteMachineMeta; cudaVisibleDevice: string };