diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 0f5e4c5..98ead0d 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -14,6 +14,11 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - name: Setup toolchain + uses: dtolnay/rust-toolchain@master + with: + toolchain: nightly + components: rustfmt - name: Cache uses: actions/cache@v4.0.2 with: @@ -22,13 +27,7 @@ jobs: ~/.cargo/git ~/.rustup target - key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} - - name: Setup toolchain - uses: actions-rs/toolchain@v1 - with: - toolchain: nightly - override: true - components: rustfmt, clippy + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-${{steps.toolchain.outputs.name}} - name: Formatting run: cargo fmt --check - name: Build diff --git a/Cargo.lock b/Cargo.lock index f5894e7..99fab4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -372,6 +372,7 @@ dependencies = [ [[package]] name = "fftw" version = "0.8.0" +source = "git+https://github.com/SallySoul/fftw3-rs.git?tag=fftw3-v0.8.1#ba98096fae4cc55e37f3deba19a5a696af551692" dependencies = [ "bitflags 2.6.0", "fftw-sys", @@ -385,6 +386,7 @@ dependencies = [ [[package]] name = "fftw-src" version = "0.8.0" +source = "git+https://github.com/SallySoul/fftw3-rs.git?tag=fftw3-v0.8.1#ba98096fae4cc55e37f3deba19a5a696af551692" dependencies = [ "anyhow", "cc", @@ -396,6 +398,7 @@ dependencies = [ [[package]] name = "fftw-sys" version = "0.8.0" +source = "git+https://github.com/SallySoul/fftw3-rs.git?tag=fftw3-v0.8.1#ba98096fae4cc55e37f3deba19a5a696af551692" dependencies = [ "fftw-src", "libc", diff --git a/Cargo.toml b/Cargo.toml index b418bcc..b7b069e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" [dependencies] colorous = "1.0.14" -fftw = { path = "/Users/russell/projects/rust/fftw3-rs/fftw", features = ["system"] } +fftw = { git = "https://github.com/SallySoul/fftw3-rs.git", tag = "fftw3-v0.8.1" } image = "0.25.2" num = "0.4.3" diff --git a/examples/heat_2d_p_fft.rs b/examples/heat_2d_p_fft.rs index af28d28..a3566d1 100644 --- a/examples/heat_2d_p_fft.rs +++ b/examples/heat_2d_p_fft.rs @@ -157,10 +157,6 @@ fn main() { fft_backwards_buffer[i] = fft_ic_output_buffer[i]; } - for i in 0..width * height { - fft_ic_input_buffer[i] = 0.0; - } - backward_plan .c2r(&mut fft_backwards_buffer, &mut fft_ic_input_buffer) .unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 01756ef..5d3da55 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,4 +5,5 @@ pub mod basic_domain; pub mod domain; pub mod linear_stencil; pub mod naive_solver; +pub mod squaring; pub mod stencil; diff --git a/src/squaring.rs b/src/squaring.rs new file mode 100644 index 0000000..253737e --- /dev/null +++ b/src/squaring.rs @@ -0,0 +1,55 @@ +pub fn log2ceil(i: usize) -> usize { + (usize::BITS - i.leading_zeros()) as usize +} + +pub fn repeated_square(exp0: usize, i: usize) -> usize { + let mut exp = exp0; + let mut squares = i; + let mut result = 1; + let n = log2ceil(exp0); + println!( + "repeated, n: {}, exp: {}, result: {}, squares: {}", + n, exp, result, squares + ); + for _ in 0..n { + if exp % 2 == 1 { + result *= squares; + exp -= 1; + println!( + " - odd exp, result: {}, squares: {}, exp: {}", + result, squares, exp + ); + } + exp /= 2; + squares *= squares; + println!(" * exp: {}, result: {}, squares: {}", exp, result, squares); + } + + result +} + +#[cfg(test)] +mod unit_tests { + use super::*; + + #[test] + fn log2ceil_test() { + assert_eq!(log2ceil(1), 1); + assert_eq!(log2ceil(2), 2); + assert_eq!(log2ceil(3), 2); + assert_eq!(log2ceil(4), 3); + assert_eq!(log2ceil(5), 3); + assert_eq!(log2ceil(6), 3); + assert_eq!(log2ceil(7), 3); + assert_eq!(log2ceil(8), 4); + } + + #[test] + fn repeated_square_test() { + assert_eq!(repeated_square(2, 2), 4); + assert_eq!(repeated_square(3, 2), 8); + assert_eq!(repeated_square(4, 2), 16); + assert_eq!(repeated_square(5, 2), 32); + assert_eq!(repeated_square(9, 3), 19683); + } +}