openmp并行编程_转载:多线程编程方法3OpenMPI框架
介紹
OpenMPI官方link:https://www.open-mpi.org/
我們先來看下MPI和OpenMPI的關系 :
MPI:英文全稱是Message Passing Interface,這個就很明了了,信息傳遞接口,是獨立于語言的通信協議(標準),是一個庫。
OpenMPI:英文全稱是open Message Passing Interface。openMPI是MPI的一種實現,一種庫項目。
MPI是一種進程級的并行方式,它支持分布式存儲。不過需要程序員顯式分配數據,編程模型較為復雜。
關于OpenMPI在Linux下的安裝和配置,全網有豐富的教程,這里就不贅述了,這里先來看一個最簡單的hello world程序:
//hello.c#include
#include "mpi.h"
int main(int argc, char* argv[])
{
int rank, size, len;
char version[MPI_MAX_LIBRARY_VERSION_STRING];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Get_library_version(version, &len);
printf("Hello, world, I am %d of %d, (%s, %d)\n",
rank, size, version, len);
MPI_Finalize();
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
編譯hello world的C程序,mpicc hello.c -o test
或者編譯hello world的C++程序,mpicxx hello.c -o test
這樣生成名叫test的可執行程序,然后使用mpirun執行:mpirun -np 2 ./test
這里用np來指定使用幾個進程
以上程序打印輸出如下:
Hello, world, I am 0 of 1, (Open MPI v3.0.0, package: Open MPI root@kerie-PC Distribution, ident: 3.0.0, repo rev: v3.0.0, Sep 12, 2017, 108)1
我們再來看一個矩陣乘的OpenMP并行實現:
//matrix_multiply.cpp#include
#include
#include
#include
#pragma comment(lib,"mpi.lib")
#define n 1000
using namespace std;
int main(int argv, char *argc[])
{
int rank, p, a;
MPI_Init(&argv, &argc);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
MPI_Status status;
if (p!=1)
a = n / (p - 1);
if (rank == 0)
{
int* A = new int[n*n];
int* B = new int[n*n];
int* C = new int[n*n];
// int * recptr = NULL;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)// 時間是 O nn
{
A[i*n + j] = i + j; //A[i][j]
B[i*n + j] = 1; //B[i][j]
}
if (p == 1)
{
double tb, te;
tb = MPI_Wtime();
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
C[i*n + j] = 0; //C[i][j]
for (int k = 0; k < n; k++)
{
C[i*n + j] = A[i*n + k] * B[k*n + j];
}
}
te = MPI_Wtime();
cout << "time is " << te - tb;// << "s" << endl;
}
if (p != 1)
{
double tb, te;
tb = MPI_Wtime();
for (int i = 0; i < p-1; i++){//給每個寄存器發送 數組 A,B,C
MPI_Send(&A[0+0], n*n, MPI_INT, i+1, 1, MPI_COMM_WORLD);//每個發送 a行,a*n大小的數據
MPI_Send(&B[0+0], n*n, MPI_INT, i+1,2, MPI_COMM_WORLD);
}
for (int i =0; i < p-1; i++)
MPI_Recv(&C[i*a+0], a*n, MPI_INT, i+1,3, MPI_COMM_WORLD, &status);//每個接受 a行,a*n大小的數據
te = MPI_Wtime();
cout << "time is " << te - tb;// << "s" << endl;
}
delete[] A;
delete[] B;
delete[] C;
}
if (p != 1)
if (rank != 0){
int* A = new int[n*n];
int* B = new int[n*n];
int* C = new int[n*n];
MPI_Recv(&A[0+0], n*n, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);//從A[0][0]和B[0][0]開始接受
MPI_Recv(&B[0+0], n*n, MPI_INT,0, 2, MPI_COMM_WORLD, &status);
for (int i =a*(rank-1); i < (a*(rank)); i++)//按照行間隔分,每個cpu計算自己的a行
for (int j = 0; j < n; j++)
{
C[i*n + j] = 0; //C[i][j]
for (int k = 0; k < n; k++)
{
C[i*n + j] = A[i*n + k] * B[k*n + j];
}
}
{//向rank=0發送自己的那a行C,大小是a*n
//int * sendptr = &(C[a*(rank - 1)+0]);
MPI_Send(&C[a*(rank - 1) + 0], a*n, MPI_INT, 0,3, MPI_COMM_WORLD);//起始地址是C[rank-1][0],大小是a*n
}
}
MPI_Finalize();
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
源碼解析
MPI_Send函數用于發送一個消息到目標進程。通信域中的dest進程發送數據,數據存放在buf中,類型是datatype,個數是count,這個消息的標志是tag,用以和本進程向同一目的進程發送的其它消息區別開來。MPI_Recv函數用于從指定進程接收一個消息。它的含義是進程從comm域中source進程接收標簽號為tag的數據,并保存到buf中。接收緩沖區buf的大小不能小于發送過來的消息的長度。否則會由于數組越界導致程序出錯。MPI_Isend/MPI_Irecv是異步發送和接收的語句,顯然比MPI_Send/MPI_Recv這樣的阻塞語句更可能提高性能。另外,還可以避免循環死鎖。
安裝OpenMPI環境
1.下載安裝包
wget https://www.open-mpi.org/software/ompi/v1.10/downloads/openmpi-1.10.2.tar.gz1
2.安裝依賴插件
sudo apt-get install libibnetdisc.dev1
3.解壓縮下載包,至目錄/opt
cp openmpi-1.10.2.tar.gz /opt && cd /opt && tar -xvf openmpi-1.10.0.tar.gz1
4.配置安裝文件
./configure --prefix="/home/$USER/.openmpi"1
5.安裝openMPI
make && sudo make install1
6.配置環境變量
export PATH="$PATH:/home/$USER/.openmpi/bin"export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/home/$USER/.openmpi/lib/"
1
2
7.測試是否安裝成功
總結
以上是生活随笔為你收集整理的openmp并行编程_转载:多线程编程方法3OpenMPI框架的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: onenote快捷键_onenote链接
- 下一篇: usestate中的回调函数_React