From 36c9fccd542577bded8d8524f2ba9c66c54336c9 Mon Sep 17 00:00:00 2001 From: Suryansh Gupta Date: Wed, 11 Dec 2024 15:18:35 +0530 Subject: [PATCH 1/9] Add single threaded benchmark_disk_read script --- apps/CMakeLists.txt | 4 +++ apps/benchmark_reads.cpp | 53 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 apps/benchmark_reads.cpp diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index e42c0b6cb..5d2c8faaa 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -28,6 +28,9 @@ target_link_libraries(test_streaming_scenario ${PROJECT_NAME} ${DISKANN_TOOLS_TC add_executable(test_insert_deletes_consolidate test_insert_deletes_consolidate.cpp) target_link_libraries(test_insert_deletes_consolidate ${PROJECT_NAME} ${DISKANN_TOOLS_TCMALLOC_LINK_OPTIONS} Boost::program_options) +add_executable(benchmark_reads benchmark_reads.cpp) +target_link_libraries(benchmark_reads ${PROJECT_NAME} ${DISKANN_TOOLS_TCMALLOC_LINK_OPTIONS} Boost::program_options) + if (NOT MSVC) install(TARGETS build_memory_index build_stitched_index @@ -37,6 +40,7 @@ if (NOT MSVC) range_search_disk_index test_streaming_scenario test_insert_deletes_consolidate + benchmark_reads RUNTIME ) endif() diff --git a/apps/benchmark_reads.cpp b/apps/benchmark_reads.cpp new file mode 100644 index 000000000..a66218375 --- /dev/null +++ b/apps/benchmark_reads.cpp @@ -0,0 +1,53 @@ +#include +#include +#include "windows_aligned_file_reader.h" +#include "aligned_file_reader.h" +#include "utils.h" + +using namespace std; +using namespace diskann; + +#define IS_ALIGNED(X, Y) ((uint64_t)(X) % (uint64_t)(Y) == 0) + +#define SECTOR_LEN 4096 + +void do_reads() +{ + string file_name = "C:\\DiskANN\\Data\\turning_100k\\index_disk.index"; + auto reader = new WindowsAlignedFileReader(); + reader->open(file_name.c_str()); + auto ctx = reader->get_ctx(); + + std::vector read_reqs; + int num_sectors = 100; + + char *buf = nullptr; + alloc_aligned((void **)&buf, num_sectors * SECTOR_LEN, SECTOR_LEN); + + // create read requests + for (size_t i = 0; i < num_sectors; ++i) + { + AlignedRead read; + read.len = SECTOR_LEN; + read.buf = buf + i * SECTOR_LEN; + auto sector_id = (rand() % 10000); + read.offset = sector_id * SECTOR_LEN; + if (read.offset) + read_reqs.push_back(read); + } + + auto s = std::chrono::high_resolution_clock::now(); + for (int i = 0; i < 10000; i++) + { + reader->read(read_reqs, ctx, false); + } + auto e = std::chrono::high_resolution_clock::now(); + std::chrono::duration diff = e - s; + cout << "Time taken to read: " << diff.count() << endl; +} + +int main() +{ + cout << "Hello World" << endl; + do_reads(); +} \ No newline at end of file From b501c2d13df80ada3574022553628b4a8255e7b6 Mon Sep 17 00:00:00 2001 From: Suryansh Gupta Date: Wed, 11 Dec 2024 18:56:43 +0530 Subject: [PATCH 2/9] Convert benchmark_reads to multithreaded --- apps/benchmark_reads.cpp | 65 ++++++++++++++++++++++------- src/windows_aligned_file_reader.cpp | 2 +- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/apps/benchmark_reads.cpp b/apps/benchmark_reads.cpp index a66218375..8a3a5f227 100644 --- a/apps/benchmark_reads.cpp +++ b/apps/benchmark_reads.cpp @@ -3,27 +3,22 @@ #include "windows_aligned_file_reader.h" #include "aligned_file_reader.h" #include "utils.h" +#include "timer.h" +#include +#include using namespace std; using namespace diskann; -#define IS_ALIGNED(X, Y) ((uint64_t)(X) % (uint64_t)(Y) == 0) - #define SECTOR_LEN 4096 -void do_reads() +void do_reads(WindowsAlignedFileReader *reader, char *buf) { - string file_name = "C:\\DiskANN\\Data\\turning_100k\\index_disk.index"; - auto reader = new WindowsAlignedFileReader(); - reader->open(file_name.c_str()); auto ctx = reader->get_ctx(); std::vector read_reqs; int num_sectors = 100; - char *buf = nullptr; - alloc_aligned((void **)&buf, num_sectors * SECTOR_LEN, SECTOR_LEN); - // create read requests for (size_t i = 0; i < num_sectors; ++i) { @@ -36,18 +31,56 @@ void do_reads() read_reqs.push_back(read); } - auto s = std::chrono::high_resolution_clock::now(); + reader->read(read_reqs, ctx, false); +} + +void do_multiple_reads_with_threads(int thread_count) +{ + string file_name = "C:\\DiskANN\\Data\\turning_100k\\index_disk.index"; + auto reader = new WindowsAlignedFileReader(); + reader->open(file_name.c_str()); + int num_sectors = 100; + + ConcurrentQueue buffer_pool; + + omp_set_num_threads(thread_count); + +#pragma omp parallel for num_threads((int)thread_count) + for (int i = 0; i < thread_count; i++) + { + char *buf = nullptr; + alloc_aligned((void **)&buf, num_sectors * SECTOR_LEN, SECTOR_LEN); + buffer_pool.push(buf); + reader->register_thread(); + } + + Timer timer; +#pragma omp parallel for schedule(dynamic, 1) for (int i = 0; i < 10000; i++) { - reader->read(read_reqs, ctx, false); + char *buf = buffer_pool.pop(); + do_reads(reader, buf); + buffer_pool.push(buf); } - auto e = std::chrono::high_resolution_clock::now(); - std::chrono::duration diff = e - s; - cout << "Time taken to read: " << diff.count() << endl; + cout << "Time taken to read in microseconds: " << timer.elapsed() << endl; + + reader->close(); } -int main() +int main(int argc, char *argv[]) { + int val = 1; + if (argc >= 2) + { + std::istringstream iss( argv[1] ); + + if (iss >> val) + { + cout<<"Got cmd argument"<register_thread(); + // this->register_thread(); } void WindowsAlignedFileReader::close() From e89b93cfc1a84a2a9317d637abb2cd23cef52bff Mon Sep 17 00:00:00 2001 From: Suryansh Gupta Date: Wed, 11 Dec 2024 19:27:47 +0530 Subject: [PATCH 3/9] Remove concurent queue and use vector --- apps/benchmark_reads.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/apps/benchmark_reads.cpp b/apps/benchmark_reads.cpp index 8a3a5f227..aa376e9cb 100644 --- a/apps/benchmark_reads.cpp +++ b/apps/benchmark_reads.cpp @@ -5,7 +5,6 @@ #include "utils.h" #include "timer.h" #include -#include using namespace std; using namespace diskann; @@ -41,7 +40,7 @@ void do_multiple_reads_with_threads(int thread_count) reader->open(file_name.c_str()); int num_sectors = 100; - ConcurrentQueue buffer_pool; + vector buffers(thread_count); omp_set_num_threads(thread_count); @@ -50,7 +49,7 @@ void do_multiple_reads_with_threads(int thread_count) { char *buf = nullptr; alloc_aligned((void **)&buf, num_sectors * SECTOR_LEN, SECTOR_LEN); - buffer_pool.push(buf); + buffers[i] = buf; reader->register_thread(); } @@ -58,9 +57,8 @@ void do_multiple_reads_with_threads(int thread_count) #pragma omp parallel for schedule(dynamic, 1) for (int i = 0; i < 10000; i++) { - char *buf = buffer_pool.pop(); + char *buf = buffers[omp_get_thread_num()]; do_reads(reader, buf); - buffer_pool.push(buf); } cout << "Time taken to read in microseconds: " << timer.elapsed() << endl; @@ -72,14 +70,14 @@ int main(int argc, char *argv[]) int val = 1; if (argc >= 2) { - std::istringstream iss( argv[1] ); + std::istringstream iss(argv[1]); if (iss >> val) { - cout<<"Got cmd argument"< Date: Mon, 27 Jan 2025 20:35:40 +0530 Subject: [PATCH 4/9] Align with Rust --- apps/benchmark_reads.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/apps/benchmark_reads.cpp b/apps/benchmark_reads.cpp index aa376e9cb..be6280fb0 100644 --- a/apps/benchmark_reads.cpp +++ b/apps/benchmark_reads.cpp @@ -11,20 +11,20 @@ using namespace diskann; #define SECTOR_LEN 4096 -void do_reads(WindowsAlignedFileReader *reader, char *buf) +void do_reads(WindowsAlignedFileReader *reader, char *buf, int batches_of) { auto ctx = reader->get_ctx(); std::vector read_reqs; - int num_sectors = 100; + read_reqs.reserve(batches_of); // create read requests - for (size_t i = 0; i < num_sectors; ++i) + for (size_t i = 0; i < batches_of; ++i) { AlignedRead read; read.len = SECTOR_LEN; read.buf = buf + i * SECTOR_LEN; - auto sector_id = (rand() % 10000); + auto sector_id = (rand() % 1650000); read.offset = sector_id * SECTOR_LEN; if (read.offset) read_reqs.push_back(read); @@ -35,10 +35,10 @@ void do_reads(WindowsAlignedFileReader *reader, char *buf) void do_multiple_reads_with_threads(int thread_count) { - string file_name = "C:\\DiskANN\\Data\\turning_100k\\index_disk.index"; + string file_name = "F:\\indices\\turing_10m\\disk_index_disk.index"; auto reader = new WindowsAlignedFileReader(); reader->open(file_name.c_str()); - int num_sectors = 100; + int batches_of = 100; vector buffers(thread_count); @@ -48,19 +48,21 @@ void do_multiple_reads_with_threads(int thread_count) for (int i = 0; i < thread_count; i++) { char *buf = nullptr; - alloc_aligned((void **)&buf, num_sectors * SECTOR_LEN, SECTOR_LEN); + alloc_aligned((void **)&buf, batches_of * SECTOR_LEN, SECTOR_LEN); buffers[i] = buf; reader->register_thread(); } + int no_of_reads = 10000; Timer timer; #pragma omp parallel for schedule(dynamic, 1) - for (int i = 0; i < 10000; i++) + for (int i = 0; i < no_of_reads; i++) { char *buf = buffers[omp_get_thread_num()]; - do_reads(reader, buf); + do_reads(reader, buf, batches_of); } - cout << "Time taken to read in microseconds: " << timer.elapsed() << endl; + // cout << "Time taken to read in microseconds: " << timer.elapsed() << endl; + cout<close(); } @@ -74,11 +76,11 @@ int main(int argc, char *argv[]) if (iss >> val) { - cout << "Got cmd argument" << endl; + // cout << "Got cmd argument" << endl; } } - cout << "Using " << val << " threads." << endl; + // cout << "Using " << val << " threads." << endl; - cout << "Hello World" << endl; + // cout << "Hello World" << endl; do_multiple_reads_with_threads(val); } \ No newline at end of file From fe76ff14e52ec0225d2f9cb36387d379d489ff97 Mon Sep 17 00:00:00 2001 From: Suryansh Gupta Date: Thu, 30 Jan 2025 00:25:47 +0530 Subject: [PATCH 5/9] Change to read in batch of 5 --- apps/benchmark_reads.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/apps/benchmark_reads.cpp b/apps/benchmark_reads.cpp index be6280fb0..79c755dd3 100644 --- a/apps/benchmark_reads.cpp +++ b/apps/benchmark_reads.cpp @@ -38,13 +38,14 @@ void do_multiple_reads_with_threads(int thread_count) string file_name = "F:\\indices\\turing_10m\\disk_index_disk.index"; auto reader = new WindowsAlignedFileReader(); reader->open(file_name.c_str()); - int batches_of = 100; + int total_reads = 1000000; + int batches_of = 5; vector buffers(thread_count); - omp_set_num_threads(thread_count); + // omp_set_num_threads(thread_count); -#pragma omp parallel for num_threads((int)thread_count) +// #pragma omp parallel for num_threads((int)thread_count) for (int i = 0; i < thread_count; i++) { char *buf = nullptr; @@ -53,9 +54,9 @@ void do_multiple_reads_with_threads(int thread_count) reader->register_thread(); } - int no_of_reads = 10000; + int no_of_reads = total_reads / batches_of; Timer timer; -#pragma omp parallel for schedule(dynamic, 1) +// #pragma omp parallel for schedule(dynamic, 1) for (int i = 0; i < no_of_reads; i++) { char *buf = buffers[omp_get_thread_num()]; From 4369c76fdf7bbc9ccb7c534141c50beafccc9231 Mon Sep 17 00:00:00 2001 From: Suryansh Gupta Date: Thu, 30 Jan 2025 17:10:44 +0530 Subject: [PATCH 6/9] Add single threaded benchmarking script --- apps/CMakeLists.txt | 3 + apps/benchmark_reads.cpp | 6 +- apps/benchmark_reads_single_threaded.cpp | 73 ++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 apps/benchmark_reads_single_threaded.cpp diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 5d2c8faaa..736460246 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -31,6 +31,9 @@ target_link_libraries(test_insert_deletes_consolidate ${PROJECT_NAME} ${DISKANN_ add_executable(benchmark_reads benchmark_reads.cpp) target_link_libraries(benchmark_reads ${PROJECT_NAME} ${DISKANN_TOOLS_TCMALLOC_LINK_OPTIONS} Boost::program_options) +add_executable(benchmark_reads_single_threaded benchmark_reads_single_threaded.cpp) +target_link_libraries(benchmark_reads_single_threaded ${PROJECT_NAME} ${DISKANN_TOOLS_TCMALLOC_LINK_OPTIONS} Boost::program_options) + if (NOT MSVC) install(TARGETS build_memory_index build_stitched_index diff --git a/apps/benchmark_reads.cpp b/apps/benchmark_reads.cpp index 79c755dd3..1b05c2b47 100644 --- a/apps/benchmark_reads.cpp +++ b/apps/benchmark_reads.cpp @@ -43,9 +43,9 @@ void do_multiple_reads_with_threads(int thread_count) vector buffers(thread_count); - // omp_set_num_threads(thread_count); + omp_set_num_threads(thread_count); -// #pragma omp parallel for num_threads((int)thread_count) +#pragma omp parallel for num_threads((int)thread_count) for (int i = 0; i < thread_count; i++) { char *buf = nullptr; @@ -56,7 +56,7 @@ void do_multiple_reads_with_threads(int thread_count) int no_of_reads = total_reads / batches_of; Timer timer; -// #pragma omp parallel for schedule(dynamic, 1) +#pragma omp parallel for schedule(dynamic, 1) for (int i = 0; i < no_of_reads; i++) { char *buf = buffers[omp_get_thread_num()]; diff --git a/apps/benchmark_reads_single_threaded.cpp b/apps/benchmark_reads_single_threaded.cpp new file mode 100644 index 000000000..9c1b9e70f --- /dev/null +++ b/apps/benchmark_reads_single_threaded.cpp @@ -0,0 +1,73 @@ +#include +#include +#include "windows_aligned_file_reader.h" +#include "aligned_file_reader.h" +#include "utils.h" +#include "timer.h" +#include + +using namespace std; +using namespace diskann; + +#define SECTOR_LEN 4096 +#define TOTAL_READS 1000000 + +void do_reads(WindowsAlignedFileReader *reader, char *buf, int batches_of) +{ + auto ctx = reader->get_ctx(); + + std::vector read_reqs; + read_reqs.reserve(batches_of); + + // create read requests + for (size_t i = 0; i < batches_of; ++i) + { + AlignedRead read; + read.len = SECTOR_LEN; + read.buf = buf + i * SECTOR_LEN; + auto sector_id = (rand() % 1650000); + read.offset = sector_id * SECTOR_LEN; + read_reqs.push_back(read); + } + + reader->read(read_reqs, ctx, false); +} + +void do_reads_in_batches_of(int batches_of) +{ + string file_name = "F:\\indices\\turing_10m\\disk_index_disk.index"; + auto reader = new WindowsAlignedFileReader(); + reader->open(file_name.c_str()); + char *buf = nullptr; + alloc_aligned((void **)&buf, batches_of * SECTOR_LEN, SECTOR_LEN); + reader->register_thread(); + + int no_of_reads = TOTAL_READS / batches_of; + Timer timer; + for (int i = 0; i < no_of_reads; i++) + { + do_reads(reader, buf, batches_of); + } + // cout << "Time taken to read in microseconds: " << timer.elapsed() << endl; + cout<close(); +} + +int main(int argc, char *argv[]) +{ + int val = 10; + if (argc >= 2) + { + std::istringstream iss(argv[1]); + + if (iss >> val) + { + // cout << "Got cmd argument" << endl; + } + } + cout << "Using batches of " << val << endl; + + // cout << "Hello World" << endl; + do_reads_in_batches_of(val); +} \ No newline at end of file From 3b3a4a817ad7029e831948f516c09172952c2bd5 Mon Sep 17 00:00:00 2001 From: Suryansh Gupta Date: Thu, 30 Jan 2025 19:35:17 +0530 Subject: [PATCH 7/9] Add benchmark_reads_single_threaded.cpp --- apps/benchmark_reads_single_threaded.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/benchmark_reads_single_threaded.cpp b/apps/benchmark_reads_single_threaded.cpp index 9c1b9e70f..b5c7d9697 100644 --- a/apps/benchmark_reads_single_threaded.cpp +++ b/apps/benchmark_reads_single_threaded.cpp @@ -66,7 +66,7 @@ int main(int argc, char *argv[]) // cout << "Got cmd argument" << endl; } } - cout << "Using batches of " << val << endl; + // cout << "Using batches of " << val << endl; // cout << "Hello World" << endl; do_reads_in_batches_of(val); From 63eceefd2e7a9d7423f739ec6be2c20279c41501 Mon Sep 17 00:00:00 2001 From: Suryansh Gupta Date: Fri, 31 Jan 2025 22:59:25 +0530 Subject: [PATCH 8/9] Remove some allocations from the timer --- apps/benchmark_reads.cpp | 75 ++++++++++++++---------- apps/benchmark_reads_single_threaded.cpp | 46 +++++++++------ 2 files changed, 72 insertions(+), 49 deletions(-) diff --git a/apps/benchmark_reads.cpp b/apps/benchmark_reads.cpp index 1b05c2b47..cb18e9ace 100644 --- a/apps/benchmark_reads.cpp +++ b/apps/benchmark_reads.cpp @@ -10,78 +10,89 @@ using namespace std; using namespace diskann; #define SECTOR_LEN 4096 +#define TOTAL_READS 1000000 -void do_reads(WindowsAlignedFileReader *reader, char *buf, int batches_of) +void do_reads(WindowsAlignedFileReader* reader, vector& read_reqs, uniform_int_distribution<>& distrib) { auto ctx = reader->get_ctx(); + random_device rd; + mt19937 gen(rd()); - std::vector read_reqs; - read_reqs.reserve(batches_of); - - // create read requests - for (size_t i = 0; i < batches_of; ++i) + // Modify read requests + for (auto& read_req : read_reqs) { - AlignedRead read; - read.len = SECTOR_LEN; - read.buf = buf + i * SECTOR_LEN; - auto sector_id = (rand() % 1650000); - read.offset = sector_id * SECTOR_LEN; - if (read.offset) - read_reqs.push_back(read); + long long int sector_id = distrib(gen); + read_req.offset = sector_id * SECTOR_LEN; } reader->read(read_reqs, ctx, false); } -void do_multiple_reads_with_threads(int thread_count) +void do_multiple_reads_with_threads(int thread_count, int batches_of) { string file_name = "F:\\indices\\turing_10m\\disk_index_disk.index"; auto reader = new WindowsAlignedFileReader(); reader->open(file_name.c_str()); - int total_reads = 1000000; - int batches_of = 5; - vector buffers(thread_count); + vector> read_reqs(thread_count); omp_set_num_threads(thread_count); #pragma omp parallel for num_threads((int)thread_count) for (int i = 0; i < thread_count; i++) { - char *buf = nullptr; - alloc_aligned((void **)&buf, batches_of * SECTOR_LEN, SECTOR_LEN); - buffers[i] = buf; reader->register_thread(); + read_reqs[i].reserve(batches_of); + + // create read requests + for (size_t j = 0; j < batches_of; ++j) + { + char* buf = nullptr; + alloc_aligned((void**)&buf, SECTOR_LEN, SECTOR_LEN); + + AlignedRead read; + read.buf = buf; + read.len = SECTOR_LEN; + read_reqs[i].push_back(read); + } } - int no_of_reads = total_reads / batches_of; + // Initialize a random number generator + uniform_int_distribution<> distrib(0, 1650000); + + int no_of_reads = TOTAL_READS / batches_of; Timer timer; #pragma omp parallel for schedule(dynamic, 1) for (int i = 0; i < no_of_reads; i++) { - char *buf = buffers[omp_get_thread_num()]; - do_reads(reader, buf, batches_of); + do_reads(reader, read_reqs[omp_get_thread_num()], distrib); } // cout << "Time taken to read in microseconds: " << timer.elapsed() << endl; - cout<close(); } -int main(int argc, char *argv[]) +int main(int argc, char* argv[]) { - int val = 1; - if (argc >= 2) - { + int thread_count = 1; + int batches_of = 128; + if (argc >= 2) { std::istringstream iss(argv[1]); - - if (iss >> val) + if (iss >> thread_count) { // cout << "Got cmd argument" << endl; } } - // cout << "Using " << val << " threads." << endl; + if (argc >= 3) { + std::istringstream iss(argv[2]); + if (iss >> batches_of) { + // cout<<"Got batch size argument"< +#include using namespace std; using namespace diskann; @@ -12,22 +13,15 @@ using namespace diskann; #define SECTOR_LEN 4096 #define TOTAL_READS 1000000 -void do_reads(WindowsAlignedFileReader *reader, char *buf, int batches_of) +void do_reads(WindowsAlignedFileReader* reader, vector& read_reqs, uniform_int_distribution<> &distrib, mt19937 &gen) { auto ctx = reader->get_ctx(); - std::vector read_reqs; - read_reqs.reserve(batches_of); - - // create read requests - for (size_t i = 0; i < batches_of; ++i) + // Modify read requests + for (auto& read_req : read_reqs) { - AlignedRead read; - read.len = SECTOR_LEN; - read.buf = buf + i * SECTOR_LEN; - auto sector_id = (rand() % 1650000); - read.offset = sector_id * SECTOR_LEN; - read_reqs.push_back(read); + long long int sector_id = distrib(gen); + read_req.offset = sector_id * SECTOR_LEN; } reader->read(read_reqs, ctx, false); @@ -38,23 +32,40 @@ void do_reads_in_batches_of(int batches_of) string file_name = "F:\\indices\\turing_10m\\disk_index_disk.index"; auto reader = new WindowsAlignedFileReader(); reader->open(file_name.c_str()); - char *buf = nullptr; - alloc_aligned((void **)&buf, batches_of * SECTOR_LEN, SECTOR_LEN); + char* buf = nullptr; + alloc_aligned((void**)&buf, batches_of * SECTOR_LEN, SECTOR_LEN); reader->register_thread(); + std::vector read_reqs; + read_reqs.reserve(batches_of); + + // create read requests + for (size_t i = 0; i < batches_of; ++i) + { + AlignedRead read; + read.len = SECTOR_LEN; + read.buf = buf + i * SECTOR_LEN; + read_reqs.push_back(read); + } + + // Initialize a random number generator + uniform_int_distribution<> distrib(0, 1650000); + random_device rd; + mt19937 gen(rd()); + int no_of_reads = TOTAL_READS / batches_of; Timer timer; for (int i = 0; i < no_of_reads; i++) { - do_reads(reader, buf, batches_of); + do_reads(reader, read_reqs, distrib, gen); } // cout << "Time taken to read in microseconds: " << timer.elapsed() << endl; - cout<close(); } -int main(int argc, char *argv[]) +int main(int argc, char* argv[]) { int val = 10; if (argc >= 2) @@ -69,5 +80,6 @@ int main(int argc, char *argv[]) // cout << "Using batches of " << val << endl; // cout << "Hello World" << endl; + do_reads_in_batches_of(val); } \ No newline at end of file From 63a5de79ceef1ec23a87ea1a21d62482d0d3561d Mon Sep 17 00:00:00 2001 From: Suryansh Gupta Date: Mon, 3 Feb 2025 17:03:29 +0530 Subject: [PATCH 9/9] Add random sector_id calculation to preprocess step --- apps/benchmark_reads.cpp | 20 +++++++++++++------- apps/benchmark_reads_single_threaded.cpp | 16 +++++++++++----- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/apps/benchmark_reads.cpp b/apps/benchmark_reads.cpp index cb18e9ace..006d7aa87 100644 --- a/apps/benchmark_reads.cpp +++ b/apps/benchmark_reads.cpp @@ -12,17 +12,17 @@ using namespace diskann; #define SECTOR_LEN 4096 #define TOTAL_READS 1000000 -void do_reads(WindowsAlignedFileReader* reader, vector& read_reqs, uniform_int_distribution<>& distrib) +vector random_sector_ids(TOTAL_READS); + +void do_reads(WindowsAlignedFileReader* reader, vector& read_reqs, int batch_num) { auto ctx = reader->get_ctx(); - random_device rd; - mt19937 gen(rd()); + size_t batch_size = read_reqs.size(); // Modify read requests - for (auto& read_req : read_reqs) + for (int i = 0; i < batch_size; i++) { - long long int sector_id = distrib(gen); - read_req.offset = sector_id * SECTOR_LEN; + read_reqs[i].offset = random_sector_ids[batch_num * batch_size + i] * SECTOR_LEN; } reader->read(read_reqs, ctx, false); @@ -59,13 +59,19 @@ void do_multiple_reads_with_threads(int thread_count, int batches_of) // Initialize a random number generator uniform_int_distribution<> distrib(0, 1650000); + random_device rd; + mt19937 gen(rd()); + for (int i = 0; i < TOTAL_READS; i++) + { + random_sector_ids[i] = distrib(gen); + } int no_of_reads = TOTAL_READS / batches_of; Timer timer; #pragma omp parallel for schedule(dynamic, 1) for (int i = 0; i < no_of_reads; i++) { - do_reads(reader, read_reqs[omp_get_thread_num()], distrib); + do_reads(reader, read_reqs[omp_get_thread_num()], i); } // cout << "Time taken to read in microseconds: " << timer.elapsed() << endl; cout << timer.elapsed() << endl; diff --git a/apps/benchmark_reads_single_threaded.cpp b/apps/benchmark_reads_single_threaded.cpp index cf4e44a78..89d445810 100644 --- a/apps/benchmark_reads_single_threaded.cpp +++ b/apps/benchmark_reads_single_threaded.cpp @@ -13,15 +13,17 @@ using namespace diskann; #define SECTOR_LEN 4096 #define TOTAL_READS 1000000 -void do_reads(WindowsAlignedFileReader* reader, vector& read_reqs, uniform_int_distribution<> &distrib, mt19937 &gen) +vector random_sector_ids(TOTAL_READS); + +void do_reads(WindowsAlignedFileReader* reader, vector& read_reqs, int batch_num) { auto ctx = reader->get_ctx(); + size_t batch_size = read_reqs.size(); // Modify read requests - for (auto& read_req : read_reqs) + for (int i = 0; i < batch_size; i++) { - long long int sector_id = distrib(gen); - read_req.offset = sector_id * SECTOR_LEN; + read_reqs[i].offset = random_sector_ids[batch_num * batch_size + i] * SECTOR_LEN; } reader->read(read_reqs, ctx, false); @@ -52,12 +54,16 @@ void do_reads_in_batches_of(int batches_of) uniform_int_distribution<> distrib(0, 1650000); random_device rd; mt19937 gen(rd()); + for (int i = 0; i < TOTAL_READS; i++) + { + random_sector_ids[i] = distrib(gen); + } int no_of_reads = TOTAL_READS / batches_of; Timer timer; for (int i = 0; i < no_of_reads; i++) { - do_reads(reader, read_reqs, distrib, gen); + do_reads(reader, read_reqs, i); } // cout << "Time taken to read in microseconds: " << timer.elapsed() << endl; cout << timer.elapsed() << endl;