Skip to content

[pull] master from parallel101:master #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added 16/01/.tasks
Empty file.
14 changes: 14 additions & 0 deletions 16/01/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.18)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_CXX_STANDARD 17)

project(main LANGUAGES CXX)

add_executable(main main.cpp)

find_package(OpenCV REQUIRED COMPONENTS core videoio)
target_link_libraries(main PUBLIC OpenCV::core)
target_link_libraries(main PUBLIC OpenCV::videoio)
5 changes: 5 additions & 0 deletions 16/01/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <cstdio>

int main() {
return 0;
}
Binary file modified 16/slides.pptx
Binary file not shown.
Empty file added specalloc/.tasks
Empty file.
10 changes: 10 additions & 0 deletions specalloc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.18)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_CXX_STANDARD 20)

project(main LANGUAGES CXX)

add_executable(main main.cpp)
129 changes: 129 additions & 0 deletions specalloc/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#include <list>
#include <vector>
#include <array>
#include <memory_resource>
#include <iostream>

template <typename Func>
auto benchmark(Func test_func, int iterations) {
const auto start = std::chrono::system_clock::now();
while (iterations-- > 0) { test_func(); }
const auto stop = std::chrono::system_clock::now();
const auto secs = std::chrono::duration<double>(stop - start);
return secs.count();
}

int main() {
constexpr int kNodes = 250000;
constexpr int kRows = 500;
constexpr int kCols = 500;
constexpr int kIters = 100;

std::cout << "list=" << benchmark([&] {
std::list<int> a;
for (int i = 0; i < kNodes; i++) {
a.push_back(i);
}
}, kIters) << '\n';

std::cout << "list_pmr=" << benchmark([&] {
std::list<int, std::pmr::polymorphic_allocator<int>> a;
for (int i = 0; i < kNodes; i++) {
a.push_back(i);
}
}, kIters) << '\n';

std::cout << "list_pmr_nobuf=" << benchmark([&] {
std::pmr::monotonic_buffer_resource mbr;
std::list<int, std::pmr::polymorphic_allocator<int>> a(&mbr);
for (int i = 0; i < kNodes; i++) {
a.push_back(i);
}
}, kIters) << '\n';

std::cout << "list_pmr_buf=" << benchmark([&] {
std::array<std::byte, kNodes * 32> buf;
std::pmr::monotonic_buffer_resource mbr(buf.data(), buf.size());
std::list<int, std::pmr::polymorphic_allocator<int>> a(&mbr);
for (int i = 0; i < kNodes; i++) {
a.push_back(i);
}
}, kIters) << '\n';

std::cout << "vector=" << benchmark([&] {
std::vector<int> a;
a.reserve(kNodes);
for (int i = 0; i < kNodes; i++) {
a.push_back(i);
}
}, kIters) << '\n';

std::cout << "vector_pmr=" << benchmark([&] {
std::vector<int, std::pmr::polymorphic_allocator<int>> a;
for (int i = 0; i < kNodes; i++) {
a.push_back(i);
}
}, kIters) << '\n';

std::cout << "vector_pmr_nobuf=" << benchmark([&] {
std::pmr::monotonic_buffer_resource mbr;
std::pmr::vector<int> a(&mbr);
for (int i = 0; i < kNodes; i++) {
a.push_back(i);
}
}, kIters) << '\n';

std::cout << "vector_pmr_buf=" << benchmark([&] {
std::array<std::byte, kNodes * 32> buf;
std::pmr::monotonic_buffer_resource mbr(buf.data(), buf.size());
std::pmr::vector<int> a(&mbr);
for (int i = 0; i < kNodes; i++) {
a.push_back(i);
}
}, kIters) << '\n';

std::cout << "vector_vector=" << benchmark([&] {
std::vector<std::vector<int>> a;
for (int i = 0; i < kCols; i++) {
auto &b = a.emplace_back();
for (int j = 0; j < kRows; j++) {
b.push_back(j);
}
}
}, kIters) << '\n';

std::cout << "vector_vector_pmr=" << benchmark([&] {
std::pmr::vector<std::pmr::vector<int>> a;
for (int i = 0; i < kCols; i++) {
auto &b = a.emplace_back();
for (int j = 0; j < kRows; j++) {
b.push_back(j);
}
}
}, kIters) << '\n';

std::cout << "vector_vector_pmr_nobuf=" << benchmark([&] {
std::pmr::monotonic_buffer_resource mbr;
std::pmr::vector<std::pmr::vector<int>> a(&mbr);
for (int i = 0; i < kCols; i++) {
auto &b = a.emplace_back();
for (int j = 0; j < kRows; j++) {
b.push_back(j);
}
}
}, kIters) << '\n';

std::cout << "vector_vector_pmr_buf=" << benchmark([&] {
std::array<std::byte, kRows * kCols * 32> buf;
std::pmr::monotonic_buffer_resource mbr(buf.data(), buf.size());
std::pmr::vector<std::pmr::vector<int>> a(&mbr);
for (int i = 0; i < kCols; i++) {
auto &b = a.emplace_back();
for (int j = 0; j < kRows; j++) {
b.push_back(j);
}
}
}, kIters) << '\n';

return 0;
}
17 changes: 17 additions & 0 deletions specalloc/ticktock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <chrono>
#include <cstdio>

#define TICK(x) auto bench_##x = std::chrono::steady_clock::now();
#define TOCK(x) std::printf("%s: %lfs\n", #x, std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::steady_clock::now() - bench_##x).count());

template <class T>
#ifdef _MSC_VER
__noinline
#else
__attribute__((noinline))
#endif
static void NOPT(T const &t) {
(void)t;
}
Empty file added specifelse/.tasks
Empty file.
35 changes: 35 additions & 0 deletions specifelse/0.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "ticktock.h"
#include "randint.h"
#include <vector>
#include <algorithm>

#if 0
__attribute__((noinline)) void uppercase(char *p, int n) {
for (int i = 0; i < n; i++) {
p[i] = ('a' <= p[i] && p[i] <= 'z') ? p[i] + 'A' - 'a' : p[i];
}
}
#else
__attribute__((noinline)) void uppercase(char *p, int n) {
for (int i = 0; i < n; i++) {
if ('a' <= p[i] && p[i] <= 'z')
p[i] = p[i] + 'A' - 'a';
}
}
#endif

int main() {
int n = (int)1e7;
std::vector<char> a(n);

for (int i = 0; i < n; i++) {
a[i] = randint<char>(0, 127);
}
std::sort(a.begin(), a.end());

TICK(test);
uppercase(a.data(), n);
TOCK(test);

return 0;
}
7 changes: 7 additions & 0 deletions specifelse/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
int func(int x) {
if (x > 0) {
return 1;
} else {
return 2;
}
}
11 changes: 11 additions & 0 deletions specifelse/2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int slow(int x) {
if (x > 0) {
return 1;
} else {
return 0;
}
}

int fast(int x) {
return (x > 0);
}
11 changes: 11 additions & 0 deletions specifelse/3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int slow(int x) {
if (x > 0) {
return 42;
} else {
return 32;
}
}

int fast(int x) {
return 32 + (x > 0) * 10;
}
47 changes: 47 additions & 0 deletions specifelse/4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <vector>
#include <algorithm>
#include "ticktock.h"
#include "randint.h"

static int ifelse_clamp(int x) {
if (x < 0) {
return 0;
} else if (x > 255) {
return 255;
} else {
return x;
}
}

static int bailan_clamp(int x) {
return (x < 0) ? 0 : ((x > 255) ? 255 : x);
}

static int addmul_clamp(int x) {
return (x >= 0) * (x <= 255) * x + (x > 255);
}

__attribute__((noinline)) void test(int *a, int n, int (*clamp)(int)) {
for (int i = 0; i < n; i++) {
a[i] = clamp(a[i]);
}
}

int main() {
std::vector<int> a((int)1e7);

std::generate(a.begin(), a.end(), randint<int, -512, 512>);
TICK(ifelse);
test(a.data(), a.size(), ifelse_clamp);
TOCK(ifelse);

std::generate(a.begin(), a.end(), randint<int, -512, 512>);
TICK(bailan);
test(a.data(), a.size(), bailan_clamp);
TOCK(bailan);

std::generate(a.begin(), a.end(), randint<int, -512, 512>);
TICK(addmul);
test(a.data(), a.size(), addmul_clamp);
TOCK(addmul);
}
Loading