hw6s - COMPUTER SCIENCE 50100 Computing for Science and...

Info icon This preview shows pages 1–3. Sign up to view the full content.

COMPUTER SCIENCE 50100 Computing for Science and Engineering FALL 2014 Solution of ASSIGNMENT # 6 (35 points) November 19 Notes 1. OpenMP and threads. The scope of an OpenMP compiler directive applies only to code that physically follows the directive. In particular, it does not extend to functions that are called from that code. However, every invocation of a function creates a fresh set of local variables (in the stack part of memory) except for local variables that have been declared to be static . Such variables reside in the static part of memory and they retain their values between function calls. All invocations of a function share such variables. If any of them are read/write, the function is not thread-safe . (Nor would it be safe to make a function recursive if it employs read-write static variables.) The function rand in the standard C library is not thread-safe, because it maintains state using a variable in static storage. Using OpenMP, a call to rand should be in a critical section. This is not an issue with MPI, because every process has its own memory, and hence its own copy of rand . 2. Python upgrade on Scholar. To use Python 3 on the Scholar cluster, first enter module load anaconda/2.0.1-py34 To use Python 2, enter module load anaconda In both cases, use python or ipython for execution. Only the Python 2 has mpi4py installed. There are numerous other Pythons on Scholar, but they are missing some packages. Due Wednesday, December 3 at 11:30 am This assignment covers Sections 5.2–5.4 of the class notes. The programs that you are being asked to write are each a variation of the program that consists of file main0.c , given below and file dissoc t.c , given in the appendix. The latter defines a function dissoc t , which performs a single molecular dynamics simulation for a one-dimensional system of particles interacting with a Lennard-Jones potential. (It should be appreciated that such 1 D behavior is essentially unphysical.) The simulation is terminated when the system dissociates, and the elapsed time is returned. Also given in the appendix is a tester test0.py , which, as a check, prints the total energy at the beginning and end of the integration. It also produces a plot of the trajectory. The main program in main0.c given here computes the dissocation times for an ensemble of trajectories, writes the times to a file, and prints the average time. // main0.c #include <stdio.h> #include <stdlib.h> #include <time.h> double dissoc_t(int N, double T, int randval[N+N%2]); int main(void){ int N = 12; // number of particles double T = 38.; // temperature in K 1
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

int Nt = 500; // number of trials double t[Nt]; clock_t t0 = clock(); srand(1776); for (int k = 0; k < Nt; k++) { int randval[N+N%2]; for (int n = 0; n < N+N%2; n++) randval[n] = rand(); t[k] = dissoc_t(N, T, randval); } clock_t t1 = clock(); double time = (double)(t1 - t0)/CLOCKS_PER_SEC; printf("CPU time = %.2f\n", time); FILE *ofile = fopen("histogram.dat", "w"); for (int k = 0; k < Nt; k++) fprintf(ofile, "%g ", t[k]); fprintf(ofile, "\n"); fclose(ofile); } Below is a script that compiles and executes the program. It also produces a histogram from the data file.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '14
  • AlexPothen
  • Computer Science, CPU time, main(void){, retcode, srand

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern