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

Hw04 frsama #33

Open
wants to merge 2 commits 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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ if (NOT CMAKE_BUILD_TYPE)
endif()

add_executable(main main.cpp)

target_compile_options(main PUBLIC -ffast-math -march=native)
112 changes: 77 additions & 35 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,106 @@
#include <vector>
#include <chrono>
#include <cmath>
#include <array>

#define N 48

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

struct Star {
float px, py, pz;
float vx, vy, vz;
float mass;
struct alignas(16) Star {
std::array<float,N> px, py, pz;
std::array<float,N> vx, vy, vz;
std::array<float,N> mass;
};

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

void init() {
for (int i = 0; i < 48; i++) {
stars.push_back({
frand(), frand(), frand(),
frand(), frand(), frand(),
frand() + 1,
});
// 原计划的初始化,但是完全没有矢量化
// void init() {
// for (size_t i = 0; i < N; ++i) {
// 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;
// }
// }

template <int M = 0>
void sub_init(std::array<float,N> &arr) {
#pragma GCC unroll 4
for (size_t i = 0; i < N; ++i) {
arr[i] = frand() + M;
}
}

void init() {
sub_init<0>(stars.px);
sub_init<0>(stars.py);
sub_init<0>(stars.pz);
sub_init<0>(stars.vx);
sub_init<0>(stars.vy);
sub_init<0>(stars.vz);
sub_init<1>(stars.mass);
}


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) {
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;
for (size_t i = 0; i < N; ++i) {
float dvx = 0, dvy = 0, dvz = 0;
#pragma GCC unroll 4
for (size_t j = 0; j < N; ++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 + eps2;
d2 *= std::sqrt(d2);

float tmp = stars.mass[j] * Gdt / d2;
dvx += dx * tmp;
dvy += dy * tmp;
dvz += dz * tmp;
}
stars.vx[i] += dvx;
stars.vy[i] += dvy;
stars.vz[i] += dvz;
}
for (auto &star: stars) {
star.px += star.vx * dt;
star.py += star.vy * dt;
star.pz += star.vz * dt;
#pragma GCC unroll 4
for (size_t i = 0; i < N; ++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;
float d2 = dx * dx + dy * dy + dz * dz + eps * eps;
energy -= other.mass * star.mass * G / sqrt(d2) / 2;
#pragma GCC unroll 4
for (size_t i = 0; i < N; ++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 (size_t i = 0; i < N; ++i) {
float tmp = stars.mass[i] * G / 2;
#pragma GCC unroll 4
for (size_t j = 0; j < N; ++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 + eps2;
float reverse_sqrt_d2 = 1 / std::sqrt(d2);
energy -= stars.mass[j] * tmp *reverse_sqrt_d2;
}
}
return energy;
Expand Down