From e3c803245442360ff5bb71f8946f77a556c0501a Mon Sep 17 00:00:00 2001 From: miyanyan <1138989048@qq.com> Date: Sun, 9 Jan 2022 09:40:36 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=BE=AA=E7=8E=AF=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E4=B8=8D=E5=8F=98=E9=87=8F=E6=8C=AA=E5=88=B0=E5=A4=96=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index cf6369b..5c74569 100644 --- a/main.cpp +++ b/main.cpp @@ -29,6 +29,8 @@ void init() { float G = 0.001; float eps = 0.001; float dt = 0.01; +float eps2 = eps * eps; +float Gdt = G * dt; void step() { for (auto &star: stars) { @@ -36,11 +38,11 @@ void step() { float dx = other.px - star.px; float dy = other.py - star.py; float dz = other.pz - star.pz; - float d2 = dx * dx + dy * dy + dz * dz + eps * eps; + float d2 = dx * dx + dy * dy + dz * dz + eps2; d2 *= sqrt(d2); - star.vx += dx * other.mass * G * dt / d2; - star.vy += dy * other.mass * G * dt / d2; - star.vz += dz * other.mass * G * dt / d2; + star.vx += dx * other.mass * Gdt / d2; + star.vy += dy * other.mass * Gdt / d2; + star.vz += dz * other.mass * Gdt / d2; } } for (auto &star: stars) { @@ -59,7 +61,7 @@ float calc() { float dx = other.px - star.px; float dy = other.py - star.py; float dz = other.pz - star.pz; - float d2 = dx * dx + dy * dy + dz * dz + eps * eps; + float d2 = dx * dx + dy * dy + dz * dz + eps2; energy -= other.mass * star.mass * G / sqrt(d2) / 2; } } From 6a8b8d0da3c0d81822bc84b1bba8bda050863ac8 Mon Sep 17 00:00:00 2001 From: miyanyan <1138989048@qq.com> Date: Sun, 9 Jan 2022 10:27:07 +0800 Subject: [PATCH 2/6] sqrt -> std::sqrt --- main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 5c74569..68d2715 100644 --- a/main.cpp +++ b/main.cpp @@ -39,7 +39,7 @@ void step() { float dy = other.py - star.py; float dz = other.pz - star.pz; float d2 = dx * dx + dy * dy + dz * dz + eps2; - d2 *= sqrt(d2); + d2 *= std::sqrt(d2); star.vx += dx * other.mass * Gdt / d2; star.vy += dy * other.mass * Gdt / d2; star.vz += dz * other.mass * Gdt / d2; @@ -62,7 +62,7 @@ float calc() { float dy = other.py - star.py; float dz = other.pz - star.pz; float d2 = dx * dx + dy * dy + dz * dz + eps2; - energy -= other.mass * star.mass * G / sqrt(d2) / 2; + energy -= other.mass * star.mass * G / std::sqrt(d2) / 2; } } return energy; From faa492576bdf702a10e1afc15436c93b3672c0b7 Mon Sep 17 00:00:00 2001 From: miyanyan <1138989048@qq.com> Date: Sun, 9 Jan 2022 11:26:59 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E6=94=B9=E4=B8=BA=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E6=9C=9F=E5=B8=B8=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index 68d2715..941e29d 100644 --- a/main.cpp +++ b/main.cpp @@ -26,11 +26,11 @@ void init() { } } -float G = 0.001; -float eps = 0.001; -float dt = 0.01; -float eps2 = eps * eps; -float Gdt = G * dt; +constexpr float G = 0.001; +constexpr float eps = 0.001; +constexpr float dt = 0.01; +constexpr float eps2 = eps * eps; +constexpr float Gdt = G * dt; void step() { for (auto &star: stars) { From 377db8fa4aaf649e0b56dfd56f541b45a2987a6c Mon Sep 17 00:00:00 2001 From: miyanyan <1138989048@qq.com> Date: Tue, 11 Jan 2022 10:11:07 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E7=BC=96=E8=AF=91=E9=80=89=E9=A1=B9?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20ffast-math=EF=BC=88=E6=8F=90=E5=8D=87?= =?UTF-8?q?=E4=B8=8D=E6=98=8E=E6=98=BE=EF=BC=89=20=E5=92=8C=20march=3Dnati?= =?UTF-8?q?ve=20=EF=BC=88=E6=8F=90=E5=8D=87=E6=98=8E=E6=98=BE=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 29b152c..1c859fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,3 +7,9 @@ if (NOT CMAKE_BUILD_TYPE) endif() add_executable(main main.cpp) + +if (MSVC) + target_compile_options(main PUBLIC /fp:fast /arch:AVX2) +else() + target_compile_options(main PUBLIC -ffast-math -march=native) +endif() \ No newline at end of file From 8b30aefa108819a3411137989559eb4643089714 Mon Sep 17 00:00:00 2001 From: miyanyan <1138989048@qq.com> Date: Sat, 29 Jan 2022 14:43:14 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E7=BB=93=E6=9E=84=E4=BD=93=E5=86=85?= =?UTF-8?q?=E5=AD=98=E5=B8=83=E5=B1=80=20=EF=BC=9A=20AOS=20=20->=20SOA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 76 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/main.cpp b/main.cpp index 941e29d..36cbf7d 100644 --- a/main.cpp +++ b/main.cpp @@ -4,65 +4,69 @@ #include #include +constexpr size_t star_size = 48; +constexpr float G = 0.001; +constexpr float eps = 0.001; +constexpr float dt = 0.01; +constexpr float eps2 = eps * eps; +constexpr float Gdt = G * dt; + float frand() { return (float)rand() / RAND_MAX * 2 - 1; } +template struct Star { - float px, py, pz; - float vx, vy, vz; - float mass; + float px[N], py[N], pz[N]; + float vx[N], vy[N], vz[N]; + float mass[N]; }; -std::vector stars; +Star star; void init() { - for (int i = 0; i < 48; i++) { - stars.push_back({ - frand(), frand(), frand(), - frand(), frand(), frand(), - frand() + 1, - }); + for (int i = 0; i < star_size; i++) { + star.px[i] = frand(); + star.py[i] = frand(); + star.pz[i] = frand(); + star.vx[i] = frand(); + star.vy[i] = frand(); + star.vz[i] = frand(); + star.mass[i] = frand() + 1; } } -constexpr float G = 0.001; -constexpr float eps = 0.001; -constexpr float dt = 0.01; -constexpr float eps2 = eps * eps; -constexpr float Gdt = G * dt; - void step() { - for (auto &star: stars) { - for (auto &other: stars) { - float dx = other.px - star.px; - float dy = other.py - star.py; - float dz = other.pz - star.pz; + for (size_t i = 0; i < star_size; ++i) { + for (size_t j = 0; j < star_size; ++j) { + float dx = star.px[j] - star.px[i]; + float dy = star.py[j] - star.py[i]; + float dz = star.pz[j] - star.pz[i]; float d2 = dx * dx + dy * dy + dz * dz + eps2; d2 *= std::sqrt(d2); - star.vx += dx * other.mass * Gdt / d2; - star.vy += dy * other.mass * Gdt / d2; - star.vz += dz * other.mass * Gdt / d2; + star.vx[i] += dx * star.mass[j] * Gdt / d2; + star.vy[i] += dy * star.mass[j] * Gdt / d2; + star.vz[i] += dz * star.mass[j] * Gdt / d2; } } - for (auto &star: stars) { - star.px += star.vx * dt; - star.py += star.vy * dt; - star.pz += star.vz * dt; + for (size_t i = 0; i < star_size; ++i) { + star.px[i] += star.vx[i] * dt; + star.py[i] += star.vy[i] * dt; + star.pz[i] += star.vz[i] * dt; } } float calc() { float energy = 0; - for (auto &star: stars) { - float v2 = star.vx * star.vx + star.vy * star.vy + star.vz * star.vz; - energy += star.mass * v2 / 2; - for (auto &other: stars) { - float dx = other.px - star.px; - float dy = other.py - star.py; - float dz = other.pz - star.pz; + for (size_t i = 0; i < star_size; ++i) { + float v2 = star.vx[i] * star.vx[i] + star.vy[i] * star.vy[i] + star.vz[i] * star.vz[i]; + energy += star.mass[i] * v2 / 2; + for (size_t j = 0; j < star_size; ++j) { + float dx = star.px[j] - star.px[i]; + float dy = star.py[j] - star.py[i]; + float dz = star.pz[j] - star.pz[i]; float d2 = dx * dx + dy * dy + dz * dz + eps2; - energy -= other.mass * star.mass * G / std::sqrt(d2) / 2; + energy -= star.mass[j] * star.mass[i] * G / std::sqrt(d2) / 2; } } return energy; From ae3de2b905fe87c7baeb613ce57f9c5c1e90f794 Mon Sep 17 00:00:00 2001 From: miyanyan <1138989048@qq.com> Date: Sat, 29 Jan 2022 14:52:20 +0800 Subject: [PATCH 6/6] =?UTF-8?q?reduction=20=E7=9A=84=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=88=E6=8F=90=E5=8D=87=E5=B7=A8=E5=A4=A7=EF=BC=81=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index 36cbf7d..4097d64 100644 --- a/main.cpp +++ b/main.cpp @@ -38,16 +38,20 @@ void init() { void step() { for (size_t i = 0; i < star_size; ++i) { + float sumx = 0.0f, sumy = 0.0f, sumz = 0.0f; for (size_t j = 0; j < star_size; ++j) { float dx = star.px[j] - star.px[i]; float dy = star.py[j] - star.py[i]; float dz = star.pz[j] - star.pz[i]; float d2 = dx * dx + dy * dy + dz * dz + eps2; d2 *= std::sqrt(d2); - star.vx[i] += dx * star.mass[j] * Gdt / d2; - star.vy[i] += dy * star.mass[j] * Gdt / d2; - star.vz[i] += dz * star.mass[j] * Gdt / d2; + sumx += dx * star.mass[j] * Gdt / d2; + sumy += dy * star.mass[j] * Gdt / d2; + sumz += dz * star.mass[j] * Gdt / d2; } + star.vx[i] += sumx; + star.vy[i] += sumy; + star.vz[i] += sumz; } for (size_t i = 0; i < star_size; ++i) { star.px[i] += star.vx[i] * dt;