-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path50_Edit-MJS-Def.ps1
More file actions
199 lines (168 loc) · 7.31 KB
/
Copy path50_Edit-MJS-Def.ps1
File metadata and controls
199 lines (168 loc) · 7.31 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<#
.SYNOPSIS
Edit the MJS startup parameters in the mjs_def file
.DESCRIPTION
# This script defines MATLAB Job Scheduler Startup Parameters
# https://www.mathworks.com/help/matlab-parallel-server/define-startup-parameters.html
.OUTPUTS
0 if parameter was set successfully, non-zero on error.
.EXAMPLE
Edit-MJSDef
.NOTES
Copyright 2022-2025 The MathWorks, Inc.
#>
function Set-MJSParams {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', 'Set-MJSParams')]
param (
[Parameter(Mandatory = $true)]
[string] $DefFile,
[Parameter(Mandatory = $true)]
[string] $ParameterName,
[Parameter(Mandatory = $true)]
[string] $ParameterValue
)
(Get-Content $DefFile -Raw) -replace "REM set $ParameterName=.*", "set $ParameterName=$ParameterValue" | Set-Content $DefFile
if (-not (Select-String -Pattern "^set $ParameterName=" -Path $DefFile)) {
Write-Output "Failed to set parameter $ParameterName"
exit 1
}
}
function Set-MJSSecurityParams {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', 'Set-MJSSecurityParams')]
param (
[Parameter(Mandatory = $true)]
[string] $SecurityLevel,
[Parameter(Mandatory = $true)]
[string] $MJSAdminPasswordFile
)
if ($SecurityLevel) {
Set-MJSParams -DefFile $Env:MJSDefFile -ParameterName 'SECURITY_LEVEL' -ParameterValue $SecurityLevel
# Generate default password for ADMIN_USER at Security Level 2 and 3
if (([int]$SecurityLevel -ge 2) -and -not (Test-Path $MJSAdminPasswordFile)) {
$MJSAdminPasswordRoot = Split-Path -Parent $MJSAdminPasswordFile
if (-not (Test-Path $MJSAdminPasswordRoot)) {
New-Item -Path $MJSAdminPasswordRoot -ItemType Directory
}
Add-Type -AssemblyName 'System.Web'
[System.Web.Security.Membership]::GeneratePassword(32, 0) | Set-Content $MJSAdminPasswordFile
}
}
}
function Set-MJSHeapMemory {
param (
[Parameter(Mandatory = $true)]
[int] $WorkersPerNode,
[Parameter(Mandatory = $true)]
[int] $MaxNodes
)
$MemoryMB = (Get-CimInstance -ClassName Win32_ComputerSystem).TotalPhysicalMemory / 1024 / 1024
if ($Env:NodeType -eq 'headnode') {
$JobManagerMaximumMemoryMB = 1024 + 5 * $MaxNodes * $WorkersPerNode
if ($JobManagerMaximumMemoryMB -gt $MemoryMB / 2) {
$JobManagerMaximumMemoryMB = [int]($MemoryMB / 2)
}
Set-MJSParams -DefFile $Env:MJSDefFile -ParameterName 'JOB_MANAGER_MAXIMUM_MEMORY' -ParameterValue "${JobManagerMaximumMemoryMB}m"
}
else {
$WorkerMaximumMemoryMB = 1024 + 64 * $WorkersPerNode
if ($WorkerMaximumMemoryMB -gt $MemoryMB / 4) {
$WorkerMaximumMemoryMB = [int]($MemoryMB / 4)
}
Set-MJSParams -DefFile $Env:MJSDefFile -ParameterName 'WORKER_MAXIMUM_MEMORY' -ParameterValue "${WorkerMaximumMemoryMB}m"
}
}
function Set-MJSAutoScalingParams {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', 'Set-MJSAutoScalingParams')]
param (
[Parameter(Mandatory = $true)]
[string] $EnableAutoscaling,
[Parameter(Mandatory = $true)]
[string] $MATLABRelease,
[Parameter(Mandatory = $true)]
[int] $WorkersPerNode,
[Parameter(Mandatory = $true)]
[int] $MaxNodes
)
# Initialize the autoscaling flag
$AutoscalingFlag = $false
if ($EnableAutoscaling -eq 'Yes') {
if ($MATLABRelease -gt 'R2021b') {
$AutoscalingFlag = $true
Set-MJSParams -DefFile $Env:MJSDefFile -ParameterName 'MAX_WINDOWS_WORKERS' -ParameterValue "$(1*$MaxNodes*$WorkersPerNode)"
}
else {
Write-Output 'WARNING: Auto-Resizing is only available for R2022a and later'
}
}
# Apply the autoscaling setting to the cluster management data file
$ClusterManagementProgramData = Get-Content -Path $Env:ClusterManagementDataFile -Raw | ConvertFrom-Json
$ClusterManagementProgramData.config.autoscaling_enabled = $AutoscalingFlag
$ClusterManagementProgramData | ConvertTo-Json | Set-Content -Path $Env:ClusterManagementDataFile
}
function Set-MJSSchedulingAlgorithm {
param (
[Parameter(Mandatory = $true)]
[string] $MATLABRelease,
[Parameter(Mandatory = $true)]
[string] $SchedulingAlgorithm
)
if ($MATLABRelease -gt 'R2023a') {
Set-MJSParams -DefFile $Env:MJSDefFile -ParameterName 'SCHEDULING_ALGORITHM' -ParameterValue "$SchedulingAlgorithm"
}
else {
Write-Output 'WARNING: Selecting the scheduling algorithm is only available for R2023b and later'
}
}
function Edit-MJSDef {
if ($Env:NodeType -eq 'headnode') {
# Hostname of the job manager.
$MJSHostname = $Env:HeadnodeHostname
}
else {
# Hostname of the worker node
$MJSHostname = $Env:LocalHostname
}
$parameters = @{
'CHECKPOINTBASE' = $Env:CheckpointRoot
'USE_SECURE_COMMUNICATION' = 'true'
'REQUIRE_CLIENT_CERTIFICATE' = 'true'
'HOSTNAME' = $MJSHostname
'SHARED_SECRET_FILE' = $Env:SecretFile
}
foreach ($param in $parameters.GetEnumerator()) {
Set-MJSParams -DefFile $Env:MJSDefFile -ParameterName $param.Key -ParameterValue $param.Value
}
# Set Cluster Command Verification
# https://www.mathworks.com/help/matlab-parallel-server/set-matlab-job-scheduler-cluster-security.html#mw_ca721746-0154-4d20-9a6e-753bed4d4058
# For releases MATLAB R2023a and later, this variable makes the mjs service verify each command with the secret file before execution.
if ($Env:MATLABRelease -ge 'R2023a') {
Set-MJSParams -DefFile $Env:MJSDefFile -ParameterName 'REQUIRE_SCRIPT_VERIFICATION' -ParameterValue 'true'
}
# Use a license that is managed online.
if (-not $Env:MLMLicenseFile) {
Set-MJSParams -DefFile $Env:MJSDefFile -ParameterName 'USE_ONLINE_LICENSING' -ParameterValue 'true'
}
# Set MATLAB Job Scheduler Cluster Security
# https://www.mathworks.com/help/matlab-parallel-server/set-matlab-job-scheduler-cluster-security.html
Set-MJSSecurityParams -SecurityLevel $Env:SecurityLevel -MJSAdminPasswordFile $Env:MJSAdminPasswordFile
# Increase heap memory available to MJS
# https://www.mathworks.com/help/matlab-parallel-server/customize-startup-parameters.html
Set-MJSHeapMemory -WorkersPerNode $Env:WorkersPerNode -MaxNodes $Env:MaxNodes
# Set up MJS Cluster for Auto-Resizing
# https://www.mathworks.com/help/matlab-parallel-server/set-up-your-mjs-cluster-for-resizing.html
if ($Env:NodeType -eq 'headnode') {
Set-MJSAutoScalingParams -EnableAutoscaling $Env:EnableAutoscaling -MATLABRelease $Env:MATLABRelease -WorkersPerNode $Env:WorkersPerNode -MaxNodes $Env:MaxNodes
}
# Set scheduling algorithm for the job manager
if ($Env:SchedulingAlgorithm) {
Set-MJSSchedulingAlgorithm -MATLABRelease $Env:MATLABRelease -SchedulingAlgorithm $Env:SchedulingAlgorithm
}
}
try {
Edit-MJSDef
}
catch {
$ScriptPath = $MyInvocation.MyCommand.Path
Write-Output "ERROR - An error occurred while running script '50_Edit-MJS-Def': $ScriptPath. Error: $_"
throw
}