C MPI:广播长整型

C MPI:广播长整型,c,mpi,C,Mpi,该程序通过将随机“省道”(采样点)投射到长度为2的正方形板内内接的圆或半径为1的圆上,来估计圆周率。利用关系 Area of circle / Area of Square = Pi/4 我们可以使用表示为的相同关系来估计Pi Darts Inside Circle / Darts Outside Circle = Pi/4 当我在#define中指定NDARTS时,程序运行良好,但当尝试将其作为长整型广播时,从scanf读取,我得到以下执行错误: mpirun -np 4 ./pi_mon

该程序通过将随机“省道”(采样点)投射到长度为2的正方形板内内接的圆或半径为1的圆上,来估计圆周率。利用关系

Area of circle / Area of Square = Pi/4
我们可以使用表示为的相同关系来估计Pi

Darts Inside Circle / Darts Outside Circle = Pi/4
当我在
#define
中指定
NDARTS
时,程序运行良好,但当尝试将其作为
长整型
广播时,从
scanf
读取,我得到以下执行错误:

mpirun -np 4 ./pi_montecarlo.x
-----------------------------------------------------------------------------
One of the processes started by mpirun has exited with a nonzero exit
code.  This typically indicates that the process finished in error.
If your process did not finish in error, be sure to include a "return
0" or "exit(0)" in your C code before exiting the application.

PID 10591 failed on node n0 (127.0.0.1) due to signal 11.
为什么?

我的MPI\U Bcast声明有什么问题吗

long long int *NDARTS=0;
scanf("%Ld",NDARTS); 
MPI_Bcast(NDARTS, 1, MPI_LONG_LONG_INT, 0, MPI_COMM_WORLD);
完整代码:

/*


    mpicc -g -Wall -lm pi_montecarlo3.c -o pi_montecarlo.x 



    mpirun -np 4 ./pi_montecarlo.x





*/





#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <mpi.h>


#define MASTER 0
#define PI 3.1415926535




d    ouble pseudo_random (double a, double b) {



    double r; 


    r = ((b-a) * ((double) rand() / (double) RAND_MAX)) +a;


    return r; 
}

int main(int argc, char*argv[]){

    long long int *NDARTS=0; 

    int proc_id, 
        n_procs, 
        llimit,  
        ulimit,  
        n_circle, 
        i;      


    double pi_current, 
           pi_sum,     
           x,         
           y,         
           z,          
           error,      
           start_time, 
           end_time;   

    struct timeval stime;

    llimit = -1;
    ulimit = 1;
    n_circle =0; 

    MPI_Init(&argc, &argv); 


    MPI_Comm_rank (MPI_COMM_WORLD, &proc_id);
    MPI_Comm_size (MPI_COMM_WORLD, &n_procs);

    if (proc_id == MASTER){
        printf("\nMonte Carlo Method to estimate Pi \n\n");

                printf("Introduce Number of Darts \n");

            scanf("%Ld",NDARTS); 


        printf("  Number of processes: %d \n", n_procs);
        printf("  Number of darts: %Ld \n", *NDARTS);



                MPI_Bcast(NDARTS, 1, MPI_LONG_LONG_INT, 0, MPI_COMM_WORLD);



        start_time = MPI_Wtime();

    }


    gettimeofday(&stime, NULL); 
    srand(stime.tv_usec * stime.tv_usec * stime.tv_usec * stime.tv_usec);


    for (i=1; i<=*NDARTS;i++){



        x = pseudo_random(llimit, ulimit);
        y = pseudo_random(llimit, ulimit);


        z = pow(x,2) + pow(y,2);



        if (z<=1.0){
            n_circle++;
        }
    }


    pi_current = 4.0 * (double)n_circle / (double) *NDARTS; 



    MPI_Reduce (&pi_current, &pi_sum, 1, MPI_DOUBLE, MPI_SUM, MASTER, MPI_COMM_WORLD);



       if (proc_id == MASTER) {



        pi_sum = pi_sum / n_procs;


        error = fabs ((pi_sum -PI) / PI) *100;


        end_time = MPI_Wtime();


        printf("Known value of PI  : %11.10f \n", PI);
        printf("Estimated Value of PI  : %11.10f\n", pi_sum);
        printf("Error Percentage   : %10.8f\n", error);
        printf("Time    : %10.8f\n\n", end_time - start_time);

    }


    MPI_Finalize();

    return 0;
}
/*
mpicc-g-Wall-lm pi_montecarlo3.c-o pi_montecarlo.x
mpirun-np 4./pi_montecarlo.x
*/
#包括
#包括
#包括
#包括
#包括
#定义主机0
#定义PI 3.1415926535
双伪随机(双a,双b){
双r;
r=((b-a)*((双)兰特()/(双)兰特最大值))+a;
返回r;
}
int main(int argc,char*argv[]){
长整型*NDARTS=0;
int proc_id,
n_procs,
利米特,
乌利米特,
n_圈,
我
双pi_电流,
皮尤森,
x,,
Y
Z
错误,
开始时间,
结束时间;
结构时间间隔;
llimit=-1;
ulimit=1;
n_圆=0;
MPI_Init(&argc,&argv);
MPI通信等级(MPI通信世界和过程id);
MPI_通信大小(MPI_通信世界和n_进程);
if(proc_id==MASTER){
printf(“\nMonte Carlo方法估计Pi\n\n”);
printf(“引入省道数\n”);
scanf(“%Ld”,NDARTS);
printf(“进程数:%d\n”,n\u进程);
printf(“省道数:%Ld\n”,*ndart);
MPI_Bcast(NDARTS,1,MPI_LONG_LONG_INT,0,MPI_COMM_WORLD);
开始时间=MPI时间();
}
gettimeofday(&stime,NULL);
srand(stime.tv_usec*stime.tv_usec*stime.tv_usec*stime.tv_usec);

对于(i=1;i您没有正确使用
scanf()
。应该是这样的:

long long int NDARTS;
scanf("%lld",&NDARTS); 
MPI_Bcast(&NDARTS, 1, MPI_LONG_LONG_INT, 0, MPI_COMM_WORLD);

在当前代码中,
long-long-int*NDARTS=0;
有效地将
NDARTS
初始化为空指针
在尝试写入时显然会出现seg错误。

谢谢,这确实解决了运行时问题,但是现在我的程序遇到了不止一个进程:-/。我将在另一个问题上发布该问题。