-2
1Dアレイを並列に合計するためにAMPIをコード化しようとしています。しかし、セグメンテーションフォルトが発生しています(コアダンプされています)。並列計算におけるセグメンテーションフォールト(コアダンプ)
私は何度も自分の機能を修正しようとしましたが、どこでエラーが発生したか、または修正する方法が見つかりませんでした。私はあなたのコードを実行したとき
/* File: mpi_sum.c
* Compile as: mpicc -g -Wall -std=c99 -o mpi_sum mpi_sum.c -lm
* Run as: mpirun -n 40 ./mpi_sum
* Description: An MPI solution to sum a 1D array. */
int main(int argc, char *argv[]) {
int myID, numProcs; // myID for the index to know when should the cpu start and stop calculate
//numPro numper of cpu you need to do the calculation
double localSum; // this for one operation on one cpu
double parallelSum; // this for collecting the values of localsum
int length = 10000000; // this for how many num
double Fact = 1 ;
int i; // this for for loop
clock_t clockStart, clockEnd; // timer
srand(5); // Initialize MPI
MPI_Init(NULL, NULL); //Initialize MPI
MPI_Comm_size(MPI_COMM_WORLD, &numProcs); // Get size
MPI_Comm_rank(MPI_COMM_WORLD, &myID); // Get rank
localSum = 0.0; // the value for eash cpu is 0
int A = (length/numProcs)*((long)myID); // this is to make each cpu work on his area
int B = (length/numProcs)*((long)myID + 1); // this is to make each cpu work on his area
A ++; // add 1 to go to next num
B ++;
clockStart = clock(); // start the timer to see how much time it take
for (i = A; i < B; i++)
{
Fact = (1/myID - 1/numProcs)/(1 - 1/numProcs);
localSum += Fact ;
}
MPI_Reduce(&localSum, ¶llelSum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
clockEnd = clock();
if (myID == 0)
{
printf("Time to sum %d floats with MPI in parallel %3.5f seconds\n", length, (clockEnd - clockStart)/(float)CLOCKS_PER_SEC);
printf("The parallel sum: %f\n", parallelSum + 1);
}
MPI_Finalize();
return 0;
}
誰でもそれを見てみる前に質問のコードを適切に書式設定する必要があります –
デバッガーを使って絞り込みを試みましたか?誰かがこのコードをすべて読んであなたに指摘することは期待できません。デバッガはそれを絞り込む正しい方法です。あなたはダイナミックメモリを割り当てていません。これは一般的なセグメンテーションの問題の初心者のCプログラマが遭遇する問題です。次の候補は、アドレスを渡すMPIコールです。これらの関数呼び出しの関数または少なくともプロトタイプヘッダーは表示されていないため、渡されるデータ型に一致するかどうかの評価はできません。それを確認する必要があります。 – lurker
使用しているMPIのバージョンは? MPI_initが原因である可能性があるためです。そうでなければ、なぜあなたはここに欲望を持っていますか?ここで乱数は使用しないでください。 MPI_initのドキュメンテーションは、初期化の前にやることを避けることを言います。 –