Skip to content

Instantly share code, notes, and snippets.

@egormkn
Created February 18, 2018 21:26
Show Gist options
  • Save egormkn/b04fafb410f23f9ffe9f9291f832f898 to your computer and use it in GitHub Desktop.
Save egormkn/b04fafb410f23f9ffe9f9291f832f898 to your computer and use it in GitHub Desktop.
FOXXLL async IO test
#include <iostream>
#include <vector>
#include <omp.h>
#include <foxxll/io/request.hpp>
#include <foxxll/io/create_file.hpp>
#include <foxxll/io/request_operations.hpp>
#include <foxxll/common/aligned_alloc.hpp>
int main() {
omp_set_num_threads(4);
unsigned max = static_cast<unsigned>(omp_get_max_threads());
const int BUFFER_SIZE = 4 * 1024 * 1024;
const int NUM_FILES = 128;
std::vector<foxxll::file_ptr> files(NUM_FILES);
std::vector<uint64_t> sizes(NUM_FILES);
std::vector<char *> buffers(NUM_FILES);
std::vector<foxxll::request_ptr> requests(NUM_FILES);
# pragma omp parallel for shared(files, requests, buffers, sizes)
for (int i = 0; i < NUM_FILES; i++) {
int k = omp_get_thread_num();
buffers[i] = static_cast<char *>(foxxll::aligned_alloc<4096>(BUFFER_SIZE));
std::memset(buffers[i], 'a' + k, BUFFER_SIZE);
buffers[i][BUFFER_SIZE - 1] = '\n';
std::string filename = "async" + std::to_string(i + 1) + ".txt";
if (files[i] == nullptr) {
files[i] = foxxll::create_file("linuxaio", filename,
foxxll::file::CREAT | foxxll::file::WRONLY | foxxll::file::DIRECT);
sizes[i] = files[i]->size();
}
requests[i] = files[i]->awrite(buffers[i], sizes[i], sizeof(char) * BUFFER_SIZE);
sizes[i] += BUFFER_SIZE;
}
foxxll::wait_all(requests.data(), requests.size());
for (char *buffer : buffers) {
foxxll::aligned_dealloc<4096>(buffer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment