我正在尝试将来自2个或更多进程的数据写入单个文件。 trival helloworld exmaple很棒; 即https://wiki.scinet.utoronto.ca/wiki/images/8/88/Parallel_io_course_mpi_io.pdf 以幻灯片8为例。
我想在我的代码中做同样的事情。她是主要功能的阻挡。我只是不明白什么是错的,我现在正在排除故障。在底部的printf输出中,每件事看起来都很好,但是当我写入文件时,只有来自一个进程的数据被写入..arg:
void * charbuffer = malloc(MAX_BUFF);
if (charbuffer == NULL) {
exit(1);
}
int written_chars = 0;
int written_chars_accumulator = 0;
int n = number_of_points;
MPI_File file;
MPI_Offset offset;
MPI_File_open(MPI_COMM_WORLD,"test_write.txt",
MPI_MODE_CREATE|MPI_MODE_WRONLY,
MPI_INFO_NULL, &file);
//------------------------------------
int msgsize = (n/num_procs);
written_chars = snprintf((char *)charbuffer, MAX_BUFF, "%d %d %d %d-------------------------------------------------------------------------\n", n, context->BOX_SIDE, context->MAX_X, context->MAX_Y);
if (written_chars < 0){ exit(1); }
written_chars_accumulator += written_chars;
int my_start = my_rank*(n/num_procs);
int my_end = (my_rank+1)*(n/num_procs);
int i,j;
for(i=my_start;i<my_end;i++){ //dersom root skal skrive ut, hopper over egen del...
//d("i=%d,proc=%d\n",i,my_rank);
written_chars = snprintf((char *)charbuffer+written_chars_accumulator, (MAX_BUFF - written_chars_accumulator), "%d %d %d ", i, context->x[i], context->y[i]);
if (written_chars < 0){ exit(1); }
written_chars_accumulator += written_chars;
for(j=0;j<context->allNBfrom[i];j++){
//d("m=%d,proc=%d\n",j,my_rank);
written_chars = snprintf((char *)charbuffer+written_chars_accumulator, (MAX_BUFF - written_chars_accumulator), "%d ", context->delaunayEdges[i][j]);
if (written_chars < 0){ exit(1); }
written_chars_accumulator += written_chars;
}
written_chars = snprintf((char *)charbuffer+written_chars_accumulator, (MAX_BUFF - written_chars_accumulator), "\n");
if (written_chars < 0){ exit(1); }
written_chars_accumulator += written_chars;
}
offset = (n/num_procs)*my_rank;
MPI_File_seek(file,offset,MPI_SEEK_SET);
MPI_File_write(file,charbuffer,MAX_BUFF,MPI_CHAR,&status); //Til senere...
printf("proc=%d:\n%s",my_rank,charbuffer);
MPI_File_close(&file);
答案 0 :(得分:0)
也许您可以尝试将偏移量计算为offset = my_rank * MAX_BUFF
。我不确定它应该是MAX_BUFF
还是MAX_BUFF - 1
。