1
votes

I have rewrote my MATLAB function in C using mex and it++, but my mex implementation is a lot slower than my MATLAB function. I was wondering if anyone could tell me what I am doing wrong. MATLAB

for idx = 1:length(eps_r)
    if (idx == 1) || (eps_r(idx) ~= eps_r(idx-1))
        v_p     = c/sqrt(eps_r(idx)); % m/s - Propogation Velocity
        dz      = v_p*dt/2;
        k       = 2*w/v_p; % rad/m
        z_shift = exp(1i*dz*sqrt((repmat(k,1,size(data,2))).^2-(repmat(kx,size(data,1),1)).^2));
    end

    fk_data(idx,:) = ifft(mean(data))*exp(-1i*2*pi*freq(1)*time(idx));
    data    = data.*z_shift;
end

MEX with IT++

 for(int idx = 0; idx < eps_r.size();  idx++ )
 {
     if ( (idx == 1) || (eps_r(idx) != eps_r(idx-1) ) )
     {
         v_p =  2.9979e+08 / sqrt(eps_r(0));

         dz = v_p * time(0)/2;
         k = 2 * w / v_p;

         for(int y = 0 ; y < z_shift.size(); y++) 
             z_shift(y) = exp(dz * i * sqrt(pow(z_shift_pt1(y),2) - pow(z_shift_pt2(y),2)));
     }

     fk_data =  ifft(complex_mean(data)) * exp(-i * 2 * itpp::pi * freq(0) * time(idx));
     data = elem_mult(data,z_shift);

}

}
1
Do you compile with full optimization (-O3 or at least -O2) activated? Did you try to profile your mex function? - Thilo
No, I haven't compiled any optimization parameters. I profiled my mex function using the profiler since I am doing a function call to it - Carnez Davis
Also, when I tried using -O3 or -O2, mex complained about it not being a valid option - Carnez Davis
mex doesn't support the -O3 optimization switch, that is gcc specific. For mex, the corresponding switch is -O. Also, mex turns on optimizations by default unless you use the -g switch to suppress this. If you're using gcc to compile, you could try adding COMPFLAGS="$COMPFLAGS -O3" (on Windows), it's CFLAGS on UNIX (check the link). Lastly, MATLAB is very efficient when it comes to linear algebra, and it shouldn't be very surprising that your mex file is not able to beat it at its own game. - Praetorian
Hmm, I always compile my mex files directly with gcc - thus my misunderstanding with the compiler flags. In MATLAB: What is the call that costs you the time you want to optimize? If it is the FFT, i would assume that you will not get better than MATLAB. If it is the loop, your MEX function might have chances. @Praetorian: As far as I see, there is not too much linear algebra involved. - Thilo

1 Answers

1
votes

How big is the IFFT? If that's where the code spends most of its time, then you cannot expect significant boost from porting to C++. The documentation for IT++ FFT function itself states that the library's memory management is not optimal and calling FFTW directly may be faster. Matlab, on the other hand, is highly optimized when it comes to calling such basic functions.

Recent versions of Matlab are based on Just-In-Time (JIT) compilation and may perform better than naive C++ implementations.

[Finally, I believe IT++ is not the fastest C++ matrix library around. You may find Armadillo or Eigen faster depending on your use case].