Skip to content
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

finish hw04 #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
21 changes: 12 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
cmake_minimum_required(VERSION 3.12)
project(hellocmake LANGUAGES CXX)

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

add_executable(main main.cpp)
cmake_minimum_required(VERSION 3.12)
project(hellocmake LANGUAGES CXX)

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

add_executable(main main.cpp)
find_package(OpenMP REQUIRED)
target_link_libraries(main PUBLIC OpenMP::OpenMP_CXX)
target_compile_options(main PUBLIC -ffast-math -march=native)
80 changes: 47 additions & 33 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,76 @@
#include <chrono>
#include <cmath>

float frand() {
static float frand() {
return (float)rand() / RAND_MAX * 2 - 1;
}

struct Star {
float px, py, pz;
float vx, vy, vz;
float mass;
struct alignas(64) Star {
float px[48], py[48], pz[48];
float vx[48], vy[48], vz[48];
float mass[48];
};

std::vector<Star> stars;
Star stars;

void init() {
#pragma GCC unroll 4
for (int i = 0; i < 48; i++) {
stars.push_back({
frand(), frand(), frand(),
frand(), frand(), frand(),
frand() + 1,
});
stars.px[i] = frand();
stars.py[i] = frand();
stars.pz[i] = frand();
stars.vx[i] = frand();
stars.vy[i] = frand();
stars.vz[i] = frand();
stars.mass[i] = frand() + 1;
}
}

float G = 0.001;
float eps = 0.001;
float eps_square = eps * eps;
float dt = 0.01;

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;
float d2 = dx * dx + dy * dy + dz * dz + eps * eps;
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;
#pragma GCC unroll 4
for (int i = 0; i < 48; i++) {
float px = stars.px[i], py = stars.py[i], pz = stars.pz[i];
float vx_plus = 0, vy_plus = 0, vz_plus = 0;
#pragma GCC unroll 4
for (int j = 0; j < 48; j++) {
float dx = stars.px[j] - px;
float dy = stars.py[j] - py;
float dz = stars.pz[j] - pz;
float d2 = dx * dx + dy * dy + dz * dz + eps_square;
d2 *= std::sqrt(d2);
float value = stars.mass[j] * (G * dt) / d2;
vx_plus += dx * value;
vy_plus += dy * value;
vz_plus += dz * value;
}
stars.vx[i] += vx_plus;
stars.vy[i] += vy_plus;
stars.vz[i] += vz_plus;
}
for (auto &star: stars) {
star.px += star.vx * dt;
star.py += star.vy * dt;
star.pz += star.vz * dt;
#pragma GCC unroll 4
for (int i = 0; i < 48; i++) {
stars.px[i] += stars.vx[i] * dt;
stars.py[i] += stars.vy[i] * dt;
stars.pz[i] += stars.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 (int i = 0; i < 48; i++) {
float v2 = stars.vx[i] * stars.vx[i] + stars.vy[i] * stars.vy[i] + stars.vz[i] * stars.vz[i];
energy += stars.mass[i] * v2 / 2;
for (int j = 0; j < 48; j++) {
float dx = stars.px[j] - stars.px[i];
float dy = stars.py[j] - stars.py[i];
float dz = stars.pz[j] - stars.pz[i];
float d2 = dx * dx + dy * dy + dz * dz + eps * eps;
energy -= other.mass * star.mass * G / sqrt(d2) / 2;
energy -= stars.mass[j] * stars.mass[i] * G / sqrt(d2) / 2;
}
}
return energy;
Expand All @@ -79,6 +92,7 @@ int main() {
init();
printf("Initial energy: %f\n", calc());
auto dt = benchmark([&] {
#pragma GCC unroll 64
for (int i = 0; i < 100000; i++)
step();
});
Expand Down