-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlotterProcess.cs
More file actions
270 lines (233 loc) · 8.8 KB
/
PlotterProcess.cs
File metadata and controls
270 lines (233 loc) · 8.8 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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
namespace PoCXPlotterGui
{
/// <summary>
/// Configuration for plotting operation
/// </summary>
public class PlotterConfig
{
public string AccountAddress { get; set; }
public decimal Warps { get; set; }
public bool PlotMaximumSize { get; set; }
public List<string> OutputPaths { get; set; }
public int NumFiles { get; set; }
public bool UseMemoryLimit { get; set; }
public int MemoryLimitMiB { get; set; }
public bool DirectIo { get; set; }
public bool LowPriority { get; set; }
public bool Benchmark { get; set; }
public bool ZeroCopyBuffers { get; set; }
public bool UseCustomEncounters { get; set; }
public decimal Encounters { get; set; }
public decimal CompressionRatio { get; set; }
public bool UseCustomWorkGroups { get; set; }
public decimal WorkGroups { get; set; }
public List<DeviceConfig> Devices { get; set; }
public bool UseOpenCl { get; set; }
public bool UseFixedSeed { get; set; }
public string Seed { get; set; }
public PlotterConfig()
{
OutputPaths = new List<string>();
Devices = new List<DeviceConfig>();
}
}
/// <summary>
/// Device configuration for plotting
/// </summary>
public class DeviceConfig
{
public bool IsCpu { get; set; }
public string GpuId { get; set; }
public int ThreadCount { get; set; }
}
/// <summary>
/// Manages the plotter subprocess lifecycle
/// </summary>
public class PlotterProcess
{
private Process _process;
private ProgressParser _progressParser;
private AutoResetEvent _processCompleteEvent;
public event EventHandler ProcessExited;
public event EventHandler<HashProgressEventArgs> HashProgressUpdated;
public event EventHandler<WriteProgressEventArgs> WriteProgressUpdated;
public event EventHandler<OutputEventArgs> StandardOutputReceived;
public event EventHandler<OutputEventArgs> ErrorOutputReceived;
public bool IsRunning => _process != null && !_process.HasExited;
/// <summary>
/// Starts the plotting process
/// </summary>
public void Start(PlotterConfig config)
{
if (IsRunning)
throw new InvalidOperationException("Plotter process is already running");
_processCompleteEvent = new AutoResetEvent(false);
// Start in background thread
ThreadPool.QueueUserWorkItem(state =>
{
RunProcess(config);
_processCompleteEvent.Set();
});
}
/// <summary>
/// Stops the plotting process
/// </summary>
public void Stop()
{
try
{
if (_process != null && !_process.HasExited)
{
_process.Kill();
}
}
catch (Exception)
{
// Process may have already exited
}
}
/// <summary>
/// Waits for the plotting process to complete
/// </summary>
public void Wait()
{
_processCompleteEvent?.WaitOne();
}
/// <summary>
/// Runs the plotter process
/// </summary>
private void RunProcess(PlotterConfig config)
{
try
{
string executable = config.UseOpenCl ? "pocx_plotter_gpu.exe" : "pocx_plotter_cpu.exe";
string arguments = BuildCommandLine(config);
using (_process = new Process())
{
_process.StartInfo = new ProcessStartInfo(executable, arguments)
{
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
WorkingDirectory = Environment.CurrentDirectory,
CreateNoWindow = true
};
_process.EnableRaisingEvents = true;
_process.OutputDataReceived += OnOutputDataReceived;
_process.ErrorDataReceived += OnErrorDataReceived;
_process.Exited += OnProcessExited;
_process.Start();
// Create progress parser with process start time
_progressParser = new ProgressParser(_process.StartTime);
_progressParser.HashProgressUpdated += (s, e) => HashProgressUpdated?.Invoke(this, e);
_progressParser.WriteProgressUpdated += (s, e) => WriteProgressUpdated?.Invoke(this, e);
_progressParser.StandardOutputReceived += (s, e) => StandardOutputReceived?.Invoke(this, e);
_progressParser.ErrorOutputReceived += (s, e) => ErrorOutputReceived?.Invoke(this, e);
_process.BeginOutputReadLine();
_process.BeginErrorReadLine();
_process.WaitForExit();
}
}
catch (Exception ex)
{
// Log process start failure - raise error event
ErrorOutputReceived?.Invoke(this, new OutputEventArgs { Data = $"Failed to start plotter: {ex.Message}" });
}
}
/// <summary>
/// Handles process standard output
/// </summary>
private void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null && _progressParser != null)
{
_progressParser.ParseStandardOutput(e.Data);
}
}
/// <summary>
/// Handles process error output
/// </summary>
private void OnErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null && _progressParser != null)
{
_progressParser.ParseErrorOutput(e.Data);
}
}
/// <summary>
/// Handles process exit
/// </summary>
private void OnProcessExited(object sender, EventArgs e)
{
ProcessExited?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Gets the full command line that would be executed (executable + arguments)
/// </summary>
public static string GetFullCommandLine(PlotterConfig config)
{
string executable = config.UseOpenCl ? "pocx_plotter_gpu.exe" : "pocx_plotter_cpu.exe";
string arguments = BuildCommandLine(config);
return $"{executable} {arguments}";
}
/// <summary>
/// Builds command-line arguments for the plotter
/// </summary>
private static string BuildCommandLine(PlotterConfig config)
{
var args = new List<string>();
// Required arguments
args.Add($"-i {config.AccountAddress}");
// Only add -w parameter if not plotting maximum size
if (!config.PlotMaximumSize)
{
args.Add($"-w {config.Warps}");
}
// Output paths
foreach (var path in config.OutputPaths)
{
args.Add($"-p {path}");
}
args.Add($"-n {config.NumFiles}");
// Fixed seed
if (config.UseFixedSeed && !string.IsNullOrEmpty(config.Seed))
args.Add($"-s {config.Seed}");
// Optional arguments
if (config.UseMemoryLimit)
args.Add($"-m {config.MemoryLimitMiB}MiB");
if (config.UseCustomEncounters)
args.Add($"-e {config.Encounters}");
if (!config.DirectIo)
args.Add("-d");
if (config.LowPriority)
args.Add("-l");
if (config.Benchmark)
args.Add("-b");
if (config.ZeroCopyBuffers && config.UseOpenCl)
args.Add("-z");
if (config.UseCustomWorkGroups && config.UseOpenCl)
args.Add($"-k {config.WorkGroups}");
args.Add($"-x {config.CompressionRatio}");
args.Add("--line-progress");
// Devices
foreach (var device in config.Devices)
{
if (device.IsCpu)
{
args.Add($"-c {device.ThreadCount}");
}
else
{
args.Add($"-g {device.GpuId}:{device.ThreadCount}");
}
}
return string.Join(" ", args);
}
}
}