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

Expose floating-point precision in Python #1688

Merged
merged 3 commits into from
Jul 20, 2021
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
3 changes: 1 addition & 2 deletions doc/docs/Build_From_Source.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ This flag enables some experimental support for [OpenMP](https://en.wikipedia.or

By default, the C++ arrays used in Meep to store the time-domain fields ($\mathbf{E}$, $\mathbf{D}$, $\mathbf{H}$, $\mathbf{B}$) and materials ($\varepsilon$, $\mu$) are defined using [double-precision floating point](https://en.wikipedia.org/wiki/Double-precision_floating-point_format). Updating the fields arrays generally dominates the computational cost of the simulation because it occurs at every voxel in the cell and at every timestep. Because [discretization errors](https://en.wikipedia.org/wiki/Discretization_error) which include the [discontinuous material interfaces](Subpixel_Smoothing.md) as well as the [numerical dispersion](https://en.wikipedia.org/wiki/Numerical_dispersion) of the Yee grid typically dominates the [floating-point roundoff error](https://en.wikipedia.org/wiki/Round-off_error), the fields and materials arrays can be defined using [single-precision floating point](https://en.wikipedia.org/wiki/Single-precision_floating-point_format) to provide a significant speedup by reducing the [memory bandwidth](https://en.wikipedia.org/wiki/Memory_bandwidth) often without any loss in simulation accuracy.

This feature requires one to pass the `--enable-single` flag to
the Meep `configure` script before compiling.
This feature requires one to pass the `--enable-single` flag to the Meep `configure` script before compiling. In Python, you can determine whether the Meep module has been compiled with single-precision floating point using the boolean function `meep.is_single_precision()`.

As a demonstration of the potential improvement in runtime performance, for a benchmarking experiment based on [computing the light-extraction efficiency of an OLED](https://gist.github.com/oskooi/f745c467ae54e192d5c8340eace9a780) which includes PMLs, DFT flux monitors, and Lorentzian susceptibilities (for material dispersion), the timestepping rate (s/step) for the single-precision case using 20 MPI processes was *less than half* that of double precision.

Expand Down
4 changes: 4 additions & 0 deletions python/meep.i
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,10 @@ size_t get_realnum_size() {
return sizeof(meep::realnum);
}

bool is_single_precision() {
return sizeof(meep::realnum) == sizeof(float);
}

meep::structure *create_structure_and_set_materials(vector3 cell_size,
std::vector<meep_geom::dft_data> dft_data_list_,
std::vector<meep::volume> pml_1d_vols_,
Expand Down