MPI_Scatter:为什么必须在所有进程中分配内存? [英] MPI_Scatter: Why do I have to allocate memory in all the processes?

查看:246
本文介绍了MPI_Scatter:为什么必须在所有进程中分配内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理使用MPI(在c ++中)并行化Conways的《人生游戏》.我必须从输入中读取(非常大的)矩阵,然后将其按行分散在多个切片中,然后并行处理每个切片.我遵循的想法是只让一个进程处理I/O内容.特别是,进程0从文件中读取并将初始数据保存到一个RxC矩阵中,以散布在(R/P)xC切片矩阵"中的进程中. 现在,当我执行例程MPI_Scatter时,编译器会抱怨,因为大矩阵"仅在第一个过程中分配.为了使事情正常进行,我必须在所有过程中分配大矩阵,即使这些矩阵保持空白.这很普通,还是我做错了什么?有没有一种方法可以避免为每个进程分配一个空白的,无用的矩阵? 谢谢你们!

I am dealing with parallelizing Conways' Game of Life using MPI (in c++). I have to read a (very large) matrix from input, then scatter it in slices row-wise, and then process every slice in parallel. The idea I am following is to let only one process deal with the I/O stuff. In particular, process 0 read from file and saves the initial datas into a say RxC matrix, to be scattered among the process in (R/P)xC "slice matrices". Now, when I perform the routine MPI_Scatter, the compiler complaints because the "big matrix" is allocated only in the first process. To make things work I have to allocate the big matrix in all the process, even if those remains blank. Is this ordinary, or I am doing something wrong? Is there a way to avoid allocating a blank, useless matrix for every process? Thank you guys!

推荐答案

您不必在每个地方分配大矩阵,但是大矩阵变量需要在任何地方声明.试试这个:

You don't have to allocate the big matrix everywhere, but the big matrix variable needs to be declared everywhere. Try this:

int* big_matrix;
if(process_id == 0) {
    big_matrix = (int*) malloc(big_number * sizeof(int));
    // fill the big matrix with values
}
int* part_of_matrix = (int*) malloc(small_number * sizeof(int));
MPI_Scatter(big_matrix, small_number, MPI_INT, part_of_matrix, small_number, MPI_INT, 0, MPI_COMM_WORLD);

至少这是在C语言中执行此操作的一种方法.您可能必须将big_matrix初始化为0或在C ++中进行初始化.

At least this is a way to do it in C. You might have to initialize the big_matrix to 0 or something in C++.

这篇关于MPI_Scatter:为什么必须在所有进程中分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆