我是一名MPI初学者。我有一个大数组gmat
的数字(类型double,尺寸1x14000000),它是预先计算并存储在一个二进制文件中。它将在内存中使用大约100 MB(14000000 x8字节/ 1024/1024)。我想写一个MPI代码,它将在这个数组上进行一些计算(例如,将进程的排名数字乘以gmat
的所有元素)。这个数组gmat
本身在运行时保持不变。 的代码应该是这样的如何在MPI中使用共享全局数据集?
#include <iostream>
#include "mpi.h"
double* gmat;
long int imax;
int main(int argc, char* argv[])
{
void performcomputation(int rank); // this function performs the computation and will be called by all processes
imax=atoi(argv[1]); // user inputs the length of gmat
MPI::Init();
rank = MPI::COMM_WORLD.Get_rank();
size = MPI::COMM_WORLD.Get_size(); //i will use -np 16 = 4 processors x 4 cores
if rank==0 // read the gmat array using one of the processes
{
gmat = new double[imax];
// read values of gmat from a file
// next line is supposed to broadcast values of gmat to all processes which will use it
MPI::COMM_WORLD.Bcast(&gmat,imax,MPI::DOUBLE,1);
}
MPI::COMM_WORLD.Barrier();
performcomputation(rank);
MPI::Finalize();
return 0;
}
void performcomputation(int rank)
{
int i;
for (i=0;i <imax; i++)
cout << "the new value is" << gmat[i]*rank << endl;
}
我的问题是,当我使用运行16个进程(-np 16)这段代码,是GMAT同样为所有的人?我的意思是,代码将使用16 x 100 MB的内存来存储每个进程的gmat,还是只使用100 MB,因为我已经将gmat定义为全局的?我不希望不同的进程从文件中读取gmat,因为阅读这么多的数字需要时间。有什么更好的方法来做到这一点?谢谢。
感谢一吨:)正在等待您的回复。会记住你的观点。 – Guddu