Issue with malloc in Mex file.

5 views (last 30 days)
Meddi Tharun
Meddi Tharun on 20 Jan 2022
Answered: Aman on 23 Jan 2024
Hi there,
I have C code which uses FFTW's FFT function. I need to mex the C code, call it and give inputs from matlab and get C performance plots. But ,here the problem is "malloc" fuction used by fftw's functions.
As shown below in method 1, I used FFTW's malloc functions to create input and output array. While in method 2, I used mxMalloc functions (FFTW's malloc functions are not compulsory but recommended to use them, as it will take care of data aligment and which will be helpful for SIMD). With method 1, unable to use parfor. Getting error message as, "All workers are aborted during the execution of parfor loop" and with normal "for" loop method 1 is working fine. Note that, fft function is not called but just IO's are created and deallocated.
Eventhough method 2 is used for allocating memory, FFTW's FFT function uses there functions. 1)fftwf_plan_dft_1d 2)fftwf_execute 3)fftwf_destroy_plan. The function "fftwf_plan_dft_1d" uses malloc as shown here. Because of it I'm unable to use FFTW's FFT function inside parfor.
How to handle this problem? Is there any way to use FFTW ?.
%Method 1
fftwf_complex* inputArray = ffwtf_malloc(1024*sizeof(fftwf_complex));
fftwf_complex* outputArray = ffwtf_malloc(1024*sizeof(fftwf_complex));
//fft_function_call is commented.
fftwf_free(inputArray);
fftwf_free(outputArray);
%Method 2
fftwf_complex* inputArray = mxMalloc(1024*sizeof(fftwf_complex));
fftwf_complex* outputArray = mxMalloc(1024*sizeof(fftwf_complex));
//fft_function_call is commented.
mxFree(inputArray);
mxFree(outputArray);

Answers (1)

Aman
Aman on 23 Jan 2024
Hi Meddi,
As per my understanding, you have written a mex code that utilizes the FFTW library and are facing issues while using parallelization.
Without the actual code, it is quite difficult to tell the error upfront, but from the explanation that you have provided, you can try the below workarounds for debugging out the issue:
I hope this helps!

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!