Skip to content

Commit

Permalink
fmk - adding assignments for day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
fmckenna committed Jun 8, 2023
1 parent d194b40 commit a5299ad
Show file tree
Hide file tree
Showing 14 changed files with 407 additions and 13 deletions.
40 changes: 40 additions & 0 deletions assignments/C-Day4/ex1/gather1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#define LUMP 5

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

int numP, procID;

// the usual mpi initialization
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numP);
MPI_Comm_rank(MPI_COMM_WORLD, &procID);

int *globalData=NULL;
int localData[LUMP];

// process 0 is only 1 that needs global data
if (procID == 0) {
globalData = malloc(LUMP * numP * sizeof(int) );
for (int i=0; i<LUMP*numP; i++)
globalData[i] = 0;
}

for (int i=0; i<LUMP; i++)
localData[i] = procID*10+i;

MPI_Gather(localData, LUMP, MPI_INT, globalData, LUMP, MPI_INT, 0, MPI_COMM_WORLD);

if (procID == 0) {
for (int i=0; i<numP*LUMP; i++)
printf("%d ", globalData[i]);
printf("\n");
}

if (procID == 0)
free(globalData);

MPI_Finalize();
}
23 changes: 23 additions & 0 deletions assignments/C-Day4/ex1/pi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>
#include <time.h>
#include <math.h>

static long int numSteps = 1000000000;

int main() {

// perform calculation
double pi = 0;
double dx = 1./numSteps;
double x = dx*0.50;

for (int i=0; i<numSteps; i++) {
pi += 4./(1.+x*x);
x += dx;
}

pi *= dx;

printf("PI = %16.14f Difference from math.h definition %16.14f \n",pi, pi-M_PI);
return 0;
}
54 changes: 54 additions & 0 deletions assignments/C-Day4/ex1/solution/mpiPI.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define LUMP 1

static long int numSteps = 1000000000;

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

int numP, procID;

// the usual mpi initialization
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numP);
MPI_Comm_rank(MPI_COMM_WORLD, &procID);

double *globalData=NULL;
double localData[LUMP];

// process 0 is only 1 that needs global data
if (procID == 0) {
globalData = (double *)malloc(LUMP * numP * sizeof(double) );
for (int i=0; i<LUMP*numP; i++)
globalData[i] = 0;
}
double myPart = 0;
double dx = 1./numSteps;
double x = dx*0.50;

for (int i=procID; i<numSteps; i+=numP) {
x = (i+.5)*dx;
myPart += 4./(1.+x*x);
}

myPart *= dx;

// localData[0] = myPart;

MPI_Gather(&myPart, LUMP, MPI_DOUBLE, globalData, LUMP, MPI_DOUBLE, 0, MPI_COMM_WORLD);

double pi = 0;
if (procID == 0) {
for (int i=0; i<numP; i++)
pi += globalData[i];
printf("PI = %16.14f Difference from math.h definition %16.14f \n",pi, pi-M_PI);
}


if (procID == 0)
free(globalData);

MPI_Finalize();
}
19 changes: 19 additions & 0 deletions assignments/C-Day4/ex1/submit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
#--------------------------------------------------------------------
# Generic SLURM script – MPI Hello World
#
# This script requests 1 node and 8 cores/node (out of total 64 avail)
# for a total of 1*8 = 8 MPI tasks.
#---------------------------------------------------------------------
#SBATCH -J myjob
#SBATCH -o myjob.%j.out
#SBATCH -e myjob.%j.err
#SBATCH -p development
#SBATCH -N 1
#SBATCH -n 8
#SBATCH -t 00:02:00
#SBATCH -A DesignSafe-SimCenter

ibrun ./pi


19 changes: 19 additions & 0 deletions assignments/C-Day4/ex1/submitPI.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
#--------------------------------------------------------------------
# Generic SLURM script – MPI Hello World
#
# This script requests 1 node and 8 cores/node (out of total 64 avail)
# for a total of 1*8 = 8 MPI tasks.
#---------------------------------------------------------------------
#SBATCH -J myjob
#SBATCH -o myjob.%j.out
#SBATCH -e myjob.%j.err
#SBATCH -p development
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 00:02:00
#SBATCH -A DesignSafe-SimCenter

ibrun ./pi


65 changes: 65 additions & 0 deletions assignments/C-Day4/ex2/scatterArray.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

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

int procID, numP;

double* globalVector = NULL;
double* localVector = NULL;

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &procID);
MPI_Comm_size(MPI_COMM_WORLD, &numP);

if (argc != 2) {
printf("Error correct usage: app vectorSize\n");
return 0;
}
int vectorSize = atoi(argv[1]);
int remainder = vectorSize % numP;

// Only the root process initializes the global array
if (procID == 0) {
globalVector = (double*)malloc(sizeof(double) * vectorSize);
srand(50);
for (int i = 0; i < vectorSize; i++) {
double random_number = 1.0 + (double)rand() / RAND_MAX;
globalVector[i] = random_number;
}
}

// Determine the size of the local array for each process
int localSize = vectorSize / numP;

// Allocate memory for the local array
localVector = (double*)malloc(sizeof(double) * localSize);

// Scatter the global array to all processes
MPI_Scatter(globalVector, localSize, MPI_DOUBLE,
localVector, localSize, MPI_DOUBLE,
0, MPI_COMM_WORLD);

// Print the local array for each process
printf("Process %d received: ", procID);
for (int i = 0; i < localSize; i++) {
printf("%.2f ", localVector[i]);
}
printf("\n");

// process0 has some stuff in the globalArray that was not sent!
if (procID == 0) {
printf("Process 0 Additional NOT SENT still in globalVector: ");
for (int i=numP*localSize; i<vectorSize; i++)
printf("%.2f ", globalVector[i]);
printf("\n");
}

// Clean up memory
free(globalVector);
free(localVector);

MPI_Finalize();
return 0;
}
Binary file modified docs/.doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/.doctrees/source/assignment_C4.doctree
Binary file not shown.
22 changes: 19 additions & 3 deletions docs/_sources/source/assignment_C4.rst.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
C: Assignments Day 4
====================

Problem 1: Parallelize **pi.c** using **MPI**
---------------------------------------------

Today we are going to parallelize the **pi.c** code you developed for day 1. to run at TACC you will need to use either **idev** or **sbatch**.

The figure below shows an method to compute **pi** by numerical integration. We would like you to implement that computation in a **C** program.
Expand Down Expand Up @@ -41,8 +44,6 @@ The solution `pi.c' is in the solitions/C-Day1/pi folder. The contents of that f

Our solution of **pi.c** as written as a loop dependency which may need to revise for tomorrows OpenMPI problem.

Problem 1: Parallelize **pi.c** 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:

Expand All @@ -56,7 +57,22 @@ The submit script is as shown below.
:linenos:


Problem 2: Bonus Parallelize your **matMul** solution using **MPI**
Problem 2: Compute the Norm of a vector using **MPI**
-----------------------------------------------------

Given what you just did with **pi** can you now write a program to compute the norm of avector. In the **ex2** directory I have placed a file **scatterArray.c**. This file will use MPI_Scatter to send components of the vector to the different processes in the running parallel application.


.. literalinclude:: ./assignments/c4/scatterArray.c
:language: c
:linenos:

.. note::

The vector size may not always be divisible by the number of processes. In such a case there will be additional terms not sent. Don;t forget to include them in the computation!


Problem 3: Bonus Parallelize your **matMul** solution using **MPI**
-------------------------------------------------------------------

If you want a more complicated problem to parallelize, I suggest parallelizing you matMul application from Day 2.
Expand Down
3 changes: 2 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ <h1>Welcome to the NHERI-SimCenter 2023 Programming Bootcamp<a class="headerlink
</li>
<li class="toctree-l2"><a class="reference internal" href="source/assignment_C4.html">C: Assignments Day 4</a><ul>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C4.html#problem-1-parallelize-pi-c-using-mpi">Problem 1: Parallelize <strong>pi.c</strong> using <strong>MPI</strong></a></li>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C4.html#problem-2-bonus-parallelize-your-matmul-solution-using-mpi">Problem 2: Bonus Parallelize your <strong>matMul</strong> solution using <strong>MPI</strong></a></li>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C4.html#problem-2-compute-the-norm-of-a-vector-using-mpi">Problem 2: Compute the Norm of a vector using <strong>MPI</strong></a></li>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C4.html#problem-3-bonus-parallelize-your-matmul-solution-using-mpi">Problem 3: Bonus Parallelize your <strong>matMul</strong> solution using <strong>MPI</strong></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="source/assignment_C5.html">C: Assignments Day 5</a><ul>
Expand Down
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

0 comments on commit a5299ad

Please sign in to comment.