Divisor-Cycle Cache Rework
This commit is contained in:
1 parent
f9e548279c
commit
ebe48d5f2f
11 files changed
+1836
-2660
No files matched your search
@@ -1,10 +1,10 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using ILGPU;
|
||||
using ILGPU.Runtime;
|
||||
using PerfectNumbers.Core.Gpu;
|
||||
@@ -13,535 +13,232 @@ namespace PerfectNumbers.Core;
|
||||
|
||||
public sealed class DivisorCycleCache
|
||||
{
|
||||
public sealed class CycleBlock : IDisposable
|
||||
{
|
||||
private readonly DivisorCycleCache _owner;
|
||||
private int _referenceCount;
|
||||
|
||||
internal CycleBlock(DivisorCycleCache owner, int index, ulong start, in ulong[] cycles)
|
||||
{
|
||||
_owner = owner;
|
||||
Index = index;
|
||||
Start = start;
|
||||
End = start + (ulong)cycles.Length - 1UL;
|
||||
Cycles = cycles;
|
||||
}
|
||||
|
||||
internal int Index { get; }
|
||||
|
||||
internal ulong Start { get; }
|
||||
|
||||
internal ulong End { get; }
|
||||
|
||||
internal ulong[] Cycles { get; }
|
||||
|
||||
internal int ReferenceCount => Volatile.Read(ref _referenceCount);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ulong GetCycle(ulong divisor) => Cycles[(int)(divisor - Start)];
|
||||
|
||||
internal void Retain()
|
||||
{
|
||||
Interlocked.Increment(ref _referenceCount);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
int remaining = Interlocked.Decrement(ref _referenceCount);
|
||||
if (remaining < 0)
|
||||
{
|
||||
throw new InvalidOperationException("Cycle block released more times than it was acquired.");
|
||||
}
|
||||
|
||||
if (remaining == 0)
|
||||
{
|
||||
_owner.OnCycleBlockReleased(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private const int CycleGenerationBatchSize = 262_144; // TODO: Remove the block-sized batch once the single-cycle generator
|
||||
// replaces block hydration so we stop materializing large buffers
|
||||
// just to satisfy one missing divisor lookup.
|
||||
private const byte ByteZero = 0;
|
||||
private const byte ByteOne = 1;
|
||||
private const int StackBufferThreshold = 128;
|
||||
|
||||
private readonly object _sync = new(); // TODO: Delete the synchronization object once the cache exposes only snapshot reads and single-cycle computation so callers no longer contend on locks.
|
||||
private readonly ConcurrentDictionary<int, Task<CycleBlock>> _pending = new(); // TODO: Drop the concurrent dictionary entirely when the block pipeline is replaced with single-cycle generation; the new plan never queues background work.
|
||||
private readonly ConcurrentDictionary<Accelerator, Action<Index1D, int, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<byte, Stride1D.Dense>>> _gpuKernelCache = new(AcceleratorReferenceComparer.Instance); // TODO: Swap this ConcurrentDictionary for a simple accelerator-indexed array once the single-cycle helper precompiles kernels during startup, eliminating the need for thread-safe lookups.
|
||||
private CycleBlock _baseBlock = null!;
|
||||
private CycleBlock? _activeBlock; // TODO: Remove dynamic blocks altogether once lookups route to the snapshot or compute an individual cycle without mutating shared state.
|
||||
private CycleBlock? _prefetchedBlock; // TODO: Delete prefetch support so the cache never materializes extra blocks and callers always compute the missing cycle inline.
|
||||
private bool _useGpuGeneration = true;
|
||||
private readonly int _divisorCyclesBatchSize;
|
||||
private static int _sharedDivisorCyclesBatchSize = GpuConstants.GpuCycleStepsPerInvocation;
|
||||
private readonly ConcurrentDictionary<Accelerator, Action<Index1D, int, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<byte, Stride1D.Dense>>> _gpuKernelCache = new(AcceleratorReferenceComparer.Instance);
|
||||
private volatile ulong[] _snapshot;
|
||||
private volatile bool _useGpuGeneration = true;
|
||||
private readonly int _divisorCyclesBatchSize;
|
||||
private static int _sharedDivisorCyclesBatchSize = GpuConstants.GpuCycleStepsPerInvocation;
|
||||
|
||||
public static void SetDivisorCyclesBatchSize(int divisorCyclesBatchSize)
|
||||
{
|
||||
_sharedDivisorCyclesBatchSize = Math.Max(1, divisorCyclesBatchSize);
|
||||
}
|
||||
|
||||
public static void SetDivisorCyclesBatchSize(int divisorCyclesBatchSize) => _sharedDivisorCyclesBatchSize = divisorCyclesBatchSize;
|
||||
public static DivisorCycleCache Shared { get; } = new DivisorCycleCache(_sharedDivisorCyclesBatchSize);
|
||||
|
||||
public int PreferredBatchSize => _divisorCyclesBatchSize;
|
||||
|
||||
private DivisorCycleCache(int divisorCyclesBatchSize)
|
||||
{
|
||||
_divisorCyclesBatchSize = divisorCyclesBatchSize;
|
||||
ulong[] snapshot = MersenneDivisorCycles.Shared.ExportSmallCyclesSnapshot();
|
||||
Initialize(snapshot);
|
||||
}
|
||||
|
||||
// TODO: Remove the ability to reload at runtime once the cache becomes a read-only snapshot plus
|
||||
// transient single-cycle generator; future callers should load the snapshot during startup only.
|
||||
public void ReloadFromCurrentSnapshot()
|
||||
{
|
||||
ulong[] snapshot = MersenneDivisorCycles.Shared.ExportSmallCyclesSnapshot();
|
||||
Initialize(snapshot);
|
||||
_divisorCyclesBatchSize = Math.Max(1, divisorCyclesBatchSize);
|
||||
_snapshot = MersenneDivisorCycles.Shared.ExportSmallCyclesSnapshot();
|
||||
}
|
||||
|
||||
public void ConfigureGeneratorDevice(bool useGpu)
|
||||
{
|
||||
lock (_sync)
|
||||
{
|
||||
_useGpuGeneration = useGpu;
|
||||
}
|
||||
// TODO: Remove the lock once single-cycle generation eliminates shared mutable state; a simple
|
||||
// volatile write will be enough when the cache becomes read-only.
|
||||
_useGpuGeneration = useGpu;
|
||||
}
|
||||
|
||||
private void Initialize(ulong[] snapshot)
|
||||
public void RefreshSnapshot()
|
||||
{
|
||||
CycleBlock? activeToDispose = null;
|
||||
CycleBlock? prefetchedToDispose = null;
|
||||
_snapshot = MersenneDivisorCycles.Shared.ExportSmallCyclesSnapshot();
|
||||
}
|
||||
|
||||
lock (_sync)
|
||||
public ulong GetCycleLength(ulong divisor)
|
||||
{
|
||||
Span<ulong> result = stackalloc ulong[1];
|
||||
ReadOnlySpan<ulong> singleDivisor = stackalloc ulong[1] { divisor };
|
||||
GetCycleLengths(singleDivisor, result);
|
||||
return result[0];
|
||||
}
|
||||
|
||||
public void GetCycleLengths(ReadOnlySpan<ulong> divisors, Span<ulong> cycles)
|
||||
{
|
||||
ulong[] snapshot = _snapshot;
|
||||
int length = divisors.Length;
|
||||
int[]? rentedMissing = null;
|
||||
Span<int> missingBuffer = length <= StackBufferThreshold
|
||||
? stackalloc int[length]
|
||||
: new Span<int>(rentedMissing = ArrayPool<int>.Shared.Rent(length), 0, length);
|
||||
|
||||
int missingCount = 0;
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
if (_activeBlock is not null && _activeBlock.Index != 0)
|
||||
ulong divisor = divisors[i];
|
||||
if (divisor <= 1UL)
|
||||
{
|
||||
activeToDispose = _activeBlock;
|
||||
throw new InvalidDataException("Divisor must be > 1");
|
||||
}
|
||||
|
||||
if (_prefetchedBlock is not null && _prefetchedBlock.Index != 0)
|
||||
if (divisor < (ulong)snapshot.Length)
|
||||
{
|
||||
prefetchedToDispose = _prefetchedBlock;
|
||||
}
|
||||
|
||||
_pending.Clear();
|
||||
// TODO: Remove the dictionary once prefetching and background block tracking disappear; with single-cycle generation we will keep only the snapshot and skip additional bookkeeping entirely.
|
||||
_baseBlock = new CycleBlock(this, index: 0, start: 0UL, snapshot);
|
||||
_activeBlock = null;
|
||||
_prefetchedBlock = null;
|
||||
StartPrefetchLocked(1);
|
||||
}
|
||||
|
||||
activeToDispose?.Dispose();
|
||||
prefetchedToDispose?.Dispose();
|
||||
}
|
||||
|
||||
public CycleBlock Acquire(ulong divisor)
|
||||
{
|
||||
CycleBlock block = divisor <= _baseBlock.End ? _baseBlock : AcquireDynamicBlock(divisor);
|
||||
// TODO: Replace this block-based acquisition with a direct GetCycle API that computes the missing cycle synchronously on the configured device so the cache never hands out mutable blocks or requires disposal.
|
||||
block.Retain();
|
||||
return block;
|
||||
}
|
||||
|
||||
private CycleBlock AcquireDynamicBlock(ulong divisor)
|
||||
{
|
||||
int blockIndex = GetBlockIndex(divisor);
|
||||
|
||||
while (true)
|
||||
{
|
||||
Task<CycleBlock>? pendingTask = null;
|
||||
lock (_sync)
|
||||
{
|
||||
if (TryGetCachedBlockLocked(blockIndex, out CycleBlock? cached))
|
||||
ulong cached = snapshot[divisor];
|
||||
if (cached == 0UL)
|
||||
{
|
||||
return cached!;
|
||||
throw new InvalidDataException($"Divisor cycle is missing for {divisor}");
|
||||
}
|
||||
|
||||
if (!_pending.TryGetValue(blockIndex, out pendingTask))
|
||||
{
|
||||
// TODO: Replace Task.Run with an inline single-cycle computation that runs on the configured
|
||||
// device so we materialize exactly one missing cycle and stop scheduling thread-pool work for
|
||||
// entire blocks.
|
||||
pendingTask = Task.Run(() => GenerateBlock(blockIndex));
|
||||
_pending[blockIndex] = pendingTask;
|
||||
}
|
||||
cycles[i] = cached;
|
||||
}
|
||||
|
||||
CycleBlock block = pendingTask.GetAwaiter().GetResult();
|
||||
lock (_sync)
|
||||
else
|
||||
{
|
||||
_pending.TryRemove(blockIndex, out _);
|
||||
|
||||
if (TryGetCachedBlockLocked(blockIndex, out CycleBlock? cachedAfterAwait))
|
||||
{
|
||||
return cachedAfterAwait!;
|
||||
}
|
||||
|
||||
SetActiveBlockLocked(block);
|
||||
// TODO: Stop promoting freshly generated blocks into the shared cache and, instead of
|
||||
// returning a block, surface the computed cycle directly to the caller so no extra cache
|
||||
// state survives beyond the single snapshot block.
|
||||
return block;
|
||||
missingBuffer[missingCount++] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool TryGetCachedBlockLocked(int blockIndex, out CycleBlock? block)
|
||||
{
|
||||
CycleBlock? active = _activeBlock;
|
||||
if (active is not null && active.Index == blockIndex)
|
||||
if (missingCount != 0)
|
||||
{
|
||||
block = active;
|
||||
return true;
|
||||
}
|
||||
|
||||
CycleBlock? prefetched = _prefetchedBlock;
|
||||
if (prefetched is not null && prefetched.Index == blockIndex)
|
||||
{
|
||||
PromotePrefetchedLocked();
|
||||
block = _activeBlock!;
|
||||
return true;
|
||||
}
|
||||
|
||||
block = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
private void PromotePrefetchedLocked()
|
||||
{
|
||||
CycleBlock? prefetched = _prefetchedBlock;
|
||||
if (prefetched is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CycleBlock? previousActive = _activeBlock;
|
||||
_activeBlock = prefetched;
|
||||
_prefetchedBlock = null;
|
||||
prefetched.Retain();
|
||||
prefetched.Dispose();
|
||||
|
||||
if (previousActive is not null && previousActive.Index != 0 && previousActive.Index != prefetched.Index)
|
||||
{
|
||||
previousActive.Dispose();
|
||||
}
|
||||
|
||||
StartPrefetchLocked(prefetched.Index + 1); // TODO: Remove this call once prefetching is dropped
|
||||
// so we strictly keep a single active block in memory.
|
||||
}
|
||||
|
||||
private void SetActiveBlockLocked(CycleBlock block)
|
||||
{
|
||||
CycleBlock? previousActive = _activeBlock;
|
||||
_activeBlock = block;
|
||||
block.Retain();
|
||||
|
||||
CycleBlock? prefetched = _prefetchedBlock;
|
||||
if (prefetched is not null && prefetched.Index != block.Index + 1)
|
||||
{
|
||||
if (prefetched.Index != 0)
|
||||
ReadOnlySpan<int> missingIndices = missingBuffer[..missingCount];
|
||||
if (_useGpuGeneration)
|
||||
{
|
||||
prefetched.Dispose();
|
||||
ComputeCyclesGpu(divisors, cycles, missingIndices);
|
||||
}
|
||||
|
||||
_prefetchedBlock = null;
|
||||
}
|
||||
|
||||
if (previousActive is not null && previousActive.Index != 0 && previousActive.Index != block.Index)
|
||||
{
|
||||
previousActive.Dispose();
|
||||
}
|
||||
|
||||
StartPrefetchLocked(block.Index + 1); // TODO: Remove once prefetching is removed and the cache
|
||||
// no longer speculatively requests the next block after a miss.
|
||||
}
|
||||
|
||||
private void StartPrefetchLocked(int blockIndex)
|
||||
{
|
||||
// TODO: Remove prefetching entirely so the cache maintains a single snapshot block and computes
|
||||
// individual cycles on demand instead of triggering background generation for future indices.
|
||||
if (blockIndex <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CycleBlock? prefetched = _prefetchedBlock;
|
||||
if (prefetched is not null && prefetched.Index == blockIndex)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_pending.TryGetValue(blockIndex, out Task<CycleBlock>? existing))
|
||||
{
|
||||
if (existing.IsCompletedSuccessfully)
|
||||
else
|
||||
{
|
||||
_pending.TryRemove(blockIndex, out _);
|
||||
AssignPrefetchedBlockLocked(blockIndex, existing.Result);
|
||||
ComputeCyclesCpu(divisors, cycles, missingIndices);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Delete this once prefetching is removed; single-cycle generation will happen inline and
|
||||
// no asynchronous work should be scheduled.
|
||||
Task<CycleBlock> task = Task.Run(() => GenerateBlock(blockIndex));
|
||||
_pending[blockIndex] = task;
|
||||
task.ContinueWith(t => OnPrefetchCompleted(blockIndex, t), TaskScheduler.Default);
|
||||
if (rentedMissing is not null)
|
||||
{
|
||||
ArrayPool<int>.Shared.Return(rentedMissing, clearArray: false);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPrefetchCompleted(int blockIndex, Task<CycleBlock> task)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void ComputeCyclesCpu(ReadOnlySpan<ulong> divisors, Span<ulong> cycles, ReadOnlySpan<int> indices)
|
||||
{
|
||||
// TODO: Delete this callback when we remove prefetching. Single-cycle generation will run inline,
|
||||
// eliminating the need for asynchronous continuation handlers and concurrent dictionaries.
|
||||
if (!task.IsCompletedSuccessfully)
|
||||
for (int i = 0; i < indices.Length; i++)
|
||||
{
|
||||
_pending.TryRemove(blockIndex, out _);
|
||||
return;
|
||||
}
|
||||
|
||||
lock (_sync)
|
||||
{
|
||||
_pending.TryRemove(blockIndex, out _);
|
||||
AssignPrefetchedBlockLocked(blockIndex, task.Result);
|
||||
int targetIndex = indices[i];
|
||||
cycles[targetIndex] = MersenneDivisorCycles.CalculateCycleLength(divisors[targetIndex]);
|
||||
}
|
||||
}
|
||||
|
||||
private void AssignPrefetchedBlockLocked(int blockIndex, CycleBlock block)
|
||||
private void ComputeCyclesGpu(ReadOnlySpan<ulong> divisors, Span<ulong> cycles, ReadOnlySpan<int> indices)
|
||||
{
|
||||
// TODO: Remove this entire method when prefetching goes away. The single-cycle flow should never
|
||||
// allocate or retain extra blocks beyond the startup snapshot, so prefetched block bookkeeping
|
||||
// becomes unnecessary.
|
||||
CycleBlock? active = _activeBlock;
|
||||
if (active is not null && blockIndex != active.Index + 1)
|
||||
int count = indices.Length;
|
||||
ulong[]? rentedDivisors = null;
|
||||
Span<ulong> workDivisors = count <= StackBufferThreshold
|
||||
? stackalloc ulong[count]
|
||||
: new Span<ulong>(rentedDivisors = ArrayPool<ulong>.Shared.Rent(count), 0, count);
|
||||
|
||||
ulong[]? rentedResults = null;
|
||||
Span<ulong> workResults = count <= StackBufferThreshold
|
||||
? stackalloc ulong[count]
|
||||
: new Span<ulong>(rentedResults = ArrayPool<ulong>.Shared.Rent(count), 0, count);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
MontgomeryDivisorDataCache.ReleaseBlock(block);
|
||||
return;
|
||||
workDivisors[i] = divisors[indices[i]];
|
||||
}
|
||||
|
||||
CycleBlock? previousPrefetched = _prefetchedBlock;
|
||||
if (ReferenceEquals(previousPrefetched, block))
|
||||
ComputeCyclesGpuCore(workDivisors, workResults);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
return;
|
||||
cycles[indices[i]] = workResults[i];
|
||||
}
|
||||
|
||||
if (_prefetchedBlock is null || _prefetchedBlock.Index <= blockIndex)
|
||||
if (rentedDivisors is not null)
|
||||
{
|
||||
if (previousPrefetched is not null && previousPrefetched.Index != 0)
|
||||
{
|
||||
previousPrefetched.Dispose();
|
||||
}
|
||||
|
||||
_prefetchedBlock = block;
|
||||
block.Retain();
|
||||
}
|
||||
else
|
||||
{
|
||||
MontgomeryDivisorDataCache.ReleaseBlock(block);
|
||||
ArrayPool<ulong>.Shared.Return(rentedDivisors, clearArray: false);
|
||||
ArrayPool<ulong>.Shared.Return(rentedResults!, clearArray: false);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCycleBlockReleased(CycleBlock block)
|
||||
private void ComputeCyclesGpuCore(ReadOnlySpan<ulong> divisors, Span<ulong> destination)
|
||||
{
|
||||
if (block.Index == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int attempt = 0; attempt < 8; attempt++)
|
||||
{
|
||||
if (block.ReferenceCount != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Thread.SpinWait(1 << attempt);
|
||||
}
|
||||
|
||||
bool stillTracked;
|
||||
|
||||
lock (_sync)
|
||||
{
|
||||
stillTracked = ReferenceEquals(_activeBlock, block) || ReferenceEquals(_prefetchedBlock, block);
|
||||
}
|
||||
|
||||
if (!stillTracked && block.ReferenceCount == 0)
|
||||
{
|
||||
MontgomeryDivisorDataCache.ReleaseBlock(block);
|
||||
}
|
||||
}
|
||||
|
||||
private CycleBlock GenerateBlock(int blockIndex)
|
||||
{
|
||||
if (blockIndex == 0)
|
||||
{
|
||||
return _baseBlock;
|
||||
}
|
||||
|
||||
ulong start = GetBlockStart(blockIndex);
|
||||
int length = _baseBlock.Cycles.Length;
|
||||
ulong[] cycles = new ulong[length];
|
||||
|
||||
if (_useGpuGeneration)
|
||||
{
|
||||
ComputeCyclesGpu(start, cycles, _divisorCyclesBatchSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
ComputeCyclesCpu(start, cycles);
|
||||
}
|
||||
|
||||
// TODO: Replace this block generator with a single-cycle helper that reuses shared buffers,
|
||||
// returns just the requested divisor's cycle, and avoids mutating cache state beyond the
|
||||
// preloaded snapshot.
|
||||
return new CycleBlock(this, blockIndex, start, cycles);
|
||||
}
|
||||
|
||||
private int GetBlockIndex(ulong divisor)
|
||||
{
|
||||
if (divisor <= _baseBlock.End)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
ulong offset = divisor - (_baseBlock.End + 1UL);
|
||||
return (int)(offset / (ulong)PerfectNumberConstants.MaxQForDivisorCycles) + 1;
|
||||
}
|
||||
|
||||
private ulong GetBlockStart(int blockIndex)
|
||||
{
|
||||
if (blockIndex == 0)
|
||||
{
|
||||
return 0UL;
|
||||
}
|
||||
|
||||
ulong baseEnd = _baseBlock.End;
|
||||
return baseEnd + 1UL + (ulong)(blockIndex - 1) * (ulong)PerfectNumberConstants.MaxQForDivisorCycles;
|
||||
}
|
||||
|
||||
private void ComputeCyclesCpu(ulong start, ulong[] destination)
|
||||
{
|
||||
ulong divisor = start;
|
||||
for (int i = 0; i < destination.Length; i++)
|
||||
{
|
||||
// TODO: Port this CPU fallback to the unrolled-hex cycle generator once it is shared so misses outside the snapshot
|
||||
// stop looping through CalculateCycleLength, which benchmarks showed is far slower than the GPU-assisted pipeline.
|
||||
destination[i] = MersenneDivisorCycles.CalculateCycleLength(divisor);
|
||||
divisor++;
|
||||
}
|
||||
}
|
||||
|
||||
private void ComputeCyclesGpu(ulong start, ulong[] destination, int divisorCyclesBatchSize)
|
||||
{
|
||||
var gpuLease = GpuKernelPool.GetKernel(_useGpuGeneration);
|
||||
int length = divisors.Length;
|
||||
var gpuLease = GpuKernelPool.GetKernel(useGpuOrder: true);
|
||||
var execution = gpuLease.EnterExecutionScope();
|
||||
|
||||
try
|
||||
Accelerator accelerator = gpuLease.Accelerator;
|
||||
var kernel = _gpuKernelCache.GetOrAdd(accelerator, LoadKernel);
|
||||
|
||||
using MemoryBuffer1D<ulong, Stride1D.Dense> divisorBuffer = accelerator.Allocate1D<ulong>(length);
|
||||
using MemoryBuffer1D<ulong, Stride1D.Dense> powBuffer = accelerator.Allocate1D<ulong>(length);
|
||||
using MemoryBuffer1D<ulong, Stride1D.Dense> orderBuffer = accelerator.Allocate1D<ulong>(length);
|
||||
using MemoryBuffer1D<ulong, Stride1D.Dense> resultBuffer = accelerator.Allocate1D<ulong>(length);
|
||||
using MemoryBuffer1D<byte, Stride1D.Dense> statusBuffer = accelerator.Allocate1D<byte>(length);
|
||||
|
||||
ulong[]? rentedPow = null;
|
||||
Span<ulong> powSpan = length <= StackBufferThreshold
|
||||
? stackalloc ulong[length]
|
||||
: new Span<ulong>(rentedPow = ArrayPool<ulong>.Shared.Rent(length), 0, length);
|
||||
|
||||
ulong[]? rentedOrder = null;
|
||||
Span<ulong> orderSpan = length <= StackBufferThreshold
|
||||
? stackalloc ulong[length]
|
||||
: new Span<ulong>(rentedOrder = ArrayPool<ulong>.Shared.Rent(length), 0, length);
|
||||
|
||||
ulong[]? rentedResult = null;
|
||||
Span<ulong> resultSpan = length <= StackBufferThreshold
|
||||
? stackalloc ulong[length]
|
||||
: new Span<ulong>(rentedResult = ArrayPool<ulong>.Shared.Rent(length), 0, length);
|
||||
|
||||
byte[]? rentedStatus = null;
|
||||
Span<byte> statusSpan = length <= StackBufferThreshold
|
||||
? stackalloc byte[length]
|
||||
: new Span<byte>(rentedStatus = ArrayPool<byte>.Shared.Rent(length), 0, length);
|
||||
|
||||
ref ulong divisorRef = ref MemoryMarshal.GetReference(divisors);
|
||||
divisorBuffer.View.CopyFromCPU(ref divisorRef, length);
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
Accelerator accelerator = gpuLease.Accelerator;
|
||||
Action<Index1D, int, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<byte, Stride1D.Dense>> kernel = _gpuKernelCache.GetOrAdd(accelerator, LoadKernel);
|
||||
// TODO: Once single-cycle generation is in place, bypass this block-sized kernel entirely and
|
||||
// call the ProcessEightBitWindows-based GPU helper that materializes only the requested
|
||||
// divisor, reusing a pinned upload buffer instead of allocating per block.
|
||||
|
||||
int length = destination.Length;
|
||||
int chunkCapacity = Math.Min(length, CycleGenerationBatchSize);
|
||||
ulong[] divisors = ArrayPool<ulong>.Shared.Rent(chunkCapacity);
|
||||
ulong[] powScratch = ArrayPool<ulong>.Shared.Rent(chunkCapacity);
|
||||
ulong[] orderScratch = ArrayPool<ulong>.Shared.Rent(chunkCapacity);
|
||||
byte[] statusScratch = ArrayPool<byte>.Shared.Rent(chunkCapacity);
|
||||
|
||||
try
|
||||
ulong divisor = divisors[i];
|
||||
ulong initialPow = 2UL;
|
||||
if (initialPow >= divisor)
|
||||
{
|
||||
using MemoryBuffer1D<ulong, Stride1D.Dense> divisorBuffer = accelerator.Allocate1D<ulong>(chunkCapacity);
|
||||
using MemoryBuffer1D<ulong, Stride1D.Dense> powBuffer = accelerator.Allocate1D<ulong>(chunkCapacity);
|
||||
using MemoryBuffer1D<ulong, Stride1D.Dense> orderBuffer = accelerator.Allocate1D<ulong>(chunkCapacity);
|
||||
using MemoryBuffer1D<ulong, Stride1D.Dense> resultBuffer = accelerator.Allocate1D<ulong>(chunkCapacity);
|
||||
using MemoryBuffer1D<byte, Stride1D.Dense> statusBuffer = accelerator.Allocate1D<byte>(chunkCapacity);
|
||||
initialPow -= divisor;
|
||||
}
|
||||
|
||||
int offset = 0;
|
||||
while (offset < length)
|
||||
powSpan[i] = initialPow;
|
||||
orderSpan[i] = 1UL;
|
||||
resultSpan[i] = 0UL;
|
||||
statusSpan[i] = ByteZero;
|
||||
}
|
||||
|
||||
powBuffer.View.CopyFromCPU(ref MemoryMarshal.GetReference(powSpan), length);
|
||||
orderBuffer.View.CopyFromCPU(ref MemoryMarshal.GetReference(orderSpan), length);
|
||||
resultBuffer.View.CopyFromCPU(ref MemoryMarshal.GetReference(resultSpan), length);
|
||||
statusBuffer.View.CopyFromCPU(ref MemoryMarshal.GetReference(statusSpan), length);
|
||||
|
||||
int pending;
|
||||
do
|
||||
{
|
||||
kernel(length, _divisorCyclesBatchSize, divisorBuffer.View, powBuffer.View, orderBuffer.View, resultBuffer.View, statusBuffer.View);
|
||||
accelerator.Synchronize();
|
||||
|
||||
statusBuffer.View.CopyToCPU(ref MemoryMarshal.GetReference(statusSpan), length);
|
||||
pending = 0;
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
if (statusSpan[i] == ByteZero)
|
||||
{
|
||||
int batchSize = Math.Min(chunkCapacity, length - offset);
|
||||
Span<ulong> divisorsSpan = divisors.AsSpan(0, batchSize);
|
||||
Span<ulong> destinationSpan = destination.AsSpan(offset, batchSize);
|
||||
Span<ulong> powSpan = powScratch.AsSpan(0, batchSize);
|
||||
Span<ulong> orderSpan = orderScratch.AsSpan(0, batchSize);
|
||||
Span<byte> statusSpan = statusScratch.AsSpan(0, batchSize);
|
||||
|
||||
int pending = 0;
|
||||
for (int i = 0; i < batchSize; i++)
|
||||
{
|
||||
ulong divisorValue = start + (ulong)(offset + i);
|
||||
divisorsSpan[i] = divisorValue;
|
||||
|
||||
if ((divisorValue & (divisorValue - 1UL)) == 0UL)
|
||||
{
|
||||
destinationSpan[i] = 1UL;
|
||||
powSpan[i] = 0UL;
|
||||
orderSpan[i] = 1UL;
|
||||
statusSpan[i] = ByteOne;
|
||||
}
|
||||
else
|
||||
{
|
||||
ulong initialPow = 2UL;
|
||||
if (initialPow >= divisorValue)
|
||||
{
|
||||
initialPow -= divisorValue;
|
||||
}
|
||||
|
||||
destinationSpan[i] = 0UL;
|
||||
powSpan[i] = initialPow;
|
||||
orderSpan[i] = 1UL;
|
||||
statusSpan[i] = ByteZero;
|
||||
pending++;
|
||||
}
|
||||
}
|
||||
|
||||
ArrayView1D<ulong, Stride1D.Dense> divisorView = divisorBuffer.View.SubView(0, batchSize);
|
||||
ArrayView1D<ulong, Stride1D.Dense> powView = powBuffer.View.SubView(0, batchSize);
|
||||
ArrayView1D<ulong, Stride1D.Dense> orderView = orderBuffer.View.SubView(0, batchSize);
|
||||
ArrayView1D<ulong, Stride1D.Dense> resultView = resultBuffer.View.SubView(0, batchSize);
|
||||
ArrayView1D<byte, Stride1D.Dense> statusView = statusBuffer.View.SubView(0, batchSize);
|
||||
|
||||
divisorView.CopyFromCPU(ref MemoryMarshal.GetReference(divisorsSpan), batchSize);
|
||||
powView.CopyFromCPU(ref MemoryMarshal.GetReference(powSpan), batchSize);
|
||||
orderView.CopyFromCPU(ref MemoryMarshal.GetReference(orderSpan), batchSize);
|
||||
resultView.CopyFromCPU(ref MemoryMarshal.GetReference(destinationSpan), batchSize);
|
||||
statusView.CopyFromCPU(ref MemoryMarshal.GetReference(statusSpan), batchSize);
|
||||
|
||||
while (pending > 0)
|
||||
{
|
||||
kernel(batchSize, divisorCyclesBatchSize, divisorView, powView, orderView, resultView, statusView);
|
||||
accelerator.Synchronize();
|
||||
|
||||
statusView.CopyToCPU(ref MemoryMarshal.GetReference(statusSpan), batchSize);
|
||||
|
||||
pending = 0;
|
||||
for (int i = 0; i < batchSize; i++)
|
||||
{
|
||||
if (statusSpan[i] == ByteZero)
|
||||
{
|
||||
pending++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resultView.CopyToCPU(ref MemoryMarshal.GetReference(destinationSpan), batchSize);
|
||||
|
||||
offset += batchSize;
|
||||
pending++;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<ulong>.Shared.Return(divisors, clearArray: false);
|
||||
ArrayPool<ulong>.Shared.Return(powScratch, clearArray: false);
|
||||
ArrayPool<ulong>.Shared.Return(orderScratch, clearArray: false);
|
||||
ArrayPool<byte>.Shared.Return(statusScratch, clearArray: false);
|
||||
}
|
||||
}
|
||||
finally
|
||||
while (pending > 0);
|
||||
|
||||
resultBuffer.View.CopyToCPU(ref MemoryMarshal.GetReference(resultSpan), length);
|
||||
resultSpan.CopyTo(destination);
|
||||
if (rentedPow is not null)
|
||||
{
|
||||
execution.Dispose();
|
||||
gpuLease.Dispose();
|
||||
ArrayPool<ulong>.Shared.Return(rentedPow, clearArray: false);
|
||||
ArrayPool<ulong>.Shared.Return(rentedOrder!, clearArray: false);
|
||||
ArrayPool<ulong>.Shared.Return(rentedResult!, clearArray: false);
|
||||
ArrayPool<byte>.Shared.Return(rentedStatus!, clearArray: false);
|
||||
}
|
||||
|
||||
execution.Dispose();
|
||||
gpuLease.Dispose();
|
||||
}
|
||||
|
||||
private static Action<Index1D, int, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<byte, Stride1D.Dense>> LoadKernel(Accelerator accelerator)
|
||||
@@ -551,7 +248,7 @@ public sealed class DivisorCycleCache
|
||||
|
||||
private static void GpuAdvanceDivisorCyclesKernel(
|
||||
Index1D index,
|
||||
int steps,
|
||||
int steps,
|
||||
ArrayView1D<ulong, Stride1D.Dense> divisors,
|
||||
ArrayView1D<ulong, Stride1D.Dense> pow,
|
||||
ArrayView1D<ulong, Stride1D.Dense> order,
|
||||
@@ -602,3 +299,6 @@ public sealed class DivisorCycleCache
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user