完整代码:

#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);
}
}
}

运行截图:

基于 MPI/OpenMP 混合编程的大规模多体(N-Body)问题仿真实验的更多相关文章

  1. 基于WebView的混合编程

    近日公司需求变更,以前一个页面是后台返回HTML字段,然后我们直接用webView接收,现在新增一个页面,数据后台返回非HTML,页面跟前面一直,所幸自己会点HTML,所以偷了个懒,自己用代码把数据组 ...

  2. Matlab与.NET基于类型安全的接口混合编程入门

    原文:[原创]Matlab与.NET基于类型安全的接口混合编程入门 如果这些文章对你有用,有帮助,期待更多开源组件介绍,请不要吝啬手中的鼠标. [原创分享]Matlab.NET混编调用Figure窗体 ...

  3. 基于引擎的matlab+vc混合编程的配置

    前段时间在项目中做了一些关于基于引擎的vc+matlab混合编程的工作. 如果你是混合编程新手,我相信使用引擎的方式编程是比较简单快捷的一种方式. 当然这种方法也有其缺点,就是不能脱离matlab运行 ...

  4. 由基于qml,c++的串口调试工具浅谈qml与c++混合编程

    最近在做一个基于sim900 的串口通信工具,基于qml和c++来实现. 首先,对于串口,qt有自带的QSerialPort,可以实现同步,和异步通信,qt creator也有自带的例子,本例子是从其 ...

  5. mpi和cuda混合编程的正确编译

    针对大数据的计算,很多程序通过搭建mpi集群进行加速,并取得了很好的效果.算法内部的加速,当前的并行化趋势是利用GPU显卡进行算法加速.针对并行性非常好的算法,GPU加速效果将远大于集群带来的加速效果 ...

  6. 大数据并行计算利器之MPI/OpenMP

    大数据集群计算利器之MPI/OpenMP ---以连通域标记算法并行化为例 1 背景 图像连通域标记算法是从一幅栅格图像(通常为二值图像)中,将互相邻接(4邻接或8邻接)的具有非背景值的像素集合提取出 ...

  7. Atitit 基于sql编程语言的oo面向对象大规模应用解决方案attilax总结

    Atitit 基于sql编程语言的oo面向对象大规模应用解决方案attilax总结 1. Sql语言应该得到更大的范围的应用,1 1.1. 在小型系统项目中,很适合存储过程写业务逻辑2 1.2. 大型 ...

  8. C和C++混合编程中的extern "C" {}

    引言 在用C++的项目源码中,经常会不可避免的会看到下面的代码: 1 2 3 4 5 6 7 8 9 #ifdef __cplusplus extern "C" { #endif ...

  9. [转载:]C#与Fortran混合编程之本地调用Fortran动态链接库

    前言 C#发展到现在,已是一门相当完善的语言,他基于C语言风格,演化于C++.并依靠强大的.NET底层框架.C#可以用来快速构建桌面及Web应用.然而在我们的实际工作中,尽管C#已经非常完善,但还是不 ...

随机推荐

  1. 你必须要知道的HTTP协议原理

    1 基本概念 HTTP协议:基于TCP协议之上实现的无状态.全文本的标准通信协议. 客户端:例如pc浏览器,移动应用端,第三方服务器等能发起http访问的设备. 服务器:能够接受HTTP协议请求,并且 ...

  2. EditPlus各个版本的注册码,亲测可用

    原文链接:https://www.cnblogs.com/shihaiming/p/6422441.html EditPlus4注册码 注册名:host1991    序列号:14F50-CD5C8- ...

  3. go并发之goroutine和channel,并发控制入门篇

    并发的概念及其重要性 这段是简单科普,大佬可以跳过 并发:并发程序指同时进行多个任务的程序.在操作系统中,是指一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行 ...

  4. Unity GameObject

    GameObject 游戏对象 GameObject是unity所有实体的基类 gameObject 获取当前脚本所挂载的游戏对象 一般来说,在属性视图中能看到或修改的属性,我们同样可以在脚本中获取并 ...

  5. 使用pdfobject.js

    一.下载pdfobject.js 二.CSS样式 <style type="text/css"> html, body, #pdf_viewer { width: 10 ...

  6. 学习tomcat-如何建立连接,处理请求

    tomcat如何建立连接,处理请求 学习探讨tomcat如何建立网络连接协议,并处理客户端过来的请求 建立http网络连接,指定通信协议 tomcat在创建时,会创建连接对象,负责处理客户端的请求,基 ...

  7. 详解Python Google Protocol Buffer

    为什么要使用PB? PB(Protocol Buffer)是 Google 开发的用于结构化数据交换格式,作为腾讯云日志服务标准写入格式.因此用于写入日志数据前,需要将日志原始数据序列化为 PB 数据 ...

  8. win7激活不支持的启动引导分区完美解决方法

    前言: 激活win7显示不支持的启动引导分区怎么办?有用户使用暴风激活工具给win7 64位系统激活时,弹出Error提示框"不支持的启动引导分区". 这是因为传统的win7激活工 ...

  9. 网络 IO 工作机制

    ref: 深入分析 java web 技术内幕 2.3 两台计算机之间进行数据的传输需要经过很多步骤.首先有相互沟通的意向,然后还要有沟通的通道:通过电话还是面对面交流(物理链路).最后,两个人说话的 ...

  10. 【Java并发编程】阿里最喜欢问的几道线程池的面试题?

    引言 上一篇文章我们有介绍过线程池的一个基本执行流程<[Java并发编程]面试必备之线程池>以及它的7个核心参数,以及每个参数的作用.以及如何去使用线程池 还留了几个小问题..建议看这篇文 ...