[英]MPI_send and MPI_Resc - updating single array value
我正在編寫一個MPI版本的數組更新,我正在從多個進程更新單個數組。 以下是我的代碼 -
uint n_sigm;
int *suma_sigm;
int my_first_i = 0;
int my_last_i = 0;
using namespace std;
int main(int argc, char *argv[])
{
int rank, size, i;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
n_sigm=40;
int allocatedTask = n_sigm / size;
suma_sigm=(int *)malloc(sizeof(int)*n_sigm);
if (size < 2)
{
printf("Please run with two processes.\n");fflush(stdout);
MPI_Finalize();
return 0;
}
if (rank != 0)
{
my_first_i = rank*allocatedTask;
my_last_i = my_first_i+allocatedTask;
cout<<rank<<" is rank and "<<my_first_i<<" is first and "<<my_last_i<<" is my last "<<endl;
for (i=my_first_i; i<my_last_i; i++)
{
suma_sigm[i] = rand()%n_sigm;
cout<<"value at "<<i<<" is : "<<suma_sigm[i]<<endl;
}
MPI_Send(suma_sigm, allocatedTask, MPI_INT, 0, 123, MPI_COMM_WORLD);
}
else
{
for (i=0; i<allocatedTask; i++)
{ // process 0 executing its array
suma_sigm[i] = rand()%n_sigm;
}
MPI_Send(suma_sigm, allocatedTask, MPI_INT, 0, 123, MPI_COMM_WORLD);
for (i=0; i<n_sigm; i++){
suma_sigm[i] = 0;}
for (int q = 0; q < size; q++)
{
MPI_Recv(suma_sigm, allocatedTask, MPI_INT, q, 123, MPI_COMM_WORLD, &status);
cout<<" Process_"<<q<<" :";
int start = q*allocatedTask;
int last = start +allocatedTask;
for (int h=start; h<last; h++)
{
cout<<"value2 at "<<h<<" is : "<<suma_sigm[h]<<endl;
}cout<<endl;
}
fflush(stdout);
}
free(suma_sigm);
MPI_Finalize();
return 0;
}
正如您所看到的,我從所有等級生成數組“suma_sigm”的值,然后傳遞它,然后傳遞值顯示正常。 但是,除了過程0之外的所有過程的接收值都顯示為零。只有過程零能夠發送在接收函數中成功使用的值。
使用MPI_Gather
可以更輕松地解決您要解決的任務。
文件: http : //www.mcs.anl.gov/research/projects/mpi/mpi-standard/mpi-report-1.1/node69.htm#Node69
每個進程(包括根進程)將其發送緩沖區的內容發送到根進程。 根進程接收消息並按排名順序存儲它們。
文檔還顯示了與您的代碼類似的等效MPI_Send / MPI_Recv用法,但請注意MPI_Recv
的“ +i*recvcount*extent
”偏移量:
結果就好像組中的每個進程(包括根進程)都已執行了調用
MPI_Send(sendbuf, sendcount, sendtype, root , ...),
並且root執行了n次調用
MPI_Recv(recvbuf+i · recvcount· extent(recvtype), recvcount, recvtype, i ,...),
示例: http : //www.mcs.anl.gov/research/projects/mpi/mpi-standard/mpi-report-1.1/node70.htm
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.