目录
- 冒泡排序
- 九九乘法表
- 水仙花数
- 猜数字
- 时间戳
- 7的倍数,含有7就拍桌子
- goto
- 数组名
- 最大值
- 数组逆序
- 二维数组的数组名
- 成绩求和
- 两数相加函数
- 值传递,形参发生任何改变,都不会影响实参
- 函数的定义
- 函数的声明和定义
- 函数的分文件编写
- 指针的定义和使用
- 指针所占内存空间
- 空指针
- 野指针
- const 修饰指针
- 指针和数组
- 值传递和地址传递
- 指针配合数组和函数
- 结构体定义和使用
- 结构体数组
- 结构体指针
- 结构体嵌套结构体
- 结构体做函数参数
- 结构体中const的使用场景
- 结构体案例1
- 结构体案例2
- MPI并行——梯形积分
- MPI并行——局部二分自适应区间加密积分
- MPI并行——9点平滑(阻塞式通讯)
- MPI并行——9点平滑(非阻塞式通讯)
- MPI并行——Jacobi迭代 解二维 Poisson 方程(待完善)
- MPI并行——显式偏移读写
冒泡排序
void Bubble_Sort(int* const array, int array_size) {
for (int i = 1; i < array_size; i++)
{
for (int j = 0; j < array_size - i; j++)
{
if (array[j] > array[j + 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
九九乘法表
#include<iostream>
using namespace std;
int main() {
for (int i = 1; i < 10; i++) {
for (int j = 1; j <=i; j++) {
cout << j << "*" << i << "=" << i * j << "\t";
}
cout <<"\n" << endl;
}
system("pause");
return 0;
}
水仙花数
#include<iostream>
#include<math.h>
using namespace std;
int main() {
int a = 0, b = 0, c = 0;
for (int i = 100; i < 1000; i++){
a = i / 100;//百位
b = i / 10 % 10;//十位
c=i % 10;//个位
if (pow(a, 3) + pow(b, 3) + pow(c, 3) == i) {
cout << "水仙花数: " << i << endl;
}
}
system("pause");
return 0;
}
猜数字
#include<iostream>
#include<ctime>
using namespace std;
int main() {
srand((unsigned int)time(NULL));//随机数种子
int num = rand() % 100 + 1;//生成1~100的一个随机数
int value;
while (num) {
cout << "输入一个数: ";
cin >> value;
if (value > num) {
cout << "猜大了" << endl;
}
else if(value < num){
cout << "猜小了" << endl;
}
else {
cout << "猜对了!" << endl;
break;
}
}
system("pause");
return 0;
}
时间戳
#include<iostream>
#include<ctime>
using namespace std;
int main() {
cout << "时间戳: " << (unsigned int)time(NULL) << endl;
system("pause");
return 0;
}
7的倍数,含有7就拍桌子
#include<iostream>
using namespace std;
int main() {
for (int i = 1; i <= 100; i++) {
/*
!(i%7)//7的倍数
i/10==7//十位上有7
i%10==7//个位上有7
*/
if (!(i % 7) || i / 10 == 7 || i % 10 == 7) {
cout << "拍桌子(" << i << ")" << endl;
}
else {
cout << i << endl;
}
}
system("pause");
return 0;
}
goto
#include<iostream>
using namespace std;
int main() {
cout << "1.XXXXXX" << endl;
cout << "2.XXXXXX" << endl;
goto FLAG;
cout << "3.XXXXXX" << endl;
cout << "4.XXXXXX" << endl;
FLAG:
cout << "5.XXXXXX" << endl;
system("pause");
return 0;
}
数组名
#include<iostream>
using namespace std;
int main() {
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
cout << "数组在内存中的首地址(十六进制): " << arr << endl;
cout << "数组在内存中的首地址(十进制): " << (int)arr << endl;
cout << "数组中的第一个元素在内存中的地址(十进制): " << (int)&arr[0] << endl;
cout << "数组占用的内存空间: : " << sizeof(arr) << endl;
cout << "数组中每个元素占用的内存空间 : " << sizeof(arr[0]) << endl;
cout << "数组元素的个数 : " << sizeof(arr)/sizeof(arr[0]) << endl;
system("pause");
return 0;
}
最大值
#include<iostream>
using namespace std;
int main() {
int arr[] = { 300,350,200,400,250 }, max = 0;
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {
max = arr[i] > max ? arr[i] : max;
}
cout << "数组中最大值: " << max << endl;
system("pause");
return 0;
}
数组逆序
#include<iostream>
using namespace std;
int main() {
int arr[] = { 1,3,2,5,4,6,2}, temp = 0;
int num = sizeof(arr) / sizeof(arr[0]) - 1;//数组最后一个元素索引
for (int i = 0; i <= num/2; i++) {
temp = arr[i];
arr[i] = arr[num - i];
arr[num - i] = temp;
}
for (int i = 0; i <= num; i++) {
cout << arr[i] << endl;
}
system("pause");
return 0;
}
二维数组的数组名
#include<iostream>
using namespace std;
int main() {
int arr[][3] = {
{1,2,3},
{4,5,6}
};
cout << "数组大小 " << sizeof(arr) << endl;
cout << "二维数组第一行大小 " << sizeof(arr[0]) << endl;
cout << "二维数组元素大小 " << sizeof(arr[0][0]) << endl;
cout << "二维数组行数 " << sizeof(arr)/ sizeof(arr[0]) << endl;
cout << "二维数组列数 " << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;
cout << "二维数组首地址 " << (int)arr << endl;
cout << "二维数组第一行首地址 " << (int)arr[0] << endl;
cout << "二维数组第二行首地址 " << (int)arr[1] << endl;
cout << "二维数组第一个元素首地址 " << (int)&arr[0][0] << endl;
cout << "二维数组第二个元素首地址 " << (int)&arr[0][1] << endl;
system("pause");
return 0;
}
成绩求和
#include<iostream>
#include<string>
using namespace std;
int main() {
int arr[][3] = {
{100,100,100},
{90,50,100},
{60,70,80}
};
string name[3] = { "张三","李四","王五" };
int raw = sizeof(arr) / sizeof(arr[0]);
int col = sizeof(arr[0]) / sizeof(arr[0][0]);
for (int i = 0; i < raw; i++) {
int sum = 0;
for (int j = 0; j < col; j++) {
sum += arr[i][j];
}
cout << name[i] << "的总成绩: " << sum << endl;
}
system("pause");
return 0;
}
两数相加函数
#include<iostream>
using namespace std;
int add(int num1, int num2) {
return num1 + num2;
}
int main() {
cout << add(1, 3) << endl;
system("pause");
return 0;
}
值传递,形参发生任何改变,都不会影响实参
#include<iostream>
using namespace std;
void swap(int num1, int num2) {
cout << "交换前" << endl;
cout << "num1= " << num1 << endl;
cout << "num2= " << num2 << endl;
int temp = num1;
num1 = num2;
num2 = temp;
cout << "交换后" << endl;
cout << "num1= " << num1 << endl;
cout << "num2= " << num2 << endl;
}
int main() {
int a = 10, b = 20;
cout << "a= " << a << endl;
cout << "b " << b << endl;
swap(a, b);
cout << "a= " << a << endl;
cout << "b " << b << endl;
system("pause");
return 0;
}
函数的定义
#include<iostream>
using namespace std;
//在main()之前定义
int max(int num1, int num2) {
return num1 > num2 ? num1 : num2;
}
int main() {
cout << max(10, 20) << endl;
system("pause");
return 0;
}
函数的声明和定义
#include<iostream>
using namespace std;
//在main()之前声明
int max(int num1, int num2);
int main() {
cout << max(10, 20) << endl;
system("pause");
return 0;
}
//在main()之后定义
int max(int num1, int num2) {
return num1 > num2 ? num1 : num2;
}
函数的分文件编写
- 创建.h的头文件
- 创建.cpp的源文件
- 在头文件中写函数声明
- 在源文件中写函数的定义
//swap.h
#pragma once
#include<iostream>
using namespace std;
void swap(int a, int b);
//swap.cpp
#include "swap.h"
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
cout << "a= " << a << endl;
cout << "b= " << b << endl;
}
//source.cpp
#include<iostream>
using namespace std;
#include "swap.h"
int main() {
swap(10, 20);
system("pause");
return 0;
}
指针的定义和使用
#include<iostream>
using namespace std;
int main() {
int a = 10;
//1.定义指针
int* p = &a;
cout << "a的地址: " << &a << endl;
cout << "指针p为: " << p << endl;
//2.使用指针
*p = 100;
cout << "a= " << a << endl;
cout << "*p= " << *p << endl;
system("pause");
return 0;
}
指针所占内存空间
#include<iostream>
using namespace std;
int main() {
int a = 10;
int* p = &a;
cout << "a= " << *p << endl;
cout << "p " << sizeof(p) << endl;//指针所占内存空间
cout << "*p " << sizeof(*p) << endl;//数据所占内存空间
cout << "char* " << sizeof(char*) << endl;
cout << "float* " << sizeof(float*) << endl;
cout << "double* " << sizeof(double*) << endl;
system("pause");
return 0;
}
空指针
#include<iostream>
using namespace std;
int main() {
int* p = NULL;//空指针
system("pause");
return 0;
}
野指针
#include<iostream>
using namespace std;
int main() {
//野指针
//int* p = (int*)0x1100;//错误
system("pause");
return 0;
}
const 修饰指针
#include<iostream>
using namespace std;
int main() {
//const修饰的值不能改
int a = 10, b = 20;
//1.常量指针
const int* p1 = &a;
p1 = &b;//正确
//2.指针常量
int* const p2 = &b;
*p2 = a;
//3.const 修饰指针和常量
const int* const p3 = &a;
system("pause");
return 0;
}
指针和数组
#include<iostream>
using namespace std;
int main() {
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int* p = arr;
cout << "第一个元素 " << arr[0] << endl;
cout << "指针访问第一个元素 " << *p << endl;
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++, p++) {
cout << *p << endl;
}
system("pause");
return 0;
}
值传递和地址传递
#include<iostream>
using namespace std;
//值传递
void swap01(int a, int b) {
int temp = a;
a = b;
b = temp;
}
//地址传递
void swap02(int* p1, int* p2) {
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int main() {
int a = 10, b = 20;
cout << "a= " << a << endl;
cout << "b= " << b << endl;
cout << "值传递" << endl;
swap01(a, b);
cout << "a= " << a << endl;
cout << "b= " << b << endl;
cout << "地址传递" << endl;
swap02(&a, &b);
cout << "a= " << a << endl;
cout << "b= " << b << endl;
system("pause");
return 0;
}
指针配合数组和函数
#include<iostream>
using namespace std;
void bubleSort(int* arr, int len) {
for (int i = 0; i < len; i++) {
for (int j = 0; j < len - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {4,3,6,9,1,2,10,8,7,5};
int len = sizeof(arr) / sizeof(arr[0]);
bubleSort(arr, len);
for (int i = 0; i < len; i++) {
cout << arr[i] << "\t";
}
cout <<endl;
system("pause");
return 0;
}
结构体定义和使用
#include<iostream>
#include<string>
using namespace std;
struct Student {
string name;
int age;
int score;
};
int main() {
Student s1;
s1.name = "张三";
s1.age = 18;
s1.score = 60;
Student s2 = { "李四",19,70 };
cout << "姓名: " << s2.name
<< " 年龄: " << s2.age
<< " 成绩: " << s2.score << endl;
system("pause");
return 0;
}
结构体数组
#include<iostream>
#include<string>
using namespace std;
struct Student {
string name;
int age;
int score;
};
int main() {
Student stuArray[3] = {
{"张三",18,60},
{"李四",19,70},
{"王五",20,80}
};
for (int i = 0; i < 3; i++) {
cout << "姓名: " << stuArray[i].name
<< " 年龄: " << stuArray[i].age
<< " 成绩: " << stuArray[i].score << endl;
}
system("pause");
return 0;
}
结构体指针
#include<iostream>
#include<string>
using namespace std;
struct Student {
string name;
int age;
int score;
};
int main() {
Student s1 = {"张三",18,60};
cout << "姓名: " << s1.name
<< " 年龄: " << s1.age
<< " 分数: " << s1.score << endl;
Student* p = &s1;
cout << "姓名: " << p->name
<< " 年龄: " << p->age
<< " 分数: " << p->score << endl;
system("pause");
return 0;
}
结构体嵌套结构体
#include<iostream>
#include<string>
using namespace std;
struct Student {
string name;
int age;
int score;
};
struct Teacher {
int id;
string name;
int age;
Student stuArray[2];
};
int main() {
Teacher t;
t.name = "老王";
t.id = 001;
t.age = 50;
t.stuArray[0].name = "张三";
t.stuArray[0].age = 18;
t.stuArray[0].score = 60;
t.stuArray[1].name = "李四";
t.stuArray[1].age = 19;
t.stuArray[1].score = 70;
system("pause");
return 0;
}
结构体做函数参数
#include<iostream>
#include<string>
using namespace std;
struct Student {
string name;
int age;
int score;
};
void print1(Student stu) {
cout << "值传递打印 " << " 姓名: " << stu.name
<< " 年龄: " << stu.age
<< " 分数: " << stu.score << endl;
};
void print2(Student* p) {
p->name = "李四";
cout << "地址传递打印 " << " 姓名: " << p->name
<< " 年龄: " << p->age
<< " 分数: " << p->score << endl;
};
int main() {
Student s = { "张三",18,60 };
cout << "main函数中打印 " << " 姓名: " << s.name
<< " 年龄: " << s.age
<< " 分数: " << s.score << endl;
print1(s);
print2(&s);
system("pause");
return 0;
}
结构体中const的使用场景
#include<iostream>
#include<string>
using namespace std;
struct Student {
string name;
int age;
int score;
};
void print1(const Student* p) {
cout << "地址传递打印 " << " 姓名: " << p->name
<< " 年龄: " << p->age
<< " 分数: " << p->score << endl;
};
int main() {
Student s = { "张三",18,60 };
cout << "main函数中打印 " << " 姓名: " << s.name
<< " 年龄: " << s.age
<< " 分数: " << s.score << endl;
print1(&s);
system("pause");
return 0;
}
结构体案例1
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
struct Student {
string sName;
int score;
};
struct Teacher
{
string tName;
Student sArray[5];
};
void allocatespace(Teacher tArray[], int len) {
string nameSeed = "ABCDE";
for (int i = 0; i < len; i++) {
tArray[i].tName = "teacher_";
tArray[i].tName += nameSeed[i];
for (int j = 0; j < 5; j++) {
tArray[i].sArray[j].sName = "student_";
tArray[i].sArray[j].sName += nameSeed[j];
int random = 60 + rand() % 41;
tArray[i].sArray[j].score = random;
}
}
};
void printinfo( Teacher tArray[],int len) {
for (int i = 0; i < len; i++) {
cout << "老师姓名: " << tArray[i].tName << endl;
for (int j = 0; j < 5; j++) {
cout << "\t学生姓名: " << tArray[i].sArray[j].sName
<< " 得分: " << tArray[i].sArray[j].score << endl;
}
}
};
int main() {
srand((unsigned int)time(NULL));
Teacher tArray[3];
int len = sizeof(tArray) / sizeof(tArray[0]);
allocatespace(tArray, len);
printinfo(tArray, len);
system("pause");
return 0;
}
结构体案例2
#include<iostream>
#include<string>
using namespace std;
struct Hero {
string name;
int age;
string sex;
};
void bubbleSorrt(Hero HeroArray[], int len) {
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (HeroArray[j].age > HeroArray[j + 1].age) {
Hero temp = HeroArray[j];
HeroArray[j] = HeroArray[j + 1];
HeroArray[j + 1] = temp;
}
}
}
};
void printinfo(Hero HeroArray[], int len) {
for (int i = 0; i < len; i++) {
cout << HeroArray[i].name << "\t"
<< HeroArray[i].age << "\t"
<< HeroArray[i].sex << endl;
}
};
int main() {
Hero HeroArray[5] = {
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"男"}
};
int len = sizeof(HeroArray) / sizeof(HeroArray[0]);
cout << "\t排序前" << endl;
printinfo(HeroArray, len);
bubbleSorrt(HeroArray, len);
cout << "\t排序后" << endl;
printinfo(HeroArray, len);
system("pause");
return 0;
}
MPI并行——梯形积分
#include<iostream>
#include<math.h>
#include "mpi.h"
using namespace std;
double f(double x) {
return x*x*x;
}
double trap(double a, double b, int n) {
double h = (b - a) / n;
double sum = (f(a) + f(b)) / 2;
for (int i = 1; i < n; i++)
{
sum += f(a + i * h);
}
return sum * h;
}
int main(void) {
int n = 9000;
double a = -2, b = 2;
int rank, comm_sz;
MPI_Status status;
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
int local_n = n / comm_sz;
double local_width = (b - a) / comm_sz;
double local_a = a + rank * local_width;
double local_b = local_a + local_width;
double local_sum = trap(local_a, local_b, local_n);
double cacha_sum;
MPI_Reduce(&local_sum, &cacha_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
if (rank == 0) {
cout << "With n = " << n << " trapeziods, our estimate" << endl;
cout << "of the integral from " << a << " to " << b << " = " << cacha_sum << endl;
}
MPI_Finalize();
return 0;
}
With n = 9000 trapeziods, our estimate
of the integral from -2 to 2 = -8.88178e-16
MPI并行——局部二分自适应区间加密积分
∫ 0 1 4 1 + x 2 d x = π \int_0^1\frac{4}{1+x^2}\mathrm{d}x=\pi ∫011+x24dx=π
#include<iostream>
#include "mpi.h"
using namespace std;
//被积函数
double f(double x) {
return 4.0 / (1 + x * x);
}
//局部二分自适应区间加密积分函数(梯形)
double trap(double a, double b, double e) {
double Xc = 0.5 * (a + b), h = b - a, V0 = 0.5 * h * (f(a) + f(b));
if (Xc == a || Xc == b) return V0;
double fc = f(Xc), V = (V0 + h * fc) * 0.5, sub_e = abs(V - V0);
if (sub_e >= 3 * h * e)
{
return trap(a, Xc, e) + trap(Xc, b, e);
}
else return V;
}
int main(void) {
double A[4] = { 0,0,0,0 };//下限,上限,误差,积分结果
double PI = 3.141592653589793;//pi精确值
int rank, comm_sz;
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
if (rank == 0)
{
cout << "积分下限:" << "\t";
cin >> A[0];
cout << "积分上限:" << "\t";
cin >> A[1];
if (A[0] == A[1]) return 0;
cout << "误差:" << "\t";
cin >> A[2];
}
MPI_Bcast(A, 3, MPI_DOUBLE, 0, MPI_COMM_WORLD);
//各区域的积分上下限、允许的误差
double local_a = A[0] + (rank + 0.) * (A[1] - A[0]) / comm_sz;
double local_b = A[0] + (rank + 1.) * (A[1] - A[0]) / comm_sz;
double local_e = A[2] / comm_sz;
//各区域积分值
double cache_sum = trap(local_a, local_b, local_e);
//归约到进程0
MPI_Reduce(&cache_sum, &A[3], 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
if (rank == 0) {
cout << "\n从 " << A[0] << " 到 " << A[1] << " ,误差小于 " << A[2] << " 的积分值为:" << endl;
printf("%.15lf,与真值差%.1e\n", A[3], abs(A[3] - PI));
}
MPI_Finalize();
return 0;
}
MPI并行——9点平滑(阻塞式通讯)
#include<iostream>
#include "mpi.h"
using namespace std;
int main(void) {
const int rowsize = 250, colsize = 1000;
//定义二维数组
float** a = new float* [rowsize + 2], ** b = new float* [rowsize + 2];
for (int i = 0; i < rowsize + 2; i++)
{
a[i] = new float[colsize + 2];
b[i] = new float[colsize + 2];
}
//数组a整体赋值为1
for (int i = 0; i < rowsize + 2; i++)
{
for (int j = 0; j < colsize + 2; j++)
{
a[i][j] = 1;
}
}
int rank, comm_sz;
MPI_Status status;
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
//应使用4个进程并行
if (comm_sz != 4)
{
printf("*** This program uses exactly 4 processes!Not %d ***", comm_sz);
MPI_Abort(MPI_COMM_WORLD, 1);
}
//边界赋值
for (int i = 0; i < rowsize + 2; i++) {
a[i][0] = 10;//左边界
a[i][colsize + 1] = 10;//右边界
}
if (rank == 0)
{
for (int i = 0; i < colsize + 2; i++)
{
a[0][i] = 10;//上边界
}
}
if (rank == 3)
{
for (int i = 0; i < colsize + 2; i++)
{
a[rowsize + 1][i] = 10;//下边界
}
}
//b=a
for (int i = 0; i < rowsize + 2; i++)
{
for (int j = 0; j < colsize + 2; j++)
{
b[i][j] = a[i][j];
}
}
int before_rank = rank > 0 ? rank - 1 : MPI_PROC_NULL;
int after_rank = rank < 3 ? rank + 1 : MPI_PROC_NULL;
int itag1 = 321;
int itag2 = 123;
for (int times = 0; times < 100; times++)
{
MPI_Send(a[1], colsize + 2, MPI_FLOAT, before_rank, itag1, MPI_COMM_WORLD);
MPI_Recv(a[rowsize + 1], colsize + 2, MPI_FLOAT, after_rank, itag1, MPI_COMM_WORLD, &status);
MPI_Send(a[rowsize], colsize + 2, MPI_FLOAT, after_rank, itag2, MPI_COMM_WORLD);
MPI_Recv(a[0], colsize + 2, MPI_FLOAT, before_rank, itag2, MPI_COMM_WORLD, &status);
for (int i = 1; i < rowsize + 1; i++)
{
for (int j = 1; j < colsize + 1; j++)
{ //9点平滑
b[i][j] = (a[i][j] + a[i + 1][j] + a[i - 1][j] + a[i][j + 1] + a[i][j - 1] + a[i - 1][j - 1] + a[i - 1][j + 1] + a[i + 1][j + 1] + a[i + 1][j - 1]) / 9;
}
}
for (int i = 1; i < rowsize + 1; i++)
{
for (int j = 1; j < colsize + 1; j++)
{
a[i][j] = b[i][j];
}
}
}
if (rank == 0)
{
cout << "Process " << rank << "\n " << endl;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << a[i][j] << "\t";
}
cout << endl;
}
}
//释放内存
delete[]a;
delete[]b;
MPI_Finalize();
return 0;
}
MPI并行——9点平滑(非阻塞式通讯)
#include<iostream>
#include "mpi.h"
using namespace std;
int main(void) {
const int rowsize = 250, colsize = 1000;
//定义二维数组
float** a = new float* [rowsize + 2], ** b = new float* [rowsize + 2];
for (int i = 0; i < rowsize + 2; i++)
{
a[i] = new float[colsize + 2];
b[i] = new float[colsize + 2];
}
//数组a整体赋值为1
for (int i = 0; i < rowsize + 2; i++)
{
for (int j = 0; j < colsize + 2; j++)
{
a[i][j] = 1;
}
}
int rank, comm_sz;
MPI_Status status[4];
MPI_Request req[4];
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
//应使用4个进程并行
if (comm_sz != 4)
{
printf("*** This program uses exactly 4 processes!Not %d ***", comm_sz);
MPI_Abort(MPI_COMM_WORLD, 1);
}
//边界赋值
for (int i = 0; i < rowsize + 2; i++) {
a[i][0] = 10;//左边界
a[i][colsize + 1] = 10;//右边界
}
if (rank == 0)
{
for (int i = 0; i < colsize + 2; i++)
{
a[0][i] = 10;//上边界
}
}
if (rank == 3)
{
for (int i = 0; i < colsize + 2; i++)
{
a[rowsize + 1][i] = 10;//下边界
}
}
//b=a
for (int i = 0; i < rowsize + 2; i++)
{
for (int j = 0; j < colsize + 2; j++)
{
b[i][j] = a[i][j];
}
}
int before_rank = rank > 0 ? rank - 1 : MPI_PROC_NULL;
int after_rank = rank < 3 ? rank + 1 : MPI_PROC_NULL;
int itag1 = 321;
int itag2 = 123;
for (int times = 0; times < 100; times++)
{
MPI_Isend(a[1], colsize + 2, MPI_FLOAT, before_rank, itag1, MPI_COMM_WORLD, &req[0]);
MPI_Irecv(a[rowsize + 1], colsize + 2, MPI_FLOAT, after_rank, itag1, MPI_COMM_WORLD, &req[1]);
MPI_Isend(a[rowsize], colsize + 2, MPI_FLOAT, after_rank, itag2, MPI_COMM_WORLD, &req[2]);
MPI_Irecv(a[0], colsize + 2, MPI_FLOAT, before_rank, itag2, MPI_COMM_WORLD, &req[3]);
//不需要通讯的部分 9点平滑
for (int i = 2; i < rowsize; i++)
{
for (int j = 1; j < colsize + 1; j++)
{
b[i][j] = (a[i][j] + a[i + 1][j] + a[i - 1][j] + a[i][j + 1] + a[i][j - 1] + a[i - 1][j - 1] + a[i - 1][j + 1] + a[i + 1][j + 1] + a[i + 1][j - 1]) / 9;
}
}
MPI_Waitall(4, req, status);
//非阻塞通讯完成后,开始计算 边的平滑
int iRow[2] = {1, rowsize};//上、下边索引
for (int ii = 0; ii < 2; ii++)
{
const int i = iRow[ii];
for (int j = 1; j < colsize + 1; j++)
{
// 9点平滑
b[i][j] = (a[i][j] + a[i + 1][j] + a[i - 1][j] + a[i][j + 1] + a[i][j - 1] + a[i - 1][j - 1] + a[i - 1][j + 1] + a[i + 1][j + 1] + a[i + 1][j - 1]) / 9;
}
}
for (int i = 1; i < rowsize + 1; i++)
{
for (int j = 1; j < colsize + 1; j++)
{
a[i][j] = b[i][j];
}
}
}
if (rank == 0)
{
cout << "Process " << rank << "\n " << endl;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << b[i][j] << "\t";
}
cout << endl;
}
}
//释放内存
delete[]a;
delete[]b;
MPI_Finalize();
return 0;
}
MPI并行——Jacobi迭代 解二维 Poisson 方程(待完善)
#include<iostream>
#include<fstream>
#include<math.h>
#include<string>
#include "mpi.h"
using namespace std;
double f(double x, double y) {//f(x,y)
//DEL^2 U = f(x,y)
return 4;
}
double Uedge(double x, double y) {//外边界条件
return x * x + y * y;
}
int main(void) {
double X[2] = { 0,10 }, Y[2] = { 0,10 };
const int Nx = 128, Ny = 128;
double error = 1e-4;
double dx = (X[1] - X[0]) / Nx, dy = (Y[1] - Y[0]) / Ny;//每格长度
const int nx = Nx / 2, ny = Ny / 2;//2*2的进程分配
int rank, comm_sz;
MPI_Status status[8];
MPI_Request req[8];
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
//应使用4个进程并行
if (comm_sz != 4)
{
printf("*** This program uses exactly 4 processes!Not %d ***", comm_sz);
MPI_Abort(MPI_COMM_WORLD, 1);
}
//各进程分配的位置
int top_rank, bottom_rank, left_rank, right_rank;
if (rank > 1) {
top_rank = rank - 2;
bottom_rank = MPI_PROC_NULL;
if (rank < 3)//rank == 2
{
left_rank = MPI_PROC_NULL;
right_rank = rank + 1;
}
else {//rank == 3
left_rank = rank - 1;
right_rank = MPI_PROC_NULL;
}
}
else {
top_rank = MPI_PROC_NULL;
bottom_rank = rank + 2;
if (rank < 1)//rank == 0
{
left_rank = MPI_PROC_NULL;
right_rank = rank + 1;
}
else {//rank == 1
left_rank = rank - 1;
right_rank = MPI_PROC_NULL;
}
}
/*cout << "Process " << rank
<< "\t top_rank " << top_rank
<< "\t bottom_rank " << bottom_rank
<< "\t left_rank " << left_rank
<< "\t right_rank " << right_rank << "\n" << endl;*/
double** U = new double* [nx + 2], ** temp = new double* [nx + 2];//定义U和temp,用temp储存上一次的U
for (int i = 0; i < nx + 2; i++)
{
U[i] = new double[ny + 2];
temp[i] = new double[ny + 2];
}
for (int i = 0; i < nx + 2; i++)
{
for (int j = 0; j < ny + 2; j++)
{ //中心区域赋值
U[i][j] = Uedge(X[0] + i * dx, Y[0] + j * dy);
//外边界赋值
if (rank == 0) {
U[0][j] = Uedge(X[0], Y[0] + j * dy);//U(0,y:0->Y/2)
U[i][0] = Uedge(X[0] + i * dx, Y[0]);//U(x:0->X/2,0)
}
if (rank == 1) {
U[0][j] = Uedge(X[0], (Y[1] - Y[0]) / 2 + j * dy);//U(0,y:Y/2->Y)
U[i][ny + 1] = Uedge(X[0] + i * dx, Y[1] + dy);//U(x:0->X/2,Y)
}
if (rank == 2) {
U[nx + 1][j] = Uedge((X[1] - X[0]) / 2 + i * dx, Y[0] + j * dy);//U(x:X/2->X,y:0->Y/2)
U[i][0] = Uedge((X[1] - X[0]) / 2 + i * dx, Y[0]);//U(x:X/2->X,Y)
}
if (rank == 3) {
U[nx + 1][j] = Uedge(X[1] + dx, (Y[1] - Y[0]) / 2 + j * dy);//U(X,y:Y/2->Y)
U[i][ny + 1] = Uedge((X[1] - X[0]) / 2 + i * dx, Y[1] + dy);//U(x:X/2->X,Y)
}
//给临时数组temp赋值
temp[i][j] = U[i][j];
}
}
double H = -(dx * dx * dy * dy) / 2 / (dx * dx + dy * dy), Hx = -H / dx / dx, Hy = -H / dy * dy;//定义迭代公式系数
int itag1 = 1234, itag2 = 2341, itag3 = 3412, itag4 = 4123;//消息标签
double sumError = 0;//储存误差的变量
bool TorF;//是否终止循环的变量
int times = 100000;//防止死循环,设置一个迭代上限
do
{
MPI_Isend(U[nx], ny + 2, MPI_DOUBLE, bottom_rank, itag1, MPI_COMM_WORLD, &req[0]);//向下发送
MPI_Irecv(U[0], ny + 2, MPI_DOUBLE, top_rank, itag1, MPI_COMM_WORLD, &req[1]);
MPI_Isend(U[1], ny + 2, MPI_DOUBLE, top_rank, itag2, MPI_COMM_WORLD, &req[2]);//向上发送
MPI_Irecv(U[nx + 1], ny + 2, MPI_DOUBLE, bottom_rank, itag2, MPI_COMM_WORLD, &req[3]);
double colArrSendRecv[2][nx + 2] = { 0 };//用于发送和接收 列的数组
//构造向右发送的列
for (int i = 0; i < nx + 2; i++)
{
colArrSendRecv[0][i] = U[i][ny];
}
MPI_Isend(colArrSendRecv[0], nx + 2, MPI_DOUBLE, right_rank, itag3, MPI_COMM_WORLD, &req[4]);//向右发送
MPI_Irecv(colArrSendRecv[1], nx + 2, MPI_DOUBLE, left_rank, itag3, MPI_COMM_WORLD, &req[5]);
//构造向左发送的列
for (int i = 0; i < nx + 2; i++)
{
colArrSendRecv[0][i] = U[i][1];
}
MPI_Isend(colArrSendRecv[0], nx + 2, MPI_DOUBLE, left_rank, itag4, MPI_COMM_WORLD, &req[6]);//向左发送
MPI_Irecv(colArrSendRecv[1], nx + 2, MPI_DOUBLE, right_rank, itag4, MPI_COMM_WORLD, &req[7]);
//计算不需要通讯的区域
for (int i = 2; i < nx; i++)
{
for (int j = 2; j < ny; j++)
{
temp[i][j] = H * f(X[0] + i * dx, Y[0] + j * dy) + Hx * (U[i - 1][j] + U[i + 1][j]) + Hy * (U[i][j - 1] + U[i][j + 1]);
}
}
MPI_Waitall(8, req, status);
//列边界赋值
if (right_rank == -1)
{
for (int i = 0; i < nx + 2; i++)
{
U[i][0] = colArrSendRecv[1][i];
}
}
else//right_rank != -1
{
for (int i = 0; i < nx + 2; i++)
{
U[i][ny + 1] = colArrSendRecv[1][i];
}
}
//非阻塞通讯完成后,开始计算 边界处的Uij
int iRow[2] = { 1, nx };//上、下边索引
for (int ii = 0; ii < 2; ii++)
{
const int i = iRow[ii];
for (int j = 1; j < ny + 1; j++)
{
//计算上、下边的Uij
temp[i][j] = H * f(X[0] + i * dx, Y[0] + j * dy) + Hx * (U[i - 1][j] + U[i + 1][j]) + Hy * (U[i][j - 1] + U[i][j + 1]);
}
}
int iCol[2] = { 1, ny };//左、右边索引
for (int i = 1; i < nx + 1; i++)
{
for (int jj = 0; jj < 2; jj++)
{
const int j = iCol[jj];
//计算左、右边的Uij
temp[i][j] = H * f(X[0] + i * dx, Y[0] + j * dy) + Hx * (U[i - 1][j] + U[i + 1][j]) + Hy * (U[i][j - 1] + U[i][j + 1]);
}
}
//U = temp
for (int i = 1; i < nx + 1; i++)
{
for (int j = 1; j < ny + 1; j++)
{
sumError += pow(temp[i][j] - U[i][j], 2) / pow(temp[i][j], 2);//相对误差 平方和
U[i][j] = temp[i][j];
}
}
//cout << "sqrt(sumError) " << sqrt(sumError) << endl;
//cout << "error " << error << endl;
TorF = sqrt(sumError) > error ? true : false;
} while (TorF && times--);
if (rank == 0)
{
cout << "Process " << rank << "\n" << endl;
for (int i = 0; i < nx + 2; i++)
{
for (int j = 0; j < ny + 2; j++)
{
cout << U[i][j] << ",";
}
cout << endl;
}
}
/*ofstream fout("rank " + to_string(rank) + ".csv");//csv方便查看
fout << "Process " << rank << "\n" << endl;
for (int i = 0; i < nx + 2; i++)
{
for (int j = 0; j < ny + 2; j++)
{
fout << U[i][j] << ",";
}
fout << endl;
}
fout.close();*/
delete[]U, temp;
MPI_Finalize();
return 0;
}
MPI并行——显式偏移读写
#include<iostream>
#include<mpi.h>
using namespace std;
int main(void) {
int rank, comm_sz;
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
MPI_Status status[2];
MPI_File fh,fh1;
//写文件
MPI_File_open(MPI_COMM_WORLD, "data.dat", MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
int writeData[2] = { rank * rank ,rank * rank * rank };
int offset = rank*sizeof(writeData);
MPI_File_write_at(fh, offset, writeData, 2, MPI_INT, &status[0]);
MPI_File_close(&fh);
//读文件
MPI_File_open(MPI_COMM_WORLD, "data.dat", MPI_MODE_RDONLY, MPI_INFO_NULL, &fh1);
int readData[2] = { 0 };
MPI_File_read_at(fh1, offset, readData, 2, MPI_INT, &status[1]);
cout << "Process " << rank << "\treadData\t" << readData[0] << "\t" << readData[1] << endl;
MPI_File_close(&fh1);
//收集(组收集MPI_Allgather)
int gatherData[8];
MPI_Gather(readData, 2, MPI_INT, gatherData, 2, MPI_INT, 0,MPI_COMM_WORLD);
if (rank == 0)
{
cout << "\nProcess " << rank << " Gather Data" << endl;
for (int i = 0; i < 8; i++)
{
cout << gatherData[i] << "\t";
}
cout << "\n" << endl;
}
//散发
int scatterData[2];
MPI_Scatter(gatherData, 2, MPI_INT, scatterData, 2, MPI_INT, 0, MPI_COMM_WORLD);
cout << "Process " << rank << " Recv ScatterData\t" << scatterData[0] << "\t" << scatterData[1] << endl;
//组归约
int reduceData[2];
MPI_Allreduce(scatterData, reduceData, 2, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
if (rank == 0)//rank == 0,1,2,3
{
cout << "\nProcess " << rank << " All reduce Data" << endl;
for (int i = 0; i < 2; i++)
{
cout << reduceData[i] << "\t";
}
cout << "\n" << endl;
}
MPI_Finalize();
return 0;
}