Skip to content

Instantly share code, notes, and snippets.

@melvyniandrag
Created June 26, 2018 03:15
Show Gist options
  • Save melvyniandrag/40c5ff5f03ede3445fbabb74c456be58 to your computer and use it in GitHub Desktop.
Save melvyniandrag/40c5ff5f03ede3445fbabb74c456be58 to your computer and use it in GitHub Desktop.
Experimenting with memory access timing on pic32
const unsigned long NUM_CACHE_LINES = 16;
const unsigned long CACHE_BITS = 128;
const unsigned long DATA_ELEMS_PER_LINE = CACHE_BITS / sizeof( unsigned long );
const unsigned long N = 1979;
const int N_ITER = 10000;
volatile unsigned long stride = NUM_CACHE_LINES * DATA_ELEMS_PER_LINE;
//volatile unsigned long STRIDE = DATA_ELEMS_PER_LINE;
volatile unsigned long multiplier = 1;
unsigned long bigArr[N] = {0};
unsigned long simpleStride( unsigned long iter ) {
return ( iter * multiplier ) % N;
}
unsigned long cacheStride( unsigned long iter ) {
return ( iter * stride ) % N;
}
void timerFunction( unsigned long (*f)(unsigned long) , const char* strideFunName) {
unsigned long sum = 0;
unsigned long time = millis();
for ( int iter = 0; iter < N_ITER; ++iter ) {
for ( unsigned long i = 0; i < N; ++i ) {
sum += bigArr[f(i)];
}
}
time = millis() - time;
Serial.printf( "%s | time: %lu sum : %lu\n", strideFunName, time, sum );
}
void setup() {
Serial.begin( 9600 );
for ( unsigned long i = 0; i < N; ++i) {
bigArr[i] = i;
}
Serial.printf("Stride for this test: %lu\n", stride );
}
void loop() {
timerFunction( simpleStride, "sequential access " );
timerFunction( cacheStride, "stided access " );
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment