GPU stabilization improvements to prevent calculation crashes
This commit is contained in:
1 parent
a338bb0d0b
commit
d0b1639a5d
40 files changed
+2318
-1764
No files matched your search
+175
-192
@@ -99,9 +99,10 @@ public sealed class PrimeTester
|
||||
var gpu = PrimeTesterGpuContextPool.Rent(1);
|
||||
var accelerator = gpu.Accelerator;
|
||||
var stream = gpu.Stream;
|
||||
var kernel = PrimeTesterGpuContextPool.PrimeTesterGpuContextLease.Kernel;
|
||||
var kernel = PrimeTesterGpuContextPool.PrimeTesterGpuContextLease.GetKernel(accelerator);
|
||||
|
||||
ulong value = n;
|
||||
// Span<byte> flag = stackalloc byte[1];
|
||||
byte flag = 0;
|
||||
|
||||
var input = gpu.Input;
|
||||
@@ -166,7 +167,7 @@ public sealed class PrimeTester
|
||||
var gpu = PrimeTesterGpuContextPool.Rent(GpuBatchSize);
|
||||
var accelerator = gpu.Accelerator;
|
||||
var stream = gpu.Stream;
|
||||
var kernel = PrimeTesterGpuContextPool.PrimeTesterGpuContextLease.Kernel;
|
||||
var kernel = PrimeTesterGpuContextPool.PrimeTesterGpuContextLease.GetKernel(accelerator);
|
||||
int totalLength = values.Length;
|
||||
int batchSize = GpuBatchSize;
|
||||
|
||||
@@ -215,83 +216,52 @@ public sealed class PrimeTester
|
||||
|
||||
internal static class PrimeTesterGpuContextPool
|
||||
{
|
||||
private static readonly ConcurrentQueue<PrimeTesterGpuContextLease> Pool = new();
|
||||
[ThreadStatic]
|
||||
private static Queue<PrimeTesterGpuContextLease>? Pool;
|
||||
private static readonly object WarmUpLock = new();
|
||||
private static int WarmedLeaseCount;
|
||||
private static readonly object SharedTablesLock = new();
|
||||
private static readonly Dictionary<Accelerator, SharedHeuristicGpuTables> SharedTables = new(AcceleratorReferenceComparer.Instance);
|
||||
|
||||
private static SharedHeuristicGpuTables RentSharedTables(Accelerator accelerator, AcceleratorStream stream)
|
||||
{
|
||||
lock (SharedTablesLock)
|
||||
{
|
||||
if (!SharedTables.TryGetValue(accelerator, out var tables))
|
||||
{
|
||||
tables = new SharedHeuristicGpuTables(accelerator, stream);
|
||||
SharedTables.Add(accelerator, tables);
|
||||
}
|
||||
|
||||
tables.AddReference();
|
||||
return tables;
|
||||
}
|
||||
}
|
||||
|
||||
private static void ReleaseSharedTables(SharedHeuristicGpuTables tables)
|
||||
{
|
||||
if (tables is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
lock (SharedTablesLock)
|
||||
{
|
||||
tables.Release();
|
||||
}
|
||||
}
|
||||
|
||||
internal static void EnsureStaticTables(KernelContainer kernels, Accelerator accelerator, AcceleratorStream stream)
|
||||
{
|
||||
var tables = RentSharedTables(accelerator, stream);
|
||||
var heuristicTables = tables.CreateHeuristicDivisorTables();
|
||||
HeuristicGpuDivisorTables.InitializeShared(in heuristicTables);
|
||||
ReleaseSharedTables(tables);
|
||||
}
|
||||
// internal static void EnsureStaticTables(Accelerator accelerator)
|
||||
// {
|
||||
// var tables = CreateSharedTables(accelerator);
|
||||
// }
|
||||
|
||||
internal static void WarmUp(int target, int minBufferCapacity)
|
||||
{
|
||||
if (target <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// if (target <= 0)
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
|
||||
lock (WarmUpLock)
|
||||
{
|
||||
if (target <= WarmedLeaseCount)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// lock (WarmUpLock)
|
||||
// {
|
||||
// if (target <= WarmedLeaseCount)
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
|
||||
Accelerator accelerator = SharedGpuContext.Accelerator;
|
||||
AcceleratorStream stream = accelerator.CreateStream();
|
||||
KernelContainer kernels = GpuKernelPool.Kernels;
|
||||
// Accelerator accelerator = SharedGpuContext.Accelerator;
|
||||
// AcceleratorStream stream = accelerator.CreateStream();
|
||||
// KernelContainer kernels = GpuKernelPool.Kernels;
|
||||
|
||||
GpuStaticTableInitializer.EnsureStaticTables(kernels, accelerator, stream);
|
||||
// GpuStaticTableInitializer.EnsureStaticTables(kernels, stream);
|
||||
|
||||
int toCreate = target - WarmedLeaseCount;
|
||||
for (int i = 0; i < toCreate; i++)
|
||||
{
|
||||
Pool.Enqueue(new PrimeTesterGpuContextLease(minBufferCapacity, stream));
|
||||
}
|
||||
// int toCreate = target - WarmedLeaseCount;
|
||||
// for (int i = 0; i < toCreate; i++)
|
||||
// {
|
||||
// Pool.Enqueue(new PrimeTesterGpuContextLease(minBufferCapacity, stream));
|
||||
// }
|
||||
|
||||
stream.Synchronize();
|
||||
stream.Dispose();
|
||||
WarmedLeaseCount = target;
|
||||
}
|
||||
// stream.Synchronize();
|
||||
// stream.Dispose();
|
||||
// WarmedLeaseCount = target;
|
||||
// }
|
||||
}
|
||||
|
||||
internal static PrimeTesterGpuContextLease Rent(int minBufferCapacity = 1)
|
||||
{
|
||||
if (!Pool.TryDequeue(out var lease))
|
||||
var pool = Pool ??= new();
|
||||
if (!pool.TryDequeue(out var lease))
|
||||
{
|
||||
lease = new PrimeTesterGpuContextLease(minBufferCapacity);
|
||||
}
|
||||
@@ -306,74 +276,37 @@ public sealed class PrimeTester
|
||||
|
||||
internal static void Return(PrimeTesterGpuContextLease lease)
|
||||
{
|
||||
Pool.Enqueue(lease);
|
||||
var pool = Pool ??= new();
|
||||
pool.Enqueue(lease);
|
||||
}
|
||||
|
||||
internal static void DisposeAll()
|
||||
{
|
||||
while (Pool.TryDequeue(out var lease))
|
||||
if (Pool != null)
|
||||
{
|
||||
lease.DisposeResources();
|
||||
}
|
||||
|
||||
lock (SharedTablesLock)
|
||||
{
|
||||
foreach (var entry in SharedTables.Values)
|
||||
while (Pool.TryDequeue(out var lease))
|
||||
{
|
||||
entry.Dispose();
|
||||
lease.DisposeResources();
|
||||
}
|
||||
|
||||
SharedTables.Clear();
|
||||
var emptyTables = default(HeuristicGpuDivisorTables);
|
||||
HeuristicGpuDivisorTables.InitializeShared(in emptyTables);
|
||||
}
|
||||
|
||||
WarmedLeaseCount = 0;
|
||||
}
|
||||
|
||||
internal static void Clear(Accelerator accelerator)
|
||||
internal static void Clear()
|
||||
{
|
||||
if (accelerator is null)
|
||||
if (Pool != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
lock (WarmUpLock)
|
||||
{
|
||||
var retained = new List<PrimeTesterGpuContextLease>();
|
||||
while (Pool.TryDequeue(out var lease))
|
||||
lock (WarmUpLock)
|
||||
{
|
||||
if (lease.Accelerator == accelerator)
|
||||
var retained = new List<PrimeTesterGpuContextLease>();
|
||||
while (Pool.TryDequeue(out var lease))
|
||||
{
|
||||
lease.DisposeResources();
|
||||
}
|
||||
else
|
||||
{
|
||||
retained.Add(lease);
|
||||
}
|
||||
}
|
||||
|
||||
int retainedCount = retained.Count;
|
||||
for (int i = 0; i < retainedCount; i++)
|
||||
{
|
||||
Pool.Enqueue(retained[i]);
|
||||
}
|
||||
|
||||
WarmedLeaseCount = Pool.Count;
|
||||
}
|
||||
|
||||
lock (SharedTablesLock)
|
||||
{
|
||||
if (SharedTables.Remove(accelerator, out var tables))
|
||||
{
|
||||
tables.Dispose();
|
||||
}
|
||||
|
||||
if (SharedTables.Count == 0)
|
||||
{
|
||||
var emptyTables = default(HeuristicGpuDivisorTables);
|
||||
HeuristicGpuDivisorTables.InitializeShared(in emptyTables);
|
||||
}
|
||||
WarmedLeaseCount = Pool.Count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -390,9 +323,38 @@ public sealed class PrimeTester
|
||||
get => _stream ?? Accelerator.CreateStream();
|
||||
}
|
||||
|
||||
public static readonly Action<AcceleratorStream, Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<byte>> Kernel = KernelUtil.GetKernel(SharedGpuContext.Accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<byte>>(PrimeTesterKernels.SmallPrimeSieveKernel)).CreateLauncherDelegate<Action<AcceleratorStream, Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<byte>>>();
|
||||
[ThreadStatic]
|
||||
public static Dictionary<Accelerator, Action<AcceleratorStream, Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<byte>>>? Kernel;
|
||||
|
||||
internal static Action<AcceleratorStream, Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<byte>> GetKernel(Accelerator accelerator)
|
||||
{
|
||||
var pool = Kernel ??= [];
|
||||
if (pool.TryGetValue(accelerator, out var cached))
|
||||
{
|
||||
return cached;
|
||||
}
|
||||
|
||||
cached = KernelUtil.GetKernel(accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<byte>>(PrimeTesterKernels.SmallPrimeSieveKernel)).CreateLauncherDelegate<Action<AcceleratorStream, Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<byte>>>();
|
||||
pool[accelerator] = cached;
|
||||
return cached;
|
||||
}
|
||||
|
||||
[ThreadStatic]
|
||||
public static Dictionary<Accelerator, Action<AcceleratorStream, Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables>>? HeuristicTrialDivisionKernel;
|
||||
|
||||
internal static Action<AcceleratorStream, Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables> GetHeuristicTrialDivisionKernel(Accelerator accelerator)
|
||||
{
|
||||
var pool = HeuristicTrialDivisionKernel ??= [];
|
||||
if (pool.TryGetValue(accelerator, out var cached))
|
||||
{
|
||||
return cached;
|
||||
}
|
||||
|
||||
cached = KernelUtil.GetKernel(accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables>(PrimeTesterKernels.HeuristicTrialDivisionKernel)).CreateLauncherDelegate<Action<AcceleratorStream, Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables>>();
|
||||
pool[accelerator] = cached;
|
||||
return cached;
|
||||
}
|
||||
|
||||
public static readonly Action<AcceleratorStream, Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables> HeuristicTrialDivisionKernel = KernelUtil.GetKernel(SharedGpuContext.Accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables>(PrimeTesterKernels.HeuristicTrialDivisionKernel)).CreateLauncherDelegate<Action<AcceleratorStream, Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables>>();
|
||||
|
||||
public MemoryBuffer1D<ulong, Stride1D.Dense> Input = null!;
|
||||
public MemoryBuffer1D<byte, Stride1D.Dense> Output = null!;
|
||||
@@ -431,37 +393,27 @@ public sealed class PrimeTester
|
||||
internal PrimeTesterGpuContextLease(int minBufferCapacity)
|
||||
{
|
||||
AcceleratorContext = SharedGpuContext.Context;
|
||||
Accelerator = SharedGpuContext.Accelerator;
|
||||
AcceleratorStream stream = SharedGpuContext.Accelerator.CreateStream();
|
||||
var kernels = GpuKernelPool.Kernels;
|
||||
GpuStaticTableInitializer.EnsureStaticTables(kernels, Accelerator, stream);
|
||||
Accelerator accelerator = AcceleratorPool.Shared.Rent();
|
||||
// lock (_accelerators)
|
||||
// {
|
||||
// }
|
||||
|
||||
_sharedTables = RentSharedTables(Accelerator, stream);
|
||||
Accelerator = accelerator;
|
||||
AcceleratorStream stream = accelerator.CreateStream();
|
||||
// var kernels = _kernels[index]; //GpuKernelPool.Kernels;
|
||||
// GpuStaticTableInitializer.EnsureStaticTables(accelerator, kernels, stream);
|
||||
|
||||
_heuristicDivisorTables = _sharedTables.CreateHeuristicDivisorTables();
|
||||
HeuristicGpuDivisorTables.InitializeShared(in _heuristicDivisorTables);
|
||||
var sharedTables = SharedHeuristicGpuTables.EnsureStaticTables(accelerator, stream);
|
||||
_sharedTables = sharedTables;
|
||||
|
||||
var heuristicDivisorTables = sharedTables.CreateHeuristicDivisorTables();
|
||||
_heuristicDivisorTables = heuristicDivisorTables;
|
||||
|
||||
EnsureCapacity(minBufferCapacity);
|
||||
stream.Synchronize();
|
||||
stream.Dispose();
|
||||
}
|
||||
|
||||
internal PrimeTesterGpuContextLease(int minBufferCapacity, AcceleratorStream stream)
|
||||
{
|
||||
AcceleratorContext = SharedGpuContext.Context;
|
||||
Accelerator = SharedGpuContext.Accelerator;
|
||||
// GpuStaticTableInitializer.EnsureStaticTables(Accelerator);
|
||||
// Kernel = Accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<byte>>(PrimeTesterKernels.SmallPrimeSieveKernel);
|
||||
// HeuristicTrialDivisionKernel = Accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables>(PrimeTesterKernels.HeuristicTrialDivisionKernel);
|
||||
|
||||
_sharedTables = RentSharedTables(Accelerator, stream);
|
||||
|
||||
_heuristicDivisorTables = _sharedTables.CreateHeuristicDivisorTables();
|
||||
HeuristicGpuDivisorTables.InitializeShared(in _heuristicDivisorTables);
|
||||
|
||||
EnsureCapacity(minBufferCapacity);
|
||||
}
|
||||
|
||||
public void EnsureCapacity(int minCapacity)
|
||||
{
|
||||
if (minCapacity <= BufferCapacity)
|
||||
@@ -473,14 +425,20 @@ public sealed class PrimeTester
|
||||
Output?.Dispose();
|
||||
HeuristicFlag?.Dispose();
|
||||
|
||||
Input = Accelerator.Allocate1D<ulong>(minCapacity);
|
||||
Output = Accelerator.Allocate1D<byte>(minCapacity);
|
||||
HeuristicFlag = Accelerator.Allocate1D<int>(minCapacity);
|
||||
// lock (Accelerator)
|
||||
{
|
||||
Input = Accelerator.Allocate1D<ulong>(minCapacity);
|
||||
Output = Accelerator.Allocate1D<byte>(minCapacity);
|
||||
HeuristicFlag = Accelerator.Allocate1D<int>(minCapacity);
|
||||
}
|
||||
|
||||
BufferCapacity = minCapacity;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_stream?.Dispose();
|
||||
_stream = null;
|
||||
Return(this);
|
||||
}
|
||||
|
||||
@@ -491,7 +449,7 @@ public sealed class PrimeTester
|
||||
HeuristicFlag?.Dispose();
|
||||
_stream?.Dispose();
|
||||
_stream = null;
|
||||
ReleaseSharedTables(_sharedTables);
|
||||
// ReleaseSharedTables(_sharedTables);
|
||||
// These resources are shared between GPU leases
|
||||
// Accelerator.Dispose();
|
||||
// AcceleratorContext.Dispose();
|
||||
@@ -515,49 +473,49 @@ public sealed class PrimeTester
|
||||
|
||||
private sealed class SharedHeuristicGpuTables
|
||||
{
|
||||
private static readonly ConcurrentDictionary<Accelerator, SharedHeuristicGpuTables> _sharedTables = new(20_480, PerfectNumberConstants.RollingAccelerators);
|
||||
|
||||
private int _referenceCount;
|
||||
|
||||
internal static SharedHeuristicGpuTables EnsureStaticTables(Accelerator accelerator, AcceleratorStream stream)
|
||||
=> _sharedTables.GetOrAdd(accelerator, _ => new SharedHeuristicGpuTables(accelerator, stream));
|
||||
|
||||
internal SharedHeuristicGpuTables(Accelerator accelerator, AcceleratorStream stream)
|
||||
{
|
||||
|
||||
var primesDefault = DivisorGenerator.SmallPrimes;
|
||||
DevicePrimesDefault = accelerator.Allocate1D<uint>(primesDefault.Length);
|
||||
DevicePrimesDefault.View.CopyFromCPU(stream, primesDefault);
|
||||
|
||||
var primesLastOne = DivisorGenerator.SmallPrimesLastOne;
|
||||
DevicePrimesLastOne = accelerator.Allocate1D<uint>(primesLastOne.Length);
|
||||
DevicePrimesLastOne.View.CopyFromCPU(stream, primesLastOne);
|
||||
|
||||
var primesLastSeven = DivisorGenerator.SmallPrimesLastSeven;
|
||||
DevicePrimesLastSeven = accelerator.Allocate1D<uint>(primesLastSeven.Length);
|
||||
DevicePrimesLastSeven.View.CopyFromCPU(stream, primesLastSeven);
|
||||
|
||||
var primesLastThree = DivisorGenerator.SmallPrimesLastThree;
|
||||
DevicePrimesLastThree = accelerator.Allocate1D<uint>(primesLastThree.Length);
|
||||
DevicePrimesLastThree.View.CopyFromCPU(stream, primesLastThree);
|
||||
|
||||
var primesLastNine = DivisorGenerator.SmallPrimesLastNine;
|
||||
DevicePrimesLastNine = accelerator.Allocate1D<uint>(primesLastNine.Length);
|
||||
DevicePrimesLastNine.View.CopyFromCPU(stream, primesLastNine);
|
||||
|
||||
var primesPow2Default = DivisorGenerator.SmallPrimesPow2;
|
||||
DevicePrimesPow2Default = accelerator.Allocate1D<ulong>(primesPow2Default.Length);
|
||||
DevicePrimesPow2Default.View.CopyFromCPU(stream, primesPow2Default);
|
||||
|
||||
var primesPow2LastOne = DivisorGenerator.SmallPrimesPow2LastOne;
|
||||
DevicePrimesPow2LastOne = accelerator.Allocate1D<ulong>(primesPow2LastOne.Length);
|
||||
DevicePrimesPow2LastOne.View.CopyFromCPU(stream, primesPow2LastOne);
|
||||
|
||||
var primesPow2LastSeven = DivisorGenerator.SmallPrimesPow2LastSeven;
|
||||
DevicePrimesPow2LastSeven = accelerator.Allocate1D<ulong>(primesPow2LastSeven.Length);
|
||||
DevicePrimesPow2LastSeven.View.CopyFromCPU(stream, primesPow2LastSeven);
|
||||
|
||||
var primesPow2LastThree = DivisorGenerator.SmallPrimesPow2LastThree;
|
||||
DevicePrimesPow2LastThree = accelerator.Allocate1D<ulong>(primesPow2LastThree.Length);
|
||||
DevicePrimesPow2LastThree.View.CopyFromCPU(stream, primesPow2LastThree);
|
||||
|
||||
var primesPow2LastNine = DivisorGenerator.SmallPrimesPow2LastNine;
|
||||
DevicePrimesPow2LastNine = accelerator.Allocate1D<ulong>(primesPow2LastNine.Length);
|
||||
|
||||
// lock (accelerator)
|
||||
{
|
||||
DevicePrimesDefault = accelerator.Allocate1D<uint>(primesDefault.Length);
|
||||
DevicePrimesLastOne = accelerator.Allocate1D<uint>(primesLastOne.Length);
|
||||
DevicePrimesLastSeven = accelerator.Allocate1D<uint>(primesLastSeven.Length);
|
||||
DevicePrimesLastThree = accelerator.Allocate1D<uint>(primesLastThree.Length);
|
||||
DevicePrimesLastNine = accelerator.Allocate1D<uint>(primesLastNine.Length);
|
||||
DevicePrimesPow2Default = accelerator.Allocate1D<ulong>(primesPow2Default.Length);
|
||||
DevicePrimesPow2LastOne = accelerator.Allocate1D<ulong>(primesPow2LastOne.Length);
|
||||
DevicePrimesPow2LastSeven = accelerator.Allocate1D<ulong>(primesPow2LastSeven.Length);
|
||||
DevicePrimesPow2LastThree = accelerator.Allocate1D<ulong>(primesPow2LastThree.Length);
|
||||
DevicePrimesPow2LastNine = accelerator.Allocate1D<ulong>(primesPow2LastNine.Length);
|
||||
}
|
||||
|
||||
DevicePrimesDefault.View.CopyFromCPU(stream, primesDefault);
|
||||
DevicePrimesLastOne.View.CopyFromCPU(stream, primesLastOne);
|
||||
DevicePrimesLastSeven.View.CopyFromCPU(stream, primesLastSeven);
|
||||
DevicePrimesLastThree.View.CopyFromCPU(stream, primesLastThree);
|
||||
DevicePrimesLastNine.View.CopyFromCPU(stream, primesLastNine);
|
||||
DevicePrimesPow2Default.View.CopyFromCPU(stream, primesPow2Default);
|
||||
DevicePrimesPow2LastOne.View.CopyFromCPU(stream, primesPow2LastOne);
|
||||
DevicePrimesPow2LastSeven.View.CopyFromCPU(stream, primesPow2LastSeven);
|
||||
DevicePrimesPow2LastThree.View.CopyFromCPU(stream, primesPow2LastThree);
|
||||
DevicePrimesPow2LastNine.View.CopyFromCPU(stream, primesPow2LastNine);
|
||||
|
||||
HeuristicPrimeSieves.EnsureInitialized();
|
||||
@@ -647,22 +605,20 @@ public sealed class PrimeTester
|
||||
HeuristicGroupBDivisorSquaresEnding9.View);
|
||||
}
|
||||
|
||||
internal void AddReference()
|
||||
{
|
||||
_referenceCount++;
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal void AddReference() => Atomic.Add(ref _referenceCount, 1);
|
||||
|
||||
internal void Release()
|
||||
{
|
||||
if (_referenceCount > 0)
|
||||
{
|
||||
_referenceCount--;
|
||||
}
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal void Release() => Atomic.Add(ref _referenceCount, -1);
|
||||
|
||||
private static MemoryBuffer1D<ulong, Stride1D.Dense> CopySpanToDevice(Accelerator accelerator, AcceleratorStream stream, ReadOnlySpan<ulong> span)
|
||||
{
|
||||
var buffer = accelerator.Allocate1D<ulong>(span.Length);
|
||||
MemoryBuffer1D<ulong, Stride1D.Dense>? buffer;
|
||||
// lock (accelerator)
|
||||
{
|
||||
buffer = accelerator.Allocate1D<ulong>(span.Length);
|
||||
}
|
||||
|
||||
if (!span.IsEmpty)
|
||||
{
|
||||
ref ulong sourceRef = ref MemoryMarshal.GetReference(span);
|
||||
@@ -674,7 +630,12 @@ public sealed class PrimeTester
|
||||
|
||||
private static MemoryBuffer1D<ulong, Stride1D.Dense> CopyUintSpanToDevice(Accelerator accelerator, AcceleratorStream stream, ReadOnlySpan<uint> span)
|
||||
{
|
||||
var buffer = accelerator.Allocate1D<ulong>(span.Length);
|
||||
MemoryBuffer1D<ulong, Stride1D.Dense>? buffer;
|
||||
// lock (accelerator)
|
||||
{
|
||||
buffer = accelerator.Allocate1D<ulong>(span.Length);
|
||||
}
|
||||
|
||||
if (!span.IsEmpty)
|
||||
{
|
||||
var converted = new ulong[span.Length];
|
||||
@@ -722,9 +683,9 @@ public sealed class PrimeTester
|
||||
}
|
||||
|
||||
// Expose cache clearing for accelerator disposal coordination
|
||||
public static void ClearGpuCaches(Accelerator accelerator)
|
||||
public static void ClearGpuCaches()
|
||||
{
|
||||
PrimeTesterGpuContextPool.Clear(accelerator);
|
||||
PrimeTesterGpuContextPool.Clear();
|
||||
}
|
||||
|
||||
internal static void DisposeGpuContexts()
|
||||
@@ -746,6 +707,9 @@ public sealed class PrimeTester
|
||||
return BinaryGcd(n, m) != 1UL;
|
||||
}
|
||||
|
||||
[ThreadStatic]
|
||||
private static readonly Dictionary<Accelerator, Action<AcceleratorStream, Index1D, ArrayView<ulong>, ArrayView<byte>>>? _kernel;
|
||||
|
||||
internal static void SharesFactorWithMaxExponentBatch(ReadOnlySpan<ulong> values, Span<byte> results)
|
||||
{
|
||||
// TODO: Route this batch helper through the shared GPU kernel pool from
|
||||
@@ -755,18 +719,24 @@ public sealed class PrimeTester
|
||||
var accelerator = gpu.Accelerator;
|
||||
var stream = gpu.Stream;
|
||||
|
||||
var kernel = KernelUtil.GetKernel(accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<ulong>, ArrayView<byte>>(PrimeTesterKernels.SharesFactorKernel)).CreateLauncherDelegate<Action<AcceleratorStream, Index1D, ArrayView<ulong>, ArrayView<byte>>>();
|
||||
|
||||
int length = values.Length;
|
||||
var inputBuffer = accelerator.Allocate1D<ulong>(length);
|
||||
var resultBuffer = accelerator.Allocate1D<byte>(length);
|
||||
|
||||
ArrayPool<ulong> pool = ThreadStaticPools.UlongPool;
|
||||
ulong[] temp = pool.Rent(length);
|
||||
values.CopyTo(temp);
|
||||
|
||||
MemoryBuffer1D<ulong, Stride1D.Dense>? inputBuffer;
|
||||
MemoryBuffer1D<byte, Stride1D.Dense>? resultBuffer;
|
||||
|
||||
// lock (accelerator)
|
||||
{
|
||||
inputBuffer = accelerator.Allocate1D<ulong>(length);
|
||||
resultBuffer = accelerator.Allocate1D<byte>(length);
|
||||
}
|
||||
|
||||
inputBuffer.View.CopyFromCPU(stream, ref temp[0], length);
|
||||
var kernel = GetSharesFactorKernel(accelerator);
|
||||
kernel(stream, length, inputBuffer.View, resultBuffer.View);
|
||||
resultBuffer.View.CopyToCPU(stream, ref results[0], length);
|
||||
resultBuffer.View.CopyToCPU(stream, in results);
|
||||
stream.Synchronize();
|
||||
pool.Return(temp, clearArray: false);
|
||||
resultBuffer.Dispose();
|
||||
@@ -774,6 +744,19 @@ public sealed class PrimeTester
|
||||
gpu.Dispose();
|
||||
}
|
||||
|
||||
private static Action<AcceleratorStream, Index1D, ArrayView<ulong>, ArrayView<byte>> GetSharesFactorKernel(Accelerator accelerator)
|
||||
{
|
||||
var pool = _kernel ?? [];
|
||||
if (pool.TryGetValue(accelerator, out var cached))
|
||||
{
|
||||
return cached;
|
||||
}
|
||||
|
||||
cached = KernelUtil.GetKernel(accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<ulong>, ArrayView<byte>>(PrimeTesterKernels.SharesFactorKernel)).CreateLauncherDelegate<Action<AcceleratorStream, Index1D, ArrayView<ulong>, ArrayView<byte>>>();
|
||||
pool[accelerator] = cached;
|
||||
return cached;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static ulong BinaryGcd(ulong u, ulong v)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user