MPI\u Gatherv内存问题(MPI&\x2B;C)
作为我问题的继续,我修改了可变内核数的代码。然而,在我的代码中实现GathereV的方式似乎不可靠。在3-4次运行后,收集缓冲区中的结束序列最终被损坏,这似乎是由于内存泄漏。示例代码:MPI\u Gatherv内存问题(MPI&\x2B;C),c,memory-leaks,parallel-processing,mpi,C,Memory Leaks,Parallel Processing,Mpi,作为我问题的继续,我修改了可变内核数的代码。然而,在我的代码中实现GathereV的方式似乎不可靠。在3-4次运行后,收集缓冲区中的结束序列最终被损坏,这似乎是由于内存泄漏。示例代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <mpi.h> int main (int argc, char *argv[]) { MPI_Init(&argc,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mpi.h>
int main (int argc, char *argv[]) {
MPI_Init(&argc, &argv);
int world_size,*sendarray;
int rank, *rbuf=NULL, count,total_counts=0;
int *displs=NULL,i,*rcounts=NULL;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
if(rank==0){
displs = malloc((world_size+1)*sizeof(int));
for(int i=1;i<=world_size; i++)displs[i]=0;
rcounts=malloc(world_size*sizeof(int));
sendarray=malloc(1*sizeof(int));
for(int i=0;i<1;i++)sendarray[i]=1111;
count=1;
}
if(rank!=0){
int size=rank*2;
sendarray=malloc(size*sizeof(int));
for(int i=0;i<size;i++)sendarray[i]=rank;
count=size;
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Gather(&count,1,MPI_INT,rcounts,1,MPI_INT,0,MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
if(rank==0){
displs[0]=0;
for(int i=1;i<=world_size; i++){
for(int j=0; j<i; j++)displs[i]+=rcounts[j];
}
total_counts=0;
for(int i=0;i<world_size;i++)total_counts+=rcounts[i];
rbuf = malloc(10*sizeof(int));
}
MPI_Gatherv(sendarray, count, MPI_INT, rbuf, rcounts,
displs, MPI_INT, 0, MPI_COMM_WORLD);
if(rank==0){
int SIZE=total_counts;
for(int i=0;i<SIZE;i++)printf("(%d) %d ",i, rbuf[i]);
free(rbuf);
free(displs);
free(rcounts);
}
if(rank!=0)free(sendarray);
MPI_Finalize();
}
#包括
#包括
#包括
#包括
int main(int argc,char*argv[]){
MPI_Init(&argc,&argv);
int world_size,*sendarray;
整数秩,*rbuf=NULL,计数,总计数=0;
int*disputs=NULL,i,*rcounts=NULL;
MPI通信等级(MPI通信世界和等级);
MPI_Comm_大小(MPI_Comm_WORLD和WORLD_大小);
如果(秩==0){
disfs=malloc((世界大小+1)*sizeof(int));
对于(int i=1;i更改此行:
rbuf = malloc(10*sizeof(int));
致:
作为旁注:每个MPI进程都存在于它自己的进程地址空间中,它们不能践踏彼此的数据,除非通过MPI_XXX函数显式传递错误的数据,这会导致未定义的行为。更改此行:
rbuf = malloc(10*sizeof(int));
致:
作为旁注:每个MPI进程都存在于它自己的进程地址空间中,它们不能践踏彼此的数据,除非通过MPI_XXX函数显式传递错误的数据,这会导致未定义的行为