# lab2 by dandanhuanghuang

VIEWS: 6 PAGES: 2

• pg 1
```									Uppsala University                                                                                        Programming of Parallel Computers
Department of Information Technology                                                                                             2004-11-16
Scientiﬁc Computing

Preparatory Computer Exercises 2
Compiling and running OpenMP programs on a parallel computer

Getting started

2. Three test programs are to be considered. The programs to test are, helloworld.c, pi.c, enumsort.cc,
and can be downloaded from the course web-page together with a Makeﬁle. Make a copy of all the ﬁles on the
web-page into some of your directories.

Test 1: Running with different number of threads
1. Try the program helloworld, which is the very ﬁrst step in using OpenMP. Compile by issuing the command
make helloworld
This will use the Makeﬁle to build an executable ﬁle named helloworld.
Run the program on one and more threads (processors) by setting the environment variable OMP_NUM_THREADS to
the number of threads you want to run with. For example, export OMP_NUM_THREADS=4 (for four threads), and
start running by giving the program name, helloworld, at the prompter.

2. Modify the program so that it reads in the number of threads from standard input (prompts for) and sets the number

Test 2: Numerical integration, an example of a parallel algorithm
The program pi computes       using numerical integration. A known way to compute                         is the following:
½
Ü        Ö
Ø Ò´Üµ℄½
¼
½ · Ü¾
¼

If we use the midpoint rule, we can compute the above integral as follows (see Figure 1):
½                        Ò                    Ò

Ü
¼
½ · Ü¾
½
½ · Ü¾
½
½ · ´´    ½   ¾µ µ¾

1
f(x)
1

h
0                        1       x

Figur 1: Numerical integration with the midpoint rule.

1. Parallelize the computations by inserting OpenMP directives in the code. Use PARALLEL DO with a reduction
clause. (Compare with the effort of doing this in MPI.)

Test 3: Enumeration sort
The Enumeration-sort algorithm
For each element (j) count the number of elements (i) that are smaller (   ´ µ      ´ µ).   This gives the rank of a(j), i.e.
anew(rank(j))=a(j).
In this formulation equal elements get the same rank. Study the code in enumsort.cc and parallelize the algorithm
with OpenMP. Sort 20000 elements and measure speedup for your parallelization.

Requirements to pass
After the lab hand in the following material or just discuss them with your teacher: