Inline batch copies for GPU prime tester
This commit is contained in:
1 parent
2b0fc5be13
commit
c61df1c33b
1 file changed
+17
-13
@@ -4,6 +4,7 @@ using System.Collections.Concurrent;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using ILGPU;
|
using ILGPU;
|
||||||
using ILGPU.Runtime;
|
using ILGPU.Runtime;
|
||||||
using PerfectNumbers.Core.Gpu;
|
using PerfectNumbers.Core.Gpu;
|
||||||
@@ -117,11 +118,13 @@ public sealed class PrimeTester
|
|||||||
|
|
||||||
var input = gpu.Input;
|
var input = gpu.Input;
|
||||||
var output = gpu.Output;
|
var output = gpu.Output;
|
||||||
|
var inputView = input.View;
|
||||||
|
var outputView = output.View;
|
||||||
|
|
||||||
input.View.CopyFromCPU(ref value, 1);
|
inputView.CopyFromCPU(ref value, 1);
|
||||||
kernel(
|
kernel(
|
||||||
1,
|
1,
|
||||||
input.View,
|
inputView,
|
||||||
gpu.DevicePrimesDefault.View,
|
gpu.DevicePrimesDefault.View,
|
||||||
gpu.DevicePrimesLastOne.View,
|
gpu.DevicePrimesLastOne.View,
|
||||||
gpu.DevicePrimesLastSeven.View,
|
gpu.DevicePrimesLastSeven.View,
|
||||||
@@ -132,9 +135,9 @@ public sealed class PrimeTester
|
|||||||
gpu.DevicePrimesPow2LastSeven.View,
|
gpu.DevicePrimesPow2LastSeven.View,
|
||||||
gpu.DevicePrimesPow2LastThree.View,
|
gpu.DevicePrimesPow2LastThree.View,
|
||||||
gpu.DevicePrimesPow2LastNine.View,
|
gpu.DevicePrimesPow2LastNine.View,
|
||||||
output.View);
|
outputView);
|
||||||
accelerator.Synchronize();
|
accelerator.Synchronize();
|
||||||
output.View.CopyToCPU(ref flag, 1);
|
outputView.CopyToCPU(ref flag, 1);
|
||||||
|
|
||||||
gpu.Dispose();
|
gpu.Dispose();
|
||||||
limiter.Dispose();
|
limiter.Dispose();
|
||||||
@@ -163,8 +166,8 @@ public static bool IsPrimeCpu(ulong n, CancellationToken ct)
|
|||||||
|
|
||||||
var input = gpu.Input;
|
var input = gpu.Input;
|
||||||
var output = gpu.Output;
|
var output = gpu.Output;
|
||||||
ArrayPool<ulong> pool = ThreadStaticPools.UlongPool;
|
var inputView = input.View;
|
||||||
ulong[] temp = pool.Rent(batchSize);
|
var outputView = output.View;
|
||||||
|
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
while (pos < totalLength)
|
while (pos < totalLength)
|
||||||
@@ -172,12 +175,13 @@ public static bool IsPrimeCpu(ulong n, CancellationToken ct)
|
|||||||
int remaining = totalLength - pos;
|
int remaining = totalLength - pos;
|
||||||
int count = remaining > batchSize ? batchSize : remaining;
|
int count = remaining > batchSize ? batchSize : remaining;
|
||||||
|
|
||||||
values.Slice(pos, count).CopyTo(temp);
|
var valueSlice = values.Slice(pos, count);
|
||||||
input.View.CopyFromCPU(ref temp[0], count);
|
ref ulong valueRef = ref MemoryMarshal.GetReference(valueSlice);
|
||||||
|
inputView.CopyFromCPU(ref valueRef, count);
|
||||||
|
|
||||||
kernel(
|
kernel(
|
||||||
count,
|
count,
|
||||||
input.View,
|
inputView,
|
||||||
gpu.DevicePrimesDefault.View,
|
gpu.DevicePrimesDefault.View,
|
||||||
gpu.DevicePrimesLastOne.View,
|
gpu.DevicePrimesLastOne.View,
|
||||||
gpu.DevicePrimesLastSeven.View,
|
gpu.DevicePrimesLastSeven.View,
|
||||||
@@ -188,15 +192,15 @@ public static bool IsPrimeCpu(ulong n, CancellationToken ct)
|
|||||||
gpu.DevicePrimesPow2LastSeven.View,
|
gpu.DevicePrimesPow2LastSeven.View,
|
||||||
gpu.DevicePrimesPow2LastThree.View,
|
gpu.DevicePrimesPow2LastThree.View,
|
||||||
gpu.DevicePrimesPow2LastNine.View,
|
gpu.DevicePrimesPow2LastNine.View,
|
||||||
output.View);
|
outputView);
|
||||||
accelerator.Synchronize();
|
accelerator.Synchronize();
|
||||||
output.View.CopyToCPU(ref results[pos], count);
|
var resultSlice = results.Slice(pos, count);
|
||||||
|
ref byte resultRef = ref MemoryMarshal.GetReference(resultSlice);
|
||||||
|
outputView.CopyToCPU(ref resultRef, count);
|
||||||
|
|
||||||
pos += count;
|
pos += count;
|
||||||
}
|
}
|
||||||
|
|
||||||
pool.Return(temp, clearArray: false);
|
|
||||||
|
|
||||||
gpu.Dispose();
|
gpu.Dispose();
|
||||||
limiter.Dispose();
|
limiter.Dispose();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user