Skip to content

Commit

Permalink
add 8
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Jan 1, 2022
1 parent ef9f98c commit 637967c
Show file tree
Hide file tree
Showing 63 changed files with 250 additions and 8 deletions.
16 changes: 9 additions & 7 deletions 04/00/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
struct A {
static inline int other(int a) {
return a;
}
};
#include <cmath>

int func() {
return A().other(233);
void func(float *a, float *b, float *c) {
for (int i = 0; i < 1024; i++) {
float tmp = 0;
for (int j = 0; j < 1024; j++) {
tmp += a[i] * b[j];
}
c[i] += tmp;
}
}
2 changes: 1 addition & 1 deletion 04/00/run.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set -e

gcc -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
gcc -ffast-math -O3 -fopenmp -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
vim /tmp/main.S
7 changes: 7 additions & 0 deletions 04/4_simd/09_vectorize_reduction/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
float func(float *a) {
float ret = 0;
for (int i = 0; i < 1024; i++) {
ret += a[i];
}
return ret;
}
12 changes: 12 additions & 0 deletions 04/4_simd/09_vectorize_reduction/pseudo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <x86intrin.h>

float func(float *a) {
__m128 ret = _mm_setzero_ps();
for (int i = 0; i < 1024; i += 4) {
__m128 a_i = _mm_loadu_ps(&a[i]);
ret = _mm_add_ps(ret, a_i);
}
float r[4];
_mm_storeu_ps(r, ret);
return r[0] + r[1] + r[2] + r[3];
}
4 changes: 4 additions & 0 deletions 04/4_simd/09_vectorize_reduction/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e

gcc -O3 -fopenmp -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
vim /tmp/main.S
10 changes: 10 additions & 0 deletions 04/5_loops/06_loop_with_external_call/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
void other();

float func(float *a) {
float ret = 0;
for (int i = 0; i < 1024; i++) {
ret += a[i];
other();
}
return ret;
}
4 changes: 4 additions & 0 deletions 04/5_loops/06_loop_with_external_call/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e

gcc -O3 -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
vim /tmp/main.S
File renamed without changes.
11 changes: 11 additions & 0 deletions 04/5_loops/07_loop_with_inlineable_call/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
void other() {
}

float func(float *a) {
float ret = 0;
for (int i = 0; i < 1024; i++) {
ret += a[i];
other();
}
return ret;
}
4 changes: 4 additions & 0 deletions 04/5_loops/07_loop_with_inlineable_call/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e

gcc -O3 -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
vim /tmp/main.S
2 changes: 2 additions & 0 deletions 04/5_loops/08_loop_with_random_access/.vim_localrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" vim: ft=vim
nnoremap <silent> <F7> :wa<CR>:!make\|\|(echo -n .;read -n1)<CR><CR>
5 changes: 5 additions & 0 deletions 04/5_loops/08_loop_with_random_access/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void func(float *a, int *b) {
for (int i = 0; i < 1024; i++) {
a[b[i]] += 1;
}
}
4 changes: 4 additions & 0 deletions 04/5_loops/08_loop_with_random_access/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e

gcc -O3 -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
vim /tmp/main.S
2 changes: 2 additions & 0 deletions 04/5_loops/09_loop_with_skip_access/.vim_localrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" vim: ft=vim
nnoremap <silent> <F7> :wa<CR>:!make\|\|(echo -n .;read -n1)<CR><CR>
5 changes: 5 additions & 0 deletions 04/5_loops/09_loop_with_skip_access/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void func(float *a) {
for (int i = 0; i < 1024; i++) {
a[i * 2] += 1;
}
}
4 changes: 4 additions & 0 deletions 04/5_loops/09_loop_with_skip_access/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e

gcc -O3 -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
vim /tmp/main.S
2 changes: 2 additions & 0 deletions 04/5_loops/10_loop_with_ordered_access/.vim_localrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" vim: ft=vim
nnoremap <silent> <F7> :wa<CR>:!make\|\|(echo -n .;read -n1)<CR><CR>
5 changes: 5 additions & 0 deletions 04/5_loops/10_loop_with_ordered_access/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void func(float *a) {
for (int i = 0; i < 1024; i++) {
a[i] += 1;
}
}
4 changes: 4 additions & 0 deletions 04/5_loops/10_loop_with_ordered_access/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e

gcc -O3 -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
vim /tmp/main.S
2 changes: 2 additions & 0 deletions 04/5_loops/11_loop_invariant_motion/.vim_localrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" vim: ft=vim
nnoremap <silent> <F7> :wa<CR>:!make\|\|(echo -n .;read -n1)<CR><CR>
File renamed without changes.
2 changes: 2 additions & 0 deletions 04/5_loops/12_loop_invariant_failed/.vim_localrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" vim: ft=vim
nnoremap <silent> <F7> :wa<CR>:!make\|\|(echo -n .;read -n1)<CR><CR>
File renamed without changes.
2 changes: 2 additions & 0 deletions 04/5_loops/13_loop_unrolling/.vim_localrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" vim: ft=vim
nnoremap <silent> <F7> :wa<CR>:!make\|\|(echo -n .;read -n1)<CR><CR>
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions 04/7_stl/04_std_vector_aos/.vim_localrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" vim: ft=vim
nnoremap <silent> <F7> :wa<CR>:!make\|\|(echo -n .;read -n1)<CR><CR>
15 changes: 15 additions & 0 deletions 04/7_stl/04_std_vector_aos/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <vector>

struct MyVec {
float x;
float y;
float z;
};

std::vector<MyVec> a;

void func() {
for (std::size_t i = 0; i < a.size(); i++) {
a[i].x *= a[i].y;
}
}
4 changes: 4 additions & 0 deletions 04/7_stl/04_std_vector_aos/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e

gcc -O3 -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
vim /tmp/main.S
2 changes: 2 additions & 0 deletions 04/7_stl/05_std_vector_soa/.vim_localrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" vim: ft=vim
nnoremap <silent> <F7> :wa<CR>:!make\|\|(echo -n .;read -n1)<CR><CR>
15 changes: 15 additions & 0 deletions 04/7_stl/05_std_vector_soa/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <vector>

struct MyVec {
std::vector<float> x;
std::vector<float> y;
std::vector<float> z;
};

MyVec a;

void func() {
for (std::size_t i = 0; i < a.x.size(); i++) {
a.x[i] *= a.y[i];
}
}
4 changes: 4 additions & 0 deletions 04/7_stl/05_std_vector_soa/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e

gcc -O3 -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
vim /tmp/main.S
2 changes: 2 additions & 0 deletions 04/8_math/01_div_becomes_mul/.vim_localrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" vim: ft=vim
nnoremap <silent> <F7> :wa<CR>:!make\|\|(echo -n .;read -n1)<CR><CR>
3 changes: 3 additions & 0 deletions 04/8_math/01_div_becomes_mul/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
float func(float a) {
return a / 2;
}
4 changes: 4 additions & 0 deletions 04/8_math/01_div_becomes_mul/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e

gcc -O3 -fopenmp -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
vim /tmp/main.S
2 changes: 2 additions & 0 deletions 04/8_math/02_common_multiple_div/.vim_localrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" vim: ft=vim
nnoremap <silent> <F7> :wa<CR>:!make\|\|(echo -n .;read -n1)<CR><CR>
5 changes: 5 additions & 0 deletions 04/8_math/02_common_multiple_div/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void func(float *a, float b) {
for (int i = 0; i < 1024; i++) {
a[i] /= b;
}
}
4 changes: 4 additions & 0 deletions 04/8_math/02_common_multiple_div/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e

gcc -O3 -fopenmp -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
vim /tmp/main.S
2 changes: 2 additions & 0 deletions 04/8_math/03_no_multiple_div/.vim_localrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" vim: ft=vim
nnoremap <silent> <F7> :wa<CR>:!make\|\|(echo -n .;read -n1)<CR><CR>
6 changes: 6 additions & 0 deletions 04/8_math/03_no_multiple_div/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
void func(float *a, float b) {
float inv_b = 1 / b;
for (int i = 0; i < 1024; i++) {
a[i] *= inv_b;
}
}
4 changes: 4 additions & 0 deletions 04/8_math/03_no_multiple_div/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e

gcc -O3 -fopenmp -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
vim /tmp/main.S
2 changes: 2 additions & 0 deletions 04/8_math/04_no_multiple_div_fast_math/.vim_localrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" vim: ft=vim
nnoremap <silent> <F7> :wa<CR>:!make\|\|(echo -n .;read -n1)<CR><CR>
5 changes: 5 additions & 0 deletions 04/8_math/04_no_multiple_div_fast_math/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void func(float *a, float b) {
for (int i = 0; i < 1024; i++) {
a[i] /= b;
}
}
4 changes: 4 additions & 0 deletions 04/8_math/04_no_multiple_div_fast_math/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e

gcc -ffast-math -O3 -fopenmp -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
vim /tmp/main.S
2 changes: 2 additions & 0 deletions 04/8_math/05_std_sqrt_overloads/.vim_localrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" vim: ft=vim
nnoremap <silent> <F7> :wa<CR>:!make\|\|(echo -n .;read -n1)<CR><CR>
13 changes: 13 additions & 0 deletions 04/8_math/05_std_sqrt_overloads/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <cmath>
#include <type_traits>

void func(float *a) {
for (int i = 0; i < 1024; i++) {
auto x = sqrt(a[i]); // wrong and bad
static_assert(std::is_same_v<decltype(x), double>);
auto y = sqrtf(a[i]); // correct but bad
static_assert(std::is_same_v<decltype(y), float>);
auto z = std::sqrt(a[i]); // correct and good
static_assert(std::is_same_v<decltype(z), float>);
}
}
4 changes: 4 additions & 0 deletions 04/8_math/05_std_sqrt_overloads/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e

gcc -O3 -fopenmp -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
vim /tmp/main.S
2 changes: 2 additions & 0 deletions 04/8_math/06_vectorize_sqrt/.vim_localrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" vim: ft=vim
nnoremap <silent> <F7> :wa<CR>:!make\|\|(echo -n .;read -n1)<CR><CR>
7 changes: 7 additions & 0 deletions 04/8_math/06_vectorize_sqrt/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <cmath>

void func(float *a) {
for (int i = 0; i < 1024; i++) {
a[i] = std::sqrt(a[i]);
}
}
4 changes: 4 additions & 0 deletions 04/8_math/06_vectorize_sqrt/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e

gcc -ffast-math -O3 -fopenmp -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
vim /tmp/main.S
2 changes: 2 additions & 0 deletions 04/8_math/07_nested_loop_add/.vim_localrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" vim: ft=vim
nnoremap <silent> <F7> :wa<CR>:!make\|\|(echo -n .;read -n1)<CR><CR>
9 changes: 9 additions & 0 deletions 04/8_math/07_nested_loop_add/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <cmath>

void func(float *a, float *b, float *c) {
for (int i = 0; i < 1024; i++) {
for (int j = 0; j < 1024; j++) {
c[i] += a[i] * b[j];
}
}
}
4 changes: 4 additions & 0 deletions 04/8_math/07_nested_loop_add/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e

gcc -ffast-math -O3 -fopenmp -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
vim /tmp/main.S
2 changes: 2 additions & 0 deletions 04/8_math/08_nested_loop_add_vectorize/.vim_localrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" vim: ft=vim
nnoremap <silent> <F7> :wa<CR>:!make\|\|(echo -n .;read -n1)<CR><CR>
11 changes: 11 additions & 0 deletions 04/8_math/08_nested_loop_add_vectorize/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <cmath>

void func(float *a, float *b, float *c) {
for (int i = 0; i < 1024; i++) {
float tmp = c[i];
for (int j = 0; j < 1024; j++) {
tmp += a[i] * b[j];
}
c[i] = tmp;
}
}
4 changes: 4 additions & 0 deletions 04/8_math/08_nested_loop_add_vectorize/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e

gcc -ffast-math -O3 -fopenmp -fomit-frame-pointer -fverbose-asm -S main.cpp -o /tmp/main.S
vim /tmp/main.S
Binary file modified 04/slides.pptx
Binary file not shown.

0 comments on commit 637967c

Please sign in to comment.