-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release 1.3.0: * [FEAT] Mixed-radix NTT design * [FEAT] example paths changed
- Loading branch information
Showing
31 changed files
with
1,761 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
examples/c++/polynomial_multiplication/.devcontainer/Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Make sure NVIDIA Container Toolkit is installed on your host | ||
|
||
# Use the specified base image | ||
FROM nvidia/cuda:12.0.0-devel-ubuntu22.04 | ||
|
||
# Update and install dependencies | ||
RUN apt-get update && apt-get install -y \ | ||
cmake \ | ||
curl \ | ||
build-essential \ | ||
git \ | ||
libboost-all-dev \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
# Clone Icicle from a GitHub repository | ||
RUN git clone https://github.com/ingonyama-zk/icicle.git /icicle | ||
|
||
# Set the working directory in the container | ||
WORKDIR /icicle-example | ||
|
||
# Specify the default command for the container | ||
CMD ["/bin/bash"] | ||
|
||
|
||
|
22 changes: 22 additions & 0 deletions
22
examples/c++/polynomial_multiplication/.devcontainer/devcontainer.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "Icicle Examples: polynomial multiplication", | ||
"build": { | ||
"dockerfile": "Dockerfile" | ||
}, | ||
"runArgs": [ | ||
"--gpus", | ||
"all" | ||
], | ||
"postCreateCommand": [ | ||
"nvidia-smi" | ||
], | ||
"customizations": { | ||
"vscode": { | ||
"extensions": [ | ||
"ms-vscode.cmake-tools", | ||
"ms-python.python", | ||
"ms-vscode.cpptools" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
cmake_minimum_required(VERSION 3.18) | ||
set(CMAKE_CXX_STANDARD 17) | ||
set(CMAKE_CUDA_STANDARD 17) | ||
set(CMAKE_CUDA_STANDARD_REQUIRED TRUE) | ||
set(CMAKE_CXX_STANDARD_REQUIRED TRUE) | ||
if (${CMAKE_VERSION} VERSION_LESS "3.24.0") | ||
set(CMAKE_CUDA_ARCHITECTURES ${CUDA_ARCH}) | ||
else() | ||
set(CMAKE_CUDA_ARCHITECTURES native) # on 3.24+, on earlier it is ignored, and the target is not passed | ||
endif () | ||
project(icicle LANGUAGES CUDA CXX) | ||
|
||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --expt-relaxed-constexpr") | ||
set(CMAKE_CUDA_FLAGS_RELEASE "") | ||
set(CMAKE_CUDA_FLAGS_DEBUG "${CMAKE_CUDA_FLAGS_DEBUG} -g -G -O0") | ||
# change the path to your Icicle location | ||
include_directories("../../../icicle") | ||
add_executable( | ||
example | ||
example.cu | ||
) | ||
|
||
find_library(NVML_LIBRARY nvidia-ml PATHS /usr/local/cuda-12.0/targets/x86_64-linux/lib/stubs/ ) | ||
target_link_libraries(example ${NVML_LIBRARY}) | ||
set_target_properties(example PROPERTIES CUDA_SEPARABLE_COMPILATION ON) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
|
||
# Exit immediately on error | ||
set -e | ||
|
||
rm -rf build | ||
mkdir -p build | ||
cmake -S . -B build | ||
cmake --build build | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#define CURVE_ID BLS12_381 | ||
|
||
#include <chrono> | ||
#include <iostream> | ||
#include <vector> | ||
|
||
#include "curves/curve_config.cuh" | ||
#include "appUtils/ntt/ntt.cu" | ||
#include "appUtils/ntt/kernel_ntt.cu" | ||
#include "utils/vec_ops.cu" | ||
#include "utils/error_handler.cuh" | ||
#include <memory> | ||
|
||
typedef curve_config::scalar_t test_scalar; | ||
typedef curve_config::scalar_t test_data; | ||
|
||
void random_samples(test_data* res, uint32_t count) | ||
{ | ||
for (int i = 0; i < count; i++) | ||
res[i] = i < 1000 ? test_data::rand_host() : res[i - 1000]; | ||
} | ||
|
||
void incremental_values(test_scalar* res, uint32_t count) | ||
{ | ||
for (int i = 0; i < count; i++) { | ||
res[i] = i ? res[i - 1] + test_scalar::one() * test_scalar::omega(4) : test_scalar::zero(); | ||
} | ||
} | ||
|
||
// calcaulting polynomial multiplication A*B via NTT,pointwise-multiplication and INTT | ||
// (1) allocate A,B on CPU. Randomize first half, zero second half | ||
// (2) allocate NttAGpu, NttBGpu on GPU | ||
// (3) calc NTT for A and for B from cpu to GPU | ||
// (4) multiply MulGpu = NttAGpu * NttBGpu (pointwise) | ||
// (5) INTT MulGpu inplace | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
cudaEvent_t start, stop; | ||
float measured_time; | ||
|
||
int NTT_LOG_SIZE = 23; | ||
int NTT_SIZE = 1 << NTT_LOG_SIZE; | ||
|
||
CHK_IF_RETURN(cudaFree(nullptr)); // init GPU context | ||
|
||
// init domain | ||
auto ntt_config = ntt::DefaultNTTConfig<test_scalar>(); | ||
ntt_config.ordering = ntt::Ordering::kNN; // TODO: use NR for forward and RN for backward | ||
ntt_config.is_force_radix2 = (argc > 1) ? atoi(argv[1]) : false; | ||
|
||
const char* ntt_alg_str = ntt_config.is_force_radix2 ? "Radix-2" : "Mixed-Radix"; | ||
std::cout << "Polynomial multiplication with " << ntt_alg_str << " NTT: "; | ||
|
||
CHK_IF_RETURN(cudaEventCreate(&start)); | ||
CHK_IF_RETURN(cudaEventCreate(&stop)); | ||
|
||
const test_scalar basic_root = test_scalar::omega(NTT_LOG_SIZE); | ||
ntt::InitDomain(basic_root, ntt_config.ctx); | ||
|
||
// (1) cpu allocation | ||
auto CpuA = std::make_unique<test_data[]>(NTT_SIZE); | ||
auto CpuB = std::make_unique<test_data[]>(NTT_SIZE); | ||
random_samples(CpuA.get(), NTT_SIZE >> 1); // second half zeros | ||
random_samples(CpuB.get(), NTT_SIZE >> 1); // second half zeros | ||
|
||
test_data *GpuA, *GpuB, *MulGpu; | ||
|
||
auto benchmark = [&](bool print, int iterations = 1) { | ||
// start recording | ||
CHK_IF_RETURN(cudaEventRecord(start, ntt_config.ctx.stream)); | ||
|
||
for (int iter = 0; iter < iterations; ++iter) { | ||
// (2) gpu input allocation | ||
CHK_IF_RETURN(cudaMallocAsync(&GpuA, sizeof(test_data) * NTT_SIZE, ntt_config.ctx.stream)); | ||
CHK_IF_RETURN(cudaMallocAsync(&GpuB, sizeof(test_data) * NTT_SIZE, ntt_config.ctx.stream)); | ||
|
||
// (3) NTT for A,B from cpu to gpu | ||
ntt_config.are_inputs_on_device = false; | ||
ntt_config.are_outputs_on_device = true; | ||
CHK_IF_RETURN(ntt::NTT(CpuA.get(), NTT_SIZE, ntt::NTTDir::kForward, ntt_config, GpuA)); | ||
CHK_IF_RETURN(ntt::NTT(CpuB.get(), NTT_SIZE, ntt::NTTDir::kForward, ntt_config, GpuB)); | ||
|
||
// (4) multiply A,B | ||
CHK_IF_RETURN(cudaMallocAsync(&MulGpu, sizeof(test_data) * NTT_SIZE, ntt_config.ctx.stream)); | ||
CHK_IF_RETURN( | ||
vec_ops::Mul(GpuA, GpuB, NTT_SIZE, true /*=is_on_device*/, false /*=is_montgomery*/, ntt_config.ctx, MulGpu)); | ||
|
||
// (5) INTT (in place) | ||
ntt_config.are_inputs_on_device = true; | ||
ntt_config.are_outputs_on_device = true; | ||
CHK_IF_RETURN(ntt::NTT(MulGpu, NTT_SIZE, ntt::NTTDir::kInverse, ntt_config, MulGpu)); | ||
|
||
CHK_IF_RETURN(cudaFreeAsync(GpuA, ntt_config.ctx.stream)); | ||
CHK_IF_RETURN(cudaFreeAsync(GpuB, ntt_config.ctx.stream)); | ||
CHK_IF_RETURN(cudaFreeAsync(MulGpu, ntt_config.ctx.stream)); | ||
} | ||
|
||
CHK_IF_RETURN(cudaEventRecord(stop, ntt_config.ctx.stream)); | ||
CHK_IF_RETURN(cudaStreamSynchronize(ntt_config.ctx.stream)); | ||
CHK_IF_RETURN(cudaEventElapsedTime(&measured_time, start, stop)); | ||
|
||
if (print) { std::cout << measured_time / iterations << " MS" << std::endl; } | ||
|
||
return CHK_LAST(); | ||
}; | ||
|
||
benchmark(false); // warmup | ||
benchmark(true, 20); | ||
|
||
CHK_IF_RETURN(cudaStreamSynchronize(ntt_config.ctx.stream)); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
./build/example 1 # radix2 | ||
./build/example 0 # mixed-radix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[toolchain] | ||
channel = "1.70.0" | ||
components = ["rustfmt"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
[package] | ||
name = "ntt" | ||
version = "1.0.0" | ||
version = "1.2.0" | ||
edition = "2018" | ||
|
||
[dependencies] | ||
icicle-cuda-runtime = { git = "https://github.com/ingonyama-zk/icicle.git", tag = "v1.1.0" } | ||
icicle-core = { git = "https://github.com/ingonyama-zk/icicle.git", tag = "v1.1.0", features = ["arkworks"] } | ||
icicle-bn254 = { git = "https://github.com/ingonyama-zk/icicle.git", tag = "v1.1.0", features = ["arkworks"] } | ||
icicle-bls12-377 = { git = "https://github.com/ingonyama-zk/icicle.git", tag = "v1.1.0", features = ["arkworks"] } | ||
icicle-cuda-runtime = { path = "../../../wrappers/rust/icicle-cuda-runtime" } | ||
icicle-core = { path = "../../../wrappers/rust/icicle-core", features = ["arkworks"] } | ||
icicle-bn254 = { path = "../../../wrappers/rust/icicle-curves/icicle-bn254", features = ["arkworks"] } | ||
icicle-bls12-377 = { path = "../../../wrappers/rust/icicle-curves/icicle-bls12-377", features = ["arkworks"] } | ||
|
||
ark-ff = { version = "0.4.0" } | ||
ark-poly = "0.4.0" | ||
ark-std = "0.4.0" | ||
ark-bn254 = { version = "0.4.0" } | ||
ark-bls12-377 = { version = "0.4.0" } | ||
clap = { version = "4.4.12", features = ["derive"] } | ||
clap = { version = "<=4.4.12", features = ["derive"] } | ||
|
||
[features] | ||
profile = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[toolchain] | ||
channel = "1.70.0" | ||
components = ["rustfmt"] |
Oops, something went wrong.