On Thursday, January 2, 2020 at 9:47:01 AM UTC, Frederick Gotham wrote:
> I think this would be possible under Unix using the POSIX functions open, pwrite, writenv. Has anyone tried it?
Making a few changes to my original code, here's what I have so far for writing to disk. I'm not getting consistent behaviour yet (the output files have a different sha256 sum depending on how many cores I use). Anyway here's what I have so far:
unsigned constexpr g_amount_threads = 8;
char const g_filepath[] = "/home/fgotham/special.txt";
#include <cstddef>
using std::size_t;
#include <cstdint> /* uint8_t, uint64_t */
#include <numeric> /* iota */
#include <thread> /* thread */
#include <stdio.h> /* fopen, fseek, fputc, fclose */
#include <iostream>
using std::cout;
using std::endl;
/* The next three lines are for POSIX functions: open, pwrite, close */
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
struct Thread_Local_Stuff {
std::thread *ptr_thread;
int fd;
};
Thread_Local_Stuff g_thread_stuffs[g_amount_threads];
typedef std::uint8_t Element_Type;
size_t constexpr g_total_bytes = 4294967296ull; // Four gigabytes
size_t constexpr g_bytes_per_thread = g_total_bytes / g_amount_threads;
size_t constexpr g_total_elements = g_total_bytes / sizeof(Element_Type);
size_t constexpr g_elements_per_thread = g_total_elements / g_amount_threads;
Element_Type *g_p_mem = nullptr;
void Thread_Entry_Point(unsigned const thread_number)
{
Element_Type *const p = g_p_mem + (g_elements_per_thread * thread_number);
Element_Type *const q = p + g_elements_per_thread;
std::iota(p, q, static_cast<Element_Type>(g_elements_per_thread * thread_number));
pwrite( g_thread_stuffs[thread_number].fd, p, g_bytes_per_thread, p - g_p_mem );
}
auto main(void) -> int
{
// First create the very large file
FILE *pfile = fopen(g_filepath, "w");
fseek(pfile, g_total_bytes - 1u, SEEK_SET);
fputc('\0', pfile);
fclose(pfile);
// Now start the threads
g_p_mem = new Element_Type[g_total_elements];
for (unsigned i = 0; i != g_amount_threads; ++i)
{
g_thread_stuffs[i].fd = open(g_filepath, O_WRONLY|O_SYNC);
g_thread_stuffs[i].ptr_thread = new std::thread(Thread_Entry_Point,i);
}
// Now join all the threads
for (auto &e : g_thread_stuffs)
{
e.ptr_thread->join();
delete e.ptr_thread;
close(e.fd);
}
delete [] g_p_mem;
}