日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MPI派生数据类型发送接收,降低发送时间

發布時間:2025/4/16 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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派生数据类型发送接收,降低发送时间的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。