假设我的输入文件很大。
假设此文件包含我要并行处理的项目。
std::vector<std::string> items(100000,"");
for(int i = 0; i < 1000000; i++)
items[i] = pop_item(file);
接下来,我想通过与MPI并行处理这些项目来加快处理速度:
std::vector<MyObj> processed_items(100000); // pseudo-code, i handle the memory mallocing
int size; rank;
MPI_INIT();
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
for(i = rank; i < 100000; i += size)
processed_items[i] = process_item(items[i]);
MPI_FINALIZE();
好的,很好。
现在,我想在while循环中一遍又一遍地做它:
while(!done){
done = fill_items(&items, file);
MPI_INIT();
...;
MPI_FINALIZE();
print_items(&processed_items);
}
但是,我失败并出现“错误:在调用mpi finalize之后调用了mpi_init”。
我在MPI中处理此问题的预期方式是什么?
答案 0 :(得分:2)
MPI_INIT()和MPI_FINALIZE,因为您的错误提示。半年前的This old answer概述了如何使MPI并行运行程序的某些部分:
int main(int argc, char *argv[]) {
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
if (myid == 0) { // Do the serial part on a single MPI thread
printf("Performing serial computation on cpu %d\n", myid);
PreParallelWork();
}
ParallelWork(); // Every MPI thread will run the parallel work
if (myid == 0) { // Do the final serial part on a single MPI thread
printf("Performing the final serial computation on cpu %d\n", myid);
PostParallelWork();
}
MPI_Finalize();
return 0;
}