-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
182 lines (171 loc) · 7.76 KB
/
Program.cs
File metadata and controls
182 lines (171 loc) · 7.76 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
// Copyright (c) 2025 Michael Ivertowski
// Licensed under the MIT License. See LICENSE file in the project root for license information.
using DotCompute.Backends.CUDA.ErrorHandling;
using DotCompute.Backends.CUDA.Native.Exceptions;
using DotCompute.Backends.CUDA.Types.Native;
namespace DotCompute.Samples.ErrorRecovery;
/// <summary>
/// Error-recovery sample - demonstrates how to use the CudaException classification
/// model to drive a simple retry policy. The classification is the same one Ring Kernels
/// and IComputeOrchestrator use under the covers; this sample shows the call site
/// directly so you can see how it works without a real GPU.
///
/// Every CUDA error maps to exactly one of four classes:
/// Transient - retry without changing state (NotReady, Timeout, ...)
/// Resource - free memory / wait, then retry (OOM, OutOfResources, ...)
/// Programmer - caller bug, do not retry (InvalidValue, NotSupported, ...)
/// Fatal - device is hosed, fail fast (IllegalAddress, NoDevice, ...)
///
/// This sample runs on any machine - no GPU required. It fabricates CudaException
/// instances and walks them through a retry policy so you can see the decisions the
/// framework makes without triggering real GPU faults.
/// </summary>
internal static class Program
{
private static async Task<int> Main()
{
Console.WriteLine("===========================================================");
Console.WriteLine(" DotCompute Error Recovery - Classification & Retry Demo");
Console.WriteLine("===========================================================");
Console.WriteLine();
PrintClassificationTable();
// Scenario 1: A transient error recovers on the first retry.
Console.WriteLine("-- Scenario 1: transient error (cudaErrorNotReady) --");
var attemptCounter1 = 0;
var result1 = await ExecuteWithRetryAsync(
operationName: "LaunchVectorAdd",
maxAttempts: 3,
operation: attempt =>
{
attemptCounter1 = attempt;
if (attempt == 1)
{
throw new CudaException(
"Stream not ready on first attempt",
CudaError.NotReady);
}
return "Kernel completed";
});
Console.WriteLine($"Result after {attemptCounter1} attempt(s): {result1}");
Console.WriteLine();
// Scenario 2: A resource error - caller drains caches between retries.
Console.WriteLine("-- Scenario 2: resource error (cudaErrorMemoryAllocation) --");
var cacheSize = 4;
var attemptCounter2 = 0;
var result2 = await ExecuteWithRetryAsync(
operationName: "AllocateHugeBuffer",
maxAttempts: 6,
operation: attempt =>
{
attemptCounter2 = attempt;
if (cacheSize > 0)
{
cacheSize--;
throw new CudaException(
$"cudaMalloc failed (attempt {attempt}, cache still held {cacheSize + 1} blocks)",
CudaError.MemoryAllocation);
}
return "Allocation succeeded after cache drain";
},
onResource: () =>
{
Console.WriteLine($" [recovery] freeing 1 cached buffer (cache size now {cacheSize})");
});
Console.WriteLine($"Result after {attemptCounter2} attempt(s): {result2}");
Console.WriteLine();
// Scenario 3: A programmer error - the retry loop must NOT retry.
Console.WriteLine("-- Scenario 3: programmer error (cudaErrorInvalidValue) --");
try
{
_ = await ExecuteWithRetryAsync<string>(
operationName: "LaunchBadArgs",
maxAttempts: 5,
operation: _ => throw new CudaException(
"Negative kernel argument passed to launch",
CudaError.InvalidValue));
}
catch (CudaException ex)
{
Console.WriteLine($"Surfaced to caller (correct): {ex.ErrorCode} - {ex.Message}");
}
Console.WriteLine();
// Scenario 4: A fatal error - circuit-break, do NOT retry.
Console.WriteLine("-- Scenario 4: fatal error (cudaErrorIllegalAddress) --");
try
{
_ = await ExecuteWithRetryAsync<string>(
operationName: "BrokenKernel",
maxAttempts: 5,
operation: _ => throw new CudaException(
"Kernel wrote to unmapped address",
CudaError.IllegalAddress));
}
catch (CudaException ex)
{
Console.WriteLine($"Surfaced to caller (circuit-breaker open): {ex.ErrorCode} - {ex.Message}");
Console.WriteLine(" Classification: " + ex.Classification);
Console.WriteLine(" IsFatal: " + ex.IsFatal);
Console.WriteLine(" IsRetryable: " + ex.IsRetryable);
}
Console.WriteLine();
Console.WriteLine("Done. See CudaErrorClassification for the full error->class mapping.");
return 0;
}
/// <summary>
/// Minimal retry policy driven entirely by CudaException classification.
/// Real production code would add exponential backoff, jitter, and circuit-breaker
/// state; this version is deliberately tiny so you can read it top-to-bottom quickly.
/// </summary>
private static async Task<T> ExecuteWithRetryAsync<T>(
string operationName,
int maxAttempts,
Func<int, T> operation,
Action? onResource = null)
{
for (var attempt = 1; attempt <= maxAttempts; attempt++)
{
try
{
var result = operation(attempt);
if (attempt > 1)
{
Console.WriteLine($" [{operationName}] succeeded on attempt {attempt}");
}
return result;
}
catch (CudaException ex) when (ex.IsRecoverable)
{
Console.WriteLine($" [{operationName}] attempt {attempt} transient: {ex.ErrorCode}; retrying");
await Task.Delay(TimeSpan.FromMilliseconds(10 * attempt));
}
catch (CudaException ex) when (ex.IsResourceError)
{
Console.WriteLine($" [{operationName}] attempt {attempt} resource: {ex.ErrorCode}; invoking cleanup");
onResource?.Invoke();
await Task.Delay(TimeSpan.FromMilliseconds(10 * attempt));
}
catch (CudaException ex) when (ex.IsFatal)
{
Console.WriteLine($" [{operationName}] attempt {attempt} fatal: {ex.ErrorCode}; failing fast");
throw;
}
catch (CudaException ex)
{
Console.WriteLine($" [{operationName}] attempt {attempt} programmer: {ex.ErrorCode}; failing fast");
throw;
}
}
throw new InvalidOperationException(
$"{operationName} exhausted {maxAttempts} attempts without success");
}
private static void PrintClassificationTable()
{
Console.WriteLine("CudaErrorClass mapping (from DotCompute.Backends.CUDA.ErrorHandling):");
Console.WriteLine(" NotReady -> " + CudaError.NotReady.Classify());
Console.WriteLine(" MemoryAllocation -> " + CudaError.MemoryAllocation.Classify());
Console.WriteLine(" InvalidValue -> " + CudaError.InvalidValue.Classify());
Console.WriteLine(" IllegalAddress -> " + CudaError.IllegalAddress.Classify());
Console.WriteLine(" NoDevice -> " + CudaError.NoDevice.Classify());
Console.WriteLine();
}
}