New version page

CORNELL CS 404 - Problem Set #2

Upgrade to remove ads
Upgrade to remove ads
Unformatted text preview:

CS 404: Problem Set 2Directions for SubmissionE-mail your answers to me at [email protected] The subject of your mes-sage should be “CS404 PS2,” and the body of the message should containyour answers. Some mailers can format messages using HTML or RTF.Please turn this feature off and send your message using plain text. If forsome reason you cannot send your message as text, you may attach youranswers as a text file.Essential Knowledge–Please give a brief answer (1-2sentences) for each1. Ocean currents are driven by very small differences in the height of thesea surface. Thus, one way to compute the velo city of a current is tomeasure the sea-surface height at two locations and take the difference.One ocean circulation model does exactly this: at each time step, itcomputes the change in sea-surface height due to a variety of processesand then computes the velocity field by taking the height differencebetween adjacent points. Values for mean sea-surface height (cheifly,the depth of the seafloor) vary from 100m on the continental shelf>4,000m in the deep sea, while the height difference between adjacentpoints is usually less than 0.1cm (0.001m). Based on your knowledge offloating point arithmetic, would the currents predicted by this modelbe more accurate in deep or shallow water. Please explain.2. Why is it easier to link a C program which calls FORTRAN routinesusing a FORTRAN compiler? If you wanted to do the linking with theC compiler, what would you need to do?3. Computer memory is one dimensional. How would the 2D array:1 2 3 410 20 30 40100 200 300 400be stored in the memory of a C (or C++ or Java) program. How wouldMatlab or FORTRAN store the array?1Calling C from FORTRAN (and maybe even some MAT-LAB...)Download the TAR file FpcaC.tar from the course website (the link is in theProblem Set section). Untarring this file will create the director FpcaC witha Makefile, data file (data.txt), several FORTRAN files (.f), and one C file(savemat.c). The FORTRAN code is identical to the code used for PS I,including a correct call to the BLAS routine SGEMV, except for a call tothe C subroutine SaveToMat. Presently, the call looks like:CALL SaveToMat(PC,TRIM(name2)//CHAR(0),TRIM(name3)//CHAR(0),m)Eventually, this call will save the prinicpal component vector PC in a MAT-LAB .mat file called PC.mat (name2). The FORTRAN function TRIM(string)removes trailing spaces from string. The code “//CHAR(0)” appends theASCII character 0 (the null character) to the string. This is needed whencalling C since C strings are terminated by the null character. The secondstring (name3=’PC’) says that upon loading PC.mat, a Matlab array PC willappear in your workspace containing the data. Presently, it is not possibleto call the MATLAB C Library in ACCEL–hopefully, this will be possibleon Wednesday. Our strategy will b e to get the subroutine call to work, thenwe will worry about MATLAB.Your task can be divided into two pieces: 1. Get the call to SaveToMatto work, and 2. Get SaveToMat to call MATLAB correctly. To make thefirst step easier, I’ve placed two versions of SaveToMat in savemat.c, onethat calls MATLAB, the other that just saves the data as a text file. Whichversion gets compiled is controlled using C-preprocessor directives (these arethe commands preceeded by “#”). Specifically, if you want MATLAB callsto work, you must define a variable caled CALLMATLAB by placing thecommand “#define CALLMATLAB” at the top of the file. If you don’twant MATLAB, you should un-define CALLMATLAB by commenting outthe line So, the first thing you should do is to undefine CALLMATLAB.Now that MATLAB is turned off, you need to get the call to SaveToMatto work. Here are some things you might need to fix:a. When the C-compiler creates savemat.o, it will give the SaveToMatroutine a name (probably, SaveToMat). Similarly, the FORTRANcompiler will replace “CALL SaveToMat” with a reference to somesubroutine. The FORTRAN function will assume that SaveToMat will2be named like a FORTRAN routine. You need to make sure that theC routine is named in the way FORTRAN expects. To find the correctname, compile the objects and try to link them. If your names arewrong, the linker will give you an error message telling you the namethe calling program is using. This message should help you figure outhow to rename SaveToMat (be sure to rename the prototype at the topof savemat.c).b. FORTRAN uses call-by-reference exclusively. Thus, to call a C routinefrom FORTRAN, we nee d to change any variables that are called byvalue (that are NOT pointers or arrays) to call by reference. This isactually an easy thing to do. If a variable X is called by value, youcan replace it with *X in the subroutine declaration, prototype, and allplaces in the code.To make the build easier, I’ve provided a Makefile. You should changethe F77, CC, and LIBPATH macros to the correct versions for your system(they are set for ACCEL).4. Get the call to SaveToMat to work without calling MATLAB. Describein detail the changes you made to main.f and savemat.c. If you areworking on a system other than ACCEL, please describe the systemand any differences between it and ACCEL. For example, “I built Fpcaon my Mac using an Absoft FORTRAN compiler and GNU C compiler.To get the call to work, I had to compile the FORTRAN code with the-f and -N15 flags.”5. If ACCEL gets its act together, I will provide directions on how to getthe MATLAB calls to

View Full Document
Download Problem Set #2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...

Join to view Problem Set #2 and access 3M+ class-specific study document.

We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Problem Set #2 2 2 and access 3M+ class-specific study document.


By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?