-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_hashtable.c
More file actions
155 lines (129 loc) · 4.32 KB
/
Copy pathparallel_hashtable.c
File metadata and controls
155 lines (129 loc) · 4.32 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <assert.h>
#include <sys/time.h>
#define NUM_BUCKETS 5 // Buckets in hash table
#define NUM_KEYS 100000 // Number of keys inserted per thread
int num_threads = 1; // Number of threads (configurable)
int keys[NUM_KEYS];
typedef struct _bucket_entry {
int key;
int val;
struct _bucket_entry *next;
} bucket_entry;
bucket_entry *table[NUM_BUCKETS];
void panic(char *msg) {
printf("%s\n", msg);
exit(1);
}
double now() {
struct timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec + tv.tv_usec / 1000000.0;
}
int init = 0; //flag: do we need to initialize mutex array or now?
pthread_mutex_t locks[NUM_BUCKETS]; //Array of mutexes, each one is related to a bucket in the hashtalbe. Another way to do is have each bucket_entry have a mutex, but then you either have to share the mutexes throughout each bucket's list or have a lot of mutexes.
void init_mutex()
{
int i;
//For each bucket initialize a mutex for that bucket.
for(i = 0; i < NUM_BUCKETS; i+=1)
{
pthread_mutex_t lock;
locks[i] = lock;
pthread_mutex_init(&locks[i], NULL);
}
init = 1;
}
// Inserts a key-value pair into the table
void insert(int key, int val) {
int i = key % NUM_BUCKETS;
bucket_entry *e = (bucket_entry *) malloc(sizeof(bucket_entry));
if(init == 0)
init_mutex(); //Should be done in main, but trying to keep all changes to insert
if (!e) panic("No memory to allocate bucket!");
e->key = key;
e->val = val; //Minimize time locked by assigning these outside lock.
pthread_mutex_lock(&locks[i]); //Lock the mutex associated with the bucket: no other thread can insert. READING CAN BE DONE
e->next = table[i];
table[i] = e;
pthread_mutex_unlock(&locks[i]); //Unlock, new insertions can be done
}
// Retrieves an entry from the hash table by key
// Returns NULL if the key isn't found in the table
//concurrent reads are fine, shouldn't read while insertions are happening, but that does not happen in this program.
bucket_entry * retrieve(int key) {
bucket_entry *b;
for (b = table[key % NUM_BUCKETS]; b != NULL; b = b->next) {
if (b->key == key) return b;
}
return NULL;
}
void * put_phase(void *arg) {
long tid = (long) arg;
int key = 0;
// If there are k threads, thread i inserts
// (i, i), (i+k, i), (i+k*2)
for (key = tid ; key < NUM_KEYS; key += num_threads) {
insert(keys[key], tid);
}
pthread_exit(NULL);
}
void * get_phase(void *arg) {
long tid = (long) arg;
int key = 0;
long lost = 0;
for (key = tid ; key < NUM_KEYS; key += num_threads) {
if (retrieve(keys[key]) == NULL) lost++;
}
printf("[thread %ld] %ld keys lost!\n", tid, lost);
pthread_exit((void *)lost);
}
int main(int argc, char **argv) {
long i;
pthread_t *threads;
double start, end;
if (argc != 2) {
panic("usage: ./parallel_hashtable <num_threads>");
}
if ((num_threads = atoi(argv[1])) <= 0) {
panic("must enter a valid number of threads to run");
}
srandom(time(NULL));
for (i = 0; i < NUM_KEYS; i++)
keys[i] = random();
threads = (pthread_t *) malloc(sizeof(pthread_t)*num_threads);
if (!threads) {
panic("out of memory allocating thread handles");
}
// Insert keys in parallel
start = now();
for (i = 0; i < num_threads; i++) {
pthread_create(&threads[i], NULL, put_phase, (void *)i);
}
// Barrier
for (i = 0; i < num_threads; i++) {
pthread_join(threads[i], NULL);
}
end = now();
printf("[main] Inserted %d keys in %f seconds\n", NUM_KEYS, end - start);
// Reset the thread array
memset(threads, 0, sizeof(pthread_t)*num_threads);
// Retrieve keys in parallel
start = now();
for (i = 0; i < num_threads; i++) {
pthread_create(&threads[i], NULL, get_phase, (void *)i);
}
// Collect count of lost keys
long total_lost = 0;
long *lost_keys = (long *) malloc(sizeof(long) * num_threads);
for (i = 0; i < num_threads; i++) {
pthread_join(threads[i], (void **)&lost_keys[i]);
total_lost += lost_keys[i];
}
end = now();
printf("[main] Retrieved %ld/%d keys in %f seconds\n", NUM_KEYS - total_lost, NUM_KEYS, end - start);
return 0;
}