AMSC 662 / CMSC 662 Fall 2013

Frequently Asked Questions for Homework 3

Some reminders and advice for Matlab programmers programming in C:

  • VERY IMPORTANT: C is fussy, and its error messages are cryptic, so if you are a novice:
  • Start with a working program, make a single change, check it, change again, etc.
  • Save your last working C file so that you can back up to it if necessary.
  • If gcc fails, it leaves its older output unchanged.
  • So, suppose you type
    gcc −g −Wall −fopenmp −o omp_run omp_hello.c
    and then
    omp_run
  • Then suppose you change omp_hello.c, type
    gcc −g −Wall −fopenmp −o omp_run omp_hello.c
    get an error message and then type
    omp_run
  • It will happily run the old version, the one before your change, and that can be very confusing.
  • So if you don't pay close attention to error messages, you can keep changing omp_hello.c and not change omp_run at all.
  • To avoid that confusion, you might want to always remove the file omp_run before calling gcc.
  • Use brackets { ... } to enclose statements in a "for" loop, a "while" loop, an "if" statement, a function, and statements governed by a "pragma".
  • Declare all variables and functions, indicating int, float, void, or char.
  • If you declare
    int A[10];
    then A is an array of integers with entries indexed as A[0], A[1], ... , A[9]
    (square brackets, not round, and indices counting from 0, not 1).
  • In print statements, the format string uses double quotes, not single, and you probably want to end the string with \n to end the line.
  • It is ok to break a statement into multiple lines. C does not require anything like Matlab's '...', except for Pragmas.
  • To make an entire line a comment, begin it with //
  • To make part of a line a comment, begin the comment with /* and end it with */
  • (09/26/13) In Matlab, "[A,b] = myfunct(x,y,Z)" might take variables x and y and array Z and compute array A and variable b. A copy of the variables x and y and a copy of all elements of Z are sent to myfunct, and myfunct might change the copy, but after return, the values of x, y, and Z in the calling program are unchanged.

    In C, the call to function myfunct must have 5 arguments: x, y, Z, A, and &b. The values x and y are sent to myfunct, and changes made in the copy disappear after return, just as in Matlab. But since Z is an array, the address of its first element of Z is sent to myfunct, so changes made in Z are permanent. Similarly, A can be changed, and the contents of b can be changed because myfunct has a pointer to its address.
  • Question: 09/29/13 How can I time the parallel code?

    Answer: Use the instructions in omp_timing.c

    10-05-13 Error discovered: The prototype for the function should say "int nthread" instead of "double* nthread. Sorry for the mistake.

    Answer:

    Question:

    Answer: