MPI初心者です。事前に計算され、バイナリ ファイルに格納されている多数の数値配列gmat
(double 型、次元 1x14000000) があります。約 100 MB のメモリを使用します (14000000 x8 バイト /1024 /1024)。この配列に対して何らかの計算を行う MPI コードを書きたいと思います (たとえば、gmat
プロセスのすべての要素をランク数で乗算します)。この配列gmat
自体は、実行時に一定のままです。コードは次のようなものになっているはずです
#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 はそれらすべてで同じですか? つまり、コードは各プロセスの gmat を格納するためにメモリ内で 16 x 100 MB を使用しますか、それとも gmat をグローバルに定義したので 100 MB しか使用しませんか? また、非常に多くの数値を読み取るのに時間がかかるため、さまざまなプロセスが gmat をファイルから個別に読み取ることは望ましくありません。これを行うより良い方法は何ですか? ありがとう。