Skip to content

Commit

Permalink
Remove some allocations from the timer
Browse files Browse the repository at this point in the history
  • Loading branch information
Suryansh Gupta committed Jan 31, 2025
1 parent 3b3a4a8 commit 63eceef
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 49 deletions.
75 changes: 43 additions & 32 deletions apps/benchmark_reads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<AlignedRead>& read_reqs, uniform_int_distribution<>& distrib)
{
auto ctx = reader->get_ctx();
random_device rd;
mt19937 gen(rd());

std::vector<AlignedRead> 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<char *> buffers(thread_count);
vector<vector<AlignedRead>> 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<<timer.elapsed()<<endl;
cout << timer.elapsed() << endl;

reader->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"<<endl;
}
}
// cout << "Using " << thread_count << " threads." << endl;
// cout << "Using batch size of " << batches_of << endl;

// cout << "Hello World" << endl;
do_multiple_reads_with_threads(val);
do_multiple_reads_with_threads(thread_count, batches_of);
}
46 changes: 29 additions & 17 deletions apps/benchmark_reads_single_threaded.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,23 @@
#include "utils.h"
#include "timer.h"
#include <omp.h>
#include <random>

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<AlignedRead>& read_reqs, uniform_int_distribution<> &distrib, mt19937 &gen)
{
auto ctx = reader->get_ctx();

std::vector<AlignedRead> 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);
Expand All @@ -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<AlignedRead> 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<<timer.elapsed()<<endl;
cout << timer.elapsed() << endl;

reader->close();
}

int main(int argc, char *argv[])
int main(int argc, char* argv[])
{
int val = 10;
if (argc >= 2)
Expand All @@ -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);
}

0 comments on commit 63eceef

Please sign in to comment.