Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.

How to use both: FFTW3 and MKL

Sebastian_L_1
Beginner
2,706 Views
Hey! I've got the the following problem: I want to keep the flexibility within my code to switch between FFTW3 and MKL-FFT (with the purpose to test performance issues of both FFTs and thus, decide which to use in the final version). Since the MKL provides the FFTW3 Interfaces, I thought it would be possible to maintain both FFTs without touching the code. According to the MKL-manual this is possible just by linking the correct libraries to your code. The problem is, that the FFTW3 interface of MKL is not within a separate library, it is already within the "basic" MKL-Libs (e.g. libmkl_intel_lp64). Consequently I don't know whether my code uses the original FFTW3 or the MKL-interface version. How can I explicilty choose the underlying FFT or is it not possible to use MKL features together with the original FFTW3? But here in more detail: For testing purpose I'v taken one of the fftw3 examples which is provided by the MKL (dp_plan_dft_1d.c) and compiled and linked it with fftw3: icc -c dp_plan_dft_1d.c icc dp_plan_dft_1d.o -o dp_plan_dft_1d -lfftw3 which works. This example is now linked to the fftw3. Without -lfftw3 there would be undefined references. Further, my code will use other MKL-functions, so I've added a simple function (see attached code, first lines in main): vdAdd (adds to vectors element-wise): vdAdd( myMklVar, test1, test2, addtest ); // see attached code for full change Compiling and linking with icc -c dp_plan_dft_1d.c icc dp_plan_dft_1d.o -o dp_plan_dft_1d -lfftw3 -L /opt/intel/composerxe/mkl/lib/intel64/ -lmkl_core -lmkl_intel_lp64 -lmkl_sequential -lpthread -lm ./dp_plan_dft_1d works also fine. But now, the fft-functions might be taken from MKL, since the interface handler is included in the MKL-Libs. Thus, when I remove -lfftw3, the code is works also - there are no undefined references. So, how can I be sure which FFT is used? Is it selected by the order of linking the libs? (Ok - I can check which FFT is used, by calling MKL-unsupported FFTW3 functions, but that's not convenient...) I would have expected, that I have to link the MKL-FFTW3 interface explicitly with -lfftw3xc_intel (in exchange for "original" -lfftw3), otherwise there should be the undefined references for fftw functions, but obviously this is not necessary. Did I get something wrong, or is it not possible to use MKL-features and the genuine FFTW3 together in the same code? Cheers and thanks for answers! Sebastian
0 Kudos
2 Replies
mecej4
Honored Contributor III
2,706 Views

As long as you aim to build a.out or .so objects that contain only the MKL FFTW compatible routines or only the open source FFTW library routines, there should be no problems. In fact, if your code needs no MKL routines other than the FFTW-compatible routines, you list only the MKL libraries or only the FFTW libraries in your linker command line.

You can see from the manual pages for the linker ld that the first-seen library object is chosen for linking, so if you want to use the open source FFTW routines and also some non-FFTW routines from MKL, you can name the FFTW library first and the MKL libraries afterwards. 

See the online MKL Link Advisor, https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor/ .

0 Kudos
Sebastian_L_1
Beginner
2,706 Views
Thank you for your answer! Ok - I've checked this in the example above: According to the order of linking icc dp_plan_dft_1d.o -o dp_plan_dft_1d -lfftw3 -L /opt/intel/composerxe/mkl/lib/intel64/ -lmkl_core -lmkl_intel_lp64 -lmkl_sequential -lpthread -lm should use FFTW3 and not MKL-FFT. In order to test this, I've added in my example file the line int fftw_export_wisdom_to_filename(const char *filename); after creating the fft-plan, which is a FFTW3 function and not supported by MKL-FFT. Result: -> with -lfftw3 it works, i.e. the original FFTW3 was linked to the code. -> without -lfftw3 the linker has an undefined reference to fftw_export_wisdom_to_filename, i.e. MKL-FFT was linked, but did not support this function So, I've understood how it works, now. What still confuses me is the purpose of the "interfaces" directory in the MKL installdir. Out of the manual I've understood that I should build the libfftw3xc_intel_... and link this lib instead of the -lfftw3. This is, at least for me, the more intuitive way and less fault-prone. When I mess with the linker line I wouldn't even notice, that I've accidentally switched the underlying FFT... Linking is something which I don't trust 100%, since there are often surprises (e.g. the intel linker advisor says for the upper example: -L $MKL/lib/intel64/ -lmkl_intel_lp64 -lmkl_core, but only -lmkl_core -lmkl_intel_lp64 works. The first order causes: "can't find libmkl_intel_lp64") But that would be another topic ;) Cheers Sebastian
0 Kudos
Reply