minor fix
[cdsspec-compiler.git] / benchmark / cliffc-hashtable / cliffc_hashtable.h
index 6edee89d33e1def8358b3e6b749355de08ef6ad2..c3cea0d20fef1ae5130a603201508c7acfedc279 100644 (file)
@@ -3,8 +3,19 @@
 
 #include <iostream>
 #include <atomic>
-#include <memory>
+//#include <common.h>
+#ifdef STANDALONE
 #include <assert.h>
+#define MODEL_ASSERT assert 
+#else
+#include <model-assert.h>
+#endif
+
+#include <spec_lib.h>
+#include <stdlib.h>
+#include <cdsannotate.h>
+#include <specannotation.h>
+#include <model_memory.h>
 
 using namespace std;
 
@@ -29,7 +40,7 @@ struct kvs_data {
        
        kvs_data(int sz) {
                _size = sz;
-               int real_size = sizeof(atomic<void*>) * 2 + 2;
+               int real_size = sz * 2 + 2;
                _data = new atomic<void*>[real_size];
                // The control block should be initialized in resize()
                // Init the hash record array
@@ -69,6 +80,11 @@ struct slot {
        used to judge equality.
        TypeK and TypeV should define their own copy constructor.
 */
+/**
+       @Begin
+       @Class_begin
+       @End
+*/
 template<typename TypeK, typename TypeV>
 class cliffc_hashtable {
        /**
@@ -78,34 +94,59 @@ class cliffc_hashtable {
        
                @Begin
                @Options:
-                       LANG = C;
+                       LANG = CPP;
                        CLASS = cliffc_hashtable;
                @Global_define:
                        @DeclareVar:
-                       spec_hashtable<TypeK, TypeV*> map;
-                       spec_hashtable<TypeK, Tag> id_map;
-                       Tag tag;
+                       spec_table *map;
+                       spec_table *id_map;
+                       id_tag_t *tag;
                        @InitVar:
-                               map = spec_hashtable<TypeK, TypeV*>();
-                               id_map = spec_hashtable<TypeK, TypeV*>();
-                               tag = Tag();
+                               map = new_spec_table_default(equals_key);
+                               id_map = new_spec_table_default(equals_id);
+                               tag = new_id_tag();
+
                        @DefineFunc:
-                       bool equals_val(TypeV *ptr1, TypeV *ptr2) {
-                               // ...
+                       bool equals_key(void *ptr1, void *ptr2) {
+                               TypeK *key1 = (TypeK*) ptr1,
+                                       *key2 = (TypeK*) ptr2;
+                               if (key1 == NULL || key2 == NULL)
+                                       return false;
+                               return key1->equals(key2);
                        }
                        
+                       @DefineFunc:
+                       bool equals_val(void *ptr1, void *ptr2) {
+                               TypeV *val1 = (TypeV*) ptr1,
+                                       *val2 = (TypeV*) ptr2;
+                               if (val1 == NULL || val2 == NULL)
+                                       return false;
+                               return val1->equals(val2);
+                       }
+
+                       @DefineFunc:
+                       bool equals_id(void *ptr1, void *ptr2) {
+                               id_tag_t *id1 = (id_tag_t*) ptr1,
+                                       *id2 = (id_tag_t*) ptr2;
+                               if (id1 == NULL || id2 == NULL)
+                                       return false;
+                               return (*id1).tag == (*id2).tag;
+                       }
+
                        @DefineFunc:
                        # Update the tag for the current key slot if the corresponding tag
                        # is NULL, otherwise just return that tag. It will update the next
                        # available tag too if it requires a new tag for that key slot.
-                       Tag getKeyTag(TypeK &key) {
-                               if (id_map.get(key) == NULL) {
-                                       Tag cur_tag = tag.current();
-                                       id_map.put(key, cur_tag);
-                                       tag.next();
+                       id_tag_t getKeyTag(TypeK *key) {
+                               if (spec_table_contains(id_map, key)) {
+                                       id_tag_t *cur_tag = MODEL_MALLOC(sizeof(id_tag_t));
+                                       *cur_tag = current(tag);
+                                       spec_table_put(id_map, key, cur_tag);
+                                       next(tag);
                                        return cur_tag;
                                } else {
-                                       return id_map.get(key);
+                                       id_tag_t *res = (id_tag_t*) spec_table_get(id_map, key);
+                                       return *res;
                                }
                        }
                
@@ -128,7 +169,9 @@ class cliffc_hashtable {
                                ReplaceIfMatch(COND_ReplaceIfMatchSucc)
                        }
                @Happens_before:
-                       Write_interface -> Read_interface
+                       //Write_interface -> Read_interface
+                       Put->Get
+                       Put->Put
                @End
        */
 
@@ -176,6 +219,7 @@ friend class CHM;
                }
        
                kvs_data* resize(cliffc_hashtable *topmap, kvs_data *kvs) {
+                       //model_print("resizing...\n");
                        kvs_data *newkvs = _newkvs.load(memory_order_acquire);
                        if (newkvs != NULL)
                                return newkvs;
@@ -203,7 +247,7 @@ friend class CHM;
        
                        newkvs = new kvs_data(newsz);
                        void *chm = (void*) new CHM(sz);
-                       newkvs->_data[0].store(chm, memory_order_relaxed);
+                       newkvs->_data[0].store(chm, memory_order_release);
        
                        kvs_data *cur_newkvs; 
                        // Another check after the slow allocation
@@ -223,7 +267,7 @@ friend class CHM;
        
                void help_copy_impl(cliffc_hashtable *topmap, kvs_data *oldkvs,
                        bool copy_all) {
-                       assert (get_chm(oldkvs) == this);
+                       MODEL_ASSERT (get_chm(oldkvs) == this);
                        kvs_data *newkvs = _newkvs.load(memory_order_acquire);
                        int oldlen = oldkvs->_size;
                        int min_copy_work = oldlen > 1024 ? 1024 : oldlen;
@@ -328,7 +372,7 @@ friend class CHM;
        
 
        private:
-       static const int Default_Init_Size = 8; // Intial table size
+       static const int Default_Init_Size = 4; // Intial table size
 
        static slot* const MATCH_ANY;
        static slot* const NO_MATCH_OLD;
@@ -355,6 +399,12 @@ friend class CHM;
                // Should initialize the CHM for the construction of the table
                // For other CHM in kvs_data, they should be initialzed in resize()
                // because the size is determined dynamically
+               /**
+                       @Begin
+                       @Entry_point
+                       @End
+               */
+
                kvs_data *kvs = new kvs_data(init_size);
                void *chm = (void*) new CHM(0);
                kvs->_data[0].store(chm, memory_order_relaxed);
@@ -364,142 +414,142 @@ friend class CHM;
        /**
                @Begin
                @Interface: Get
-               @Commit_point_set: Read_Val_Point1 | Read_Val_Point2 | Read_Val_Point3
-               @ID: __sequential.getKeyTag(key)
+               @Commit_point_set: Get_Success_Point1 | Get_Success_Point2 | Get_Success_Point3 
+               @ID: getKeyTag(key)
                @Action:
-                       TypeV *_Old_Val = __sequential.map.get(key)
+                       void *_Old_Val = spec_table_get(map, key);
                @Post_check:
-                       __sequential.equals_val(_Old_Val, __RET__)
+                       equals_val(_Old_Val, __RET__)
                @End
        */
-       TypeV* get(TypeK& key) {
-               void *key_ptr = (void*) new TypeK(key);
-               slot *key_slot = new slot(false, key_ptr);
+       TypeV* get(TypeK *key) {
+               slot *key_slot = new slot(false, key);
                int fullhash = hash(key_slot);
                kvs_data *kvs = _kvs.load(memory_order_acquire);
                slot *V = get_impl(this, kvs, key_slot, fullhash);
                if (V == NULL) return NULL;
-               assert (!is_prime(V));
+               MODEL_ASSERT (!is_prime(V));
                return (TypeV*) V->_ptr;
        }
 
        /**
                @Begin
                @Interface: Put
-               @Commit_point_set: Write_Val_Point
-               @ID: __sequential.getKeyTag(key)
+               @Commit_point_set: Write_Success_Point
+               @ID: getKeyTag(key)
                @Action:
                        # Remember this old value at checking point
-                       TypeV *_Old_Val = __sequential.map.get(key)
-                       __sequential.map.put(key, &val);
+                       void *_Old_Val = spec_table_get(map, key);
+                       spec_table_put(map, key, val);
                @Post_check:
-                       __sequential.equals_val(__RET__, _Old_Val)
+                       equals_val(__RET__, _Old_Val)
                @End
        */
-       TypeV* put(TypeK& key, TypeV& val) {
+       TypeV* put(TypeK *key, TypeV *val) {
                return putIfMatch(key, val, NO_MATCH_OLD);
        }
 
        /**
-               @Begin
+//             @Begin
                @Interface: PutIfAbsent
                @Commit_point_set:
-                       Write_Val_Point | PutIfAbsent_Fail_Point
-               @Condition: __sequential.map.get(key) == NULL
+                       Write_Success_Point | PutIfAbsent_Fail_Point
+               @Condition: !spec_table_contains(map, key)
                @HB_condition:
                        COND_PutIfAbsentSucc :: __RET__ == NULL
-               @ID: __sequential.getKeyTag(key)
+               @ID: getKeyTag(key)
                @Action:
-                       TypeV *_Old_Val = __sequential.map.get(key)
+                       void *_Old_Val = spec_table_get(map, key);
                        if (__COND_SAT__)
-                               __sequential.map.put(key, &value);
+                               spec_table_put(map, key, value);
                @Post_check:
-                       __COND_SAT__ ? __RET__ == NULL : __sequential.equals_val(_Old_Val, __RET__) 
+                       __COND_SAT__ ? __RET__ == NULL : equals_val(_Old_Val, __RET__) 
                @End
        */
-       TypeV* putIfAbsent(TypeK& key, TypeV& value) {
+       TypeV* putIfAbsent(TypeK *key, TypeV *value) {
                return putIfMatch(key, val, TOMBSTONE);
        }
 
        /**
-               @Begin
+//             @Begin
                @Interface: RemoveAny
-               @Commit_point_set: Write_Val_Point
-               @ID: __sequential.getKeyTag(key)
+               @Commit_point_set: Write_Success_Point
+               @ID: getKeyTag(key)
                @Action:
-                       TypeV *_Old_Val = __sequential.map.get(key)
-                       __sequential.map.put(key, NULL);
+                       void *_Old_Val = spec_table_get(map, key);
+                       spec_table_put(map, key, NULL);
                @Post_check:
-                       __sequential.equals_val(__RET__, _Old_Val)
+                       equals_val(__RET__, _Old_Val)
                @End
        */
-       TypeV* remove(TypeKkey) {
+       TypeV* remove(TypeK *key) {
                return putIfMatch(key, TOMBSTONE, NO_MATCH_OLD);
        }
 
        /**
-               @Begin
+//             @Begin
                @Interface: RemoveIfMatch
                @Commit_point_set:
-                       Write_Val_Point | RemoveIfMatch_Fail_Point
+                       Write_Success_Point | RemoveIfMatch_Fail_Point
                @Condition:
-                       __sequential.equals_val(__sequential.map.get(key), &val)
+                       equals_val(spec_table_get(map, key), val)
                @HB_condition:
                        COND_RemoveIfMatchSucc :: __RET__ == true
-               @ID: __sequential.getKeyTag(key)
+               @ID: getKeyTag(key)
                @Action:
                        if (__COND_SAT__)
-                               __sequential.map.put(key, NULL);
+                               spec_table_put(map, key, NULL);
                @Post_check:
                        __COND_SAT__ ? __RET__ : !__RET__
                @End
        */
-       bool remove(TypeK& key, TypeV& val) {
+       bool remove(TypeK *key, TypeV *val) {
                slot *val_slot = val == NULL ? NULL : new slot(false, val);
                return putIfMatch(key, TOMBSTONE, val) == val;
 
        }
 
        /**
-               @Begin
+//             @Begin
                @Interface: ReplaceAny
                @Commit_point_set:
-                       Write_Val_Point
-               @ID: __sequential.getKeyTag(key)
+                       Write_Success_Point
+               @ID: getKeyTag(key)
                @Action:
-                       TypeV *_Old_Val = __sequential.map.get(key)
+                       void *_Old_Val = spec_table_get(map, key);
                @Post_check:
-                       __sequential.equals_val(__RET__, _Old_Val)
+                       equals_val(__RET__, _Old_Val)
                @End
        */
-       TypeV* replace(TypeK& key, TypeV& val) {
+       TypeV* replace(TypeK *key, TypeV *val) {
                return putIfMatch(key, val, MATCH_ANY);
        }
 
        /**
-               @Begin
+//             @Begin
                @Interface: ReplaceIfMatch
                @Commit_point_set:
-                       Write_Val_Point | ReplaceIfMatch_Fail_Point
+                       Write_Success_Point | ReplaceIfMatch_Fail_Point
                @Condition:
-                       __sequential.equals_val(__sequential.map.get(key), &oldval)
+                       equals_val(spec_table_get(map, key), oldval)
                @HB_condition:
                        COND_ReplaceIfMatchSucc :: __RET__ == true
-               @ID: __sequential.getKeyTag(key)
+               @ID: getKeyTag(key)
                @Action:
                        if (__COND_SAT__)
-                               __sequential.map.put(key, &newval);
+                               spec_table_put(map, key, newval);
                @Post_check:
                        __COND_SAT__ ? __RET__ : !__RET__
                @End
        */
-       bool replace(TypeK& key, TypeV& oldval, TypeV& newval) {
+       bool replace(TypeK *key, TypeV *oldval, TypeV *newval) {
                return putIfMatch(key, newval, oldval) == oldval;
        }
 
        private:
        static CHM* get_chm(kvs_data* kvs) {
-               return (CHM*) kvs->_data[0].load(memory_order_relaxed);
+               CHM *res = (CHM*) kvs->_data[0].load(memory_order_relaxed);
+               return res;
        }
 
        static int* get_hashes(kvs_data *kvs) {
@@ -508,10 +558,11 @@ friend class CHM;
        
        // Preserve happens-before semantics on newly inserted keys
        static inline slot* key(kvs_data *kvs, int idx) {
-               assert (idx >= 0 && idx < kvs->_size);
+               MODEL_ASSERT (idx >= 0 && idx < kvs->_size);
                // Corresponding to the volatile read in get_impl() and putIfMatch in
                // Cliff Click's Java implementation
-               return (slot*) kvs->_data[idx * 2 + 2].load(memory_order_acquire);
+               slot *res = (slot*) kvs->_data[idx * 2 + 2].load(memory_order_acquire);
+               return res;
        }
 
        /**
@@ -524,7 +575,7 @@ friend class CHM;
        */
        // Preserve happens-before semantics on newly inserted values
        static inline slot* val(kvs_data *kvs, int idx) {
-               assert (idx >= 0 && idx < kvs->_size);
+               MODEL_ASSERT (idx >= 0 && idx < kvs->_size);
                // Corresponding to the volatile read in get_impl() and putIfMatch in
                // Cliff Click's Java implementation
                slot *res = (slot*) kvs->_data[idx * 2 + 3].load(memory_order_acquire);
@@ -542,7 +593,7 @@ friend class CHM;
        }
 
        static int hash(slot *key_slot) {
-               assert(key_slot != NULL && key_slot->_ptr != NULL);
+               MODEL_ASSERT(key_slot != NULL && key_slot->_ptr != NULL);
                TypeK* key = (TypeK*) key_slot->_ptr;
                int h = key->hashCode();
                // Spread bits according to Cliff Click's code
@@ -570,7 +621,7 @@ friend class CHM;
        static bool keyeq(slot *K, slot *key_slot, int *hashes, int hash,
                int fullhash) {
                // Caller should've checked this.
-               assert (K != NULL);
+               MODEL_ASSERT (K != NULL);
                TypeK* key_ptr = (TypeK*) key_slot->_ptr;
                return
                        K == key_slot ||
@@ -580,7 +631,7 @@ friend class CHM;
        }
 
        static bool valeq(slot *val_slot1, slot *val_slot2) {
-               assert (val_slot1 != NULL);
+               MODEL_ASSERT (val_slot1 != NULL);
                TypeK* ptr1 = (TypeV*) val_slot1->_ptr;
                if (val_slot2 == NULL || ptr1 == NULL) return false;
                return ptr1->equals(val_slot2->_ptr);
@@ -607,7 +658,7 @@ friend class CHM;
                        # If it is a successful put instead of a copy or any other internal
                        # operantions, expected != NULL
                        @Begin
-                       @Potential_commit_point_define: __ATOMIC_RET__ == true
+                       @Potential_commit_point_define: res == true
                        @Label: Write_Val_Point
                        @End
                */
@@ -633,7 +684,7 @@ friend class CHM;
                                @End
                        */
 
-                       if (V == NULL) return NULL; // A miss
+                       if (K == NULL) return NULL; // A miss
                        
                        if (keyeq(K, key_slot, hashes, idx, fullhash)) {
                                // Key hit! Check if table-resize in progress
@@ -672,21 +723,19 @@ friend class CHM;
        }
 
        // A wrapper of the essential function putIfMatch()
-       TypeV* putIfMatch(TypeK& key, TypeV& value, slot *old_val) {
+       TypeV* putIfMatch(TypeK *key, TypeV *value, slot *old_val) {
                // TODO: Should throw an exception rather return NULL
                if (old_val == NULL) {
                        return NULL;
                }
-               void *key_ptr = (void*) new TypeK(key);
-               slot *key_slot = new slot(false, key_ptr);
+               slot *key_slot = new slot(false, key);
 
-               void *val_ptr = (void*) new TypeV(value);
-               slot *value_slot = new slot(false, val_ptr);
+               slot *value_slot = new slot(false, value);
                kvs_data *kvs = _kvs.load(memory_order_acquire);
                slot *res = putIfMatch(this, kvs, key_slot, value_slot, old_val);
                // Only when copy_slot() call putIfMatch() will it return NULL
-               assert (res != NULL); 
-               assert (!is_prime(res));
+               MODEL_ASSERT (res != NULL); 
+               MODEL_ASSERT (!is_prime(res));
                return res == TOMBSTONE ? NULL : (TypeV*) res->_ptr;
        }
 
@@ -699,9 +748,9 @@ friend class CHM;
        */
        static slot* putIfMatch(cliffc_hashtable *topmap, kvs_data *kvs, slot
                *key_slot, slot *val_slot, slot *expVal) {
-               assert (val_slot != NULL);
-               assert (!is_prime(val_slot));
-               assert (!is_prime(expVal));
+               MODEL_ASSERT (val_slot != NULL);
+               MODEL_ASSERT (!is_prime(val_slot));
+               MODEL_ASSERT (!is_prime(expVal));
 
                int fullhash = hash(key_slot);
                int len = kvs->_size;
@@ -727,7 +776,7 @@ friend class CHM;
                                        break;
                                }
                                K = key(kvs, idx); // CAS failed, get updated value
-                               assert (K != NULL);
+                               MODEL_ASSERT (K != NULL);
                        }
 
                        // Key slot not null, there exists a Key here
@@ -764,7 +813,7 @@ friend class CHM;
                
                // Decided to update the existing table
                while (true) {
-                       assert (!is_prime(V));
+                       MODEL_ASSERT (!is_prime(V));
 
                        if (expVal != NO_MATCH_OLD &&
                                V != expVal &&
@@ -837,5 +886,10 @@ friend class CHM;
                return helper;
        }
 };
+/**
+       @Begin
+       @Class_end
+       @End
+*/
 
 #endif