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

update code #29

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
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
cmake_minimum_required(VERSION 3.12)
project(hellocmake LANGUAGES CXX)

FIND_PACKAGE( OpenMP REQUIRED)
if(OPENMP_FOUND)
message("OPENMP FOUND")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS"${CMAKE_EXE_LINKER_FLAGS}${OpenMP_EXE_LINKER_FLAGS}")
endif()
set(CMAKE_CXX_STANDARD 17)
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
add_executable(main main.cpp)
69 changes: 43 additions & 26 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <vector>
#include <chrono>
#include <cmath>

#include <array>
float frand() {
return (float)rand() / RAND_MAX * 2 - 1;
}
Expand All @@ -12,13 +12,16 @@ struct Star {
float px, py, pz;
float vx, vy, vz;
float mass;

};

std::vector<Star> stars;
std::array<Star,48> stars;

void init() {
// #pragma omp simd
#pragma GCC ivdep
for (int i = 0; i < 48; i++) {
stars.push_back({
stars[i] = (Star{
frand(), frand(), frand(),
frand(), frand(), frand(),
frand() + 1,
Expand All @@ -31,43 +34,56 @@ float eps = 0.001;
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;
float G = 0.001;
float eps = 0.001;
float dt = 0.01;
float epss = eps*eps;
float gpdt = G*dt;
//#pragma omp simd
#pragma GCC ivdep
for (int i=0;i<48;i++) {
for (int j=0;j<48;j++) {
float dx = stars[j].px - stars[i].px;
float dy = stars[j].py - stars[i].py;
float dz = stars[j].pz - stars[i].pz;
float d2 = dx * dx + dy * dy + dz * dz + epss;
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;
stars[i].vx += dx * stars[j].mass * gpdt / d2;
stars[i].vy += dy * stars[j].mass * gpdt / d2;
stars[i].vz += dz * stars[j].mass * gpdt / d2;
}
}
for (auto &star: stars) {
star.px += star.vx * dt;
star.py += star.vy * dt;
star.pz += star.vz * dt;
for (int i=0;i<48;i++) {
stars[i].px += stars[i].vx * dt;
stars[i].py += stars[i].vy * dt;
stars[i].pz += stars[i].vz * dt;
}
}

float calc() {
float G = 0.001;
float eps = 0.001;
float dt = 0.01;
float epss = eps*eps;
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 omp simd
#pragma GCC ivdep
for (int i=0;i<48;i++) {
float v2 = stars[i].vx * stars[i].vx + stars[i].vy * stars[i].vy + stars[i].vz * stars[i].vz;
energy += stars[i].mass * v2 / 2;
for (int j=0;j<48;j++) {
float dx = stars[j].px - stars[i].px;
float dy = stars[j].py - stars[i].py;
float dz = stars[j].pz - stars[i].pz;
float d2 = dx * dx + dy * dy + dz * dz + epss;
energy -= stars[j].mass * stars[i].mass * G / sqrt(d2) / 2;
}
}
return energy;
}

template <class Func>
long benchmark(Func const &func) {
constexpr long benchmark(Func const &func) {
auto t0 = std::chrono::steady_clock::now();
func();
auto t1 = std::chrono::steady_clock::now();
Expand All @@ -79,6 +95,7 @@ int main() {
init();
printf("Initial energy: %f\n", calc());
auto dt = benchmark([&] {
#pragma omp simd
for (int i = 0; i < 100000; i++)
step();
});
Expand Down