| … | … | @@ -8,164 +8,70 @@ |
| 8 | 8 | #include "random" |
| 9 | 9 | #include "linearprobing.h" |
| 10 | 10 | |
| 11 | | std::vector<KeyValue> generate_random_keyvalues() |
| 11 | void test_correctness(std::vector<KeyValue> all_kvs, std::vector<KeyValue> kvs) |
| 12 | 12 | { |
| 13 | | std::random_device rd; |
| 14 | | std::mt19937 gen(rd()); // mersenne_twister_engine |
| 15 | | std::uniform_int_distribution<uint32_t> dis(1, 0xffffffff); // 0 is reserved for empty keys |
| 16 | | |
| 17 | | std::vector<KeyValue> kvs; |
| 18 | | kvs.reserve(kNumKeyValues); |
| 19 | | |
| 20 | | for (int i = 0; i < kNumKeyValues; i++) |
| 13 | printf("Testing that there are no duplicate keys...\n"); |
| 14 | std::unordered_set<uint32_t> unique_keys; |
| 15 | for (uint32_t i = 0; i < kvs.size(); i++) |
| 21 | 16 | { |
| 22 | | uint32_t rand0 = dis(gen); |
| 23 | | uint32_t rand1 = dis(gen); |
| 24 | | kvs.push_back(KeyValue{ rand0, rand1 }); |
| 25 | | } |
| 26 | | |
| 27 | | return kvs; |
| 28 | | } |
| 29 | | |
| 30 | | LARGE_INTEGER start_timer() |
| 31 | | { |
| 32 | | LARGE_INTEGER timer; |
| 33 | | QueryPerformanceCounter(&timer); |
| 34 | | return timer; |
| 35 | | } |
| 36 | | |
| 37 | | double get_elapsed_time(LARGE_INTEGER start) |
| 38 | | { |
| 39 | | LARGE_INTEGER end; |
| 40 | | QueryPerformanceCounter(&end); |
| 41 | | |
| 42 | | LARGE_INTEGER frequency; |
| 43 | | QueryPerformanceFrequency(&frequency); |
| 44 | | |
| 45 | | LARGE_INTEGER elapsed; |
| 46 | | elapsed.QuadPart = end.QuadPart - start.QuadPart; |
| 47 | | elapsed.QuadPart *= 1000000; |
| 48 | | double milliseconds = elapsed.QuadPart / (double)frequency.QuadPart / 1000; |
| 17 | if (i % 10000000 == 0) |
| 18 | printf(" Verifying %d/%d\n", i, (uint32_t)kvs.size()); |
| 49 | 19 | |
| 50 | | return milliseconds; |
| 51 | | } |
| 52 | | |
| 53 | | void test_unordered_map(std::vector<KeyValue> all_kvs) |
| 54 | | { |
| 55 | | LARGE_INTEGER timer = start_timer(); |
| 56 | | |
| 57 | | std::unordered_map<uint32_t, uint32_t> kvs_map; |
| 58 | | for (auto& kv : all_kvs) |
| 59 | | { |
| 60 | | kvs_map[kv.key] = kv.value; |
| 20 | KeyValue* node = &kvs[i]; |
| 21 | if (unique_keys.find(node->key) != unique_keys.end()) |
| 22 | { |
| 23 | printf("Duplicate key found in GPU hash table at slot %d\n", i); |
| 24 | exit(-1); |
| 25 | } |
| 26 | unique_keys.insert(node->key); |
| 61 | 27 | } |
| 62 | 28 | |
| 63 | | double milliseconds = get_elapsed_time(timer); |
| 64 | | double seconds = milliseconds / 1000.0f; |
| 65 | | printf("std::unordered_map time : %f ms (%f million keys/second)\n", milliseconds, kNumKeyValues / seconds / 1000000.0f); |
| 66 | | } |
| 67 | | |
| 68 | | int main() |
| 69 | | { |
| 70 | | printf("Testing insertion of %d elements\n", kNumKeyValues); |
| 71 | | |
| 72 | | // Initialize keyvalue pairs with random numbers |
| 73 | | std::vector<KeyValue> all_kvs = generate_random_keyvalues(); |
| 74 | | |
| 75 | | // Begin test |
| 76 | | LARGE_INTEGER timer = start_timer(); |
| 77 | | |
| 78 | | KeyValue* pHashTable = create_hashtable(kHashTableCapacity); |
| 79 | | |
| 80 | | // Insert all the elements; each batch is processed concurrently on the GPU |
| 81 | | const uint32_t num_batch_insertions = 16; |
| 82 | | static_assert(kNumKeyValues % num_batch_insertions == 0, "needs even divisor"); |
| 83 | | uint32_t num_kvs_in_batch = kNumKeyValues / num_batch_insertions; |
| 84 | | for (int i = 0; i < num_batch_insertions; i++) |
| 29 | printf("Building unordered_map from insertion list...\n"); |
| 30 | std::unordered_map<uint32_t, std::vector<uint32_t>> all_kvs_map; |
| 31 | for (int i = 0; i < all_kvs.size(); i++) |
| 85 | 32 | { |
| 86 | | insert_hashtable(pHashTable, all_kvs.data() + i*num_kvs_in_batch, num_kvs_in_batch); |
| 87 | | } |
| 88 | | |
| 89 | | // Get all the key-values from the hash table |
| 90 | | std::vector<KeyValue> kvs = iterate_hashtable(pHashTable); |
| 33 | if (i % 10000000 == 0) |
| 34 | printf(" Building %d/%d\n", i, (uint32_t)all_kvs.size()); |
| 91 | 35 | |
| 92 | | destroy_hashtable(pHashTable); |
| 93 | | |
| 94 | | // Summarize results |
| 95 | | double milliseconds = get_elapsed_time(timer); |
| 96 | | double seconds = milliseconds / 1000.0f; |
| 97 | | printf("Total time (including memory copies, readback, etc.): %f ms (%f million keys/second)\n", milliseconds, kNumKeyValues / seconds / 1000000.0f); |
| 98 | | |
| 99 | | test_unordered_map(all_kvs); |
| 100 | | |
| 101 | | // Test the results |
| 102 | | if (true) |
| 103 | | { |
| 104 | | printf("Testing that there are no duplicate keys...\n"); |
| 105 | | std::unordered_set<uint32_t> unique_keys; |
| 106 | | for (uint32_t i = 0; i < kvs.size(); i++) |
| 36 | auto iter = all_kvs_map.find(all_kvs[i].key); |
| 37 | if (iter == all_kvs_map.end()) |
| 107 | 38 | { |
| 108 | | if (i % 10000000 == 0) |
| 109 | | printf(" Verifying %d/%d\n", i, (uint32_t)kvs.size()); |
| 110 | | |
| 111 | | KeyValue* node = &kvs[i]; |
| 112 | | if (unique_keys.find(node->key) != unique_keys.end()) |
| 113 | | { |
| 114 | | printf("Duplicate key found in GPU hash table at slot %d\n", i); |
| 115 | | exit(-1); |
| 116 | | } |
| 117 | | unique_keys.insert(node->key); |
| 39 | all_kvs_map[all_kvs[i].key] = std::vector<uint32_t>({ all_kvs[i].value }); |
| 118 | 40 | } |
| 119 | | |
| 120 | | printf("Building unordered_map from insertion list...\n"); |
| 121 | | std::unordered_map<uint32_t, std::vector<uint32_t>> all_kvs_map; |
| 122 | | for (int i = 0; i < all_kvs.size(); i++) |
| 41 | else |
| 123 | 42 | { |
| 124 | | if (i % 10000000 == 0) |
| 125 | | printf(" Building %d/%d\n", i, (uint32_t)all_kvs.size()); |
| 126 | | |
| 127 | | auto iter = all_kvs_map.find(all_kvs[i].key); |
| 128 | | if (iter == all_kvs_map.end()) |
| 129 | | { |
| 130 | | all_kvs_map[all_kvs[i].key] = std::vector<uint32_t>({ all_kvs[i].value }); |
| 131 | | } |
| 132 | | else |
| 133 | | { |
| 134 | | iter->second.push_back(all_kvs[i].value); |
| 135 | | } |
| 43 | iter->second.push_back(all_kvs[i].value); |
| 136 | 44 | } |
| 45 | } |
| 46 | |
| 47 | if (unique_keys.size() != all_kvs_map.size()) |
| 48 | { |
| 49 | printf("# of unique keys in hashtable is incorrect\n"); |
| 50 | exit(-1); |
| 51 | } |
| 137 | 52 | |
| 138 | | if (unique_keys.size() != all_kvs_map.size()) |
| 53 | printf("Testing that each key/value in hashtable is in the insertion list...\n"); |
| 54 | for (uint32_t i = 0; i < kvs.size(); i++) |
| 55 | { |
| 56 | if (i % 10000000 == 0) |
| 57 | printf(" Verifying %d/%d\n", i, (uint32_t)kvs.size()); |
| 58 | |
| 59 | auto iter = all_kvs_map.find(kvs[i].key); |
| 60 | if (iter == all_kvs_map.end()) |
| 139 | 61 | { |
| 140 | | printf("# of unique keys in hashtable is incorrect\n"); |
| 62 | printf("Hashtable key not found in insertion list\n"); |
| 141 | 63 | exit(-1); |
| 142 | 64 | } |
| 143 | 65 | |
| 144 | | printf("Testing that each key/value in hashtable is in the insertion list...\n"); |
| 145 | | for (uint32_t i = 0; i < kvs.size(); i++) |
| 66 | std::vector<uint32_t>& values = iter->second; |
| 67 | if (std::find(values.begin(), values.end(), kvs[i].value) == values.end()) |
| 146 | 68 | { |
| 147 | | if (i % 10000000 == 0) |
| 148 | | printf(" Verifying %d/%d\n", i, (uint32_t)kvs.size()); |
| 149 | | |
| 150 | | auto iter = all_kvs_map.find(kvs[i].key); |
| 151 | | if (iter == all_kvs_map.end()) |
| 152 | | { |
| 153 | | printf("Hashtable key not found in insertion list\n"); |
| 154 | | exit(-1); |
| 155 | | } |
| 156 | | |
| 157 | | std::vector<uint32_t>& values = iter->second; |
| 158 | | if (std::find(values.begin(), values.end(), kvs[i].value) == values.end()) |
| 159 | | { |
| 160 | | printf("Hashtable value not found in insertion list\n"); |
| 161 | | exit(-1); |
| 162 | | } |
| 69 | printf("Hashtable value not found in insertion list\n"); |
| 70 | exit(-1); |
| 163 | 71 | } |
| 164 | | |
| 165 | | printf("Deleting std::unordered_map and std::unique_set...\n"); |
| 166 | 72 | } |
| 167 | 73 | |
| 168 | | printf("Success\n"); |
| 74 | printf("Deleting std::unordered_map and std::unique_set...\n"); |
| 169 | 75 | |
| 170 | | return 0; |
| 76 | return; |
| 171 | 77 | } |