基于 MPI/OpenMP 混合编程的大规模多体(N-Body)问题仿真实验
完整代码:
#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)问题仿真实验的更多相关文章
- 基于WebView的混合编程
近日公司需求变更,以前一个页面是后台返回HTML字段,然后我们直接用webView接收,现在新增一个页面,数据后台返回非HTML,页面跟前面一直,所幸自己会点HTML,所以偷了个懒,自己用代码把数据组 ...
- Matlab与.NET基于类型安全的接口混合编程入门
原文:[原创]Matlab与.NET基于类型安全的接口混合编程入门 如果这些文章对你有用,有帮助,期待更多开源组件介绍,请不要吝啬手中的鼠标. [原创分享]Matlab.NET混编调用Figure窗体 ...
- 基于引擎的matlab+vc混合编程的配置
前段时间在项目中做了一些关于基于引擎的vc+matlab混合编程的工作. 如果你是混合编程新手,我相信使用引擎的方式编程是比较简单快捷的一种方式. 当然这种方法也有其缺点,就是不能脱离matlab运行 ...
- 由基于qml,c++的串口调试工具浅谈qml与c++混合编程
最近在做一个基于sim900 的串口通信工具,基于qml和c++来实现. 首先,对于串口,qt有自带的QSerialPort,可以实现同步,和异步通信,qt creator也有自带的例子,本例子是从其 ...
- mpi和cuda混合编程的正确编译
针对大数据的计算,很多程序通过搭建mpi集群进行加速,并取得了很好的效果.算法内部的加速,当前的并行化趋势是利用GPU显卡进行算法加速.针对并行性非常好的算法,GPU加速效果将远大于集群带来的加速效果 ...
- 大数据并行计算利器之MPI/OpenMP
大数据集群计算利器之MPI/OpenMP ---以连通域标记算法并行化为例 1 背景 图像连通域标记算法是从一幅栅格图像(通常为二值图像)中,将互相邻接(4邻接或8邻接)的具有非背景值的像素集合提取出 ...
- Atitit 基于sql编程语言的oo面向对象大规模应用解决方案attilax总结
Atitit 基于sql编程语言的oo面向对象大规模应用解决方案attilax总结 1. Sql语言应该得到更大的范围的应用,1 1.1. 在小型系统项目中,很适合存储过程写业务逻辑2 1.2. 大型 ...
- C和C++混合编程中的extern "C" {}
引言 在用C++的项目源码中,经常会不可避免的会看到下面的代码: 1 2 3 4 5 6 7 8 9 #ifdef __cplusplus extern "C" { #endif ...
- [转载:]C#与Fortran混合编程之本地调用Fortran动态链接库
前言 C#发展到现在,已是一门相当完善的语言,他基于C语言风格,演化于C++.并依靠强大的.NET底层框架.C#可以用来快速构建桌面及Web应用.然而在我们的实际工作中,尽管C#已经非常完善,但还是不 ...
随机推荐
- 【题解】「SP34013」SEUG - Seetha’s Unique Game
这道题一看就是 贪心 . 使放的石头少,就需要石头大. 那么就可以将石头重量排序,从大到小. 这道题里面看似东西很多,但是很多东西都是没有用的.比如说:箱子的长和宽,因为题目中说「每加一个石头,水的高 ...
- linux下安装Zookeeper 3.4.14
1.下载Zookeeper 3.4.14(https://zookeeper.apache.org/) wget https://mirror.bit.edu.cn/apache/zookeeper/ ...
- python魔术方法总结
获取属性 __ getattr __(self, name) 定义当用户试图获取一个不存在的属性时的行为 __ getattribute __(self, name) 定义当该类的属性被访问时的行为 ...
- Nginx安装配置教程
转自https://www.cnblogs.com/zhouxinfei/p/7862285.html nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是 ...
- Elasticsearch.Net
今天使用Elasticsearch作开发,很简单的查询,就出现Elasticsearch.Net.UnexpectedElasticsearchClientException异常,看样子像是序列化的异 ...
- matplotlib的学习12-Subplot 多合一显示
import matplotlib.pyplot as plt # matplotlib 是可以组合许多的小图, 放在一张大图里面显示的. 使用到的方法叫作 subplot. plt.figure() ...
- Spring框架之spring-web http源码完全解析
Spring框架之spring-web http源码完全解析 Spring-web是Spring webMVC的基础,由http.remoting.web三部分组成. http:封装了http协议中的 ...
- 程序运行慢?你怕是写的假 Python
Python程序运行太慢的一个可能的原因是没有尽可能的调用内置方法,下面通过5个例子来演示如何用内置方法提升Python程序的性能. 1. 数组求平方和 输入一个列表,要求计算出该列表中数字的的平方和 ...
- matplotlib学习日记(二)----图表组成练习
''' 将前面的知识进行练习 plot,scatter,legend等 ''' import matplotlib.pyplot as plt import numpy as np from matp ...
- 为什么MySQL不推荐使用uuid作为主键?
前言 在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一,单机递增),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么 ...