MPI派生数据类型发送接收,降低发送时间
生活随笔
收集整理的這篇文章主要介紹了
MPI派生数据类型发送接收,降低发送时间
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡述
在很多并行計算問題中,最消耗時間的其實是在進程間交互的情況。
所以,如果能降低在進程間的交互速度,那絕對是有幫助的。這就是學MPI派生數據類型的原因。
派生數據類型
- 非常繁瑣… 但是也沒辦法啦
派生部分的代碼和注釋
// 創建自定義數據類型// { int[3], double[5], char };// MPI_Aint 存地址MPI_Aint intarr_p, doublearr_p, c_p; int intarr[3]; double doublearr[5];char c;// 獲取各個元素之間的相對地址MPI_Get_address(intarr, &intarr_p);MPI_Get_address(doublearr, &doublearr_p);MPI_Get_address(&c, &c_p); // 注意注意使用指針// 計算偏移值數組MPI_Aint displacement[3] = {0, // 第一個偏移值為0doublearr_p - intarr_p,c_p - intarr_p};// 定義各個部分的類型int types[3] = { MPI_INT, MPI_DOUBLE, MPI_CHAR };// 每個部分的長度int blockLength[3] = { 3, 5, 1 };// 新定義類型MPI_Datatype newtype;MPI_Type_create_struct(3, // 三個元素 { int[3], double[5], char };blockLength, // 分塊長度displacement, // 偏移值types, // 類型&newtype // 類型指針);// 提交新類型MPI_Type_commit(&newtype);但是再用完之后,記得free掉,會占用空間的
// 釋放類型空間 MPI_Type_free(&newtype);這里,我們在0號進程生成數據,然后發給1號進程。
// Testif (my_rank == 0) {for (int i = 0; i < 3; ++i) intarr[i] = i + 1;for (int i = 0; i < 5; ++i) doublearr[i] = i * 0.1;c = 'c';MPI_Send(intarr, 1, newtype, 1, 0, MPI_COMM_WORLD);}else {MPI_Recv(intarr, 1, newtype, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);for (int i = 0; i < 3; ++i) cout << intarr[i] << " ";cout << endl;for (int i = 0; i < 5; ++i) cout << doublearr[i] << " ";cout << endl;cout << c << endl;}效果也確實如我們所預料的那樣
PS D:\Code\C++\repo\MPITest\x64\Debug> mpiexec -n 2 ./MPITest.exe 1 2 3 0 0.1 0.2 0.3 0.4 c PS D:\Code\C++\repo\MPITest\x64\Debug>完整代碼
#include <mpi.h> #include <stdio.h> #include <string> #include <string.h> #include <iostream> using namespace std; #pragma warning(disable : 4996) const int MAX_STRING = 100;#define FUN(x) (x * x)int main(int argc, char **argv) {int comm_sz;int my_rank;MPI_Init(NULL, NULL);MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);if (comm_sz != 2) return 0;// 創建自定義數據類型// { int[3], double[5], char };// MPI_Aint 存地址MPI_Aint intarr_p, doublearr_p, c_p; int intarr[3]; double doublearr[5];char c;// 獲取各個元素之間的相對地址MPI_Get_address(intarr, &intarr_p);MPI_Get_address(doublearr, &doublearr_p);MPI_Get_address(&c, &c_p); // 注意注意使用指針// 計算偏移值數組MPI_Aint displacement[3] = {0, // 第一個偏移值為0doublearr_p - intarr_p,c_p - intarr_p};// 定義各個部分的類型int types[3] = { MPI_INT, MPI_DOUBLE, MPI_CHAR };// 每個部分的長度int blockLength[3] = { 3, 5, 1 };// 新定義類型MPI_Datatype newtype;MPI_Type_create_struct(3, // 三個元素 { int[3], double[5], char };blockLength, // 分塊長度displacement, // 偏移值types, // 類型&newtype // 類型指針);// 提交新類型MPI_Type_commit(&newtype);// Testif (my_rank == 0) {for (int i = 0; i < 3; ++i) intarr[i] = i + 1;for (int i = 0; i < 5; ++i) doublearr[i] = i * 0.1;c = 'c';MPI_Send(intarr, 1, newtype, 1, 0, MPI_COMM_WORLD);}else {MPI_Recv(intarr, 1, newtype, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);for (int i = 0; i < 3; ++i) cout << intarr[i] << " ";cout << endl;for (int i = 0; i < 5; ++i) cout << doublearr[i] << " ";cout << endl;cout << c << endl;}// 釋放類型空間MPI_Type_free(&newtype);MPI_Finalize(); }總結
以上是生活随笔為你收集整理的MPI派生数据类型发送接收,降低发送时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【MPI高性能计算】用集合通信改进梯形求
- 下一篇: 【OpenMP实现】任意线程数并行化快排