Documente Academic
Documente Profesional
Documente Cultură
Comments
Code
---------------------------------------
1. Problem:
The goal of the assignment was to implement a parallel, binary reduction on a list
barrier.
2. Approach:
In order to complete this assignment, three C++ files were written: Barrier.h,
Barrier.cpp, and max.cpp. The idea was to implement the Barrier.cpp (whose
constructors and variables where defined in Barrier.h) into max.cpp. Within the
Wait- This function is used to synchronize threads at the point the barrier
is specified. The calling thread blocks until the required number of threads
then upon return from the signal handler, the thread resumes waiting at the
barrier if the required number of threads have not arrived at the barrier.
There are two functions (main and max) and two typedef (sem and argstruct)
single thread: two ints for the two numbers compared in each thread; an
int to hold the maximum of each round; a barrier object for each thread; a
semaphore pointer struct (sem from above); and an array of the maxes of
max- This function finds the maximum value inside an int array. More
doing the following: first, the two numbers within a given thread were
compared to see which the bigger number is. Then, that max number was
put into an array of maximum numbers alongside the other max numbers
of the other threads. Assuming the threads id was odd, sem_wait was
called. This is the consumer side of the relationship; the evens consume
the max and use it for the next comparison. To insure that all the odd
terminated by pthread.exit.
main- In the main, the input from the user or file is taken and put into an
int array. In addition, all of the attributes of the argstruct is created and
used to set up the max operations using threads. Each thread is created in a
in the max function available, thus allowing the program to access the max
value.
3. Problems Encounted:
The first problem that was run into was just implementing the single barrier into
the max function. The first version of the code did not include threads or barriers;
it was just a simple for loop. The beta version included threads and run smoothly.
But even so, there did not seem a necessity to add a barrier, let alone how to
include. It was only at a realization that one could use the threads as odds and
evens to be producers and consumers that gave the correct places to put the barrier
The second problem arose when I run the code on my own machine. For some
reason, the program was caught in an unending loop, were all but the last the
threads would go through. This looked like a typical case of Dr. D (deadlock), but
when running the same code with the same inputs and same test file on the VM
image provided in class, it run fine. This just lets me know that in the future, I
shouldnt rely on the outputs/ results given to me on my computer for future
assignments.
4. Testing:
Testing the output of the code was relatively simple. First, an input of one integer
was tested; output was just that number. Then was couple of 2^N list numbers
were tested and successfully yielded the max value in each list. To test a huge list
of 2^12 numbers, a testfile.txt holding that number of integers was created and
tested by putting into the command line: ./a.out < testfile.txt . It worked.
For testing the barrier, I tested the code without the line which implements the
which clearly indicated the threads were doing their own thing and accessing
random things. The second time the same input was run, the output was
24363072, now indicating a total randomness and no real way the threads were
being regulated. When the barrier was put back in place, the output was 8.
5. Conclusion:
Conclusively, barriers can be used to regulate threads for perform operations such
as finding the largest number in an array or the sum numbers in an array. I learnt
the relationship between producer and consumer threads and how to use counting
#include <stdio.h>
#include <semaphore.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <string>
#include <iostream>
#include <pthread.h>
#include <math.h>
#include "Barrier.h"
argstruct* a = (argstruct*)p;
int rounds = 0;
if (a->isAct){
a->maxes[(a->id-1)/2] = a->maximum;
/*An array of n/2 maximums. The odd semaphors are producers. They take
their max and store it in this maxes array. The evens comsume the max
and use it for the next comparison.
a->isAct = false;
/*Essentially, this makes that odd thread inactive, meaning we don't use
it anymore. */
}
}
rounds++;
a->b->wait();
/*The barrier all threads have to wait until all threads are at that
barrier. */
if (a->isAct && rounds < a->numRounds){
/* The case for the even ones, the producers. It gives them their
attributes */
a->num1 = a->maximum;
a->num2 = a->maxes[(a->id)/2];
sem_post(&(a->s[(a->id)/2].mutex));
/* Main method */
int main() {
int j =0;
Barrier.h:
#include <semaphore.h>
class Barrier {
public:
Barrier(int k); //Barrier constructor with k parameter
void wait(); //Wait function for Barrier
private:
sem_t waitq; //Semaphore type wait to halt threads
sem_t mutex; //Semaphore type mutex to allow threads
sem_t throttle;//Allow hand shakes between threads
int k; //Number of threads
int count; //Keep up with the current no. of threads
} ;
Barrier.cpp:
#include "Barrier.h"
/* The Barrier's function wait that halts threads until all threads are
done doing a specific job. */
void Barrier::wait() {
sem_wait(&mutex);
count++;
if (count < k) {
sem_post(&mutex); //Allows threads to modify variables
sem_wait(&waitq); //Halts thread (locks current thread)
sem_post(&throttle); //Handshake betw/ releaser and waiter
}
else {
/*For each thread, unlock and allow handshake for next in queue */
HONOR PLEDGE:
On my honor as a student, I have neither given nor received aid on this assignment.
-Samuel Boakye