MTH 655 LAB 3

This lab spans two weeks. You learn how to operate in a terminal-based computing environment based on Unix (Linux), and how to use a compilable programming environment (in contrast, MATLAB is an interpretive environemnt). Most will actually use Cygwin and Cygwin-X which emulate Unix and X-Windows on Windows machines. This is useful to us because while learning Unix you can for the moment use your favorite Windows editor and file manager ... to see the correspondance between what is happening simultaneously under Unix and Windows. Also, you will learn how to use a remote Unix computing platform.

You will use Cygwin to implement your first programs in Fortran or/and in C in a Unix environment.
Implement = write Fortran/C code + compile it + run it.
Rules:
  • This assignment will vary depending on your initial skill level (whether you know Unix, Fortran, C etc.). I ask that you skip the parts that are easy for you and move on to steps/projects that are new for you. (If all steps are easy, then go straight to the last Projects, and I can also suggest extras).
  • You can use either Fortran, or C, or both. The choices may affect what you will have to learn later.

    Our MPI examples will be in Fortran and/or in C so this class is preparing you for this.

  • Turn in solutions to at least one of projects 2-4, and to 6.
  • If needed, the labs page has links to reference information.
  • Ask for help if you get stuck.

  • Get familiar with Unix environment
    • Click on the icon Cygwin on your Desktop to start Cygwin.
    • Where are you ... ? ... move within your file system (and home directory)
      pwd
      mkdir lab3
      ls
      cd lab3
      pwd
      go back to where you started cd ..
      pwd
      ls
      Now move to the Desktop.
    • Learn how to create a text file, see its contents, and copy and move files: Explore (use man pages) commands cat, cp, mv, rm, rmdir
      cat > myfile.txt
      type anything you want ... and finish with Ctrl-D
      cat myfile.txt
    • Now copy a few files, move between folders ...
    • Now find out who you are and on which system you really are...
      whoami
      uname

  • Project 1. Run a baby example code written in Fortran or in C. The program computes the norm inefficiently. Please correct. Make it compute also the L1 norm of your birthday as well as the product of the random numbers.
    • download the file code to Desktop and place it in your cygwin lab3 folder (the one you created)
    • compile the code (and link) (NOTE: the program has intentional mistakes which you have to correct)
      g77 hellof.f
    • run: (the program has now a default name: a.exe, on Unix it gets the name a.out)
      ./a.exe
    • compile and link and give it a name
      g77 -o hellof.exe hellof.f
    • run:
      ./hellof.exe

    Helpful hints
    • memory allocation in Fortran 77 is static
    • Fortran code MUST start in column 7
    • put a character in the first column for lines with comments
    • you can also put comments after a ! in the middle of the line
    • use IMPLICIT NONE: this has saved many from mismatched type death
    • This is an useful when comuting Linfty norm
      if (mymax.lt.mynumber) then
      mymax = mynumber
      endif
    • download the file code to Desktop and place it in your cygwin lab3 folder (the one you created)
    • compile the code (and link) (NOTE: the program has intentional mistakes which you have to correct)
      gcc helloc.c
    • run: (the program has now a default name: a.exe, on Unix it gets the name a.out)
      ./a.exe
    • compile and link and give it a name
      gcc -o helloc.exe helloc.c
    • run:
      ./helloc.exe


    Helpful hints
    • Memory allocation is dynamic
    • C code can start in any column
    • You can use // and /* my comment */ for comments
    • you have to declare/allocate each variable
    • this can be useful for Linfty norm
      if (mymax < mynumber ) {
      mymax = mynumber;
      }
  • Project 2. Rewrite your code from LAB1 to compute pi in Fortran. Use single and double precision. Compare results with MATLAB (always uses double precision).
    Helpful hints
    • use single (real) precision variables for a(i)
    • when setting up vector elements, check that you are getting what you expect to get ! (print values)
    • when setting up your vector you may want to use lines
      sumx = 2*i - 1
      a(i) = 1/sumx
      (ask me why)
    • push the computer to allocate a REALLY long vector
    • check when the sum stops changing
    • repeat for double precision
    • what was the largest vector you could allocate ?

  • Project 3. Do Project 2 in C.
  • Project 4. Write Fortran or C code which does the same job as newton.m from LAB1.
    Helpful hints
    • Structure of program c(sh)ould be
      program mynewton
      implicit none
      real .... ! here go your variables and parameters
      ! read tolerance
      ....
      do while (error.gt.tolerance) !
      ...
      enddo
      ....
      end ! end of program
      real function myf(x)
      implicit none
      real x
      myf = ....
      real function mydf(x)
      implicit none
      real x
      mydf = ....
      end

    Verify that your code runs as smoothly as the MATLAB routine. Compare the results. Discuss the difference between single and double precision. EXPLAIN.


  • Project 5. In this project you will work on a remote Unix server app.science.oregonstate.edu and you will learn how to use state-of-the-art linear solver libraries ... because there is no sense to redevelop the wheel.

    Steps:
    • Log in to the (application) Linux server using ssh. Have your SCIENCE account name and password ready .
      • I suggest you use Start X Win icon to start X Windows under CYGWIN. Open a few terminal windows so you will have something to work with. For example,
        type xterm -bg red -fg black &
        type xterm -fg yellow -bg black &
        so you will have an additional "black" and "red" windows.
      • in the black terminal, type ssh -Y you@app.science.oregonstate.edu and input password
      • type echo $SHELL and if you do not see tcsh as the answer, start tcsh in each window.
      • move around in your home directory ... make folder lab3 .. or something like that.
    • On app server, you will compile and link a FORTRAN program. Take a look at what it does.. You will also need the "other" module. Plan how you will copy all the files to the application server - use scp.
      in the red terminal ... scp myfile you@app.science.oregonstate.edu:
    • The code uses a call to a separate module and to a library.

      This library is the LAPACK library; you may also want to read about BLAS. Both come from NETLIB and are installed on every "real" computing platform.

      Take a look ! You have to compile both modules and then link them together. Here is how
      f77 -o lab3.o -c lab3.f
      f77 -o lab3module.o -c lab3module.f
      f77 -o lab3 lab3.o lab3module.o -llapack
    • Now that you finish building the binary, run the code
      lab3
      Does it do what you thought it should ?

      To compile, you can also use a Makefile . Type make and things will work on their own. Or will they ?


  • Project 6:

    Find a better linear solver routine than DGESV (more suitable for the particular matrix that arises in our problem). Go to the description of DGESV.F and see in the index of solvers if you see one better suited for this example. Implement it, test it (for large N) and compare with DGESV.