Add regression test for divisor cycle GPU generation
This commit is contained in:
1 parent
3028397d6b
commit
84451aac4d
3 files changed
+57
-1
No files matched your search
@@ -0,0 +1,55 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using FluentAssertions;
|
||||||
|
using PerfectNumbers.Core;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace PerfectNumbers.Core.Tests;
|
||||||
|
|
||||||
|
public class MersenneDivisorCyclesGenerateGpuTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
[Trait("Category", "Fast")]
|
||||||
|
public void GenerateGpu_produces_expected_cycles_for_small_range()
|
||||||
|
{
|
||||||
|
string path = Path.GetTempFileName();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
MersenneDivisorCycles.GenerateGpu(path, maxDivisor: 50UL, batchSize: 16, skipCount: 0, nextPosition: 0);
|
||||||
|
var pairs = new List<(ulong divisor, ulong cycle)>();
|
||||||
|
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
|
||||||
|
using var reader = new BinaryReader(stream);
|
||||||
|
while (stream.Position < stream.Length)
|
||||||
|
{
|
||||||
|
ulong d = reader.ReadUInt64();
|
||||||
|
ulong c = reader.ReadUInt64();
|
||||||
|
pairs.Add((d, c));
|
||||||
|
}
|
||||||
|
|
||||||
|
var expected = new List<ulong>();
|
||||||
|
for (ulong d = 3; d <= 50; d++)
|
||||||
|
{
|
||||||
|
if ((d & 1UL) == 0UL || d % 3UL == 0UL || d % 5UL == 0UL || d % 7UL == 0UL || d % 11UL == 0UL)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
expected.Add(d);
|
||||||
|
}
|
||||||
|
|
||||||
|
pairs.Should().HaveCount(expected.Count);
|
||||||
|
foreach (var pair in pairs)
|
||||||
|
{
|
||||||
|
expected.Should().Contain(pair.divisor);
|
||||||
|
pair.cycle.Should().Be(MersenneDivisorCycles.CalculateCycleLength(pair.divisor));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (File.Exists(path))
|
||||||
|
{
|
||||||
|
File.Delete(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@ public class MersenneNumberResidueGpuTesterTests
|
|||||||
|
|
||||||
RunCase(tester, 23UL, 2UL, expectedPrime: false);
|
RunCase(tester, 23UL, 2UL, expectedPrime: false);
|
||||||
RunCase(tester, 29UL, 37UL, expectedPrime: false);
|
RunCase(tester, 29UL, 37UL, expectedPrime: false);
|
||||||
|
RunCase(tester, 31UL, 1_000UL, expectedPrime: true);
|
||||||
RunCase(tester, 89UL, 1_001UL, expectedPrime: true);
|
RunCase(tester, 89UL, 1_001UL, expectedPrime: true);
|
||||||
RunCase(tester, 127UL, 1_001UL, expectedPrime: true);
|
RunCase(tester, 127UL, 1_001UL, expectedPrime: true);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -281,7 +281,7 @@ public class MersenneDivisorCycles
|
|||||||
var bufferCycle = accelerator.Allocate1D<ulong>(idx);
|
var bufferCycle = accelerator.Allocate1D<ulong>(idx);
|
||||||
|
|
||||||
kernel(
|
kernel(
|
||||||
count,
|
idx,
|
||||||
bufferDiv.View,
|
bufferDiv.View,
|
||||||
bufferCycle.View);
|
bufferCycle.View);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user