目录

Monte-Carlo随机积分算法估算π

概述

名字很高大上,其实高中也接触过了,就是极限求值。

代码

参考下面链接的源码。

https://www.olcf.ornl.gov/tutorials/monte-carlo-pi/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
#include <math.h>
#define SEED 35791246
 
int main(int argc, char* argv[])
{
    long niter = 1000000;
    int myid;                       //holds process's rank id
    double x,y;                     //x,y value for the random coordinate
    int i, count=0;                 //Count holds all the number of how many good coordinates
    double z;                       //Used to check if x^2+y^2<=1
    double pi;                      //holds approx value of pi
    int nodenum;
 
    MPI_Init(&argc, &argv);                 //Start MPI
    MPI_Comm_rank(MPI_COMM_WORLD, &myid);           //get rank of node's process
    MPI_Comm_size(MPI_COMM_WORLD, &nodenum);
    int recieved[nodenum];
    long recvniter[nodenum];
    srand(SEED+myid);                       //Give rand() a seed value. Needs to be different on each node
 
    if(myid != 0)
    {
        for (i=0; i<niter; ++i)                  //main loop
        {
            x= ((double)rand())/RAND_MAX;           //gets a random x coordinate
            y =((double)rand())/RAND_MAX;           //gets a random y coordinate
            z = sqrt(x*x+y*y);                  //Checks to see if number in inside unit circle
            if (z<=1)
            {
                count++;                //if it is, consider it a valid random point
            }
        }
        for(i=0; i<nodenum; ++i)
        {
            MPI_Send(&count,
                                 1,
                                 MPI_INT,
                                 0,
                                 1,
                                 MPI_COMM_WORLD);
            MPI_Send(&niter,
                                 1,
                                 MPI_LONG,
                                 0,
                                 2,
                                 MPI_COMM_WORLD);
        }
    }
    else if (myid == 0)
    {
        for(i=0; i<nodenum; ++i)
        {
            MPI_Recv(&recieved[i],
                                 nodenum,
                                 MPI_INT,
                                 MPI_ANY_SOURCE,
                                 1,
                                 MPI_COMM_WORLD,
                                 MPI_STATUS_IGNORE);
            MPI_Recv(&recvniter[i],
                                 nodenum,
                                 MPI_LONG,
                                 MPI_ANY_SOURCE,
                                 2,
                                 MPI_COMM_WORLD,
                                 MPI_STATUS_IGNORE);
        }
    }
 
    if (myid == 0)                      //if root process
    {
        int finalcount = 0;
        long finalniter = 0;
        for(i = 0; i<nodenum; ++i)
        {
            finalcount += recieved[i];
            finalniter += recvniter[i];
        }
 
        pi = ((double)finalcount/(double)finalniter)*4.0;               //p = 4(m/n)
        printf("Pi: %f\n", pi);             //Print the calculated value of pi
 
    }
 
    MPI_Finalize();                     //Close the MPI instance
    return 0;
}

总结

参考资料

警告
本文最后更新于 2017年2月1日,文中内容可能已过时,请谨慎参考。