-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMissionStatusChecker.wl
306 lines (239 loc) · 10.2 KB
/
MissionStatusChecker.wl
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
(* ::Package:: *)
MemoryUsedLimit=Infinity
ThreadUsedLimit=Infinity
(*why?*)
commandLineMode=True
If[commandLineMode,
packagePath=DirectoryName[$InputFileName];
AbsMissionInput=$CommandLine[[-1]];
workingPath=DirectoryName[AbsMissionInput];
missionInput=FileNameSplit[AbsMissionInput][[-1]];
(*workingPath=Directory[]<>"/";
missionInput=$CommandLine[[-1]];*)
MathematicaCommand=Import[packagePath<>"/preload/MathematicaCommand.txt"];
ShellProcessor=Import[packagePath<>"/preload/ShellProcessor.txt"];
,
Print["WARNING: program is not running in command line mode!"];
workingPath=NotebookDirectory[];
missionInput="example.txt"
]
(*AppendTo[$Path,workingPath];*)
If[Get[packagePath<>"default_settings.txt"]===$Failed,Exit[0]]
If[Get[workingPath<>missionInput]===$Failed,Print["Unable to open config file "<>workingPath<>missionInput<>". Exiting."];Exit[]]
If[Get[kinematicsFile]===$Failed,Print["Unable to open kinematics file "<>kinematicsFile<>". Exiting."];Exit[]]
(*TargetIntegrals=Get[targetIntegralsFile]
If[TargetIntegrals===$Failed,Print["Unable to open target intergals file "<>targetIntegralsFile<>". Exiting."];Exit[]]*)
(*renaming the setting, because NeatIBP... actually, dose not perform "reduction" by default*)
If[ValueQ[ReductionOutputName],
If[ReductionOutputName=!=OutputName,
If[OutputName==="Untitled",
ReductionOutputName=ReductionOutputName;
(*use ReductionOutputName*)
,
ReductionOutputName=OutputName
]
]
,
ReductionOutputName=OutputName
]
If[CutIndices==="spanning cuts",
(*PrintAndLog[
"!!![Notice]: the config setting CutIndices=\"spanning cuts\" is an out-of-date gramma since v1.1.0.0.\n",
"It is still supported, but it is recommended to use the equivalent, new gramma: \n",
"\tCutIndices={};\n",
"\tSpanningCutsMode=True;"
];*)(*not print in this wl*)
CutIndices={};
SpanningCutsMode=True;
]
If[outputPath===Automatic,
outputPath=workingPath<>"outputs/"<>ReductionOutputName<>"/";
]
If[Intersection[StringSplit[outputPath,""],{" ","\t","\n","?","@","#","$","*","&","(",")","\"","\'","|"}]=!={},
Print["Path "<>outputPath<>" is illegal. Exiting."];
Exit[0];
]
If[StringSplit[outputPath,""][[-1]]=!="/",outputPath=outputPath<>"/"]
missionStatusFolder=outputPath<>"tmp/mission_status/"
SectorNumberToSectorIndex//ClearAll
SectorNumber//ClearAll
SectorNumberToSectorIndex[num_]:=IntegerDigits[num,2,Length[Propagators]]//Reverse
SectorNumber[sec_]:=FromDigits[sec//Reverse,2];
SectorOrdering//ClearAll
SectorOrdering[sector_]:={Total[sector],SectorNumber[sector]}
(*Add1[sector_,position_]:=Module[{result=sector,i},
For[i=1,i<=Length[position],i++,
result[[position[[i]]]]=1;
];
result
]
SuperSectors[sector_]:=Module[{addingPositions,possibleSupersectors},
addingPositions=DeleteCases[Subsets[Flatten[Position[sector,0]]],{}];
possibleSupersectors=Add1[sector,#]&/@addingPositions;
SortBy[Intersection[NonZeroSectors,possibleSupersectors],SectorOrdering]
]*)
superOrSourceSectors=Get[outputPath<>"tmp/superOrSourceSectors.txt"];
SuperSectors[sector_]:=sector/.superOrSourceSectors
(*To get rid of some abnormal large values in a list to make sure all values are within [0, abnormalBound*current mean value]*)
PoissonWinzorize[list_,abnormalBound_]:=Module[{oldList,newList},
oldList=list;
While[True,
newList=Select[oldList,#<=abnormalBound*Mean[oldList]&];
If[Length[newList]==Length[oldList],Break[];];
oldList=newList;
];
newList
]
MissionLimitByMemory[number_,maxUsage_]:=Module[{MUH,AMUH},
(*MUH:memory usage history*)
If[!FileExistsQ[outputPath<>"tmp/log.txt"],Return[Infinity]];
MUH=ToExpression[StringSplit[#," "][[-2]]]&/@StringSplit[Import[outputPath<>"tmp/log.txt"],"\n"];
If[MUH==={},Return[Infinity]];
MUH=PoissonWinzorize[MUH,3];(*The value "3" is put in by hand, can be modified*)
If[Length[MUH]<number,MUH=Join[MUH,Table[Round[N[Total[MUH]/Length[MUH]]],number-Length[MUH]]]];
AMUH=MUH//Sort//Reverse//Accumulate;
Length[Select[AMUH,#<=maxUsage&]]
]
(*missionStatus=((ToExpression[StringReplace[FileNameSplit[#][[-1]],".txt"->""]]//SectorNumberToSectorIndex)->Get[#])&/@FileNames[All,missionStatusFolder]
missionReadyToCompute=(
SortBy[Select[missionStatus,#[[2]]==="ReadyToCompute"&],SectorOrdering[#[[1]]]&]//Reverse
)[[All,1]];
MissionLimitByMemory[Length[missionReadyToCompute],Round[MemoryAvailable[]/(1024^2)]-MinMemoryReserved]*)
TimeString[]:=StringRiffle[#[[1;;3]],"."]<>" "<>StringRiffle[#[[4;;6]],":"]&[(ToString[Round[#]]&/@FromAbsoluteTime[AbsoluteTime[]][[1,1;;6]])]
ProcessRunningQ[commandLine_]:=Module[{ps},
ps=Select[
StringSplit[RunProcess[StringSplit["ps -ef"]]["StandardOutput"],"\n"],
StringContainsQ[#,commandLine]&
];
ps=!={}
]
If[MathKernelLimit<Infinity&&IsASpanningCutsSubMission,
WKFolder=outputPath<>"tmp/worker_kernels/";
SubmittingKernelsFolder=WKFolder<>"submitting_kernels/";
OccupiedKernelsFolder=WKFolder<>"occupied_kernels/";
RecievedKernelsFolder=WKFolder<>"recieved_kernels/";
]
reportClock=0
While[True,
Pause[0.5];
missionStatus=(
(
ToExpression[StringReplace[FileNameSplit[#][[-1]],".txt"->""]]//SectorNumberToSectorIndex
)->Get[#]
)&/@FileNames[All,missionStatusFolder];
NonZeroSectors=missionStatus[[All,1]];
If[Complement[Union[missionStatus[[All,2]]],{"ComputationFinished"(*,"Computing"*)}]==={},
script="echo \"finished!\"\n";
Break[];
];
missionReportingFinished=(
SortBy[
Select[
missionStatus,
StringSplit[#[[2]],"\n"][[1]]==="ReportingFinished"&
],
SectorOrdering[#[[1]]]&
]//Reverse
)[[All,1]];
While[True,
If[missionReportingFinished==={},Break[]];
actuallyFinishedMissions=Select[
missionReportingFinished,
Not[ProcessRunningQ[StringSplit[#/.missionStatus,"\n"][[2]]]]&
];
missionReportingFinished=Complement[missionReportingFinished,actuallyFinishedMissions];
Export[
missionStatusFolder<>ToString[#//SectorNumber]<>".txt",
"ComputationFinished"//InputForm//ToString
]&/@actuallyFinishedMissions;
If[MathKernelLimit<Infinity&&IsASpanningCutsSubMission,
occupiedKernels=FileNames[All,OccupiedKernelsFolder];
If[Length[occupiedKernels]<Length[actuallyFinishedMissions],
Print["echo \"MissionStatusChecker: ERROR20241112 at "<>outputPath<>"\""];
Run["echo \""<>"MissionStatusChecker: ERROR20241112 at "<>outputPath<>"\" >> "<>outputPath<>"tmp/log3.txt"];
Exit[1];
(*I once met a case that we, suspeciously, reached here... at 2024.11.24*)
];
freeKernels=occupiedKernels[[;;Length[actuallyFinishedMissions]]];
Run["mv "<>#<>" "<>SubmittingKernelsFolder]&/@freeKernels
]
];
missionStatus=(
(
ToExpression[StringReplace[FileNameSplit[#][[-1]],".txt"->""]]//SectorNumberToSectorIndex
)->Get[#]
)&/@FileNames[All,missionStatusFolder];
missionWaitingSupersectors=(
SortBy[Select[missionStatus,#[[2]]==="WaitingSupersectors"&],SectorOrdering[#[[1]]]&]//Reverse
)[[All,1]];
newReadySectors=Select[missionWaitingSupersectors,DeleteCases[Union[SuperSectors[#]/.missionStatus],"ComputationFinished"]==={}&];
newReadySectorNumbers=SectorNumber/@newReadySectors;
Export[missionStatusFolder<>ToString[#]<>".txt","ReadyToCompute"//InputForm//ToString]&/@newReadySectorNumbers;
(* Run["echo \""<>
TimeString[]<>"\t Mission status checked and possibly changed"<>
"\" >> tmp/log4.txt"
];*)
missionReadyToCompute=(
SortBy[Select[missionStatus,#[[2]]==="ReadyToCompute"&],SectorOrdering[#[[1]]]&]//Reverse
)[[All,1]];
missionComputing=(
SortBy[Select[missionStatus,#[[2]]==="Computing"||#[[2]]==="AboutToCompute"&],SectorOrdering[#[[1]]]&]//Reverse
)[[All,1]];
numOfNewComputingMissions=Min[
Length[missionReadyToCompute],
ThreadUsedLimit-Length[missionComputing],
MathKernelLimit-1-Length[missionComputing],
Max[MissionLimitByMemory[Length[missionReadyToCompute]+Length[missionComputing],MemoryUsedLimit]-Length[missionComputing],0]
(*Max[MissionLimitByMemory[Length[missionReadyToCompute],Round[MemoryAvailable[]/(1024^2)]-MinMemoryReserved]-Length[missionComputing],0]*)
];
If[reportClock==0&&(DebugMode===True),
Run["echo \""<>
TimeString[]<>"\t"<>
ToString[InputForm[{
Length[missionReadyToCompute],
ThreadUsedLimit-Length[missionComputing],
MissionLimitByMemory[Length[missionReadyToCompute]+Length[missionComputing],MemoryUsedLimit]-Length[missionComputing]
(*Max[MissionLimitByMemory[Length[missionReadyToCompute],Round[MemoryAvailable[]/(1024^2)]-MinMemoryReserved]-Length[missionComputing],0]*)
}]]<>
"\" >> "<>outputPath<>"tmp/log4.txt"
]
];
reportClock=Mod[reportClock+1,1000];
If[MathKernelLimit<Infinity&&IsASpanningCutsSubMission,
recievedKernels=FileNames[All,RecievedKernelsFolder];
If[Length[recievedKernels]<numOfNewComputingMissions,
requestedKernels=numOfNewComputingMissions-Length[recievedKernels];
newOccupiedKernels=recievedKernels;
numOfNewComputingMissions=Length[recievedKernels];
,
requestedKernels=0;
newOccupiedKernels=recievedKernels[[;;numOfNewComputingMissions]];
];
uselessKernels=Complement[recievedKernels,newOccupiedKernels];
Run["mv "<>#<>" "<>OccupiedKernelsFolder]&/@newOccupiedKernels;
Run["mv "<>#<>" "<>SubmittingKernelsFolder]&/@uselessKernels;
Export[
WKFolder<>"requested_kernels.txt",
ToString[requestedKernels]
];
];
scriptModifyStatus="";
If[numOfNewComputingMissions>0,
newComputingSectors=missionReadyToCompute[[1;;numOfNewComputingMissions]];
newComputingSectorNumbers=SectorNumber/@newComputingSectors;
Export[missionStatusFolder<>ToString[#]<>".txt","AboutToCompute"//InputForm//ToString]&/@newComputingSectorNumbers;
scriptModifyStatus=StringRiffle[
"echo \\\"Computing\\\" > "<>missionStatusFolder<>ToString[#]<>".txt"&/@newComputingSectorNumbers,
"\n"
];
script=""<>
StringRiffle[MathematicaCommand<>" -script "<>packagePath<>"Analyze_Sector.wl "<>AbsMissionInput<>" "<>ToString[#]<>" "<>outputPath<>" &\n"&/@newComputingSectorNumbers,""]<>
MathematicaCommand<>" -script "<>packagePath<>"AllMissionCompleteQ.wl "<>AbsMissionInput<>" | "<>ShellProcessor<>" &\n"<>
"wait\n";
(*script=StringRiffle[MathematicaCommand<>" -script Analyze_Sector.wl "<>missionInput<>" "<>ToString[#]<>"\n"&/@newReadySectorNumbers,""];*)
Break[];
];
]
script="sleep 1\n"<>scriptModifyStatus<>"\n"<>script<>"\n" (*sleep: to makesure that when script runs, this wl really ends.*)
Run["echo \""<>script<>"\" >> "<>outputPath<>"tmp/log3.txt"]
Print[script]