Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for importing and exporting wisdom files #1

Merged
merged 2 commits into from
Oct 16, 2024
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
13 changes: 13 additions & 0 deletions fftw/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::array::Alignment;
use std::ffi::NulError;
use thiserror::Error;

pub type Result<T> = ::std::result::Result<T, Error>;
Expand All @@ -19,4 +20,16 @@ pub enum Error {
expect: (usize, Alignment),
actual: (usize, Alignment),
},

#[error("Failed to convert path into str: {:?}", path)]
PathToStrConversionError { path: std::path::PathBuf },

#[error("Failed to convert path into CString: {}", conversion_error)]
PathToCStringConversionError { conversion_error: NulError },

#[error("Failed to import wisdom file: {:?}", path)]
ImportWisdomError { path: std::path::PathBuf },

#[error("Failed to export wisdom file: {:?}", path)]
ExportWisdomError { path: std::path::PathBuf },
}
1 change: 1 addition & 0 deletions fftw/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ pub mod array;
pub mod error;
pub mod plan;
pub mod types;
pub mod wisdom;
90 changes: 90 additions & 0 deletions fftw/src/wisdom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//! Wisdom Files
//!
//! FFTW3 supports importing and exporting "wisdom" about plan execution.
//! See [Words of Wisdom - Saving
//! Plans](http://www.fftw.org/fftw3_doc/Words-of-Wisdom_002dSaving-Plans.html)
//! for more information.

use crate::error::*;
use crate::ffi::*;
use std::ffi::CString;
use std::path::Path;

macro_rules! impl_wisdom_call {
($(#[$attr:meta])* => $name:ident, $exec:ident, $err:ident) => {
$(#[$attr])*
pub fn $name<P: AsRef<Path>>(filename: &P) -> Result<()> {
let path_str = match filename.as_ref().to_str() {
Some(str) => str,
None => {
return Err(Error::PathToStrConversionError {
path: filename.as_ref().to_owned(),
})
}
};
let c_path_string = match CString::new(path_str) {
Ok(c_str) => c_str,
Err(e) => {
return Err(Error::PathToCStringConversionError {
conversion_error: e,
})
}
};

let result: i32;
excall! {
result = $exec(c_path_string.as_ptr())
}
if result != 1 {
return Err(Error::$err {
path: filename.as_ref().to_owned(),
});
}
Ok(())
}
};
} // impl_wisdom_call!

impl_wisdom_call!(
/// Import an existing wisdom file for double precision plans
///
/// See [Wisdom Import](http://www.fftw.org/fftw3_doc/Wisdom-Import.html)
/// for more information.
=>
import_wisdom_file_f64,
fftw_import_wisdom_from_filename,
ImportWisdomError
);

impl_wisdom_call!(
/// Import an existing wisdom file for single precision plans
///
/// See [Wisdom Import](http://www.fftw.org/fftw3_doc/Wisdom-Import.html)
/// for more information.
=>
import_wisdom_file_f32,
fftwf_import_wisdom_from_filename,
ImportWisdomError
);

impl_wisdom_call!(
/// Export wisdom for double precision plans to file
///
/// See [Wisdom Export](http://www.fftw.org/fftw3_doc/Wisdom-Export.html)
/// for more information.
=>
export_wisdom_file_f64,
fftw_export_wisdom_to_filename,
ExportWisdomError
);

impl_wisdom_call!(
/// Export wisdom for single precision plans to file
///
/// See [Wisdom Export](http://www.fftw.org/fftw3_doc/Wisdom-Export.html)
/// for more information.
=>
export_wisdom_file_f32,
fftwf_export_wisdom_to_filename,
ExportWisdomError
);
Loading