2
我試圖編寫這個非常基本的MPI代碼,但我一直掛斷。 該任務是爲MPI_Send和Receive例程編寫一個包裝器,以便可以隱藏指針的使用情況。MPI_Send和接收 - 包裝
以下是我開發的:
#include "mpi.h"
#include<iostream>
#include<cstdlib>
#define _MAXSIZE_ 10
using namespace std;
/** Goal: Avoid pointers in MPI_Send and MPI_Recieve */
/* Wrapper for regular MPI_Send. */
void Send(int data, int destination, MPI_Comm mpicomm) {
MPI_Send(&data, 1, MPI_INT, destination, 0, mpicomm);
cout << "Data sent successfully" << data << endl;
}
/* Wrapper for regular MPI_Recieve */
int Recieve(MPI_Status stat, MPI_Comm mpicomm, int source_id = 0) {
int data;
MPI_Recv(&data, 1, MPI_INT, source_id, 0, mpicomm, &stat);
cout << "Data Recieved: " << data << endl;
return data;
}
int main(int argc, char ** argv) {
int myid, numprocs;
int arr[10];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Status status;
MPI_Comm mpicomm;
/** Trying to send an array of 10 integers without pointer usage */
int data = 3;
int destination = rand() % numprocs; // choose a destination to send other than the master itself
cout << "Destination: " << destination << "\n" << endl;
if(myid == 0) {
if(destination != 0) {
Send(data, destination, mpicomm);
}
}
else if(myid == destination) {
int data = Recieve(status,mpicomm, 0);
cout << "Data Received Successfully" << data << endl;
}
MPI_Finalize();
return 0;
}
附:我正在跟蹤我現在得到的答覆。謝謝。
桑傑
Jonathan非常感謝你。我正在嘗試它。會及時向大家發佈。我也正確地說(與你好的類比)與集羣中的處理器進行通信,我指定了該處理器的等級,並且還必須指定可以使用對所有節點通用的通信器句柄MPI_COMM_WORLD? – svk 2011-06-12 22:31:27
沒錯。當你啓動一個MPI程序時,所有處理器都是MPI_COMM_WORLD的成員,並且通過指定(rank,MPI_COMM_WORLD)與它們通信。您也可以創建自己的傳播者,這些傳播者只包含所有流程的子集,或者包含所有流程但爲了方便而以某種方式重新標記它們。在這種情況下,同一過程在不同的傳播者中可能有不同的等級;這就是爲什麼你總是需要指定傳播者。 – 2011-06-13 13:14:40
非常感謝。你還可以建議一本好書/資源來理解這些細節。 (雖然我有邁克爾艾倫和威爾金森) – svk 2011-06-14 02:49:23