Issue with malloc in Mex file.

6 次查看(过去 30 天)
Meddi Tharun
Meddi Tharun 2022-1-20
回答: Aman 2024-1-23
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);

回答(1 个)

Aman
Aman 2024-1-23
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!

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by