Subscribe to Anarion's Blog        RSS Feed
-----

C++: Cache-Friendly Code

Icon 6 Comments
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.

6 Comments On This Entry

Page 1 of 1

CasiOo Icon

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

Anarion Icon

22 December 2014 - 02:06 PM

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

Very good post
Thanks for sharing :)

You are most welcome :) Glad that it was helpful
0

CTphpnwb Icon

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

Anarion Icon

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 Icon

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 Icon

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

October 2017

S M T W T F S
1234567
891011121314
15161718192021
22 23 2425262728
293031    

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)