How do I speed up lsim computation in a for loop?
14 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
I am using the MATLAB command lsim to simulate the response of a linear time-invariant (LTI) system to various inputs (that are independent) using a for loop. I discovered that when running multiple identical computations on independent data sets using a for loop, this computation is appropriate for parallelization using parfor. So, what I tried was running the same script with parfor instead of for and the resulting computing time was actually a little longer than the baseline case with single core. Do you have any thoughts on why that could be and whether there is any room for speeding up the computation?
For reference,
- The linear system matrices in state-space form are generated (A,B,C,D) and a state-space model object is created using those matrices: sys = ss(A,B,C,D). The sizes of the matrices, for reference, are 1626x1626, 1626x2, 813x1626 and 813x2, respectively.
- Then, to simulate the system response to various forcing frequencies (inputs), a for loop is run in which lsim command runs for each input: yOut = lsim(sys, u, time); where u = input matrix and time = corresponding time vector
I tried generating the matrices inside the parfor loop so as to minimize the data transmission time between my client and the pool workers, but this didn't show much improvement in speed. In order to check whether my implementation of parfor loop was correct, I tried evaluating the eigenvalues of the system for different system parameters (parametric sweep) and this resulted in 66% increase in computation time, suggesting that my implentation was correct. I am running the script locally on my 2019 macbook pro with 8 cores.
Thank you for your suggestions and time!
Best,
Peter
0 个评论
回答(2 个)
Jayant Gangwar
2022-10-3
Hey Young,
You may find this documentation page useful for helping decide if parfor is useful in your application, and how to best apply it.
It is always impossible to have a linear relationship between the performance and the number of working cores. This is not just for MATLAB, even for operating systems. A 4 cores computer is not 4 times faster than a 1 core computer, because there is necessary overhead for distribution, communication, and management among the cores. Meantime the speedup might be also limited by other resources, like the main memory, cache or the Internet. The starting and stopping of a parallel pool of workers will also add to the overhead of using parfor.
And some of the MATLAB functions, for example, "fft" are internally optimized as a multithread algorithm. Thus, when running these functions, it will automatically use multiple cores if possible. If we use these function in Parallel Computing Toolbox again, the performance might not get better. Sometimes might even get worse, because of the overhead. However, for most cases, Parallel Computing Toolbox will give a better performance.
It might be that the actual speed up part of the computation is not much, and the overhead is larger than the speedup therefore the performance is going down.
You can refer to this ML answer for details on how you can improve the performance of your for loops without parallelization.
0 个评论
Michal
2022-11-27
编辑:Michal
2022-11-27
Better than naive using of parfor loop is transform to your final simulation model to the sparse form ... see here
Transform model from dense (default) to the sparse form is not always benefitial, but in a case of significant sparsity of the model matrices, or in case of many appended independent models (see here) brings this trasformation very significant speed up.
And after that, if you still looking for additional speedup, is time to apply paralelization ...
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!