This repository was archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu.ts
More file actions
70 lines (61 loc) · 1.67 KB
/
gpu.ts
File metadata and controls
70 lines (61 loc) · 1.67 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { loadFFmpegConfig } from "../ffmpeg/config";
interface FFmpegGPUAllocationInterface {
id: number;
maxRenderCount: number;
render: number[];
}
export const gpuList: FFmpegGPUAllocationInterface[] = [];
const ffmpegConfig = loadFFmpegConfig();
if (ffmpegConfig.gpus) {
for (const gpu of ffmpegConfig.gpus) {
gpuList.push({
id: gpu.id,
maxRenderCount: gpu.maxRenderCount,
render: []
})
}
}
/**
* GPU 번호를 할당받습니다.
*
* @param streamSessionId 할당할 스트리밍 세션의 ID를 입력 받습니다.
*/
export function allocateGPU(streamSessionId: number) {
let mostLessGPUId = gpuList[0].id;
let mostLessGPURenderCount = gpuList[0].render.length;
for (const gpu of gpuList) {
const isGPUReachedLimit = gpu.maxRenderCount >= gpu.render.length;
if (isGPUReachedLimit) { continue; }
if (gpu.render.length < mostLessGPURenderCount) {
mostLessGPUId = gpu.id;
mostLessGPURenderCount = gpu.render.length;
}
}
if (mostLessGPUId === gpuList[0].id) {
if (gpuList[0].render.length >= gpuList[0].maxRenderCount) {
throw new Error("Out of GPUs");
}
}
for (const gpu of gpuList) {
if (gpu.id === mostLessGPUId) {
gpu.render.push(streamSessionId);
break;
}
}
return mostLessGPUId;
}
/**
* 할당받은 GPU를 반환합니다.
*
* @param streamSessionId 반환할 스트리밍 세션의 ID를 입력 받습니다.
*/
export function freeGPU(streamSessionId: number) {
for (const gpu of gpuList) {
for (let i = 0; i < gpu.render.length; i++) {
if (gpu.render[i] === streamSessionId) {
gpu.render.splice(i, 1);
return;
}
}
}
}