aboutsummaryrefslogtreecommitdiffstats
path: root/row_dispenser.c
blob: 0d9b47fef850f2165921b41e55616abd5b1157d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef GTHREAD_DISPENSER
#define GTHREAD_DISPENSER GTHREAD_DISPENSER
#define function_pointer(name) void (*name)()
#include<pthread.h>
#include<semaphore.h>
#include<stdlib.h>//malloc et al.
#include"gmp.h"

/*
 * we clean the 'source_row' column using the source row
 */
struct row_pair
{
	size_t source_row;
	size_t target_row;
};

/*
 * stuff conserning the synchronisation of a single pass ( sync. between passes is in threadet)
 * could consider these the parameters passed to the threads ( but they are shared )
 */
struct Pair_Dispenser
{
	/* mutex */
	sem_t semp;
	
	/*
	 * current is the next pair to be calculated
	 * pairs given are unique in the scope of the pass
	 *
	 */
	struct row_pair current;
	/*
	 * max is the maximum index. = matrix size - 1
	 * this value is not modified by the threads
	 *
	 */
	size_t max;
	/*
	 * the matrix that we calculate the matrix for
	 * this value is not modified by the threads
	 *
	 */
	mpq_t** matrix;
	/*
	 * should the thread print stuff
	 * this value is not modified by the threads
	 *
	 */
	char verbose;

};

void Pair_Dispenser_Init(struct Pair_Dispenser *thrd,size_t max,mpq_t** matrix,char verbose)
{
	thrd->current.source_row = 0;
	thrd->current.target_row = 0;
	thrd->max=max;
	thrd->matrix=matrix;
	sem_init(&(thrd->semp),0,1);
	thrd->verbose = verbose;
}
/*
 * Get a row pair without breaking the row dispenser invariant
 */
struct row_pair Pair_Dispenser_Get_Pair(struct Pair_Dispenser *thrd)
{
	struct row_pair temp;
	/* this is 1/2 of what will slow down parallel execution */
	/* critical part */
	sem_wait(&(thrd->semp));	
	temp=thrd->current;
	if(thrd->current.target_row<= thrd->max)
	{
		++thrd->current.target_row; 
	}
	sem_post(&(thrd->semp));	
	/* critical part */
	return temp;

}




#endif