Highly variable execution time of mex file
3 次查看(过去 30 天)
显示 更早的评论
Hello,
I am running an EM type minimization where the E step is essentially a Kalman filter+smoother with a few minor additional details. I have a matlab version and a mex C version. The C version uses BLAS/LAPACK for the multiplications and divisions. I am running both at each EM step to compare timing and accuracy etc. but I have noticed that the mex timing is highly variable, ranging from 0.5 to 1.0 seconds for a pass while the Matlab version is more stable at 1.53 to 1.75. Below are some example tic-tocs. Each pass is doing exactly the same thing (same memory management, same lapack function calls etc.). The question is, why is the mex version so much more variable in its timing? Is this known behavior for some reason? Its not the end of the world, I'm just curious as it probably means I have more optimization to do.
For the record, the mex file is using persistent variables a few mxCalloc's and mxFree's, and the usual math suspects (dsymm, dgemv, dgemm, dposv, dsyr, dger, and memcpy) and the gcc compiler.
Thanks in advance.
Jason
(First is matlab then mex then the next matlab etc.):
Elapsed time is 1.559690 seconds.
Elapsed time is 0.768989 seconds.
Elapsed time is 1.607263 seconds.
Elapsed time is 0.799670 seconds.
Elapsed time is 1.745036 seconds.
Elapsed time is 0.871407 seconds.
Elapsed time is 1.603699 seconds.
Elapsed time is 0.842408 seconds.
Elapsed time is 1.542582 seconds.
Elapsed time is 0.827200 seconds.
Elapsed time is 1.570363 seconds.
Elapsed time is 0.516239 seconds.
Elapsed time is 1.566661 seconds.
Elapsed time is 1.048197 seconds.
Elapsed time is 1.720891 seconds.
Elapsed time is 0.852531 seconds.
Elapsed time is 1.577776 seconds.
Elapsed time is 0.763253 seconds.
Elapsed time is 1.557377 seconds.
Elapsed time is 0.675226 seconds.
Elapsed time is 1.558302 seconds.
Elapsed time is 0.607389 seconds.
Elapsed time is 1.554754 seconds.
Elapsed time is 0.849879 seconds.
Elapsed time is 1.559411 seconds.
Elapsed time is 0.863618 seconds.
Elapsed time is 1.588187 seconds.
Elapsed time is 0.824819 seconds.
Elapsed time is 1.616166 seconds.
Elapsed time is 0.829500 seconds.
Elapsed time is 1.617957 seconds.
Elapsed time is 0.909654 seconds.
Elapsed time is 1.595710 seconds.
Elapsed time is 0.523104 seconds.
Elapsed time is 1.532362 seconds.
Elapsed time is 0.819856 seconds.
0 个评论
回答(2 个)
James Tursa
2013-12-20
I have no real explanation for this behavior (underlying cache system?), but I have noticed it on many occasions for some of my mex routines as well (32-bit Windows XP). My experience is just like above, seeming to cycle between two different values, which is why I have gotten into the habit of always timing functions with a multiple of 2 iterations and then averaging.
Jan
2013-12-20
There seems to be a pattern in the runtimes: Slow and fast runs are alternating.
I've seen this behavior, when an array of doubles is stored either matching in the 128 bit cache-line-size or not. All of my tests seems to imply, that mxMalloc reserves memory aligned with the cacheline size, but I did not find this reliably documented.
mxCalloc bahve differently depending on the availability of free zeroed memory, which is controlled by the operating system. You could try to allocate the memory persistently to reduce this effect.
0 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!