fixed hashmap, add better makefile
authorPeizhao Ou <peizhaoo@uci.edu>
Thu, 12 Feb 2015 00:40:36 +0000 (16:40 -0800)
committerPeizhao Ou <peizhaoo@uci.edu>
Thu, 12 Feb 2015 00:40:36 +0000 (16:40 -0800)
concurrent-hashmap/Makefile
concurrent-hashmap/hashmap.h
concurrent-hashmap/hashmap_wildcard.h
concurrent-hashmap/main.cc [deleted file]
concurrent-hashmap/testcase1.cc [new file with mode: 0644]
concurrent-hashmap/testcase2.cc [new file with mode: 0644]

index d39e8315c683ebb8041c9b2353a40e0737be69b2..e386b4db1b76f6b31a686bdcb52a5e007a68c34f 100644 (file)
@@ -1,17 +1,20 @@
 include ../benchmarks.mk
 
-TESTS := table table_wildcard1
+NORMAL_TESTS := testcase1 testcase2
+
+WILDCARD_TESTS := $(patsubst %, %_wildcard, $(NORMAL_TESTS))
+
+TESTS := $(NORMAL_TESTS) $(WILDCARD_TESTS)
 
 all: $(TESTS)
 
-table: main.cc hashmap.h
-       $(CXX) -o $@ $^ $(SPEC_OBJ) $(CXXFLAGS) -std=c++0x $(LDFLAGS)
+$(WILDCARD_TESTS): CXXFLAGS += -DWILDCARD
 
-table_wildcard1: main_wildcard1.cc hashmap_wildcard.h
-       $(CXX) -o $@ $^ $(SPEC_OBJ) $(CXXFLAGS) -std=c++0x $(LDFLAGS)
+$(WILDCARD_TESTS): %_wildcard : %.cc hashmap_wildcard.h 
+       $(CXX) -o $@ $< $(SPEC_OBJ) $(CXXFLAGS) $(LDFLAGS)
 
-table_wildcard2: main_wildcard1.cc hashmap_wildcard.h
-       $(CXX) -o $@ $^ $(SPEC_OBJ) $(CXXFLAGS) -std=c++0x $(LDFLAGS)
+$(NORMAL_TESTS): % : %.cc hashmap.h
+       $(CXX) -o $@ $< $(SPEC_OBJ) $(CXXFLAGS) $(LDFLAGS)
 
 clean:
        rm -f *.o *.d $(TESTS)
index 936f284635a1b71946b1fe867c964852aace5e04..8e6f196cb0e8c73d8a02f9dee01b152dddea7cc9 100644 (file)
@@ -14,6 +14,7 @@
 #include <stdlib.h>
 #include <mutex>
 
+#include "common.h"
 #include "sc_annotation.h"
 
 #define relaxed memory_order_relaxed
@@ -164,10 +165,10 @@ class HashMap {
                // lock, we ignore this operation for the SC analysis, and otherwise we
                // take it into consideration
                
-               SC_BEGIN();
+               //SC_BEGIN();
                Entry *firstPtr = first->load(acquire);
-               SC_KEEP();
-               SC_END();
+               //SC_KEEP();
+               //SC_END();
 
                e = firstPtr;
                while (e != NULL) {
@@ -177,9 +178,9 @@ class HashMap {
                                        return res;
                                else
                                        break;
-                               // Loading the next entry
-                               e = e->next.load(acquire);
                        }
+                       // Loading the next entry
+                       e = e->next.load(acquire);
                }
        
                // Recheck under synch if key apparently not there or interference
index 557a918f8aca49ae7d3445db7e9a391b17638c4c..0713b782c9288d7101bf7de8427c2ae22a91333d 100644 (file)
@@ -178,9 +178,9 @@ class HashMap {
                                        return res;
                                else
                                        break;
-                               // Loading the next entry
-                               e = e->next.load(wildcard(5)); // acquire
                        }
+                       // Loading the next entry
+                       e = e->next.load(wildcard(5)); // acquire
                }
        
                // Recheck under synch if key apparently not there or interference
diff --git a/concurrent-hashmap/main.cc b/concurrent-hashmap/main.cc
deleted file mode 100644 (file)
index fcb1186..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-#include <iostream>
-#include <threads.h>
-#include "hashmap.h"
-
-HashMap *table;
-
-Key *k1, *k2;
-Value *r1, *r2, *r3, *r4, *v1, *v2;
-
-void printKey(Key *key) {
-       if (key)
-               printf("pos = (%d, %d, %d)\n", key->x, key->y, key->z);
-       else
-               printf("pos = NULL\n");
-}
-
-void printValue(Value *value) {
-       if (value)
-               printf("velocity = (%d, %d, %d)\n", value->vX, value->vY, value->vZ);
-       else
-               printf("velocity = NULL\n");
-}
-
-void threadA(void *arg) {
-       k1 = new Key(1, 1, 1);
-       k2 = new Key(3, 4, 5);
-       v1 = new Value(10, 10, 10);
-       r1 = table->put(k1, v1);
-       //printValue(r1);
-       r2 = table->get(k2);
-       printf("Thrd A:\n");
-       printValue(r2);
-}
-
-void threadB(void *arg) {
-       k1 = new Key(1, 1, 1);
-       k2 = new Key(3, 4, 5);
-       v2 = new Value(30, 40, 50);
-       r3 = table->put(k2, v2);
-       //printValue(r3);
-       r4 = table->get(k1);
-       printf("Thrd B:\n");
-       printValue(r4);
-}
-
-int user_main(int argc, char *argv[]) {
-       thrd_t t1, t2;
-       table = new HashMap;
-
-       thrd_create(&t1, threadA, NULL);
-       thrd_create(&t2, threadB, NULL);
-       thrd_join(t1);
-       thrd_join(t2);
-       
-       return 0;
-}
-
-
diff --git a/concurrent-hashmap/testcase1.cc b/concurrent-hashmap/testcase1.cc
new file mode 100644 (file)
index 0000000..939e250
--- /dev/null
@@ -0,0 +1,65 @@
+#include <threads.h>
+
+#ifdef WILDCARD
+#include "hashmap_wildcard.h"
+#else
+#include "hashmap.h"
+#endif
+
+HashMap *table;
+
+void printKey(Key *key) {
+       if (key)
+               printf("pos = (%d, %d, %d)\n", key->x, key->y, key->z);
+       else
+               printf("pos = NULL\n");
+}
+
+void printValue(Value *value) {
+       if (value)
+               printf("velocity = (%d, %d, %d)\n", value->vX, value->vY, value->vZ);
+       else
+               printf("velocity = NULL\n");
+}
+
+// Key(3, 2, 6) & Key(1, 3, 3) are hashed to the same slot -> 4
+// Key(1, 1, 1) & Key(3, 2, 2) are hashed to the same slot -> 0
+// Key(2, 4, 1) & Key(3, 4, 2) are hashed to the same slot -> 3
+// Key(3, 4, 5) & Key(1, 4, 3) are hashed to the same slot -> 5
+
+
+void threadA(void *arg) {
+       Key *k1 = new Key(3, 2, 6);
+       Key *k2 = new Key(1, 1, 1);
+       Value *v1 = new Value(10, 10, 10);
+       Value *r1 = table->put(k1, v1);
+       //printValue(r1);
+       Value *r2 = table->get(k2);
+       //printf("Thrd A:\n");
+       printValue(r2);
+}
+
+void threadB(void *arg) {
+       Key *k1 = new Key(3, 2, 6);
+       Key *k2 = new Key(1, 1, 1);
+       Value *v2 = new Value(30, 40, 50);
+       Value *r3 = table->put(k2, v2);
+       //printValue(r3);
+       Value *r4 = table->get(k1);
+       printf("Thrd B:\n");
+       printValue(r4);
+}
+
+int user_main(int argc, char *argv[]) {
+       thrd_t t1, t2;
+       table = new HashMap;
+
+       thrd_create(&t1, threadA, NULL);
+       thrd_create(&t2, threadB, NULL);
+       thrd_join(t1);
+       thrd_join(t2);
+       
+       return 0;
+}
+
+
diff --git a/concurrent-hashmap/testcase2.cc b/concurrent-hashmap/testcase2.cc
new file mode 100644 (file)
index 0000000..17f79d8
--- /dev/null
@@ -0,0 +1,72 @@
+#include <threads.h>
+
+#ifdef WILDCARD
+#include "hashmap_wildcard.h"
+#else
+#include "hashmap.h"
+#endif
+
+HashMap *table;
+
+void printKey(Key *key) {
+       if (key)
+               printf("pos = (%d, %d, %d)\n", key->x, key->y, key->z);
+       else
+               printf("pos = NULL\n");
+}
+
+void printValue(Value *value) {
+       if (value)
+               printf("velocity = (%d, %d, %d)\n", value->vX, value->vY, value->vZ);
+       else
+               printf("velocity = NULL\n");
+}
+
+// Key(3, 2, 6) & Key(1, 3, 3) are hashed to the same slot -> 4
+// Key(1, 1, 1) & Key(3, 2, 2) are hashed to the same slot -> 0
+// Key(2, 4, 1) & Key(3, 4, 2) are hashed to the same slot -> 3
+// Key(3, 4, 5) & Key(1, 4, 3) are hashed to the same slot -> 5
+
+
+void threadA(void *arg) {
+       Key *k1 = new Key(3, 2, 6);
+       Key *k2 = new Key(1, 1, 1);
+       Value *v1 = new Value(10, 10, 10);
+       Value *r1 = table->put(k1, v1);
+       //printValue(r1);
+       Value *r2 = table->get(k2);
+       //printf("Thrd A:\n");
+       printValue(r2);
+}
+
+void threadB(void *arg) {
+       Key *k1 = new Key(3, 2, 6);
+       Key *k2 = new Key(1, 1, 1);
+       Value *v2 = new Value(30, 40, 50);
+       Value *r3 = table->put(k2, v2);
+       //printValue(r3);
+       Value *r4 = table->get(k1);
+       printf("Thrd B:\n");
+       printValue(r4);
+}
+
+int user_main(int argc, char *argv[]) {
+       
+       Key *k1 = new Key(3, 2, 6);
+       Key *k2 = new Key(1, 1, 1);
+       Value *v1 = new Value(111, 111, 111);
+       Value *v2 = new Value(222, 222, 222);
+       thrd_t t1, t2;
+       table = new HashMap;
+       table->put(k1, v1);
+       table->put(k2, v2);
+
+       thrd_create(&t1, threadA, NULL);
+       thrd_create(&t2, threadB, NULL);
+       thrd_join(t1);
+       thrd_join(t2);
+       
+       return 0;
+}
+
+