-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel_spin.c
149 lines (122 loc) · 3.45 KB
/
parallel_spin.c
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
#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];
pthread_spinlock_t spinlock;
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;
}
// 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 (!e) panic("No memory to allocate bucket!");
pthread_spin_lock(&spinlock);
e->next = table[i];
e->key = key;
e->val = val;
table[i] = e;
pthread_spin_unlock(&spinlock);
}
// Retrieves an entry from the hash table by key
// Returns NULL if the key isn't found in the table
bucket_entry * retrieve(int key) {
bucket_entry *b;
pthread_spin_lock(&spinlock);
for (b = table[key % NUM_BUCKETS]; b != NULL; b = b->next) {
if (b->key == key) {
pthread_spin_unlock(&spinlock);
return b;
}
}
pthread_spin_unlock(&spinlock);
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");
}
if (pthread_spin_init(&spinlock, 0) != 0) {
panic("failed to initialize spinlock");
}
// 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();
pthread_spin_destroy(&spinlock);
printf("[main] Retrieved %ld/%d keys in %f seconds\n", NUM_KEYS - total_lost, NUM_KEYS, end - start);
return 0;
}