18. What Happens when we build code?#

Important

Since I’ve been sick, this is not fully filled in yet. Use a previous semester’s equivalent sessions as a reference for more detail for now. Create a PR that inserts an explanation of a few steps for a community badge.

18.1. Using SSH Keys#

To use it we use he -i option and then the path to the private key file

ssh -i ~/seawulf brownsarahm@seawulf.uri.edu
Last login: Tue Apr  4 11:53:06 2023 from 172.20.207.131

For today, we will use an interactive session

[brownsarahm@seawulf ~]$ interactive
salloc: Granted job allocation 26525
salloc: Waiting for resource configuration
salloc: Nodes n005 are ready for job

We will make an empty directory to work in for today.

[brownsarahm@n005 ~]$ mkdir compilec
[brownsarahm@n005 ~]$ cd compilec/
[brownsarahm@n005 compilec]$ ls

18.2. A simple program#

[brownsarahm@n005 compilec]$ nano hello.c
#include <stdio.h>
void main () {

 printf("Hello world\n");

}

We will see this is the only file in the folder

[brownsarahm@n005 compilec]$ ls
hello.c
[brownsarahm@n005 compilec]$ gcc -E hello.c -o hello.i
[brownsarahm@n005 compilec]$ ls
hello.c  hello.i
[brownsarahm@n005 compilec]$ cat hello.i | head 
# 1 "hello.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "hello.c"
# 1 "/usr/include/stdio.h" 1 3 4
# 27 "/usr/include/stdio.h" 3 4
# 1 "/usr/include/features.h" 1 3 4
# 375 "/usr/include/features.h" 3 4
[brownsarahm@n005 compilec]$ cat hello.c
#include <stdio.h>
void main () {

 printf("Hello world\n");

}
[brownsarahm@n005 compilec]$ cat hello.i | tail 
extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
# 943 "/usr/include/stdio.h" 3 4

# 2 "hello.c" 2
void main () {

 printf("Hello world\n");

}
[brownsarahm@n005 compilec]$ cat hello.i | wc -l
842
[brownsarahm@n005 compilec]$ cat hello.c | wc -l
6
[brownsarahm@n005 compilec]$ gcc -S hello.i
[brownsarahm@n005 compilec]$ ls
hello.c  hello.i  hello.s
[brownsarahm@n005 compilec]$ cat hello.s
	.file	"hello.c"
	.section	.rodata
.LC0:
	.string	"Hello world"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	$.LC0, %edi
	call	puts
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-44)"
	.section	.note.GNU-stack,"",@progbits
[brownsarahm@n005 compilec]$ cat hello.c
#include <stdio.h>
void main () {

 printf("Hello world\n");

}
[brownsarahm@n005 compilec]$ gcc -c hello.s -o hello.o
[brownsarahm@n005 compilec]$ ls
hello.c  hello.i  hello.o  hello.s
[brownsarahm@n005 compilec]$ cat hello.o
ELF>?@@
UH???]?Hello worldGCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-44)zRx
K                                                               A?C
??      hello.cmainputs


???????? .symtab.strtab.shstrtab.rela.text.data.bss.rodata.comment.note.GNU-stack.rela.eh_frame @?0
&PP1P
     90\.B?W?R@
?
	?0a[brownsarahm@n005 compilec]$ 
[brownsarahm@n005 compilec]$ gcc -o hello hello.o -lm 
[brownsarahm@n005 compilec]$ ls
hello  hello.c  hello.i  hello.o  hello.s
[brownsarahm@n005 compilec]$ ./hello 
Hello world
[brownsarahm@n005 compilec]$ ls -la
total 48
drwxr-xr-x 2 brownsarahm spring2022-csc392    98 Apr  4 13:29 .
drwx------ 7 brownsarahm spring2022-csc392  4096 Apr  4 13:01 ..
-rwxr-xr-x 1 brownsarahm spring2022-csc392  8360 Apr  4 13:29 hello
-rw-r--r-- 1 brownsarahm spring2022-csc392    64 Apr  4 13:03 hello.c
-rw-r--r-- 1 brownsarahm spring2022-csc392 16865 Apr  4 13:07 hello.i
-rw-r--r-- 1 brownsarahm spring2022-csc392  1496 Apr  4 13:24 hello.o
-rw-r--r-- 1 brownsarahm spring2022-csc392   433 Apr  4 13:14 hello.s
[brownsarahm@n005 compilec]$ ls
hello  hello.c  hello.i  hello.o  hello.s
[brownsarahm@n005 compilec]$ rm hello.i hello.s hello.o hello
[brownsarahm@n005 compilec]$ ls
hello.c
[brownsarahm@n005 compilec]$ gcc --help
Usage: gcc [options] file...
Options:
  -pass-exit-codes         Exit with highest error code from a phase
  --help                   Display this information
  --target-help            Display target specific command line options
  --help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...]
                           Display specific types of command line options
  (Use '-v --help' to display command line options of sub-processes)
  --version                Display compiler version information
  -dumpspecs               Display all of the built in spec strings
  -dumpversion             Display the version of the compiler
  -dumpmachine             Display the compiler's target processor
  -print-search-dirs       Display the directories in the compiler's search path
  -print-libgcc-file-name  Display the name of the compiler's companion library
  -print-file-name=<lib>   Display the full path to library <lib>
  -print-prog-name=<prog>  Display the full path to compiler component <prog>
  -print-multiarch         Display the target's normalized GNU triplet, used as
                           a component in the library path
  -print-multi-directory   Display the root directory for versions of libgcc
  -print-multi-lib         Display the mapping between command line options and
                           multiple library search directories
  -print-multi-os-directory Display the relative path to OS libraries
  -print-sysroot           Display the target libraries directory
  -print-sysroot-headers-suffix Display the sysroot suffix used to find headers
  -Wa,<options>            Pass comma-separated <options> on to the assembler
  -Wp,<options>            Pass comma-separated <options> on to the preprocessor
  -Wl,<options>            Pass comma-separated <options> on to the linker
  -Xassembler <arg>        Pass <arg> on to the assembler
  -Xpreprocessor <arg>     Pass <arg> on to the preprocessor
  -Xlinker <arg>           Pass <arg> on to the linker
  -save-temps              Do not delete intermediate files
  -save-temps=<arg>        Do not delete intermediate files
  -no-canonical-prefixes   Do not canonicalize paths when building relative
                           prefixes to other gcc components
  -pipe                    Use pipes rather than intermediate files
  -time                    Time the execution of each subprocess
  -specs=<file>            Override built-in specs with the contents of <file>
  -std=<standard>          Assume that the input sources are for <standard>
  --sysroot=<directory>    Use <directory> as the root directory for headers
                           and libraries
  -B <directory>           Add <directory> to the compiler's search paths
  -v                       Display the programs invoked by the compiler
  -###                     Like -v but options quoted and commands not executed
  -E                       Preprocess only; do not compile, assemble or link
  -S                       Compile only; do not assemble or link
  -c                       Compile and assemble, but do not link
  -o <file>                Place the output into <file>
  -pie                     Create a position independent executable
  -shared                  Create a shared library
  -x <language>            Specify the language of the following input files
                           Permissible languages include: c c++ assembler none
                           'none' means revert to the default behavior of
                           guessing the language based on the file's extension

Options starting with -g, -f, -m, -O, -W, or --param are automatically
 passed on to the various sub-processes invoked by gcc.  In order to pass
 other options on to these processes the -W<letter> options must be used.

For bug reporting instructions, please see:
<http://bugzilla.redhat.com/bugzilla>.
[brownsarahm@n005 compilec]$ gcc -Wall -g -o hello hello.c -lm
hello.c:2:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main () {
      ^
[brownsarahm@n005 compilec]$ ls
hello  hello.c

18.3. Working with Multiple Files#

[brownsarahm@n005 compilec]$ nano main.c
[brownsarahm@n005 compilec]$ nano help.c
/* Used to illustrate separate compilation.
Created: Joe Zachary, October 22, 1992
Modified:
*/

#include <stdio.h>

void main () {
 int n;
 printf("Please enter a small positive integer: ");
 scanf("%d", &n);
 printf("The sum of the first n integers is %d\n", sum(n));
 printf("The product of the first n integers is %d\n", product(n));
}

and help.c

/* Used to illustrate separate compilation

Created: Joe Zachary, October 22, 1992
Modified:

*/

/* Requires that "n" be positive. Returns the sum of the
  first "n" integers. */

int sum (int n) {
 int i;
 int total = 0;
 for (i = 1; i <= n; i++)
  total += i;
 return(total);
}


/* Requires that "n" be positive. Returns the product of the
  first "n" integers. */

int product (int n) {
 int i;
 int total = 1;
 for (i = 1; i <= n; i++)
  total *= i;
 return(total);
}
[brownsarahm@n005 compilec]$ gcc -Wall -g -c main.c 
main.c:14:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main () {
      ^
main.c: In function ‘main’:
main.c:18:2: warning: implicit declaration of function ‘sum’ [-Wimplicit-function-declaration]
  printf("The sum of the first n integers is %d\n", sum(n));
  ^
main.c:19:2: warning: implicit declaration of function ‘product’ [-Wimplicit-function-declaration]
  printf("The product of the first n integers is %d\n", product(n));
  ^
[brownsarahm@n005 compilec]$ nano main.c
[brownsarahm@n005 compilec]$ cat main.c
/* Used to illustrate separate compilation.
Created: Joe Zachary, October 22, 1992
Modified:
*/

#include <stdio.h>
int sum(int n);
int product(int n);


void main () {
 int n;
 printf("Please enter a small positive integer: ");
 scanf("%d", &n);
 printf("The sum of the first n integers is %d\n", sum(n));
 printf("The product of the first n integers is %d\n", product(n));
}
[brownsarahm@n005 compilec]$ gcc -Wall -g -c main.c 
main.c:15:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main () {
      ^
[brownsarahm@n005 compilec]$ gcc -Wall -g -c help.c
[brownsarahm@n005 compilec]$ ls
hello  hello.c  help.c  help.o  main.c  main.o
[brownsarahm@n005 compilec]$ gcc -o demo main.o help.o -lm
[brownsarahm@n005 compilec]$ ls
demo  hello  hello.c  help.c  help.o  main.c  main.o
[brownsarahm@n005 compilec]$ ./demo 
Please enter a small positive integer: 3
The sum of the first n integers is 6
The product of the first n integers is 6
[brownsarahm@n005 compilec]$ ./demo 6
Please enter a small positive integer: 6
The sum of the first n integers is 21
The product of the first n integers is 720
[brownsarahm@n005 compilec]$ exit
logout
srun: error: n005: task 0: Exited with exit code 43
salloc: Relinquishing job allocation 26525
[brownsarahm@seawulf ~]$ exit
logout
Connection to seawulf.uri.edu closed.

Important

To make up for missing the badges on Thursday last week, today’s badges are integrative-3. Each will count as 3.

18.4. Review today’s class#

note: this is integrative3

  1. Update your KWL Chart based on what you have learned.

  2. Contribute to your group repo and review a team mate’s PR.

  3. Install gcc locally and practice using it. Repeat steps we did in class on your computer and then change the order of parameters; try skipping steps to produce errors, etc. Export the list of variations you tried and summarize what you learned as a list of tips and reminders on what the parameters do/why/when you would need them (or not) in gcctips.md. (to reinforce what we learned)

  4. Create some variations of the hello.c we made in class. Make hello2.c print twice with 2 print commands. Make hello5.c print 5 times with a for loop and hello7.c print 7 times with a for loop. Build them all on the command line and make sure they run correctly.

  5. Write a bash script, assembly.sh to compile each program to assembly and print the number of lines in each file.

  6. Put the output of your script in hello_assembly_compare.md. Add to the file some notes on how they are similar or different based on your own reading of them.

  7. Read about sbatch. Answer the following in hpc.md of your KWL repo: (to think about how the design of the system we used in class impacts programming and connect it to other ideas taught in CS)

    1. What kinds of things would your code need to do if you were going to run it on an HPC system?
    2. What Sbatch options seem the most helpful?
    3. How might you go about setting the time limits for a script? How could you estimate how long it will take?
    

18.5. Prepare for Next Class#

  1. install this hardware simulator

  2. Create operators.md and make some notes about what you know about operators. What kinds of operators are you familiar with? Which have you seen in programming? math?

18.6. More Practice#

note: this is integrative3

  1. Update more rows on your KWL Chart based on what we did today.

  2. Contribute to your group repo and review a team mate’s PR

  3. Install gcc locally and practice using it. Repeat steps we did in class on your computer and then change the order of parameters; try skipping steps to produce errors, etc. Export the list of variations you tried and summarize what you learned as a list of tips and reminders on what the parameters do/why/when you would need them (or not) in {index} gcctips.md. (to reinforce what we learned)

  4. Write two short programs that do the same thing in different ways and compile them both to assembly (eg using a for vs while loop to sum numbers up to a number). Check the assembly to see if they produce the same thing or if it’s different. Save your code (in code blocks) and notes about your findings in assemblycompare.md

  5. On Seawulf, modfiy main.c from class to accept the integer as a command line argument instead of via input while running the program. See this tutorial for an example.

  6. Write a bash script that runs your compiled program for each integer from 10 to 30 (syntax for a range is {start..end} so this would be {10..30})

  7. Create an sbatch script to run your script on a compute node and save the output to a file. The sbatch script should compile and link the program and then call the script. see the options

  8. use scp to download your modified main, script files, and output to your local computer and include them in your kwl repo.

  9. Answer the following in hpc.md of your KWL repo: (to think about how the design of the system we used in class impacts programming and connect it to other ideas taught in CS)

    1. What kinds of things would your code need to do if you were going to run it on an HPC system?
    2. What Sbatch options seem the most helpful?
    3. How might you go about setting the time limits for a script? How could you estimate how long it will take?
    

18.7. Experience Report Evidence#

use scp (see badge for instructions) and download the files created in class.

18.8. Questions After Today’s Class#

Post via PR

Submit a PR to the course website with your question as a level 3 heading for one community badge and with an attempted answer (I’ll check it before merging it) for two community badges