Coder Social home page Coder Social logo

mpi's Introduction

MPI Programming

The purpose of this assignment is to familiarize yourself with MPI programming.

Statement of Problem 1

In this problem, you need to use MPI to parallelize the following serial program prime.c, which takes a long long int argument as an input, finds the largest prime number that is smaller than the input, and counts the prime numbers that are smaller than the input.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int isprime(int n) {
  int i,squareroot;
  if (n>10) {
    squareroot = (int) sqrt(n);
    for (i=3; i<=squareroot; i=i+2)
      if ((n%i)==0)
        return 0;
    return 1;
  }
  else
    return 0;
}

int main(int argc, char *argv[])
{
  int pc,       /* prime counter */
      foundone; /* most recent prime found */
  long long int n, limit;

  sscanf(argv[1],"%llu",&limit);    
  printf("Starting. Numbers to be scanned= %lld\n",limit);

  pc=4;     /* Assume (2,3,5,7) are counted here */

  for (n=11; n<=limit; n=n+2) {
    if (isprime(n)) {
      pc++;
      foundone = n;
    }           
  }
  printf("Done. Largest prime is %d Total primes %d\n",foundone,pc);

  return 0;
} 

Statement of Problem 2

In this problem, you need to use MPI to parallelize the following serial program integrate.c, which integrates function sin(X) over the range from 0 to pi using N intervals, where N is an argument of the program.

#include <stdio.h>
#include <math.h>

#define PI 3.1415926535

int main(int argc, char **argv) 
{
  long long i, num_intervals;
  double rect_width, area, sum, x_middle; 

  sscanf(argv[1],"%llu",&num_intervals);

  rect_width = PI / num_intervals;

  sum = 0;
  for(i = 1; i < num_intervals + 1; i++) {

    /* find the middle of the interval on the X-axis. */ 

    x_middle = (i - 0.5) * rect_width;
    area = sin(x_middle) * rect_width; 
    sum = sum + area;
  } 

  printf("The total area is: %f\n", (float)sum);

  return 0;
}   

Evaluation

Prime

number of iterations 20000000 200000000
sequential 14.3597421646 (seconds) 376.177364111 (seconds)
mpi in single machine 9.53690385818 (seconds) 151.067785978 (seconds)
mpi in multi machine 5.39104700089 (seconds) 124.963972092 (seconds)

Integrate

number of iterations 200000000 2000000000
sequential 10.6179821491 (seconds) 98.610033989 (seconds)
mpi in single machine 3.88602399826 (seconds) 31.2693638802 (seconds)
mpi in multi machine 3.93596291542 (seconds) 31.0279841423 (seconds)

mpi's People

Contributors

tclin914 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.