完整代码:

#include <iostream>
#include <ctime>
#include <mpi.h>
#include <omp.h>
#include <cstdlib>
#include <iomanip>
#include <Windows.h>
#include <cmath>
#include <algorithm> using namespace std;
const long double G = 6.67 * pow(10, -11);
const int STAR_NUM = 32;
const int THREAD_NUM = 4;
const long int TIME_STEP = 3600;
const int MAX_PROCESS = 128; struct Star
{
long double x, y, z; // Position
long double vx, vy, vz; // Speed
long double ax, ay, az; // Acceleration
long double nax, nay, naz; // Acceleration of next iteration
long double m; // Mass
}; Star stars[STAR_NUM];
Star temp[STAR_NUM];
Star buffer[STAR_NUM]; void updateNextAcceration(Star& a, Star& b) {
long double dx = a.x - b.x;
long double dy = a.y - b.y;
long double dz = a.z - b.z;
long double r2 = pow(dx, 2) + pow(dy, 2) + pow(dz, 2);
long double A = G * a.m / r2;
long double r = sqrt(r2);
long double k = A / r;
b.nax += k * dx;
b.nay += k * dy;
b.naz += k * dz;
} void updateAcceration(Star& star) {
star.ax = star.nax;
star.ay = star.nay;
star.az = star.naz;
star.nax = 0;
star.nay = 0;
star.naz = 0;
} void updateSpeed(Star& star) {
star.vx += star.ax * TIME_STEP;
star.vy += star.ay * TIME_STEP;
star.vz += star.az * TIME_STEP;
} void updatePosition(Star& star) {
star.x += star.vx * TIME_STEP;
star.y += star.vy * TIME_STEP;
star.z += star.vz * TIME_STEP;
} void print(Star* stars, int num=STAR_NUM) {
cout << setiosflags(ios::left) << setw(4) << "Num" << setw(16) << "x" << setw(16) << "y" << setw(16) << "z"
<< setw(16) << "Speed x" << setw(16) << "Speed y" << setw(16) << "Speed z"
<< setw(16) << "Acceleration x" << setw(16) << "Acceleration y" << setw(16) << "Acceleration z"
<< setw(16) << "Next a x" << setw(16) << "Next a y" << setw(16) << "Next a z"
<< setw(16) << "Mass" << endl;
for (int i = 0; i < num; i++) {
cout << setiosflags(ios::left) << setw(4) << i << setw(16) << stars[i].x << setw(16) << stars[i].y << setw(16) << stars[i].z
<< setw(16) << stars[i].vx << setw(16) << stars[i].vy << setw(16) << stars[i].vz
<< setw(16) << stars[i].ax << setw(16) << stars[i].ay << setw(16) << stars[i].az
<< setw(16) << stars[i].nax << setw(16) << stars[i].nay << setw(16) << stars[i].naz
<< setw(16) << stars[i].m << endl;
}
} int main(int argc, char* argv[]) {
MPI_Init(&argc, &argv);
int rank, size, real_size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size); real_size = min(STAR_NUM, size);
// 每个进程的工作量
int part = STAR_NUM / real_size;
if (part * real_size < STAR_NUM) part++; // 每个进程的工作量已确定
// 现在要剔除不需要的进程
size = STAR_NUM / part;
if (size * part < STAR_NUM) size++; // 进程数已确定 MPI_Comm COMM_WORLD;
if (rank < size) {
MPI_Comm_split(MPI_COMM_WORLD, 1, rank, &COMM_WORLD);
}else {
MPI_Comm_split(MPI_COMM_WORLD, MPI_UNDEFINED, rank, &COMM_WORLD);
} MPI_Comm_rank(COMM_WORLD, &rank);
MPI_Comm_size(COMM_WORLD, &size);
//int part = STAR_NUM / size;
//if (part * size < STAR_NUM) part++; // Create custome mpi datatype.
const int nitems = 13;
int blocklengths[nitems] = { 1,1,1,1, 1,1, 1,1, 1,1, 1,1,1 };
MPI_Datatype types[nitems] = { MPI_LONG_DOUBLE,MPI_LONG_DOUBLE, MPI_LONG_DOUBLE, MPI_LONG_DOUBLE, MPI_LONG_DOUBLE, MPI_LONG_DOUBLE, MPI_LONG_DOUBLE, MPI_LONG_DOUBLE, MPI_LONG_DOUBLE, MPI_LONG_DOUBLE, MPI_LONG_DOUBLE, MPI_LONG_DOUBLE, MPI_LONG_DOUBLE };
MPI_Datatype MPI_STAR;
MPI_Aint offsets[nitems];
offsets[0] = offsetof(Star, x);
offsets[1] = offsetof(Star, y);
offsets[2] = offsetof(Star, z);
offsets[3] = offsetof(Star, vx);
offsets[4] = offsetof(Star, vy);
offsets[5] = offsetof(Star, vz);
offsets[6] = offsetof(Star, ax);
offsets[7] = offsetof(Star, ay);
offsets[8] = offsetof(Star, az);
offsets[9] = offsetof(Star, nax);
offsets[10] = offsetof(Star, nay);
offsets[11] = offsetof(Star, naz);
offsets[12] = offsetof(Star, m);
MPI_Type_create_struct(nitems, blocklengths, offsets, types, &MPI_STAR);
MPI_Type_commit(&MPI_STAR); omp_set_num_threads(THREAD_NUM); if (rank == 0) {
cout << "Generating origin data..." << endl;
srand(time(NULL));
for (int i = 0; i < STAR_NUM; i++) {
stars[i].m = pow(10, 21) + ((long double)rand() / (RAND_MAX)) * pow(10, 22);
stars[i].x = pow(10, 7) + ((long double)rand() / (RAND_MAX)) * pow(10, 8);
stars[i].y = pow(10, 7) + ((long double)rand() / (RAND_MAX)) * pow(10, 8);
stars[i].z = pow(10, 7) + ((long double)rand() / (RAND_MAX)) * pow(10, 8);
stars[i].vx = 0;
stars[i].vy = 0;
stars[i].vz = 0;
stars[i].ax = 0;
stars[i].ay = 0;
stars[i].az = 0;
stars[i].nax = 0;
stars[i].nay = 0;
stars[i].naz = 0;
}
} MPI_Bcast(&stars, STAR_NUM, MPI_STAR, 0, COMM_WORLD); int loopstart = part * rank;
int loopend = min(STAR_NUM, (rank + 1) * part); while (true)
{
// 利用 OpenMP 加速此循环
#pragma omp parallel for
for (int i = loopstart; i < loopend; i++) {
Star* s = &(stars[i]);
updatePosition(*s);
} // 计算下次的加速度需要来自其他进程的数据
MPI_Request req;
//MPI_Ibcast(&stars, STAR_NUM, MPI_STAR, rank, COMM_WORLD, &req);
//MPI_Gather(&stars, STAR_NUM, MPI_STAR, &temp, STAR_NUM, MPI_STAR, rank, COMM_WORLD);
int start = part * rank;
int end = min(part * (rank + 1), STAR_NUM);
int* displs, * rcounts;
displs = (int*)malloc(size * sizeof(int));
rcounts = (int*)malloc(size * sizeof(int));
for (int i = 0; i < size; i++) {
displs[i] = i * part;
rcounts[i] = end - start;
}
//cout << "rank " << rank << "end - start" << end << " - " << start << endl;
MPI_Gatherv(&stars[start], end - start, MPI_STAR, &temp, rcounts, displs, MPI_STAR, 0, COMM_WORLD); MPI_Bcast(&temp, STAR_NUM, MPI_STAR, 0, COMM_WORLD); // 现在 temp 中存的是更新后的数据
for (int i = loopstart; i < loopend; i++) {
for (int j = 0; j < STAR_NUM; j++) {
if (i == j) continue;
updateNextAcceration(temp[j], stars[i]);
}
} //print(stars);
for (int i = loopstart; i < loopend; i++) {
cout << rank<<" "<<setiosflags(ios::left) << setw(4) << i << setw(16) << stars[i].x << setw(16) << stars[i].y << setw(16) << stars[i].z
<< setw(16) << stars[i].vx << setw(16) << stars[i].vy << setw(16) << stars[i].vz
<< setw(16) << stars[i].ax << setw(16) << stars[i].ay << setw(16) << stars[i].az
<< setw(16) << stars[i].nax << setw(16) << stars[i].nay << setw(16) << stars[i].naz
<< setw(16) << stars[i].m << endl;
} // 利用 OpenMP 加速此循环
#pragma omp parallel for
for (int i = loopstart; i < loopend; i++) {
Star* s = &(stars[i]);
updateAcceration(*s);
} // 利用 OpenMP 加速此循环
#pragma omp parallel for
for (int i = loopstart; i < loopend; i++) {
Star* s = &(stars[i]);
updateSpeed(*s);
}
}
}

运行截图:

最新文章

  1. BZOJ 2815: [ZJOI2012]灾难
  2. Math的三个将小数值舍入为整数方法
  3. Linux indent
  4. 使用express4.X + jade + mongoose + underscore搭建个人电影网站
  5. HTMLTestRunner修改Python3的版本
  6. 10. windows与linux文件共享
  7. css的引入方法2
  8. C#写好的类库dll怎么在别人调用的时候也能看到注释?
  9. 【LeetCode】204 - Count Primes
  10. 如何成为一个牛掰的Java大神?
  11. shell 实现类似php的require_once函数
  12. uboot代码2:stage2代码,启动内核
  13. JAVA 验证码生成(转)
  14. (转)Java 网络IO编程总结(BIO、NIO、AIO均含完整实例代码)
  15. Oracle查看对象空间使用情况show_space
  16. Ext.isIterable
  17. 某喷码机品牌U盘存储的配置文件简记
  18. 用html和css制作奥运五环
  19. Mybatis in 查询
  20. vscode中live server插件的Go Live不显示问题

热门文章

  1. 【题解】CIRU - The area of the union of circles [SP8073] \ 圆的面积并 [Bzoj2178]
  2. window启动mongoDB
  3. C++异常之二 基本语法
  4. SpringBoot + Layui +Mybatis-plus实现简单后台管理系统(内置安全过滤器)
  5. 全能扫描王(一款识别率超高的OCR识别APP)
  6. [日常摸鱼]bzoj1502[NOI2005]月下柠檬树-简单几何+Simpson法
  7. nc监控实现调用受害者cmd
  8. matlab多项式拟合以及指定函数拟合
  9. OC 大数组分割成由小数组重新组合的新数组
  10. 企业运维案例:xxx is not in the sudoers file.This incident will be reported” 错误解决方法