Reader small image

You're reading from  Raspberry Pi Super Cluster

Product typeBook
Published inNov 2013
Reading LevelBeginner
PublisherPackt
ISBN-139781783286195
Edition1st Edition
Languages
Right arrow
Author (1)
Andrew K. Dennis
Andrew K. Dennis
author image
Andrew K. Dennis

Andrew K. Dennis is a full stack and cybersecurity architect with over 17 years' experience who currently works for Modus Create in Reston, VA. He holds two undergraduate degrees in software engineering and creative computing and a master's degree in information security. Andy has worked in the US, Canada, and the UK in software engineering, e-learning, data science, and cybersecurity across his career, and has written four books on IoT, the Raspberry Pi, and supercomputing. His interests range from the application of pataphysics in computing to security threat modeling. Andy lives in New England and is an organizer of Security BSides CT.
Read more about Andrew K. Dennis

Right arrow

Pi with C language and MPI


We have seen that we can calculate П with Hadoop. We can now try a similar application in C. The program we will now write will generate results similar to what we saw with the example program included with MPICH and will also use a MapReduce-style approach.

Create a new file at the following location to store your code in:

~/mpich3/code/monte_carlo_pi.c

Open this file and add the following code:

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

double insidecircle(int throws);

#define GAMES 20
#define THROWS 100

The previous block of code includes the necessary header files and defines a function and two constants. The function insidercircle() will be responsible for calculating П.

The first constant is the number of GAMES, that is, attempts at calculating П. The second defines the number of THROWS in each game. Now add the following code to the end of the file:

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

double jobaverage, calcpi...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Raspberry Pi Super Cluster
Published in: Nov 2013Publisher: PacktISBN-13: 9781783286195

Author (1)

author image
Andrew K. Dennis

Andrew K. Dennis is a full stack and cybersecurity architect with over 17 years' experience who currently works for Modus Create in Reston, VA. He holds two undergraduate degrees in software engineering and creative computing and a master's degree in information security. Andy has worked in the US, Canada, and the UK in software engineering, e-learning, data science, and cybersecurity across his career, and has written four books on IoT, the Raspberry Pi, and supercomputing. His interests range from the application of pataphysics in computing to security threat modeling. Andy lives in New England and is an organizer of Security BSides CT.
Read more about Andrew K. Dennis