Skip to content

Commit

Permalink
add unroll test
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Dec 29, 2021
1 parent 8609a61 commit 5ca8e47
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 7 deletions.
4 changes: 4 additions & 0 deletions 04/8_benchmark/01/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ project(testbench LANGUAGES CXX)
add_executable(testbench
main.cpp
aos.cpp
aos_aligned.cpp
soa.cpp
soa_size_t.cpp
soa_unroll.cpp
aosoa.cpp
)

find_package(OpenMP REQUIRED)
Expand Down
3 changes: 1 addition & 2 deletions 04/8_benchmark/01/aos.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <cstdint>
#include "common.h"

namespace aos {
Expand All @@ -12,7 +11,7 @@ struct Point {
Point ps[N];

void compute() {
for (std::size_t i = 0; i < N; i++) {
for (int i = 0; i < N; i++) {
ps[i].x = ps[i].x + ps[i].y + ps[i].z;
}
}
Expand Down
20 changes: 20 additions & 0 deletions 04/8_benchmark/01/aos_aligned.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "common.h"

namespace aos_aligned {

struct Point {
float x;
float y;
float z;
char padding[4];
};

Point ps[N];

void compute() {
for (int i = 0; i < N; i++) {
ps[i].x = ps[i].x + ps[i].y + ps[i].z;
}
}

}
21 changes: 21 additions & 0 deletions 04/8_benchmark/01/aosoa.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "common.h"

namespace aosoa {

struct Point {
float x[M];
float y[M];
float z[M];
};

Point ps[N / M];

void compute() {
for (int i = 0; i < N / M; i++) {
for (int j = 0; j < M; j++) {
ps[i].x[j] = ps[i].x[j] + ps[i].y[j] + ps[i].z[j];
}
}
}

}
3 changes: 2 additions & 1 deletion 04/8_benchmark/01/common.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once

#define N (1<<20)
#define N (1<<16)
#define M (1<<4)
28 changes: 28 additions & 0 deletions 04/8_benchmark/01/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,43 @@ namespace aos {
void compute();
}

namespace aos_aligned {
void compute();
}

namespace soa {
void compute();
}

namespace soa_size_t {
void compute();
}

namespace soa_unroll {
void compute();
}

namespace aosoa {
void compute();
}

int main() {
profile(1000, "aos", [&] {
aos::compute();
});
profile(1000, "aos_aligned", [&] {
aos_aligned::compute();
});
profile(1000, "soa", [&] {
soa::compute();
});
profile(1000, "soa_size_t", [&] {
soa_size_t::compute();
});
profile(1000, "soa_unroll", [&] {
soa_unroll::compute();
});
profile(1000, "aosoa", [&] {
aosoa::compute();
});
}
4 changes: 2 additions & 2 deletions 04/8_benchmark/01/profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ static inline void profile(int times, Name const &name, Func const &func) {
func();
}
auto t1 = std::chrono::steady_clock::now();
long dt = std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0).count() / times;
std::cout << name << ": " << dt << " us" << std::endl;
long dt = std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count() / times;
std::cout << name << ": " << dt << " ns" << std::endl;
}
3 changes: 1 addition & 2 deletions 04/8_benchmark/01/soa.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <cstdint>
#include "common.h"

namespace soa {
Expand All @@ -12,7 +11,7 @@ struct Point {
Point ps;

void compute() {
for (std::size_t i = 0; i < N; i++) {
for (int i = 0; i < N; i++) {
ps.x[i] = ps.x[i] + ps.y[i] + ps.z[i];
}
}
Expand Down
20 changes: 20 additions & 0 deletions 04/8_benchmark/01/soa_size_t.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <cstdint>
#include "common.h"

namespace soa_size_t {

struct Point {
float x[N];
float y[N];
float z[N];
};

Point ps;

void compute() {
for (std::size_t i = 0; i < N; i++) {
ps.x[i] = ps.x[i] + ps.y[i] + ps.z[i];
}
}

}
24 changes: 24 additions & 0 deletions 04/8_benchmark/01/soa_unroll.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "common.h"

namespace soa_unroll {

struct Point {
float x[N];
float y[N];
float z[N];
};

Point ps;

void compute() {
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC unroll 32
#elif defined(_MSC_VER)
#pragma unroll 32
#endif
for (int i = 0; i < N; i++) {
ps.x[i] = ps.x[i] + ps.y[i] + ps.z[i];
}
}

}

0 comments on commit 5ca8e47

Please sign in to comment.