Subscribe to Anarion's Blog

C++: Cache-Friendly Code

I have been working on a mathematical optimization project in C++ for quite some time now. While working with matrices and calculations, something caught my attention: the importance of contiguous memory allocation and writing cache-friendly code.
To be honest, until now, I haven't studied technical details of CPU structures except from some general knowledge. Today I was reading about caching in CPUs and how it can be exploited to improve performance and found this brilliant explanation.

Page 1 of 1

CasiOo

21 December 2014 - 08:42 AM
Very good post
Thanks for sharing
1

Anarion

22 December 2014 - 02:06 PM

CasiOo, on 21 December 2014 - 07:12 PM, said:

Very good post
Thanks for sharing :)

0

CTphpnwb

27 December 2014 - 07:13 AM
Yes, good post. I'm curious as to how this has affected your project.
0

Anarion

28 December 2014 - 04:56 PM

CTphpnwb, on 27 December 2014 - 05:43 PM, said:

Yes, good post. I'm curious as to how this has affected your project.

Thank you Chris. Regarding the effects, I have done some analysis by using std::chrono and multiplying matrices. The results are quite satisfying when multiplying 1000x1000 matrices. Although this is too much, it shows the advantage when things get heavy. Just have to work some more on the move/copy operations and further analyze different cases. It deserves a dedicated, detailed post! So stay tuned :)
0

CTphpnwb

30 December 2014 - 06:58 AM
Have you tried it on multidimensional arrays (more than 2) with varying sizes in each dimension? Something like Arr[10][20][30][40][50] would be a good test if you can do it.
0

Anarion

30 December 2014 - 02:35 PM

CTphpnwb, on 30 December 2014 - 05:28 PM, said:

Have you tried it on multidimensional arrays (more than 2) with varying sizes in each dimension? Something like Arr[10][20][30][40][50] would be a good test if you can do it.

Well, it can be implemented to work with multiple dimensions but as the optimization algorithms I am currently interested in require only 2d matrices (and vectors, which are 1xm or nx1 matrices), I decided it is best to write a fixed-dimension matrix class and focus on it's design an efficiency. The drawback is that it is only 2d and provides very limited facilities as needed by the algorithms.
As for the possibility of improvement in the more-than-2 dimensions I am not sure... it depends on the multiplication algorithm. For 2d matrices it is pretty straight-forward and can easily be exploited.
0
Page 1 of 1

S M T W T F S
1234567
891011121314
15 16 1718192021
22232425262728
293031