Skip to content
Snippets Groups Projects
Commit ac6244c0 authored by Mattia Mancini's avatar Mattia Mancini
Browse files

Add memory test

parent 631b5b33
No related branches found
No related tags found
No related merge requests found
#include <access_to_memory.h>
#include <benchmark/benchmark.h>
#include <iostream>
#include <memory>
constexpr size_t MB = 1024 * 1024;
class InitializeInput : public benchmark::Fixture {
public:
void SetUp(::benchmark::State& state) {
// DEFINES A LARGE COUPLES OF ARRAYS UNALIGNED
constexpr size_t bytes = 40 * MB;
array_size = bytes / sizeof(float);
A = static_cast<float*>(malloc(bytes));
B = static_cast<float*>(malloc(bytes));
// Initialize matrices with random values
Initialize(A, array_size);
Initialize(B, array_size);
}
void TearDown(::benchmark::State& state) {
// Free the allocated memory
std::free(A);
std::free(B);
}
float* A;
float* B;
size_t array_size;
};
BENCHMARK_F(InitializeInput, MemoryAccessRandomAccessData)
(benchmark::State& state) {
for (auto _ : state) {
for (size_t t = 0; t < 1000; t++) {
size_t pos = (t * array_size * 1000000000) % array_size;
simply_multiply(A, pos);
}
}
}
BENCHMARK_F(InitializeInput, MemoryAccessBigJumps)(benchmark::State& state) {
for (auto _ : state) {
for (size_t t = 0; t < 1000; t++)
simply_multiply(A, (t * 384000) % array_size);
}
}
BENCHMARK_F(InitializeInput, MemoryAccessMediumJumpsAccessData)
(benchmark::State& state) {
for (auto _ : state) {
for (size_t t = 0; t < 1000; t++) {
const size_t pos = (t * 49000) % array_size;
simply_multiply(A, pos);
}
}
}
BENCHMARK_F(InitializeInput, MemoryAccessSmallJumpsAccessData)
(benchmark::State& state) {
for (auto _ : state) {
for (size_t t = 0; t < 1000; t++) {
const size_t pos = (t * 3) % array_size;
simply_multiply(A, pos);
}
}
}
BENCHMARK_F(InitializeInput, MemoryAccessNoJump)
(benchmark::State& state) {
for (auto _ : state) {
for (size_t t = 0; t < 1000; t++) {
const size_t pos = (t) % array_size;
simply_multiply(A, pos);
}
}
}
BENCHMARK_F(InitializeInput, MemoryAccessMultiplyCopyNoJump)
(benchmark::State& state) {
for (auto _ : state) {
for (size_t t = 0; t < 1000; t++) {
// Keeps an operation just for sake of comparison
const size_t pos = (t) % array_size;
multiply_copy(A, B, pos);
}
}
}
BENCHMARK_F(InitializeInput, MemoryAccessMultiplyCopyMultipleCallsNoJump)
(benchmark::State& state) {
for (auto _ : state) {
for (size_t t = 0; t < 1000; t++) {
// Keeps an operation just for sake of comparison
const size_t pos = (t) % array_size;
multiply_copy_not_inlined(A, B, pos);
}
}
}
BENCHMARK_F(InitializeInput, MemoryAccessMultiplyCopyBulkNoJump)
(benchmark::State& state) {
for (auto _ : state) {
for (size_t t = 0; t < 10; t++) {
// Keeps an operation just for sake of comparison
const size_t pos = (t) % array_size;
multiply_copy_multiple(A, B, pos * 100, 100);
}
}
}
BENCHMARK_F(InitializeInput, MemoryAccessMultiplyCopyBulkVectorizedNoJump)
(benchmark::State& state) {
for (auto _ : state) {
for (size_t t = 0; t < 10; t++) {
// Keeps an operation just for sake of comparison
const size_t pos = (t) % array_size;
multiply_copy_multiple(A, B, pos * 100, 100);
}
}
}
#include <benchmark/benchmark.h>
BENCHMARK_MAIN();
...@@ -109,5 +109,3 @@ BENCHMARK_F(InitializeInput, MatrixMultiplicationSSE) ...@@ -109,5 +109,3 @@ BENCHMARK_F(InitializeInput, MatrixMultiplicationSSE)
} }
} }
#endif #endif
BENCHMARK_MAIN();
#include "matrix_multiplication.h"
void multiply_copy_not_inlined(float* input, float* out, size_t index) {
out[index] = 2.0f * input[index] * index;
}
void multiply_copy_multiple(float* input, float* out, size_t index,
size_t batch_size) {
for (int i = 0; i < batch_size; i++) {
out[index + i] = 2.0f * input[index + i] * (index + i);
}
}
void multiply_copy_multiple_vectorized(float* input, float* out, size_t index,
size_t batch_size) {
#pragma omp simd
for (int i = 0; i < batch_size; i++) {
out[index + i] = 2.0f * input[index + i] * (index + i);
}
}
\ No newline at end of file
#ifndef ACCESS_TO_MEMORY_H_
#define ACCESS_TO_MEMORY_H_
#include <algorithm>
#include <random>
inline void Initialize(float* a, size_t size) {
// Initialize matrices with random complex values
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<float> dis(-1.0, 1.0);
for (size_t i = 0; i < size; i++) {
a[i] = dis(gen);
}
}
inline void simply_multiply(float* data, size_t index) {
data[index] = 2.0f * data[index] * index;
}
inline void multiply_copy(float* input, float* out, size_t index) {
out[index] = 2.0f * input[index] * index;
}
void multiply_copy_not_inlined(float* input, float* out, size_t index);
void multiply_copy_multiple(float* input, float* out, size_t index,
size_t batch_size);
#endif // ACCESS_TO_MEMORY
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment