Skip to content

Commit

Permalink
fmk - adding day 3 presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
fmckenna committed Jun 7, 2023
1 parent 727dfb9 commit 8a79b15
Show file tree
Hide file tree
Showing 30 changed files with 229 additions and 128 deletions.
61 changes: 61 additions & 0 deletions assignments/C-Day3/ex1/file3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

// program to read values from a file, each file a csv list of int and two double
// written: fmk

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

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

if (argc != 3) {
fprintf(stdout, "ERROR correct usage appName inputFile outputBinaryFile\n");
return -1;
}

//
// read from ascii file
//
FILE *filePtr = fopen(argv[1],"r");

int i = 0;
float float1, float2;
int maxVectorSize = 100;
double *vector1 = (double *)malloc(maxVectorSize*sizeof(double));
double *vector2 = (double *)malloc(maxVectorSize*sizeof(double));
int vectorSize = 0;

while (fscanf(filePtr,"%d, %f, %f\n", &i, &float1, &float2) != EOF) {
vector1[vectorSize] = float1;
vector2[vectorSize] = float2;
printf("%d, %f, %f\n",i, vector2[i], vector1[i]);
vectorSize++;

if (vectorSize == maxVectorSize) {

// create new arrys & copy contents
double *newVector1 = (double *)malloc(2*vectorSize*sizeof(double));
double *newVector2 = (double *)malloc(2*vectorSize*sizeof(double));
for (int i=0; i<vectorSize; i++) {
newVector1[i]=vector1[i];
newVector2[i]=vector2[i];
}

// release old memory, set vectors to point to new ones and update max vector size
free(vector1);
free(vector2);
vector1 = newVector1;
vector2 = newVector2;
maxVectorSize *= 2;
}
}
fclose(filePtr);

//
// write to binary file
//

FILE *filePtrB = fopen(argv[2],"wb");
fwrite(vector1, sizeof(double), vectorSize, filePtrB);
fwrite(vector2, sizeof(double), vectorSize, filePtrB);
fclose(filePtrB);
}
Binary file removed docs/.doctrees/assignments.doctree
Binary file not shown.
Binary file modified docs/.doctrees/environment.pickle
Binary file not shown.
Binary file removed docs/.doctrees/git.doctree
Binary file not shown.
Binary file modified docs/.doctrees/source/assignment_C2.doctree
Binary file not shown.
Binary file modified docs/.doctrees/source/assignment_C4.doctree
Binary file not shown.
Binary file modified docs/.doctrees/source/assignment_C5.doctree
Binary file not shown.
Binary file modified docs/.doctrees/source/assignments.doctree
Binary file not shown.
Binary file removed docs/.doctrees/syllabus.doctree
Binary file not shown.
17 changes: 12 additions & 5 deletions docs/_sources/source/assignment_C2.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,16 @@ of the data file. For the next steps, run your program with the following parame



Problem 5: Reading From a binary file and Memory Allocation
-----------------------------------------------------------
Problem 5: Reading From a CSV file, Memory Allocation & Writing to Binary
-------------------------------------------------------------------------


Reading of data from files and placing them into containers such as Vectors is easy if you know the size of the data you are reading. If this is unknown the problem becomes more tricky. The solution presented on slide 22 worked for a small number of inputs, but failed with a segmentation fault for larger problems. You are to fix the problem. A copy of the offending file **file3.c** has been placed in the directory ex2-5 along with two files. The program can handle the first **small.txt**, it will fail with the second **big.txt**. Can you make the program work. The solution will test your understanding of file I/O, memory management and pointers.
Reading of data from files and placing them into containers such as Vectors is easy if you know the size of the data you are reading. If this is unknown the problem becomes more tricky. The solution presented on slide 22 worked for a small number of inputs, but failed with a segmentation fault for larger problems. You are to fix the problem. A copy of the offending file **file3.c** has been placed in the directory binaryFile along with two files. The program can handle the first **small.txt**, it will fail with the second **big.txt**. Can you make the program work. The solution will test your understanding of file I/O, memory management and pointers.

Initial code is provided in the **/assignments/C-Day2/binaryFile** directory.

At end of the program, you are asked to modify the code so that the results of the two vectors are ouput to a binary file. Output the contents of **vector1** followed by **vector2**.

The **file3.c** is as shown below. You need to put some code to replace comment at the line 41.

.. literalinclude:: ./assignments/c2/file3.c
Expand All @@ -269,11 +271,16 @@ The **small.txt** file is as shown below.

.. note::

No cmake or Makefile has been provided. You can compile the file with icc or whatever compiler you are using. The program takes a single input, the file to read. To compile and test the program, issue the following at the terminal prompt.
No cmake or Makefile has been provided. You can compile the file with icc or whatever compiler you are using. The program takes two inputs, the file to read and the file to write. To compile and test the program, issue the following at the terminal prompt. When done compare the file sizes of the binary file to the text file.


.. code::
icc file3.c -o file3
./file2 small.txt
./file2 big.txt
./file2 big.txt
.. note::

Give some thought as to how you would open the file and read back in the two vectors. If you have some time, write a program to do and have that program write the contents of the binary files to a csv file.
19 changes: 8 additions & 11 deletions docs/_sources/source/assignment_C4.rst.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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.
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 All @@ -12,7 +12,7 @@ The figure below shows an method to compute **pi** by numerical integration. We
Computation of pi numerically


The solution `pi.c <https://github.com/NHERI-SimCenter/SimCenterBootcamp2022/tree/master/code/c/ExerciseDay1/assignments/pi.c>`_ can be found on github. The contents of that file is presented here:
The solution `pi.c' is in the solitions/C-Day1/pi folder. The contents of that file is presented here:

.. literalinclude:: ./assignments/c4/pi.c
:language: c
Expand All @@ -39,9 +39,9 @@ The solution `pi.c <https://github.com/NHERI-SimCenter/SimCenterBootcamp2022/tr

.. warning::

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

Problem 1: Parallelize using **MPI**
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,12 +56,9 @@ The submit script is as shown below.
:linenos:


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:

.. literalinclude:: ./solutions/c4/submitPI.sh
:linenos:
Problem 2: 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.


12 changes: 12 additions & 0 deletions docs/_sources/source/assignment_C5.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
C: Assignments Day 5
====================

Problem 1: 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:

.. literalinclude:: ./solutions/c4/submitPI.sh
:linenos:


Problem 2: Additional Exercise JUST for Frank
---------------------------------------------

The purpose of these exercises is to set it up so that you are able to run a parallel application on Frontera or Stampede2 by issuing commands in the terminal of your desktop using your DesignSafe account and resources. Time permitting we will share the applications with fellow classmates. The advantage of being able to do this from a terminal is convenience and speed, e.g. you no longer will be required to login and find a token, cd to appropriate directories, edit submit scripts, and so on. Ultimately, as you progress in your careers you will begin to understand, the ability to share your work is one of the really great advantages provided.

There are **5** steps to the exercise today. The steps follow the videos presented for today's class (these are enclosed in hints herein). The exercises are outlined below the **hint**.
Expand Down
4 changes: 2 additions & 2 deletions docs/_sources/source/assignments.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Each assignment typically includes one or more of the following.
assignment_C2
# assignment_C2_solution
assignment_C3
# assignment_C4
assignment_C4_solution
assignment_C4
# assignment_C4_solution
assignment_C5
assignment_day1
assignment_day2
Expand Down
12 changes: 8 additions & 4 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ <h1>Welcome to the NHERI-SimCenter 2023 Programming Bootcamp<a class="headerlink
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C2.html#problem-2-using-structures">Problem 2: Using structures</a></li>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C2.html#problem-3-writing-data-for-use-by-other-programs-csv">Problem 3: Writing data for use by other programs: CSV</a></li>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C2.html#problem-4-writing-to-a-binary-file">Problem 4: Writing to a binary file</a></li>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C2.html#problem-5-reading-from-a-binary-file-and-memory-allocation">Problem 5: Reading From a binary file and Memory Allocation</a></li>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C2.html#problem-5-reading-from-a-csv-file-memory-allocation-writing-to-binary">Problem 5: Reading From a CSV file, Memory Allocation &amp; Writing to Binary</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="source/assignment_C3.html">C: Assignments Day 3</a><ul>
Expand All @@ -277,12 +277,14 @@ <h1>Welcome to the NHERI-SimCenter 2023 Programming Bootcamp<a class="headerlink
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C3.html#problem-3-an-engineering-vector-class-requires-you-to-finish-off">Problem 3: An engineering Vector Class requires you to finish off</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="source/assignment_C4_solution.html">C: Assignments Day 4</a><ul>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C4_solution.html#problem-1-parallelize-using-mpi">Problem 1: Parallelize using <strong>MPI</strong></a></li>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C4_solution.html#problem-2-parallelize-using-openmp">Problem 2: Parallelize using OpenMP</a></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>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="source/assignment_C5.html">C: Assignments Day 5</a><ul>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C5.html#problem-1-parallelize-using-openmp">Problem 1: Parallelize using OpenMP</a></li>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C5.html#problem-2-additional-exercise-just-for-frank">Problem 2: Additional Exercise JUST for Frank</a></li>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C5.html#step-1-setting-up-an-execution-system">Step 1: Setting Up an Execution System</a></li>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C5.html#step-2-exploring-file-system-commands">Step 2: Exploring File System Commands</a></li>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C5.html#step-3-build-a-tapis-app">Step 3: Build a Tapis app</a></li>
Expand Down Expand Up @@ -345,6 +347,8 @@ <h1>Welcome to the NHERI-SimCenter 2023 Programming Bootcamp<a class="headerlink
</li>
<li class="toctree-l1"><a class="reference internal" href="source/tapis.html">Tapis CLI</a><ul>
<li class="toctree-l2"><a class="reference internal" href="source/assignment_C5.html">C: Assignments Day 5</a><ul>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C5.html#problem-1-parallelize-using-openmp">Problem 1: Parallelize using OpenMP</a></li>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C5.html#problem-2-additional-exercise-just-for-frank">Problem 2: Additional Exercise JUST for Frank</a></li>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C5.html#step-1-setting-up-an-execution-system">Step 1: Setting Up an Execution System</a></li>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C5.html#step-2-exploring-file-system-commands">Step 2: Exploring File System Commands</a></li>
<li class="toctree-l3"><a class="reference internal" href="source/assignment_C5.html#step-3-build-a-tapis-app">Step 3: Build a Tapis app</a></li>
Expand Down
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/source/assignment_C1.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
</li>
<li class="toctree-l2"><a class="reference internal" href="assignment_C2.html">C: Assignments Day 2</a></li>
<li class="toctree-l2"><a class="reference internal" href="assignment_C3.html">C: Assignments Day 3</a></li>
<li class="toctree-l2"><a class="reference internal" href="assignment_C4_solution.html">C: Assignments Day 4</a></li>
<li class="toctree-l2"><a class="reference internal" href="assignment_C4.html">C: Assignments Day 4</a></li>
<li class="toctree-l2"><a class="reference internal" href="assignment_C5.html">C: Assignments Day 5</a></li>
<li class="toctree-l2"><a class="reference internal" href="assignment_day1.html">Python: Assignments Day 1</a></li>
<li class="toctree-l2"><a class="reference internal" href="assignment_day2.html">Python: Assignments Day 2</a></li>
Expand Down

0 comments on commit 8a79b15

Please sign in to comment.