C: Assignments Day 4

Today we have two problems for you to tackle. They both parallelize the pi.c code you developed for day 1. Both programs will need to be compiled at one of the TACC supercomputers.

The figure below shows an method to compute pi by numerical integration. We would like you to implement that computation in a C program.

../_images/pi.png

Computation of pi numerically

The solution pi.c can be found on github. The contents of that file is presented here:

 1#include <stdio.h>
 2#include <time.h>
 3#include <math.h>
 4
 5static long int numSteps = 1000000000;
 6
 7int main() {
 8
 9  // perform calculation
10  double pi   = 0;
11  double dx = 1./numSteps;
12  double x  = dx*0.50;
13  
14  for (int i=0; i<numSteps; i++) {
15    pi += 4./(1.+x*x);
16    x += dx;
17  }
18  
19  pi *= dx;
20  
21  printf("PI = %16.14f Difference from math.h definition %16.14f \n",pi, pi-M_PI);
22  return 0;
23}

Note

  1. When compiling at TACC if you wish to use gcc as I have done, issue the following command when you login.

module load gcc
  1. When building and testing that the application works, use idev, as I have been showing in the videos.

  2. When launchig the job to test the performance you will need to use sbatch and place your job in the queue. To do this you need to create a script that will be launched when the job runs. I have placed two scripts in each of the file folders. The script informs the system how many nodes and cores per node, what the expected run time is, and how to run the jib. Once the executable exists, the job is launched using the following command issued from a login node:

sbatch submit.sh

Full documentation on submitting scripts for OpenMP and MPI can be found online at TACC

Warning

Our solution of pi.c as written as a loop dependency which may need to revise for the second problem.

Problem 1: Parallelize using MPI

You are to modify the pi.c application and run it to use mpi. I have included a few files in code/parallel/ExercisesDay4/ex1 to help you. They include pi.c above, gather1.c and a submit.sh script. gather1.c was presented in the video, and us shown below:

 1#include <mpi.h>
 2#include <stdio.h>
 3#include <stdlib.h>
 4#define LUMP 5
 5
 6int main(int argc, char **argv) {
 7  
 8  int numP, procID;
 9
10  // the usual mpi initialization
11  MPI_Init(&argc, &argv);
12  MPI_Comm_size(MPI_COMM_WORLD, &numP);
13  MPI_Comm_rank(MPI_COMM_WORLD, &procID);
14
15  int *globalData=NULL;
16  int localData[LUMP];
17
18  // process 0 is only 1 that needs global data
19  if (procID == 0) {
20    globalData = malloc(LUMP * numP * sizeof(int) );
21    for (int i=0; i<LUMP*numP; i++)
22      globalData[i] = 0;
23  }
24
25  for (int i=0; i<LUMP; i++)
26    localData[i] = procID*10+i;
27  
28  MPI_Gather(localData, LUMP, MPI_INT, globalData, LUMP, MPI_INT, 0, MPI_COMM_WORLD);
29
30  if (procID == 0) {
31    for (int i=0; i<numP*LUMP; i++)
32      printf("%d ", globalData[i]);
33    printf("\n");
34  }
35
36  if (procID == 0)
37    free(globalData);
38
39  MPI_Finalize();
40}

The submit script is as shown below.

 1#!/bin/bash
 2#--------------------------------------------------------------------
 3# Generic SLURM script – MPI Hello World
 4#
 5# This script requests 1 node and 8 cores/node (out of total 64 avail)
 6# for a total of 1*8 = 8 MPI tasks.
 7#---------------------------------------------------------------------
 8#SBATCH -J myjob
 9#SBATCH -o myjob.%j.out 
10#SBATCH -e myjob.%j.err 
11#SBATCH -p development
12#SBATCH -N 1
13#SBATCH -n 4
14#SBATCH -t 00:02:00
15#SBATCH -A DesignSafe-SimCenter
16
17ibrun ./pi
18
19

Problem 2: Parallelize using OpenMP

You are to modify the pi.c application and run it to use mpi. I have included a few files in code/parallel/ExercisesDay4/ex1 to help you. They include pi.c above and submitPI.sh script. submitPI.sh is as shown: