Вы находитесь на странице: 1из 4

Parallel Programming Course

Lab 02 Introductory Lab


0. Familiarize yourself with the programming environment(s)
Sicstus Prolog 4

1. Problem 1 Nave prime number search

We want to find all the prime numbers in the natural interval [A, B], A < B, and we use the
following nave search algorithm: for each natural number X that belongs to the interval [A, B]
we verify if X is a prime number.
To verify the primality of X we use a brute force algorithm: we divide X sucessively to the
numbers 2, 3, 4,, floor(sqrt(X)) and verify that each remainder is different from zero.
We will also estimate the execution time of the program as a difference between the ending time
and the starting time of the program.
Optimizations:
only odd numbers from the [A, B] interval are tested
when testing the primality of X, only the odd potential divisors are tested

Programming paradigm: Logic Programming


Programming language: Prolog
Programming environment: Sicstus Prolog 4
Operating system: Windows XP
Target: Prolog virtual machine (WAM)
Predicate name/arity: prime/3
Predicate parameters: prime(A, B, P)
Validations: A < B
Computed data structure: a list P of prime numbers
Output: display the execution time in milliseconds.
Questions:
what are the Prolog predicates for computing the execution time?
how is your Prolog execution time compared to the C solution?
once your code works, compile the file using fcompile, the use load to load the
compiled code; execute the compiled version; had the execution time been improved?
2. Problem 2 Compute the entropy of a binary random sequence

We want to compute the entropy of a binary random sequence. The algorithm has several steps:
generate a large byte list S, using the standard PRNG of the Prolog language; large means
close to 1 GB. The length of the sequence (in bytes) is given as the argument N.
compute the number of 1 bits and the number of 0 bits of the sequence S in the
variables O and Z (notice the total number of bits is T = 8*N).
compute the bit entropy of the sequence as E = - (O/T)*log2(O/T) (Z/T)*log2(Z/T)
We will also estimate the execution time of the program as a difference between the ending time
and the starting time of the program.

Programming paradigm: Logic Programming


Programming language: Prolog
Programming environment: Sicstus Prolog 4
Operating system: Windows XP
Target: Prolog virtual machine (WAM)
Predicate name/arity: generate/2, compute/3, entropy/4
Predicate parameters: generate(N,S), compute(S,O,Z), entropy(O,Z,T,E)
Validations: N is a positive integer
Computed data structure: a floating point value E
Output: display the execution time in milliseconds.
Questions:
what are the standard Prolog predicates for using the PRNG?
what are the Prolog predicates for computing the execution time?
how do we compute log2(X)?
find an efficient algorithm for computing the number of 1 bits in the sequence!
3. Problem 3 Compute the value of PI ()
We will compute PI by estimating the area below the curve y = 4/(1+x2) between x=0 and x=1,
which gives the value for PI ().

This area can be aproximated using the following formula:

PI = (1/N) * sum (4 / (1 + ((i + 0.5)/N)2)), for i = 0, ..., N-1.

We will also estimate the execution time of the program as a difference between the ending time
and the starting time of the program.

Programming paradigm: Logic Programming


Programming language: Prolog
Programming environment: Sicstus Prolog 4
Operating system: Windows XP
Target: Prolog virtual machine (WAM)
Predicate name/arity: pi/2
Predicate parameters: pi(N, PI)
Validations: N is a positive integer
Computed data structure: a floating point value PI
Output: dislay the value of PI and the numer of iterations N; display the execution time in
milliseconds.
Questions:
what are the Prolog predicates for computing the execution time?
find an efficient algorithm for computing the sum PI.
what is the shortest method to compute PI in Prolog with maximum precision?
4. Problem 4 Compute the value of PI () using the Monte Carlo method

Monte Carlo methods can be thought of as methods that utilize sequences of random numbers to
perform a statistical simulation. The figure below shows a sector of a circle inscribed in a square.
The ratio between the areas of these two surfaces is:
R 2
Scircle sec tor M
= 42 =
S square R 4 N
where N is the total number of random points generated inside the square, and M is number of
points that fall inside the circle sector. Using the above formula, we obtain PI 4*M/N.

The algorithm is:


generate N points (pairs of random values X and Y in the real interval [0, 1])
count the M points that fall inside the circle sector
compute an approximation of PI as 4*M/N
compute the execution time

Programming paradigm: Logic Programming


Programming language: Prolog
Programming environment: Sicstus Prolog 4
Operating system: Windows XP
Target: Prolog virtual machine (WAM)
Predicate name/arity: montepi/2
Predicate parameters: montepi(N, PI)
Validations: N is a positive integer
Computed data structure: a floating point value PI
Output: dislay the value of PI and the number of iterations N; display the execution time in
milliseconds.
Questions:
what are the standard Prolog predicates for using the PRNG?
what are the Prolog predicates for computing the execution time?

Вам также может понравиться