diff --git a/CMakeLists.txt b/CMakeLists.txt index 29b152c..53c794c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,3 +7,7 @@ if (NOT CMAKE_BUILD_TYPE) 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) \ No newline at end of file diff --git a/main.cpp b/main.cpp index cf6369b..4929ee6 100644 --- a/main.cpp +++ b/main.cpp @@ -4,63 +4,74 @@ #include #include -float frand() { +static float frand() { return (float)rand() / RAND_MAX * 2 - 1; } struct Star { - float px, py, pz; - float vx, vy, vz; - float mass; + float px[48], py[48], pz[48]; + float vx[48], vy[48], vz[48]; + float mass[48]; }; -std::vector stars; +Star stars; + +constexpr size_t pnum = 48; void init() { - for (int i = 0; i < 48; i++) { - stars.push_back({ - frand(), frand(), frand(), - frand(), frand(), frand(), - frand() + 1, - }); + for (size_t i = 0; i < pnum; 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; } } float G = 0.001; float eps = 0.001; float dt = 0.01; +float eps2 = eps*eps; +float Gpdt = 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; +#pragma GCC ivdep +// #pragma omp simd + for (size_t i=0; i