Skip to content

Commit

Permalink
stabilize ExecDebug
Browse files Browse the repository at this point in the history
  • Loading branch information
SunSerega committed Dec 8, 2023
1 parent 59e964f commit b5c9144
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 27 deletions.
46 changes: 38 additions & 8 deletions Modules.Packed/OpenCLABC.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1492,9 +1492,12 @@ EventRetainReleaseData = record
private static procedure RegisterExecCacheTry(command: object; is_new: boolean; descr: string) :=
ExecCacheTries.GetOrAdd(MakeName(command), name->new ConcurrentQueue<(boolean,string)>).Enqueue((is_new,descr));

private static procedure DisposeAllCommands;
public static procedure ReportExecCache(otp: System.IO.TextWriter := Console.Out) :=
lock otp do
begin
DisposeAllCommands;

otp.WriteLine(System.Environment.StackTrace);

foreach var kvp in ExecCacheTries.OrderBy(kvp->kvp.Key) do
Expand Down Expand Up @@ -18886,9 +18889,10 @@ CLKernelArgPrivateCommon<TInp> = record
ExecCommandCLKernelCacheEntry = record
k: CLKernel;
cache: CLKernelArgCache;
last_use: DateTime;
last_use: TimeSpan;
static last_use_timer := Stopwatch.StartNew;

procedure Bump := last_use := DateTime.Now;
procedure Bump := last_use := last_use_timer.Elapsed;

procedure TryRelease({$ifdef ExecDebug}command: object{$endif}) := if k<>nil then
begin
Expand Down Expand Up @@ -18943,19 +18947,30 @@ ExecCommandCLKernelCache = record
end;
end;

public procedure Release({$ifdef ExecDebug}command: object{$endif}) :=
for var i := 0 to cache_size-1 do
data[i].TryRelease({$ifdef ExecDebug}command{$endif});
public procedure Release({$ifdef ExecDebug}command: object{$endif});
begin
if data=nil then raise new OpenCLABCInternalException($'');
for var i := 0 to cache_size-1 do
data[i].TryRelease({$ifdef ExecDebug}command{$endif});
data := nil;
data_ind := nil;
end;

end;

EnqueueableExecCommand = abstract class(GPUCommand<CLKernel>)
EnqueueableExecCommand = abstract class(GPUCommand<CLKernel>, IDisposable)
private args: array of CLKernelArg;
private const_args_setters: array of CLKernelArgSetter;
private args_c, args_non_const_c: integer;
{$ifdef ExecDebug}
private static All := new ConcurrentBag<EnqueueableExecCommand>;
{$endif ExecDebug}

protected constructor(args: array of CLKernelArg);
begin
{$ifdef ExecDebug}
All.Add(self);
{$endif ExecDebug}
args := args.ToArray;
self.args := args;
self.const_args_setters := new CLKernelArgSetter[args.Length];
Expand Down Expand Up @@ -19095,11 +19110,26 @@ ExecCommandCLKernelCache = record
if post_enq_act<>nil then Result.AddAction(post_enq_act);
end;

protected procedure Finalize; override :=
k_cache.Release({$ifdef ExecDebug}self{$endif});
public procedure Dispose;
begin
if k_cache.data=nil then exit;
k_cache.Release({$ifdef ExecDebug}self{$endif});
GC.SuppressFinalize(self);
end;
protected procedure Finalize; override := Dispose;

end;

{$ifdef ExecDebug}
static procedure ExecDebug.DisposeAllCommands :=
while true do
begin
var c: EnqueueableExecCommand;
if not EnqueueableExecCommand.All.TryTake(c) then break;
c.Dispose;
end;
{$endif ExecDebug}

{$endregion ExecCommand}

{$region GetCommand}
Expand Down
46 changes: 38 additions & 8 deletions Modules/OpenCLABC.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1380,9 +1380,12 @@ EventRetainReleaseData = record
private static procedure RegisterExecCacheTry(command: object; is_new: boolean; descr: string) :=
ExecCacheTries.GetOrAdd(MakeName(command), name->new ConcurrentQueue<(boolean,string)>).Enqueue((is_new,descr));

private static procedure DisposeAllCommands;
public static procedure ReportExecCache(otp: System.IO.TextWriter := Console.Out) :=
lock otp do
begin
DisposeAllCommands;

otp.WriteLine(System.Environment.StackTrace);

foreach var kvp in ExecCacheTries.OrderBy(kvp->kvp.Key) do
Expand Down Expand Up @@ -10324,9 +10327,10 @@ CLKernelArgPrivateCommon<TInp> = record
ExecCommandCLKernelCacheEntry = record
k: CLKernel;
cache: CLKernelArgCache;
last_use: DateTime;
last_use: TimeSpan;
static last_use_timer := Stopwatch.StartNew;

procedure Bump := last_use := DateTime.Now;
procedure Bump := last_use := last_use_timer.Elapsed;

procedure TryRelease({$ifdef ExecDebug}command: object{$endif}) := if k<>nil then
begin
Expand Down Expand Up @@ -10381,19 +10385,30 @@ ExecCommandCLKernelCache = record
end;
end;

public procedure Release({$ifdef ExecDebug}command: object{$endif}) :=
for var i := 0 to cache_size-1 do
data[i].TryRelease({$ifdef ExecDebug}command{$endif});
public procedure Release({$ifdef ExecDebug}command: object{$endif});
begin
if data=nil then raise new OpenCLABCInternalException($'');
for var i := 0 to cache_size-1 do
data[i].TryRelease({$ifdef ExecDebug}command{$endif});
data := nil;
data_ind := nil;
end;

end;

EnqueueableExecCommand = abstract class(GPUCommand<CLKernel>)
EnqueueableExecCommand = abstract class(GPUCommand<CLKernel>, IDisposable)
private args: array of CLKernelArg;
private const_args_setters: array of CLKernelArgSetter;
private args_c, args_non_const_c: integer;
{$ifdef ExecDebug}
private static All := new ConcurrentBag<EnqueueableExecCommand>;
{$endif ExecDebug}

protected constructor(args: array of CLKernelArg);
begin
{$ifdef ExecDebug}
All.Add(self);
{$endif ExecDebug}
args := args.ToArray;
self.args := args;
self.const_args_setters := new CLKernelArgSetter[args.Length];
Expand Down Expand Up @@ -10533,11 +10548,26 @@ ExecCommandCLKernelCache = record
if post_enq_act<>nil then Result.AddAction(post_enq_act);
end;

protected procedure Finalize; override :=
k_cache.Release({$ifdef ExecDebug}self{$endif});
public procedure Dispose;
begin
if k_cache.data=nil then exit;
k_cache.Release({$ifdef ExecDebug}self{$endif});
GC.SuppressFinalize(self);
end;
protected procedure Finalize; override := Dispose;

end;

{$ifdef ExecDebug}
static procedure ExecDebug.DisposeAllCommands :=
while true do
begin
var c: EnqueueableExecCommand;
if not EnqueueableExecCommand.All.TryTake(c) then break;
c.Dispose;
end;
{$endif ExecDebug}

{$endregion ExecCommand}

{$region GetCommand}
Expand Down
46 changes: 38 additions & 8 deletions Packing/Descriptions/OpenCLABC.predoc
Original file line number Diff line number Diff line change
Expand Up @@ -1390,9 +1390,12 @@ type
private static procedure RegisterExecCacheTry(command: object; is_new: boolean; descr: string) :=
ExecCacheTries.GetOrAdd(MakeName(command), name->new ConcurrentQueue<(boolean,string)>).Enqueue((is_new,descr));

private static procedure DisposeAllCommands;
public static procedure ReportExecCache(otp: System.IO.TextWriter := Console.Out) :=
lock otp do
begin
DisposeAllCommands;

otp.WriteLine(System.Environment.StackTrace);

foreach var kvp in ExecCacheTries.OrderBy(kvp->kvp.Key) do
Expand Down Expand Up @@ -17820,9 +17823,10 @@ type
ExecCommandCLKernelCacheEntry = record
k: CLKernel;
cache: CLKernelArgCache;
last_use: DateTime;
last_use: TimeSpan;
static last_use_timer := Stopwatch.StartNew;

procedure Bump := last_use := DateTime.Now;
procedure Bump := last_use := last_use_timer.Elapsed;

procedure TryRelease({$ifdef ExecDebug}command: object{$endif}) := if k<>nil then
begin
Expand Down Expand Up @@ -17877,19 +17881,30 @@ type
end;
end;

public procedure Release({$ifdef ExecDebug}command: object{$endif}) :=
for var i := 0 to cache_size-1 do
data[i].TryRelease({$ifdef ExecDebug}command{$endif});
public procedure Release({$ifdef ExecDebug}command: object{$endif});
begin
if data=nil then raise new OpenCLABCInternalException($'');
for var i := 0 to cache_size-1 do
data[i].TryRelease({$ifdef ExecDebug}command{$endif});
data := nil;
data_ind := nil;
end;

end;

EnqueueableExecCommand = abstract class(GPUCommand<CLKernel>)
EnqueueableExecCommand = abstract class(GPUCommand<CLKernel>, IDisposable)
private args: array of CLKernelArg;
private const_args_setters: array of CLKernelArgSetter;
private args_c, args_non_const_c: integer;
{$ifdef ExecDebug}
private static All := new ConcurrentBag<EnqueueableExecCommand>;
{$endif ExecDebug}

protected constructor(args: array of CLKernelArg);
begin
{$ifdef ExecDebug}
All.Add(self);
{$endif ExecDebug}
args := args.ToArray;
self.args := args;
self.const_args_setters := new CLKernelArgSetter[args.Length];
Expand Down Expand Up @@ -18029,11 +18044,26 @@ type
if post_enq_act<>nil then Result.AddAction(post_enq_act);
end;

protected procedure Finalize; override :=
k_cache.Release({$ifdef ExecDebug}self{$endif});
public procedure Dispose;
begin
if k_cache.data=nil then exit;
k_cache.Release({$ifdef ExecDebug}self{$endif});
GC.SuppressFinalize(self);
end;
protected procedure Finalize; override := Dispose;

end;

{$ifdef ExecDebug}
static procedure ExecDebug.DisposeAllCommands :=
while true do
begin
var c: EnqueueableExecCommand;
if not EnqueueableExecCommand.All.TryTake(c) then break;
c.Dispose;
end;
{$endif ExecDebug}

{$endregion ExecCommand}

{$region GetCommand}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

procedure Test(inp: CommandQueue<CLKernel>);
begin
var Q := CLKernelCCQ.Create(inp).ThenExec1(1, 0).DiscardResult;
var Q := inp.MakeCCQ.ThenExec1(1, 0).DiscardResult;
CLContext.Default.SyncInvoke(Q);
CLContext.Default.SyncInvoke(Q);
end;
Expand All @@ -15,4 +15,6 @@ procedure Test(inp: CommandQueue<CLKernel>);
Test(new ParameterQueue<CLKernel>('k2', code['k2']));
Test(HFQ(()->code['k3'], false));

ExecDebug.ReportExecCache;
{$ifdef ForceMaxDebug}
ExecDebug.ReportExecCache;
{$endif ForceMaxDebug}
2 changes: 1 addition & 1 deletion Tests/Tester.pas
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@

if 'OpenCLABC' in allowed_modules then
begin
yield (lvl+1, 'CLContext', $'{TimeToText(cl_context_gen_time)} ({TimeToText(get_cl_context_gen_time_sum)})');
yield (lvl+1, 'CLContext', $'{TimeToText(cl_context_gen_time)} ({get_cl_context_gen_time_sum.TotalSeconds:N4})');
yield cl_context_gen_comp.MakeLogLines(lvl+2, 'Comp').Single;
yield cl_context_gen_exec.MakeLogLines(lvl+2, 'Exec').Single;
end;
Expand Down

0 comments on commit b5c9144

Please sign in to comment.