| … | … | @@ -10,10 +10,8 @@ |
| 10 | 10 | |
| 11 | 11 | // Create random keys/values in the range [0, kEmpty) |
| 12 | 12 | // kEmpty is used to indicate an empty slot |
| 13 | | std::vector<KeyValue> generate_random_keyvalues(uint32_t numkvs) |
| 13 | std::vector<KeyValue> generate_random_keyvalues(std::mt19937& rnd, uint32_t numkvs) |
| 14 | 14 | { |
| 15 | | std::random_device rd; |
| 16 | | std::mt19937 gen(rd()); // mersenne_twister_engine |
| 17 | 15 | std::uniform_int_distribution<uint32_t> dis(0, kEmpty - 1); |
| 18 | 16 | |
| 19 | 17 | std::vector<KeyValue> kvs; |
| … | … | @@ -21,8 +19,8 @@ std::vector<KeyValue> generate_random_keyvalues(uint32_t numkvs) |
| 21 | 19 | |
| 22 | 20 | for (uint32_t i = 0; i < numkvs; i++) |
| 23 | 21 | { |
| 24 | | uint32_t rand0 = dis(gen); |
| 25 | | uint32_t rand1 = dis(gen); |
| 22 | uint32_t rand0 = dis(rnd); |
| 23 | uint32_t rand1 = dis(rnd); |
| 26 | 24 | kvs.push_back(KeyValue{rand0, rand1}); |
| 27 | 25 | } |
| 28 | 26 | |
| … | … | @@ -30,9 +28,9 @@ std::vector<KeyValue> generate_random_keyvalues(uint32_t numkvs) |
| 30 | 28 | } |
| 31 | 29 | |
| 32 | 30 | // return numshuffledkvs random items from kvs |
| 33 | | std::vector<KeyValue> shuffle_keyvalues(std::vector<KeyValue> kvs, uint32_t numshuffledkvs) |
| 31 | std::vector<KeyValue> shuffle_keyvalues(std::mt19937& rnd, std::vector<KeyValue> kvs, uint32_t numshuffledkvs) |
| 34 | 32 | { |
| 35 | | std::random_shuffle(kvs.begin(), kvs.end()); |
| 33 | std::shuffle(kvs.begin(), kvs.end(), rnd); |
| 36 | 34 | |
| 37 | 35 | std::vector<KeyValue> shuffled_kvs; |
| 38 | 36 | shuffled_kvs.resize(numshuffledkvs); |
| … | … | @@ -88,16 +86,25 @@ void test_correctness(std::vector<KeyValue>, std::vector<KeyValue>, std::vector< |
| 88 | 86 | |
| 89 | 87 | int main() |
| 90 | 88 | { |
| 89 | // To recreate the same random numbers across runs of the program, set seed to a specific |
| 90 | // number instead of a number from random_device |
| 91 | std::random_device rd; |
| 92 | uint32_t seed = rd(); |
| 93 | std::mt19937 rnd(seed); // mersenne_twister_engine |
| 94 | |
| 95 | printf("Random number generator seed = %u\n", seed); |
| 96 | |
| 91 | 97 | while (true) |
| 92 | 98 | { |
| 93 | | // Initialize keyvalue pairs with random numbers |
| 94 | | std::vector<KeyValue> insert_kvs = generate_random_keyvalues(kNumKeyValues); |
| 95 | | std::vector<KeyValue> delete_kvs = shuffle_keyvalues(insert_kvs, kNumKeyValues / 2); |
| 99 | printf("Initializing keyvalue pairs with random numbers...\n"); |
| 96 | 100 | |
| 101 | std::vector<KeyValue> insert_kvs = generate_random_keyvalues(rnd, kNumKeyValues); |
| 102 | std::vector<KeyValue> delete_kvs = shuffle_keyvalues(rnd, insert_kvs, kNumKeyValues / 2); |
| 103 | |
| 104 | // Begin test |
| 97 | 105 | printf("Testing insertion/deletion of %d/%d elements into GPU hash table...\n", |
| 98 | 106 | (uint32_t)insert_kvs.size(), (uint32_t)delete_kvs.size()); |
| 99 | 107 | |
| 100 | | // Begin test |
| 101 | 108 | Time timer = start_timer(); |
| 102 | 109 | |
| 103 | 110 | KeyValue* pHashTable = create_hashtable(kHashTableCapacity); |