![]() ![]() So, I thought it would be nice to initialize a set of spline interpolation polynomials for it, and then use those to quickly compute its value (and its derivative) at any flux density needed. However, for nonlinear materials, we don’t have any neat expression for it – only a set of measurement data. We need that in our problem, and its derivative too. You see, the term reluctivity refers to the inverse of permeability of each material. Indeed, most of the time is spent on the ppval function, a built-in function for evaluating piecewise polynomials. Below, you can see what the Profiler had to say about its insides. But, in general it’s a good idea to start going through the most time-consuming parts of code, and seeing if something could be done.įor me, that calculate_reluctivity function was obviously the worst offender after vectorization. Once that’s been sorted out, things get a lot more problem-specific. Vectorization is indeed the most powerful and most general tip for Matlab. Kind of demonstrates the power of vectorization, right? 4. (Because I’m lazy.) Despite this, the code is much faster. Indeed, the last sample of each 1000-long chunk is evaluated again as the first sample of the next chunk. Contrasting that the the earlier 43 seconds, that’s a nice improvement.Īlso, note that the actual number of operations has been increased a bit. Indeed, what used to take eight seconds is now handled on three lines, about 0.3 seconds each.Īll in all, this simple change brought the execution time to 15 seconds. Additionally, all the plain expressions are much faster to evaluate, with no red shading visible beside that one function call. Results with doubly-vectorized code.Īs you can see, the earlier two function calls with 9 seconds each, have been replaced by one 11-second call. Below, you can find the profiling results after this double vectorization. I still had a loop in my code, but its length was reduced from 32 000 to 32.Īnd the results prove it was good. I simply mean I operated on sets -chunks- of 1000 samples at a time. Then I let Matlab work its magic on those.ĭue to memory limitations, I couldn’t handle all 32 000 time-samples simultaneously. Each row corresponding to a specific time-sample and so. Meaning I computed all quantities at all positions, at several instants of time (but not all), and stored them in large matrices. To speed things up, I implemented a partial vectorization in time. I initially thought the number of time-samples would be much smaller than the number of positions. Most of those array variables you see in the screenshot are in fact row-vectors (low but wide), with each entry corresponding to a different physical position in the problem.īut, I did not vectorize in the time-domain. Remember that I needed to calculate the same type of integral at thousands of different positions ? Well, yeah, but it’s still only a partial oversight on my part. In the previous post, you saw how much time was spent on repeated function calls, one for each time-sample. Okay, let’s see how vectorization would help my code. It can be done, but chopping the corners off the peg can take longer than expected, and the results are often horrible to read. Trying to force loop-based code into vector format often is like trying to ram a square peg into a round hole. What I mean is that you should adopt a vector-based mindset early on. Noticed how I recommended write vectorized code, not to vectorize it as an afterthought. Vectorization can often improve performance by a factor of 10. Just check the documentation about the particularities of each, i.e. Most can even be vectorized both row-wise and column-wise at the same time. But, in practice, pretty much all built-in Functions in Matlab support vector input. Okay, that was an exceedingly simple example. The expression in the parentheses creates a vector with the entries running from 1 to 100, while the dot-notation corresponds to element-wise power of two. In Matlab, you can do this by writing (1:100).^2. In a lower-level language, you would write a loop with running from 1 to 100, and compute inside. Let’s say that you want to calculate, ,, …. Vectorized means exactly what it sounds like – something that operates with arrays and vectors. They used to be much worse, but they’re still not stellar.įor this reason, you should try to write your code in vectorized form whenever possible. Loops are generally somewhat slow in Matlab. Remember to check the first part, well, first.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |