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 38d573c commit fd058bc
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 35 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
13 changes: 6 additions & 7 deletions Modules/OpenCLABC.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10327,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 @@ -10400,13 +10401,13 @@ ExecCommandCLKernelCache = record
private const_args_setters: array of CLKernelArgSetter;
private args_c, args_non_const_c: integer;
{$ifdef ExecDebug}
private static All := new ConcurrentBag<WeakReference<EnqueueableExecCommand>>;
private static All := new ConcurrentBag<EnqueueableExecCommand>;
{$endif ExecDebug}

protected constructor(args: array of CLKernelArg);
begin
{$ifdef ExecDebug}
All.Add(new WeakReference<EnqueueableExecCommand>(self));
All.Add(self);
{$endif ExecDebug}
args := args.ToArray;
self.args := args;
Expand Down Expand Up @@ -10561,10 +10562,8 @@ ExecCommandCLKernelCache = record
static procedure ExecDebug.DisposeAllCommands :=
while true do
begin
var r: WeakReference<EnqueueableExecCommand>;
if not EnqueueableExecCommand.All.TryTake(r) then break;
var c: EnqueueableExecCommand;
if not r.TryGetTarget(c) then continue;
if not EnqueueableExecCommand.All.TryTake(c) then break;
c.Dispose;
end;
{$endif ExecDebug}
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 @@ -7,19 +7,14 @@ procedure Test(inp: CommandQueue<CLKernel>);
CLContext.Default.SyncInvoke(Q);
end;

procedure TestAll;
begin
var code := CLProgramCode.Create(
'#define K(name) kernel void name(int x) {}'#10
'K(k1) K(k2) K(k3)'
);
Test(code['k1']);
Test(new ParameterQueue<CLKernel>('k2', code['k2']));
Test(HFQ(()->code['k3'], false));
end;
var code := CLProgramCode.Create(
'#define K(name) kernel void name(int x) {}'#10
'K(k1) K(k2) K(k3)'
);
Test(code['k1']);
Test(new ParameterQueue<CLKernel>('k2', code['k2']));
Test(HFQ(()->code['k3'], false));

TestAll;
GC.Collect;
{$ifdef ForceMaxDebug}
ExecDebug.ReportExecCache;
{$endif ForceMaxDebug}

0 comments on commit fd058bc

Please sign in to comment.