MPI:广播 long long int [英] MPI: Broadcasting a long long int

查看:161
本文介绍了MPI:广播 long long int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个程序通过将随机的飞镖"(采样点)扔到一个圆或半径 = 1 的圆上来估计 Pi,该圆或半径 = 1 的方板内接长度 = 2.使用关系

This program estimates Pi by throwing random "darts" (sampling points) to a circle or radius=1 inscribed inside a square board of length=2. Using the relationship

Area of circle / Area of Square = Pi/4

我们可以使用相同的关系来估计 Pi

we can estimate Pi using the same relationship expressed as

Darts Inside Circle / Darts Outside Circle = Pi/4

当我在 #define 中指定 NDARTS 时,程序工作正常,但是当尝试将它作为 long long int 广播时,请阅读从 scanf,我得到以下执行错误:

The program works fine when I specify NDARTS in a #define, but when trying to broadcast it as a long long int, read from scanf, I get the following execution error:

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_Bcast 声明有什么问题吗?

Is there anything wrong with my MPI_Bcast declaration?

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;
}

推荐答案

您没有正确使用 scanf().应该是这样的:

You're not using scanf() correctly. It should be like this instead:

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 初始化为空指针.所以 scanf() 在尝试写入时显然会出现段错误.

In your current code, long long int *NDARTS=0; effectively initializes NDARTS as a NULL pointer. So scanf() will obviously seg-fault when it tries to write to it.

这篇关于MPI:广播 long long int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆