Homework Assignment 2 (due Thurs Sept. 18)
The goal of this assignment is to give you practice writing and compiling a sequential C program using a simple text editor and a Terminal command window. The directions are written for a Mac OS X, but only minimal (if any) changes will be needed for Cygwin on Windows or a terminal on a *nix box.
Open a Terminal window and change to your favorite directory (.e.g. your home directory) (hint:
cd ~). Create a cs336 directory (hint:
mkdir cs336). Change directories to
cd cs336), and create a
hw2 directory. Then, create directories for code and executables (hint:
mkdir src for the source code and
mkdir bin for the executables). Now you have a directory structure set up so that your code is separated from your executables.
Problem 0: Put hello.c and Makefile into your
src directory. Compile hello using the line
make hello. Run the program by typing
../bin/hello. If it doesn't print out "Hello, World", then something is terribly wrong. Come see me!
Problem 1: Download prob1.c to your
hw2 directory. This is similar to the last example we had in class on Thursday. In particular, it attempts to print out one element past the end of the array. Compile it and run it. Record the output. The last number printed on each line is unreliable because it is pointing to memory that has not been allocated to it. Tell me what output you see and what you think caused it (i.e. Why is the last value of the first line a 5? etc.). WARNING: Even if your conjecture is correct, you cannot count on the same thing happening every time/place you run it!
Problem 2: Create a file
prob2.c in your
hw2 directory that creates two integer variables, prints them out, swaps their values, and prints them out again. To do this you will need to write a swap routine that takes two pointers to integers. When calling the routine, the address of the integer variables should be sent so that the contents of the variables themselves can be changed, e.g. if
b are declared to be
ints, then the call should be
swap(&a,&b). There are numerous solutions to this problem on the web, but you won't learn anything by simply copying them. I want you to write it yourself and experiment with bad implementations. Turn in your code and an explanation of an alternate set of code (i.e. a code snippet) and why that alternate code will not work.
Problem 3: Chapter 2 of the textbook provides a nice explanation of the diverse configurations of parallel computers. Read the section titled "A Look at Six Parallel Computers" (pp. 31-44) and write a few sentences telling me about the computer that most grabbed your attention. Search the web for an example of that computer being used in research or industry and describe it. To get you started, check out the San Diego Supercomputer Center or the list of the top 500 fastest computers.
What to Turn inPlease put all of the homework problems into a single document (txt, doc, docx, pdf). You can turn in either a hard or electronic copy (if it is electronic, just email it to me).
- I suggest you get started on Problems 0 and 1 right away so that I can help you during the day on Friday and you don't get stuck unable to work over the weekend.
- Please write clearly and without grammatical errors or spelling mistakes.
- Code should be well commented. If you turn in non-working but well-commented code, I will be able to help you figure out what your problem is and your grade will suffer less both in the short term and the long term (because you will be better able to learn from your mistakes). It is better to think through a problem and share with me your process than to give up and turn in something that doesn't reflect the amount of time and effort you have put into the homework.
- This assignment is due Thursday because I know some of you have had a hard time getting the textbook. Do your best to have your programming assignments done by Tuesday because I may assign more short programs then.
- Bruce has written helpful tutorials on the Terminal and Makefiles. Check them out.
- Be careful if you mount a network directory on your Mac. If you use your Mac Terminal in a mounted directory then you are still "on" your Mac. Specifically, if you compile code, the binaries will execute on your Mac. If you want to create executables for a different machine (.e.g. one of the dwarves or a node on the cluster), then you must log into that machine using
sshand then navigate the appropriate directory.
- Separating code from executables makes it simpler to compile and run code on multiple machines. Everything in the
srcdirectory is portable from machine to machine, but the executables in the
bindirectory are specific to that machine. For machines on the network that share a file system but not an architecture, this may mean you must keep track of which machine you last compiled on. If your code doesn't run, then recompile it.