Parallel program to calculate PI

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Prime Mover

    Parallel program to calculate PI

    Hello all,

    I have got the pseudo-code below that I would like to convert to c
    language. The algorithm calculates Pi value. I am somewhat familiar
    with C language, but I am just starting to learn parallel programming.
    In this specific example, the idea seems to be simple: one must
    subdivide the main loop into pieces that can be executed by
    independent "tasks" (computers??).

    Then, each "worker task" executes a part of the loop a certain number
    of times, independently of the other worker tasks. One specific task
    plays the role of "master task", which will collect and sum the
    results of the worker tasks:

    % descriptive algorithm:
    1. Inscribe a circle inside a square
    2. Generate random points inside the square
    3. Determine the number of points that fell inside the circle
    4. Let r be the number of points inside the circle divided by the
    total number of points
    5. Pi is approximately equal to 4*r
    6. The more points are generated, the more is the precision in P value

    % pseudo-code (parallel):
    1. npoints = 10000
    2. circle_count = 0
    3. p = number of tasks
    4. num = npoints/p
    5. find out if I am MASTER or WORKER
    6. do j = 1,num
    7. generate 2 random numbers between 0 and 1
    8. xcoordinate = random1
    9. ycoordinate = random2
    10. if (xcoordinate, ycoordinate) inside circle then
    circle_count = circle_count + 1
    11. end do
    12. if I am MASTER
    13. receive from WORKERS their circle_counts
    14. compute PI (use MASTER and WORKER calculations)
    15. else if I am WORKER
    16. send to MASTER circle_count
    17. endif

    Any help would be much appreciated.

    I thank you all in advance.
  • Prime Mover

    #2
    Re: Parallel program to calculate PI

    Let me just be a bit more specific:

    My (understading) problem starts in the line 5 of the pseudo-code:
    5. find out if I am MASTER or WORKER

    How would I specificy a "worker"? Would that be another computer?
    If yes, how can I access this remote computer in the calculations, in
    C language?
    If yes, it means that I have to have a LAN or something to perform
    tests?

    I have found that there are some libraries such as OMP or MPI that
    could be
    used, but I'd like to know if there is a more "raw" way of doing this
    first.

    Again, thank you all.

    Comment

    • Spiros Bousbouras

      #3
      Re: Parallel program to calculate PI

      On 13 May, 15:20, Prime Mover <eple...@hotmai l.comwrote:
      Hello all,
      >
      I have got the pseudo-code below that I would like to convert to c
      language. The algorithm calculates Pi value. I am somewhat familiar
      with C language, but I am just starting to learn parallel programming.
      In this specific example, the idea seems to be simple: one must
      subdivide the main loop into pieces that can be executed by
      independent "tasks" (computers??).
      >
      Then, each "worker task" executes a part of the loop a certain number
      of times, independently of the other worker tasks. One specific task
      plays the role of "master task", which will collect and sum the
      results of the worker tasks:
      >
      % descriptive algorithm:
      1. Inscribe a circle inside a square
      2. Generate random points inside the square
      3. Determine the number of points that fell inside the circle
      4. Let r be the number of points inside the circle divided by the
      total number of points
      5. Pi is approximately equal to 4*r
      6. The more points are generated, the more is the precision in P value
      >
      % pseudo-code (parallel):
      1. npoints = 10000
      2. circle_count = 0
      3. p = number of tasks
      4. num = npoints/p
      5. find out if I am MASTER or WORKER
      6. do j = 1,num
      7. generate 2 random numbers between 0 and 1
      8. xcoordinate = random1
      9. ycoordinate = random2
      10. if (xcoordinate, ycoordinate) inside circle then
      circle_count = circle_count + 1
      11. end do
      12. if I am MASTER
      13. receive from WORKERS their circle_counts
      14. compute PI (use MASTER and WORKER calculations)
      15. else if I am WORKER
      16. send to MASTER circle_count
      17. endif

      On 13 May, 15:28, Prime Mover <eple...@hotmai l.comwrote:
      Let me just be a bit more specific:
      >
      My (understading) problem starts in the line 5 of the pseudo-code:
      5. find out if I am MASTER or WORKER
      >
      How would I specificy a "worker"? Would that be another computer?
      If yes, how can I access this remote computer in the calculations, in
      C language?
      If yes, it means that I have to have a LAN or something to perform
      tests?
      >
      I have found that there are some libraries such as OMP or MPI that
      could be
      used, but I'd like to know if there is a more "raw" way of doing this
      first.

      Standard C has no built-in support for parallel processing so an
      answer
      to the question "find out if I am MASTER or WORKER" falls outside
      standard C which is what's topical here. I have no idea what kind of
      hardware set-up and extensions to C can be used to tackle numerically
      intensive parallel algorithms. Perhaps others here have the relevant
      experience. Searching Google groups for *parallel* I found
      comp.parallel.m pi and comp.parallel.p vm where I'm guessing you might
      get more useful advice than here.

      Out of curiosity for what value of npoints are you aiming for ? In
      your example it's only 100000 and you can get that on a modern desktop
      in a few seconds.

      Comment

      • Spiros Bousbouras

        #4
        Re: Parallel program to calculate PI

        On 13 May, 15:28, Prime Mover <eple...@hotmai l.comwrote:
        >
        Let me just be a bit more specific:
        >
        My (understading) problem starts in the line 5 of the pseudo-code:
        5. find out if I am MASTER or WORKER
        >
        How would I specificy a "worker"? Would that be another computer?
        If yes, how can I access this remote computer in the calculations, in
        C language?
        If yes, it means that I have to have a LAN or something to perform
        tests?
        >
        I have found that there are some libraries such as OMP or MPI that
        could be
        used, but I'd like to know if there is a more "raw" way of doing this
        first.
        >
        Standard C has no built-in support for parallel processing so an
        answer
        to the question "find out if I am MASTER or WORKER" falls outside
        standard C which is what's topical here. I have no idea what kind of
        hardware set-up and extensions to C can be used to tackle numerically
        intensive parallel algorithms. Perhaps others here have the relevant
        experience. Searching Google groups for *parallel* I found
        comp.parallel.m pi and comp.parallel.p vm where I'm guessing you might
        get more useful advice than here.
        There's also comp.parallel

        Comment

        • Pietro Cerutti

          #5
          Re: Parallel program to calculate PI

          Prime Mover wrote:
          % descriptive algorithm:
          1. Inscribe a circle inside a square
          2. Generate random points inside the square
          3. Determine the number of points that fell inside the circle
          How can you determine it without knowing PI a priori?

          Pietro Cerutti

          Comment

          • Bart

            #6
            Re: Parallel program to calculate PI

            On May 13, 3:55 pm, Spiros Bousbouras <spi...@gmail.c omwrote:
            On 13 May, 15:20, Prime Mover <eple...@hotmai l.comwrote:
            I have got the pseudo-code below that I would like to convert to c
            language. The algorithm calculates Pi value. I am somewhat familiar
            Out of curiosity for what value of npoints are you aiming for ? In
            your example it's only 100000 and you can get that on a modern desktop
            in a few seconds.- Hide quoted text -
            I got 3.1415 using a billion points. Looks like it will converge very
            slowly.

            Also the granularity of the x,y points may affect the maximum accuracy
            (because it introduces errors near the circular edge). Tried a sphere
            too but not any better.

            --
            Bartc


            Comment

            • Richard Heathfield

              #7
              Re: Parallel program to calculate PI

              Pietro Cerutti said:
              Prime Mover wrote:
              >% descriptive algorithm:
              >1. Inscribe a circle inside a square
              >2. Generate random points inside the square
              >3. Determine the number of points that fell inside the circle
              >
              How can you determine it without knowing PI a priori?
              There's nothing in the above description that requires a knowledge of pi.

              For example, you can draw a circle of radius r easily enough just by
              applying the equation x^2 + y^2 = r^2 (no, that is not C code!) for all y
              in the range -r to +r, taking the positive result and its negative
              analogue for your x points. (Actually, it's even easier than that, because
              there are other symmetries to exploit.)

              Determining whether you're in the circle can be done either by tedious
              inspection (a process not unlike floodfill) or (much faster) by
              determining whether x^2 + y^2 <= r^2.

              --
              Richard Heathfield <http://www.cpax.org.uk >
              Email: -http://www. +rjh@
              Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
              "Usenet is a strange place" - dmr 29 July 1999

              Comment

              • Spiros Bousbouras

                #8
                Re: Parallel program to calculate PI

                On 13 May, 16:02, Pietro Cerutti <gahr@localhost wrote:
                Prime Mover wrote:
                % descriptive algorithm:
                1. Inscribe a circle inside a square
                2. Generate random points inside the square
                3. Determine the number of points that fell inside the circle
                >
                How can you determine it without knowing PI a priori?
                Imagine a circle with radius is 1 and its center has coordinates
                (0 , 0).
                A random point with coordinates (x,y) will be inside the square
                if -1 <= x <= 1 and -1 <= y <= 1
                if ( x*x + y*y < 1) /* inside the circle */


                Comment

                • Spiros Bousbouras

                  #9
                  Re: Parallel program to calculate PI

                  On 13 May, 16:15, Bart <b...@freeuk.co mwrote:
                  On May 13, 3:55 pm, Spiros Bousbouras <spi...@gmail.c omwrote:
                  >
                  On 13 May, 15:20, Prime Mover <eple...@hotmai l.comwrote:
                  I have got the pseudo-code below that I would like to convert to c
                  language. The algorithm calculates Pi value. I am somewhat familiar
                  Out of curiosity for what value of npoints are you aiming for ? In
                  your example it's only 100000 and you can get that on a modern desktop
                  in a few seconds.- Hide quoted text -
                  >
                  I got 3.1415 using a billion points. Looks like it will converge very
                  slowly.
                  >
                  Also the granularity of the x,y points may affect the maximum accuracy
                  (because it introduces errors near the circular edge). Tried a sphere
                  too but not any better.
                  And of course you must know that your random numbers
                  will be uniformly distributed within the square.

                  Comment

                  • CBFalconer

                    #10
                    Re: Parallel program to calculate PI

                    Pietro Cerutti wrote:
                    Prime Mover wrote:
                    >
                    >% descriptive algorithm:
                    >1. Inscribe a circle inside a square
                    >2. Generate random points inside the square
                    >3. Determine the number of points that fell inside the circle
                    >
                    How can you determine it without knowing PI a priori?
                    Write the expression for the area of a circle. Do the same for the
                    area of the superscribed square. Take the ratio of areas. It
                    contains PI.

                    --
                    [mail]: Chuck F (cbfalconer at maineline dot net)
                    [page]: <http://cbfalconer.home .att.net>
                    Try the download section.

                    ** Posted from http://www.teranews.com **

                    Comment

                    • Richard Heathfield

                      #11
                      Re: Parallel program to calculate PI

                      Spiros Bousbouras said:

                      <snip>
                      And of course you must know that your random numbers
                      will be uniformly distributed within the square.
                      That's easy. Use the following random point generator:

                      #include <assert.h>

                      void rndpt(unsigned long int *x,
                      unsigned long int *y,
                      unsigned long int max) /* max = side of square */
                      {
                      static unsigned long int n = 0;
                      assert(x != NULL && y != NULL);
                      *x = n % max;
                      *y = n++ / max;
                      n %= max;
                      return;
                      }

                      If you call this i * max times, where max is a constant and i is an
                      integer, the distribution of the random points will be uniform.

                      --
                      Richard Heathfield <http://www.cpax.org.uk >
                      Email: -http://www. +rjh@
                      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                      "Usenet is a strange place" - dmr 29 July 1999

                      Comment

                      • Spiros Bousbouras

                        #12
                        Re: Parallel program to calculate PI

                        On 13 May, 17:00, Richard Heathfield <r...@see.sig.i nvalidwrote:
                        Spiros Bousbouras said:
                        >
                        <snip>
                        >
                        And of course you must know that your random numbers
                        will be uniformly distributed within the square.
                        >
                        That's easy. Use the following random point generator:
                        >
                        #include <assert.h>
                        >
                        void rndpt(unsigned long int *x,
                        unsigned long int *y,
                        unsigned long int max) /* max = side of square */
                        {
                        static unsigned long int n = 0;
                        assert(x != NULL && y != NULL);
                        *x = n % max;
                        *y = n++ / max;
                        n %= max;
                        return;
                        >
                        }
                        >
                        If you call this i * max times, where max is a constant and i is an
                        integer, the distribution of the random points will be uniform.
                        Since *y will always get the value 0 I don't think
                        so.

                        Comment

                        • Richard Heathfield

                          #13
                          Re: Parallel program to calculate PI

                          Spiros Bousbouras said:
                          On 13 May, 17:00, Richard Heathfield <r...@see.sig.i nvalidwrote:
                          >Spiros Bousbouras said:
                          >>
                          ><snip>
                          >>
                          And of course you must know that your random numbers
                          will be uniformly distributed within the square.
                          >>
                          >That's easy. Use the following random point generator:
                          >>
                          >#include <assert.h>
                          >>
                          >void rndpt(unsigned long int *x,
                          > unsigned long int *y,
                          > unsigned long int max) /* max = side of square */
                          >{
                          > static unsigned long int n = 0;
                          > assert(x != NULL && y != NULL);
                          > *x = n % max;
                          > *y = n++ / max;
                          > n %= max;
                          > return;
                          >>
                          >}
                          >>
                          >If you call this i * max times, where max is a constant and i is an
                          >integer, the distribution of the random points will be uniform.
                          >
                          Since *y will always get the value 0 I don't think
                          so.
                          Since it won't, I do.

                          --
                          Richard Heathfield <http://www.cpax.org.uk >
                          Email: -http://www. +rjh@
                          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                          "Usenet is a strange place" - dmr 29 July 1999

                          Comment

                          • Eric Sosman

                            #14
                            Re: Parallel program to calculate PI

                            Richard Heathfield wrote:
                            Spiros Bousbouras said:
                            >
                            >On 13 May, 17:00, Richard Heathfield <r...@see.sig.i nvalidwrote:
                            >>Spiros Bousbouras said:
                            >>>
                            >><snip>
                            >>>
                            >>>And of course you must know that your random numbers
                            >>>will be uniformly distributed within the square.
                            >>That's easy. Use the following random point generator:
                            >>>
                            >>#include <assert.h>
                            >>>
                            >>void rndpt(unsigned long int *x,
                            >> unsigned long int *y,
                            >> unsigned long int max) /* max = side of square */
                            >>{
                            >> static unsigned long int n = 0;
                            >> assert(x != NULL && y != NULL);
                            >> *x = n % max;
                            >> *y = n++ / max;
                            >> n %= max;
                            >> return;
                            >>>
                            >>}
                            >>>
                            >>If you call this i * max times, where max is a constant and i is an
                            >>integer, the distribution of the random points will be uniform.
                            >Since *y will always get the value 0 I don't think
                            >so.
                            >
                            Since it won't, I do.
                            Try running it for a while and making a histogram of
                            the y outputs.

                            (The bug is in the line preceding the return.)

                            --
                            Eric.Sosman@sun .com

                            Comment

                            • Bart

                              #15
                              Re: Parallel program to calculate PI

                              On May 13, 5:00 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                              Spiros Bousbouras said:
                              >
                              <snip>
                              >
                              And of course you must know that your random numbers
                              will be uniformly distributed within the square.
                              >
                              That's easy. Use the following random point generator:
                              >
                              #include <assert.h>
                              >
                              void rndpt(unsigned long int *x,
                                         unsigned long int *y,
                                         unsigned long int max) /* max = side of square */
                              {
                                static unsigned long int n = 0;
                                assert(x != NULL && y != NULL);
                                *x = n % max;
                                *y = n++ / max;
                                n %= max;
                                return;
                              >
                              }
                              >
                              If you call this i * max times, where max is a constant and i is an
                              integer, the distribution of the random points will be uniform.
                              You mean max*max?

                              This looks to be simply filling in a square sequentially. You are then
                              effectively calculating the area of a circle in a square by counting
                              the all dots. That's not really in the spirit of the original method.
                              (I think it also converges more slowly compared with the same number
                              of points picked randomly.)

                              --
                              Bartc

                              Comment

                              Working...