入力として 2 つのファイル (それぞれがベクトルを表す) を受け取り、それらの間の内積を計算するプログラムを変更するのに苦労しています。並行して実行されるはずですが、各ファイルのポイント数が利用可能なプロセッサの数で割り切れない可能性があり、各プロセスがファイル内の誤った位置から読み取る可能性があると言われました。つまり、プロセッサが 4 つある場合、最初の 250 ポイントは正しく読み取られて計算される可能性がありますが、2 番目のプロセッサは同じ 250 ポイントを読み取って誤った結果を返す可能性があります。これが私がこれまでやってきたことです。私が行った変更はすべて記載されています。
#include "fstream"
#include "stdlib.h"
#include "stdio.h"
#include "iostream"
#include "mpi.h"
int main(int argc, char *argv[]){
MPI_Init(&argc, argv);
//parse command line arguments
if( argc < 3 || argc > 3){
std::cout << "*** syntax: " << argv[0] << " vecFile1.txt vecFile2.txt" << std::endl;
return(0);
}
//get input file names
std::string vecFile1(argv[1]);
std::string vecFile2(argv[2]);
//open file streams
std::ifstream vecStream1(vecFile1.c_str());
std::ifstream vecStream2(vecFile2.c_str());
//check that streams opened properly
if(!vecStream1.is_open() || !vecStream2.is_open()){
std::cout << "*** Could not open Files ***" << std::endl;
return(0);
}
//if files are open read their lengths and make sure they are compatible
long vecLength1 = 0; vecStream1 >> vecLength1;
long vecLength2 = 0; vecStream2 >> vecLength2;
if( vecLength1 != vecLength2){
std::cout << "*** Vectors are no the same length ***" << std::endl;
return(0);
}
int numProc; //New variable for managing number of processors
MPI_Comm_size(&numProc,MPI_COMM_WORLD); //Added line to obtain number of processors
int subDomainSize = (vecLength1+numProc-1)/numProc; //Not sure if this is correct calculation; meant to account for divisibility with remainders
//read in the vector components and perform dot product
double dotSum = 0.;
for(long i = 0; i < subDomainSize; i++){ //Original parameter used was vecLength1; subDomainSize used instead for each process
double ind1 = 0.; vecStream1 >> ind1;
double ind2 = 0.; vecStream2 >> ind2;
dotSum += ind1*ind2;
}
std::cout << "VECTOR DOT PRODUCT: " << dotSum << std::endl;
MPI_Finalize();
}
それらの変更は別として、ここからどこへ行くべきかわかりません。このプログラムで、2 つのテキスト ファイルを入力として並列処理を使用して 2 つのベクトルの内積を正しく計算するにはどうすればよいですか? それぞれに 100000 ポイントが含まれているため、ファイルを手動で変更することは実際的ではありません。