Calculate pi Value Using Leibniz Formula with Intel® MPI Library with Java*

790477
9/27/2023

Introduction

This sample application demonstrates how to use Intel® MPI Library with Java* to calculate the pi value using the Leibniz formula.

Available Downloads

  • Linux*
  • Size: 1.8 KB
  • SHA1: D7C108989CC5B1D2AD005CD49DCAAE88E3EFAD5C

Detailed Description

  • Intel® MPI Library provides an experimental feature to enable support for Java* MPI applications. Intel MPI Library provides Java bindings for a subset of MPI-2 routines. This sample application demonstrates how to use Intel MPI Library with Java to calculate the pi value using the Leibniz formula.
  • This Java code imports Intel MPI Library to initialize MPI and perform the Leibniz formula.
  • This Leibniz formula is used to calculate pi in this example. It's an infinite series method for calculating pi, which requires a large amount of iterations to produce an accurate value of pi.
  • In the code, intervals are defined for the calculation of pi as 100000; each process will calculate partial sum for the number of iterations equal to intervals divided by the number of processes in the communicator(MPI_COMM_WORLD). The partial sums calculated by each of the processes is added to obtain the total sum, which is then multiplied by 4 and printed in 10long float.
  • The Leibniz formula operates for calculating pi as follows:

                          partial sum = for first iteration 1, for second 1 - 1/3 , so on ..
                          π/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - ... (this is the total sum)
                           Or,
                          π = 4 ( 1 - 1/3 + 1/5 - 1/7 + 1/9 - ... )

For more information on building and running Intel MPI Library applications with Java, refer to the following documentation:

Build and Run MPI Applications from Intel with Java*

Intel® MPI Library Developer Reference for Linux* OS