Datastructure
Exercise0 : Particle datastructure
In hands-on/memory/datastructure Modify Particle.h Modify the class GoodParticle with the following members:
- 1
const std::stringto hold the particle’s name; - 3
doubles for the x, y, z velocities - 3
bools to mark if there has been a collision along the x, y z directions - 1
floatfor the mass - 1
floatfor the energy - 3
doublesfor the px, py, pz coordinates - 1
const intfor the particle’s id
What is the best order for your members?
compile and run good_particle.cc to check the size of your GoodParticle class
Exercise1: Access Pattern
Now let’s use our GoodParticle (nicely organized struct/class) and let’s do some computations
- Write a function to initialize a collectin of
NGoodParticles- The function should also initialize all the memebers of each object
- Write another function that takes as input the collection of
GoodParticles andx_maxvalue- Iterate over all the elements and for each element:
- Generate a random number representing a time interval t
- Update the position
x-->x = x + px/m * t - If the updated position x is less than 0 or greater than
x_max- set
hit_xto true
- set
- Else:
- Set
hit_xto false and change the sign of px
- Set
- Iterate over all the elements and for each element:
- How are you accessing the members in the datastructure?
Exercise2: AoS vs SoA
In hands-on/memory/datastructure Modify Particle.h Modify the class ParticleSoA with the following members:
- Implement the functions described in Exercise1 for the SoA object
- Work with fixed size objects
- Measure the time for AoS implementation and SoA implementation and compare them
- What happens if you compile with some optimization? `-O1’, ‘-O2’, ‘-O3’?
- How does SoA interface change with respect to AoS?
Bonus: Matrix
In hands-on/memory/datastructure
Compile matrix.cc and check the timing of doing matrix multiplication in “naive” way against the “transposing” method. Then:
- Comment out the execution of the “transpose” method and compile again with -O3 and -g.
- Run
perf stat -B -e L1-dcache-loads,L1-dcache-load-misses,LLC-loads,LLC-load-misses,cycles,instructions ./a.outand have a look at the cache missing and CPU cycles
- Run
- Do the same commenting out the execution of the naive method and run perf again