MPI和全局变量 [英] MPI and global variables

查看:553
本文介绍了MPI和全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须实施一个MPI程序.有一些全局变量(4个浮点数数组和其他6个单个浮点变量)首先由主进程从文件中读取数据来初始化.然后我调用MPI_Init,而等级0的进程等待结果,而其他进程(等级1,2,3,4)在数组上工作等等. 问题在于这些数组似乎不再初始化,所有都设置为0.我试图在全局函数内部移动全局变量,但结果是相同的.调用MPI_Init()时,所有进程都是由fork创建的,对吗?那么每个人都有父亲的记忆副本,那么为什么他们看不到初始化的数组?

I have to implement an MPI program. There are some global variables (4 arrays of float numbers and other 6 single float variables) which are first inizialized by the main process reading data from a file. Then I call MPI_Init and, while process of rank 0 waits for results, the other processes (rank 1,2,3,4) work on the arrays etc... The problem is that those array seem not to be initialized anymore, all is set to 0. I tried to move global variable inside the main function but the result is the same. When MPI_Init() is called all processes are created by fork right? So everyone has a memory copy of the father so why do they see not initizialized arrays?

推荐答案

我担心您误会了.

最好将每个MPI进程视为一个独立的程序,尽管它与计算中的每个其他进程都具有相同的源代码.进程0对它的地址空间中的变量执行的操作对其他进程的地址空间的内容没有影响.

It is probably best to think of each MPI process as an independent program, albeit one with the same source code as every other process in the computation. Operations that process 0 carries out on variables in its address space have no impact on the contents of the address spaces of other processes.

我不确定MPI标准是否甚至要求进程0具有在调用mpi_init之前(即进程0确实存在之前)已声明和初始化的变量的值.

I'm not sure that the MPI standard even requires process 0 to have values for variables which were declared and initialised prior to the call to mpi_init, that is before process 0 really exists.

无论是否需要,您都必须编写代码以将值放入其他进程的地址空间中的变量中.一种方法是让进程0将值一一发送到其他进程,或者使用广播.另一种方法是让所有进程从输入文件中读取值.如果选择此选项,请注意在I/O资源上的争用.

Whether it does or not you will have to write code to get the values into the variables in the address space of the other processes. One way to do this would be to have process 0 send the values to the other processes, either one by one or using a broadcast. Another way would be for all processes to read the values from the input files; if you choose this option watch out for contention over i/o resources.

顺便说一句,我不认为MPI实现通过调用mpi_init进行派生来创建进程是很常见的,派生更常用于创建线程.我认为大多数MPI实现实际上是在您调用mpiexec时创建进程的,对mpi_init的调用是一种形式,它宣布您的程序正在开始并行计算.

In passing, I don't think it is common for MPI implementations to create processes by forking at the call to mpi_init, forking is more commonly used for creating threads. I think that most MPI implementations actually create the processes when you make a call to mpiexec, the call to mpi_init is the formality which announces that your program is starting its parallel computations.

这篇关于MPI和全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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