Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #70961

Re: Write to file in 8 places simultaneously

From Paavo Helde <myfirstname@osa.pri.ee>
Newsgroups comp.lang.c++
Subject Re: Write to file in 8 places simultaneously
Date 2020-01-02 12:46 +0200
Organization A noiseless patient Spider
Message-ID <qukhmj$9ib$1@dont-email.me> (permalink)
References <24817930-f8d6-4237-8c4b-b30a75f03b27@googlegroups.com>

Show all headers | View raw


On 2.01.2020 11:46, Frederick Gotham wrote:
>
> First, let me give an example of code I wrote this morning to write to RAM in 8 places simultaneously:
>
> unsigned constexpr g_amount_threads = 8;
>
> #include <cstddef>
> using std::size_t;
>
> #include <cstdint>  /* uint8_t, uint64_t */
>
> #include <numeric>  /* iota */
>
> #include <thread>  /* thread */
>
> typedef std::uint8_t Element_Type;
>
> size_t constexpr g_total_bytes = 4294967296ull;  // Four gigabytes
>
> 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_Set_Memory(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));
> }
>
> #include <iostream>
> using std::cout;
> using std::endl;
>
> auto main(void) -> int
> {
> 	g_p_mem = new Element_Type[g_total_elements];
> 	
> 	std::thread *ptrs_threads[g_amount_threads];
> 	
> 	for (unsigned i = 0; i != g_amount_threads; ++i)
> 	{
> 		ptrs_threads[i] = new std::thread(Thread_Entry_Point_Set_Memory,i);
> 	}
> 	
> 	// Now join all the threads
> 	
> 	for (auto &p : ptrs_threads)
> 	{
> 		p->join();
> 		
> 		delete p;
> 	}
>
> 	delete [] g_p_mem;
> }
>
>
> I have measured the differences in execution time with 1 thread, with 2 threads, with 4 threads, etc..

This is basically testing the main memory throughput. For curiosity, 
what where the results?

>
> What I want to do now is create a very large file (4 GB) on the hard disk and write to it in 8 different places simultaneously. I know that I can create 8 different files and deal with them independently, but I just want one file.

And this is testing either main memory throughput or hard disk 
throughput, depending on the amount of free memory in the computer.

>
> I think this would be possible under Unix using the POSIX functions open, pwrite, writenv. Has anyone tried it?

If you aim for max speed and are willing to use non-portable code, you 
should probably consider mmap.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Write to file in 8 places simultaneously Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-02 01:46 -0800
  Re: Write to file in 8 places simultaneously Paavo Helde <myfirstname@osa.pri.ee> - 2020-01-02 12:46 +0200
    Re: Write to file in 8 places simultaneously Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-02 03:34 -0800
      Re: Write to file in 8 places simultaneously Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-03 10:57 +0000
  Re: Write to file in 8 places simultaneously Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-02 03:32 -0800
  Re: Write to file in 8 places simultaneously Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-03 13:41 +0100

csiph-web