Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion .github/workflows/native-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,53 @@ jobs:
"CMAKE_PREFIX_PATH=${{ github.workspace }}\vcpkg_installed\${{ matrix.vcpkg_triplet }}" >> $env:GITHUB_ENV
"BIGMATH_VCPKG_TRIPLET=${{ matrix.vcpkg_triplet }}" >> $env:GITHUB_ENV

- name: Install CUDA toolkit
- name: Restore Windows CUDA toolkit cache
if: matrix.platform == 'windows'
id: cuda-cache
uses: actions/cache/restore@v4
with:
path: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.2
key: cuda-toolkit-${{ matrix.classifier }}-13.2.0-jimver-v0.2.35-v1

- name: Check Windows CUDA toolkit cache
if: matrix.platform == 'windows'
id: cuda-cache-check
shell: pwsh
run: |
$nvcc = 'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.2\bin\nvcc.exe'
$available = Test-Path -LiteralPath $nvcc -PathType Leaf
"available=$($available.ToString().ToLowerInvariant())" >> $env:GITHUB_OUTPUT

- name: Install Linux CUDA toolkit
if: matrix.platform == 'linux'
uses: Jimver/cuda-toolkit@v0.2.35
with:
cuda: '13.2.0'
method: network
sub-packages: '["compiler", "cudart", "cudart-dev"]'
non-cuda-sub-packages: '["libcufft", "libcufft-dev"]'

- name: Install Windows CUDA toolkit
if: matrix.platform == 'windows' && steps.cuda-cache-check.outputs.available != 'true'
uses: Jimver/cuda-toolkit@v0.2.35
with:
cuda: '13.2.0'

- name: Configure CUDA toolkit environment
shell: pwsh
run: |
if ('${{ matrix.platform }}' -eq 'windows') {
$cudaPath = 'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.2'
"CUDA_PATH=$cudaPath" >> $env:GITHUB_ENV
"CUDA_PATH_V13_2=$cudaPath" >> $env:GITHUB_ENV
"$cudaPath\bin" >> $env:GITHUB_PATH
} else {
$cudaPath = '/usr/local/cuda-13.2'
"CUDA_PATH=$cudaPath" >> $env:GITHUB_ENV
"CUDA_PATH_V13_2=$cudaPath" >> $env:GITHUB_ENV
"LD_LIBRARY_PATH=$cudaPath/lib64:${env:LD_LIBRARY_PATH}" >> $env:GITHUB_ENV
"$cudaPath/bin" >> $env:GITHUB_PATH
}

- uses: actions/setup-java@v4
with:
Expand Down Expand Up @@ -134,6 +177,13 @@ jobs:
path: .vcpkg-cache
key: ${{ steps.vcpkg-cache.outputs.cache-primary-key }}

- name: Save Windows CUDA toolkit cache
if: matrix.platform == 'windows' && steps.cuda-cache-check.outputs.available != 'true' && !cancelled()
uses: actions/cache/save@v4
with:
path: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.2
key: ${{ steps.cuda-cache.outputs.cache-primary-key }}

- name: Upload CUDA Native artifact
uses: actions/upload-artifact@v4
with:
Expand Down
67 changes: 67 additions & 0 deletions src/jmh/java/com/modularmc/bigmath/BigDeciBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
@Fork(1)
public class BigDeciBenchmark {
private static final BigNumberFormat GROUPED_FORMAT = BigNumberFormat.ofPattern("#,##0.################");
private static final int MULTIPLY_PAIRS = 32;

@State(Scope.Thread)
public static class PrecisionState {
Expand Down Expand Up @@ -94,6 +95,58 @@ public void tearDown() {
}
}

@State(Scope.Thread)
public static class HighPrecisionMultiplyState {
@Param({"131072", "196608", "262144", "393216", "524288", "1048576"})
public int precision;

BigDeci[] left;
BigDeci[] alternateLeft;
BigDeci[] right;
BigDeci result;
int pair;
int selectedPair;

@Setup(Level.Trial)
public void setup() {
int digits = (int) (precision * 0.30103) - 8;
left = new BigDeci[MULTIPLY_PAIRS];
alternateLeft = new BigDeci[MULTIPLY_PAIRS];
right = new BigDeci[MULTIPLY_PAIRS];
for (int index = 0; index < MULTIPLY_PAIRS; index++) {
String suffix = Integer.toString(10 + index);
String leftDigits = repeatDigits("1234567890", digits - suffix.length()) + suffix;
String alternateDigits = repeatDigits("2234567890", digits - suffix.length()) + suffix;
String rightDigits = repeatDigits("9876543210", digits - suffix.length()) + suffix;
try (BigInt leftInteger = BigInt.fromString(leftDigits, 10);
BigInt alternateInteger = BigInt.fromString(alternateDigits, 10);
BigInt rightInteger = BigInt.fromString(rightDigits, 10)) {
left[index] = BigDeci.fromBigInt(leftInteger, precision);
alternateLeft[index] = BigDeci.fromBigInt(alternateInteger, precision);
right[index] = BigDeci.fromBigInt(rightInteger, precision);
}
}
result = BigDeci.fromDouble(0.0, precision);
}

@Setup(Level.Invocation)
public void prepareInvocation() {
selectedPair = pair;
pair = (pair + 1) % MULTIPLY_PAIRS;
left[selectedPair].set(alternateLeft[selectedPair]);
}

@TearDown(Level.Trial)
public void tearDown() {
for (int index = 0; index < MULTIPLY_PAIRS; index++) {
left[index].close();
alternateLeft[index].close();
right[index].close();
}
result.close();
}
}

@Benchmark
public void add(PrecisionState state, Blackhole blackhole) {
try (BigDeci result = state.left.add(state.right)) {
Expand Down Expand Up @@ -123,6 +176,20 @@ public void divideInto(PrecisionState state, Blackhole blackhole) {
blackhole.consume(state.result.divideInto(state.left, state.right));
}

@Benchmark
public void highPrecisionMultiply(HighPrecisionMultiplyState state, Blackhole blackhole) {
int pair = state.selectedPair;
try (BigDeci result = state.left[pair].multiply(state.right[pair])) {
blackhole.consume(result);
}
}

@Benchmark
public void highPrecisionMultiplyInto(HighPrecisionMultiplyState state, Blackhole blackhole) {
int pair = state.selectedPair;
blackhole.consume(state.result.multiplyInto(state.left[pair], state.right[pair]));
}

@Benchmark
public void sqrt(PrecisionState state, Blackhole blackhole) {
try (BigDeci result = state.left.sqrt()) {
Expand Down
40 changes: 33 additions & 7 deletions src/jmh/java/com/modularmc/bigmath/CudaVerify.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* compared with public JDK or Bigmath values rather than private Native exports.
*/
public final class CudaVerify {
private static boolean requireResidentModPow;

public static void main(String[] args) {
RuntimeOptions.CudaBackend backend = args.length > 0
Expand All @@ -24,6 +25,7 @@ public static void main(String[] args) {
.productCacheEnabled(false)
.build());
RuntimeDiagnostics diagnostics = BigmathRuntime.initializeAsync().join();
requireResidentModPow = backend == RuntimeOptions.CudaBackend.NTT;
if (backend != RuntimeOptions.CudaBackend.CPU &&
diagnostics.cuda().calibrationStatus() != RuntimeDiagnostics.CalibrationStatus.READY) {
throw new IllegalStateException(diagnostics.cuda().statusMessage());
Expand All @@ -47,9 +49,9 @@ public static void main(String[] args) {
failures += checkBigDeci(precisionBits, random) ? 0 : 1;
}

for (int modulusDigits : new int[]{200000, 250000}) {
failures += checkModPow(modulusDigits, random) ? 0 : 1;
}
failures += checkModPow(200000, true, ExponentDensity.LOW, random) ? 0 : 1;
failures += checkModPow(200000, false, ExponentDensity.HIGH, random) ? 0 : 1;
failures += checkModPow(250000, true, ExponentDensity.MEDIUM, random) ? 0 : 1;

System.out.printf("%nRESULT: %s%n", failures == 0 ? "ALL PASS" : failures + " FAILED");
if (failures != 0) {
Expand Down Expand Up @@ -94,24 +96,48 @@ static boolean checkBigDeci(int precisionBits, SplittableRandom random) {
}
}

static boolean checkModPow(int modulusDigits, SplittableRandom random) {
static boolean checkModPow(
int modulusDigits,
boolean oddModulus,
ExponentDensity density,
SplittableRandom random
) {
BigInteger baseValue = new BigInteger(randDigits(modulusDigits, random));
BigInteger modulusValue = new BigInteger(randDigits(modulusDigits, random));
BigInteger exponentValue = BigInteger.valueOf(random.nextLong(1L << 40, 1L << 46));
modulusValue = oddModulus ? modulusValue.setBit(0) : modulusValue.clearBit(0);
BigInteger exponentValue = switch (density) {
case LOW -> BigInteger.ONE.setBit(45).setBit(7);
case MEDIUM -> BigInteger.valueOf(random.nextLong(1L << 45, 1L << 46));
case HIGH -> BigInteger.ONE.shiftLeft(46).subtract(BigInteger.ONE);
};
BigInteger expected = baseValue.modPow(exponentValue, modulusValue);
long fallbackBefore = BigmathRuntime.diagnostics().cpuFallbackCount();
try (BigInt base = BigInt.fromBigInteger(baseValue);
BigInt exponent = BigInt.fromBigInteger(exponentValue);
BigInt modulus = BigInt.fromBigInteger(modulusValue);
BigInt result = base.modPow(exponent, modulus)) {
long fallbacks = BigmathRuntime.diagnostics().cpuFallbackCount() - fallbackBefore;
boolean matches = expected.equals(result.toBigInteger());
boolean matches = expected.equals(result.toBigInteger()) &&
(!requireResidentModPow || fallbacks == 0);
String caseName = (oddModulus ? "odd" : "even") + density.displayName;
System.out.printf("%-9d %-8s %14d %s%n",
modulusDigits, "modPow", fallbacks, matches ? "PASS" : "MISMATCH");
modulusDigits, caseName, fallbacks, matches ? "PASS" : "MISMATCH");
return matches;
}
}

enum ExponentDensity {
LOW("Low"),
MEDIUM("Medium"),
HIGH("High");

final String displayName;

ExponentDensity(String displayName) {
this.displayName = displayName;
}
}

static String randDigits(int digits, SplittableRandom random) {
StringBuilder result = new StringBuilder(digits);
result.append((char) ('1' + random.nextInt(9)));
Expand Down
4 changes: 2 additions & 2 deletions src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ target_include_directories(bigmath_ffm PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_definitions(bigmath_ffm PRIVATE BIGMATH_BUILD_ID="${BIGMATH_BUILD_ID}")

if(BIGMATH_ENABLE_CUDA AND BIGMATH_CUDA_SUPPORTED_PLATFORM)
target_sources(bigmath_ffm PRIVATE cuda_convolution.cu cuda_ntt.cu)
target_sources(bigmath_ffm PRIVATE cuda_convolution.cu cuda_modular.cu cuda_ntt.cu)
target_compile_definitions(bigmath_ffm PRIVATE BIGMATH_HAS_CUDA)
target_link_libraries(bigmath_ffm PRIVATE CUDA::cudart CUDA::cufft)
set_source_files_properties(cuda_convolution.cu cuda_ntt.cu PROPERTIES LANGUAGE CUDA)
set_source_files_properties(cuda_convolution.cu cuda_modular.cu cuda_ntt.cu PROPERTIES LANGUAGE CUDA)
endif()

if(WIN32)
Expand Down
Loading
Loading