Skip to content

Instantly share code, notes, and snippets.

@RadimBaca
Last active April 1, 2019 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RadimBaca/5bb0f55e4a6227cbd40167b26702121e to your computer and use it in GitHub Desktop.
Save RadimBaca/5bb0f55e4a6227cbd40167b26702121e to your computer and use it in GitHub Desktop.
Demostrate TLB overflow. The code has much better times for lower `array_count` (1-32) even though the number of processed numbers is constant. This behaviour occurs due to the TLB overflow in the case of large `array_count`.
#include <vector>
#include <array>
#include <algorithm>
#include <chrono>
#include <iostream>
#include <string>
//constexpr int N = 128;
constexpr int SIZE = 1024 * 150;
constexpr int REPEAT = 1024 * 10;
using TimerClock = std::chrono::system_clock;
std::vector<std::vector<int>> array_factory(int count)
{
std::vector<std::vector<int>> v(count);
std::for_each(v.begin(), v.end(), [](auto& data)
{
int counter = 0;
data.resize(SIZE);
std::for_each(data.begin(), data.end(), [&counter](auto& item) { item = counter++; });
});
return v;
}
int main(int argn, char* argv[])
{
if (argn != 2)
{
std::cout << "usage: TBL_hardware_effect array_count\n";
return 1;
}
auto count = atoi(argv[1]);
int size = SIZE / count;
std::vector<std::vector<int>> v = array_factory(count);
std::vector<int> result(count * size);
auto start = TimerClock::now();
for (auto rep = 0; rep < REPEAT; rep++)
{
auto counter = 0;
for (auto position = 0; position < size; position++)
{
for (auto vector = 0; vector < count; vector++)
{
result[counter++] = v[vector][position];
}
}
}
auto end = TimerClock::now();
std::cout << std::to_string(std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()) << " [ms]\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment