forked from abacusmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 235
feat(pw/fft): CPE-DFTI sticks FFT as a factory-selected backend (FFT_SWDFTI) #7481
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
Open
A-006
wants to merge
6
commits into
deepmodeling:develop
Choose a base branch
from
A-006:feat/swfft-dfti
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5cdb8ea
feat(pw/fft): CPE-DFTI sticks FFT as a factory backend (FFT_SWDFTI) +…
A-006 bcb162d
docs(pw/fft): remove HOWTO_swfft_dfti.md
f1eefc5
Merge branch 'develop' into feat/swfft-dfti
A-006 b780c63
fix(pw/fft): address Copilot review on SWDFTI backend
980be2d
ci: retrigger CI (flaky 17_DS_DFTU/01_LCAO_SPIN_S2_Z)
556c2b8
Merge branch 'develop' into feat/swfft-dfti
A-006 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,122 @@ | ||
| #include "fft_swdfti.h" | ||
|
|
||
| #include <cstring> | ||
| #include <cstdlib> | ||
| #include <mutex> | ||
| extern "C" { | ||
| #include "swfft.h" // xMath-SACA swFFT DFTI API (CPE) | ||
| } | ||
|
|
||
| namespace ModuleBase | ||
| { | ||
|
|
||
| template <> | ||
| void FFT_SWDFTI<double>::setupFFT() | ||
| { | ||
| // build all the FFTW plans / buffers exactly as the CPU backend does | ||
| FFT_CPU<double>::setupFFT(); | ||
|
|
||
| if (std::getenv("ABACUS_NO_DFTI") != nullptr) { return; } // A/B: keep FFTW | ||
|
|
||
| // thread-safe one-time CPE spawn (setupFFT may be reached from >1 thread) | ||
| static std::once_flag dfti_athread_once; | ||
| std::call_once(dfti_athread_once, []() { DftiInitAthread(DFTI_SPAWN_QUICK); }); | ||
|
|
||
| // batched 1D-z: ns transforms of length nz, contiguous (stride 1, distance nz), in-place | ||
| DFTI_DESCRIPTOR_HANDLE hz = nullptr; | ||
| DftiCreateDescriptor(&hz, DFTI_DOUBLE, DFTI_COMPLEX, 1, (DFTI_LONG)this->nz); | ||
| DftiSetValue(hz, DFTI_NUMBER_OF_TRANSFORMS, (DFTI_LONG)this->ns); | ||
| DftiSetValue(hz, DFTI_INPUT_DISTANCE, (DFTI_LONG)this->nz); | ||
| DftiSetValue(hz, DFTI_OUTPUT_DISTANCE, (DFTI_LONG)this->nz); | ||
| DftiSetValue(hz, DFTI_PLACEMENT, (DFTI_LONG)DFTI_INPLACE); | ||
| DftiCommitDescriptor(hz); | ||
| this->dftiz = (void*)hz; | ||
|
|
||
| // strided 1D-x: nx-length, (nplane*ny) transforms, stride npy, distance 1 | ||
| // (only the xprime / non-gamma k-point layout). y stays on FFTW. | ||
| if (this->xprime && !this->gamma_only) | ||
| { | ||
| const int npy_ = this->nplane * this->ny; | ||
| DFTI_DESCRIPTOR_HANDLE hx = nullptr; | ||
| DftiCreateDescriptor(&hx, DFTI_DOUBLE, DFTI_COMPLEX, 1, (DFTI_LONG)this->nx); | ||
| DftiSetValue(hx, DFTI_NUMBER_OF_TRANSFORMS, (DFTI_LONG)npy_); | ||
| { DFTI_LONG st[2] = {0, (DFTI_LONG)npy_}; DftiSetValue(hx, DFTI_INPUT_STRIDES, st); DftiSetValue(hx, DFTI_OUTPUT_STRIDES, st); } | ||
| DftiSetValue(hx, DFTI_INPUT_DISTANCE, (DFTI_LONG)1); | ||
| DftiSetValue(hx, DFTI_OUTPUT_DISTANCE, (DFTI_LONG)1); | ||
| DftiSetValue(hx, DFTI_PLACEMENT, (DFTI_LONG)DFTI_INPLACE); | ||
| DftiCommitDescriptor(hx); | ||
| this->dftix = (void*)hx; | ||
| } | ||
| } | ||
|
|
||
| template <> | ||
| void FFT_SWDFTI<double>::cleanFFT() | ||
| { | ||
| FFT_CPU<double>::cleanFFT(); | ||
| // release the DFTI descriptors before dropping the handles (else they leak) | ||
| if (this->dftiz != nullptr) | ||
| { | ||
| DFTI_DESCRIPTOR_HANDLE hz = (DFTI_DESCRIPTOR_HANDLE)this->dftiz; | ||
| DftiFreeDescriptor(&hz); | ||
| this->dftiz = nullptr; | ||
| } | ||
| if (this->dftix != nullptr) | ||
| { | ||
| DFTI_DESCRIPTOR_HANDLE hx = (DFTI_DESCRIPTOR_HANDLE)this->dftix; | ||
| DftiFreeDescriptor(&hx); | ||
| this->dftix = nullptr; | ||
| } | ||
| } | ||
|
A-006 marked this conversation as resolved.
|
||
|
|
||
| template <> | ||
| void FFT_SWDFTI<double>::fftzfor(std::complex<double>* in, std::complex<double>* out) const | ||
| { | ||
| if (this->dftiz == nullptr) { FFT_CPU<double>::fftzfor(in, out); return; } | ||
| if (in != out) std::memcpy(out, in, sizeof(std::complex<double>) * (size_t)this->nz * (size_t)this->ns); | ||
| DftiComputeForward((DFTI_DESCRIPTOR_HANDLE)this->dftiz, (void*)out); | ||
| } | ||
|
|
||
| template <> | ||
| void FFT_SWDFTI<double>::fftzbac(std::complex<double>* in, std::complex<double>* out) const | ||
| { | ||
| if (this->dftiz == nullptr) { FFT_CPU<double>::fftzbac(in, out); return; } | ||
| if (in != out) std::memcpy(out, in, sizeof(std::complex<double>) * (size_t)this->nz * (size_t)this->ns); | ||
| DftiComputeBackward((DFTI_DESCRIPTOR_HANDLE)this->dftiz, (void*)out); | ||
| } | ||
|
|
||
| template <> | ||
| void FFT_SWDFTI<double>::fftxyfor(std::complex<double>* in, std::complex<double>* out) const | ||
| { | ||
| const int npy = this->nplane * this->ny; | ||
| if (this->xprime && this->dftix != nullptr) | ||
| { | ||
| if (in != out) std::memcpy(out, in, sizeof(std::complex<double>) * (size_t)this->nx * (size_t)npy); | ||
| DftiComputeForward((DFTI_DESCRIPTOR_HANDLE)this->dftix, (void*)out); // x via CPE | ||
| for (int i = 0; i < this->lixy + 1; ++i) // y via FFTW | ||
| fftw_execute_dft(this->planyfor, (fftw_complex*)&out[i * npy], (fftw_complex*)&out[i * npy]); | ||
| for (int i = this->rixy; i < this->nx; ++i) | ||
| fftw_execute_dft(this->planyfor, (fftw_complex*)&out[i * npy], (fftw_complex*)&out[i * npy]); | ||
| return; | ||
| } | ||
| FFT_CPU<double>::fftxyfor(in, out); // non-xprime / disabled -> FFTW | ||
| } | ||
|
|
||
| template <> | ||
| void FFT_SWDFTI<double>::fftxybac(std::complex<double>* in, std::complex<double>* out) const | ||
| { | ||
| const int npy = this->nplane * this->ny; | ||
| if (this->xprime && this->dftix != nullptr) | ||
| { | ||
| if (in != out) std::memcpy(out, in, sizeof(std::complex<double>) * (size_t)this->nx * (size_t)npy); | ||
| for (int i = 0; i < this->lixy + 1; ++i) // y via FFTW | ||
| fftw_execute_dft(this->planybac, (fftw_complex*)&out[i * npy], (fftw_complex*)&out[i * npy]); | ||
| for (int i = this->rixy; i < this->nx; ++i) | ||
| fftw_execute_dft(this->planybac, (fftw_complex*)&out[i * npy], (fftw_complex*)&out[i * npy]); | ||
| DftiComputeBackward((DFTI_DESCRIPTOR_HANDLE)this->dftix, (void*)out); // x via CPE | ||
| return; | ||
| } | ||
| FFT_CPU<double>::fftxybac(in, out); // non-xprime / disabled -> FFTW | ||
| } | ||
|
|
||
| template class FFT_SWDFTI<double>; | ||
| } // namespace ModuleBase | ||
This file contains hidden or 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,38 @@ | ||
| #ifndef FFT_SWDFTI_H | ||
| #define FFT_SWDFTI_H | ||
| // CPE-accelerated CPU FFT backend: subclasses FFT_CPU and overrides only the | ||
| // local 1D sticks FFTs (batched z, strided x) with the Sunway swFFT xMath DFTI | ||
| // API (offloaded to the 64 CPEs via DftiInitAthread). Everything else (plan | ||
| // setup, 2D-xy y-direction, r2c/c2r, box 3D) is inherited from FFT_CPU/FFTW. | ||
| // Compiled only on Sunway (USE_SWDFTI) and selected by the FFT factory in | ||
| // FFT_Bundle for device "cpu" -- so FFT_CPU itself stays free of any DFTI macro. | ||
| #include <complex> | ||
|
|
||
| #include "fft_cpu.h" | ||
|
A-006 marked this conversation as resolved.
|
||
|
|
||
| namespace ModuleBase | ||
| { | ||
| template <typename FPTYPE> | ||
| class FFT_SWDFTI : public FFT_CPU<FPTYPE> | ||
| { | ||
| public: | ||
| FFT_SWDFTI() {}; | ||
| FFT_SWDFTI(const int fft_mode_in) : FFT_CPU<FPTYPE>(fft_mode_in) {}; | ||
| ~FFT_SWDFTI() {}; | ||
|
|
||
| void setupFFT() override; | ||
| void cleanFFT() override; | ||
|
|
||
| void fftzfor(std::complex<FPTYPE>* in, std::complex<FPTYPE>* out) const override; | ||
| void fftzbac(std::complex<FPTYPE>* in, std::complex<FPTYPE>* out) const override; | ||
| void fftxyfor(std::complex<FPTYPE>* in, std::complex<FPTYPE>* out) const override; | ||
| void fftxybac(std::complex<FPTYPE>* in, std::complex<FPTYPE>* out) const override; | ||
|
|
||
| private: | ||
| // swFFT DFTI descriptors: z (batched ns x nz contiguous) and x (strided). | ||
| // y stays on FFTW (CPE loses on the small per-slice y-batch). null => FFTW. | ||
| void* dftiz = nullptr; | ||
| void* dftix = nullptr; | ||
| }; | ||
| } // namespace ModuleBase | ||
| #endif | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.