edits
[cdsspec-compiler.git] / benchmark / concurrent-hashmap / main.cc
1 #include <iostream>
2 #include <threads.h>
3 #include "hashmap.h"
4
5 HashMap *table;
6
7
8 void printKey(Key *key) {
9         if (key)
10                 printf("pos = (%d, %d, %d)\n", key->x, key->y, key->z);
11         else
12                 printf("pos = NULL\n");
13 }
14
15 void printValue(Value *value) {
16         if (value)
17                 printf("velocity = (%d, %d, %d)\n", value->vX, value->vY, value->vZ);
18         else
19                 printf("velocity = NULL\n");
20 }
21
22 // Key(3, 2, 6) & Key(1, 3, 3) are hashed to the same slot -> 4
23 // Key(1, 1, 1) & Key(3, 2, 2) are hashed to the same slot -> 0
24 // Key(2, 4, 1) & Key(3, 4, 2) are hashed to the same slot -> 3
25 // Key(3, 4, 5) & Key(1, 4, 3) are hashed to the same slot -> 5
26
27
28 void threadA(void *arg) {
29         Key *k1 = new Key(3, 2, 6);
30         Key *k2 = new Key(1, 1, 1);
31         Value *v1 = new Value(10, 10, 10);
32         Value *r1 = table->put(k1, v1);
33         //printValue(r1);
34         Value *r2 = table->get(k2);
35         //printf("Thrd A:\n");
36         printValue(r2);
37 }
38
39 void threadB(void *arg) {
40         Key *k1 = new Key(3, 2, 6);
41         Key *k2 = new Key(1, 1, 1);
42         Value *v2 = new Value(30, 40, 50);
43         Value *r3 = table->put(k2, v2);
44         //printValue(r3);
45         Value *r4 = table->get(k1);
46         printf("Thrd B:\n");
47         printValue(r4);
48 }
49
50 int user_main(int argc, char *argv[]) {
51         thrd_t t1, t2;
52
53         Key *k1 = new Key(1, 3, 3);
54         Key *k1_prime = new Key(3, 2, 6);
55         Key *k2 = new Key(3, 2, 2);
56         Key *k2_prime = new Key(1, 1, 1);
57         Value *v1 = new Value(111, 111, 111);
58         Value *v2 = new Value(222, 222, 222);
59
60         table = new HashMap;
61
62         printf("Key1: %d\n", table->hashKey(k1) % table->capacity);
63         printf("Key1': %d\n", table->hashKey(k1_prime) % table->capacity);
64         printf("Key2: %d\n", table->hashKey(k2) % table->capacity);
65         printf("Key2': %d\n", table->hashKey(k2_prime) % table->capacity);
66
67         thrd_create(&t1, threadA, NULL);
68         thrd_create(&t2, threadB, NULL);
69         thrd_join(t1);
70         thrd_join(t2);
71         
72         return 0;
73 }
74
75