0

我编写了以下程序来查找具有#defined 值的素数。它是使用 mpi 的并行程序。谁能帮我找出其中的错误。它编译良好,但在执行时崩溃。

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#define N 65

int rank, size;
double start_time;
double end_time;

int y, x, i, port1, port2, port3;
int check =0;         // prime number checker, if a number is prime it always remains 0      through out calculation. for a number which is not prime it is turns to value 1 at some    point
int signal =0;       // has no important use. just to check if slave process work is done.

MPI_Status status;
MPI_Request request;

int main(int argc, char *argv[]){
MPI_Init(&argc, &argv); //initialize MPI operations
MPI_Comm_rank(MPI_COMM_WORLD, &rank); //get the rank
MPI_Comm_size(MPI_COMM_WORLD, &size); //get number of processes

if(rank == 0){                  // master process divides work and also does initial  work itself
start_time = MPI_Wtime();
printf("2\n");                  //print prime number 2 first  because the algorithm for finding the prime number in this program is just for odd number
port1 = (N/(size-1));           // calculating the suitable amount of work per process

for(i=1;i<size-1;i++){            // master sending the portion of work to each   slave
port2 = port1 * i;              // lower bound of work for i th  process 
port3 = ((i+1)*port1)-1;        // upper bound of work for i th process
MPI_Isend(&port2, 1, MPI_INT, i, 100, MPI_COMM_WORLD, &request);
MPI_Isend(&port3, 1, MPI_INT, i, 101, MPI_COMM_WORLD, &request);
}

port2 = (size-1)*port1; port3= N;     // the last process takes the remaining work
MPI_Isend(&port2, 1, MPI_INT, (size-1), 100, MPI_COMM_WORLD, &request);
MPI_Isend(&port3, 1, MPI_INT, (size-1), 101, MPI_COMM_WORLD, &request);

for(x = 3; x < port1; x=x+2){    // master doing initial work by itself
check = 0;
for(y = 3; y <= x/2; y=y+2){
if(x%y == 0) {check =1; break;}
}
if(check==0) printf("%d\n", x);
}
}

if (rank > 0){                    // slave working part

MPI_Recv(&port2,1,MPI_INT, 0, 100, MPI_COMM_WORLD, &status);
MPI_Recv(&port3,1,MPI_INT, 0, 101, MPI_COMM_WORLD, &status);
if (port2%2 == 0) port2++;                                        // changing the even  argument to odd to make the calculation fast because even number is never a prime except 2.
for(x=port2; x<=port3; x=x+2){
check = 0;
for(y = 3; y <= x/2; y=y+2){
if(x%y == 0) {check =1; break;}
}
    if (check==0) printf("%d\n",x);
}
signal= rank;
MPI_Isend(&signal, 1, MPI_INT, 0, 103, MPI_COMM_WORLD, &request);  // just informing  master that the work is finished

}

if (rank == 0){                                                    // master concluding the work and printing the time taken to do the work
for(i== 1; i < size; i++){
MPI_Recv(&signal,1,MPI_INT, i, 103, MPI_COMM_WORLD, &status);  // master confirming that all slaves finished their work
}
end_time = MPI_Wtime();
printf("\nRunning Time = %f \n\n", end_time - start_time);
   }
   MPI_Finalize();
   return 0;
   } 

我收到以下错误

mpirun -np 2 ./a.exe 异常:STATUS_ACCESS_VIOLATION at eip=0051401C 堆栈跟踪结束

4

1 回答 1

1

我发现我的程序出了什么问题。这是使用受限变量信号。将该变量的名称(在所有使用它的地方)更改为任何其他可行的名称,它就可以工作。

于 2013-05-28T06:48:20.623 回答