Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing Billion Integers to value 1

What is good posix thread design to initialize billion integers using c/c++ on linux platform 8-core CPU with 32GB of DRAM? Thanks for your help.

like image 981
Tibor Jiri Avatar asked Dec 01 '11 16:12

Tibor Jiri


3 Answers

This is a trivial operation and you need not consider multi-threading. Just do it with a memcpy in a single thread.

like image 112
David Heffernan Avatar answered Nov 15 '22 05:11

David Heffernan


The exact number of threads will not be such a limiting factor, but sometimes for this questions it is worth to overcommit, say use 2 threads per physical core.

But the real bottleneck will be IO, writing the data into the RAM. You'd have to take care that the data that is to be replaced will never read before you erase it. Then you should assure that writes to memory appear in large chunks and (if possible) as "write through", mondern CPU have instructions for the later.

Usually something like memcpy with a fixed sized buffer (some pages) that contains the pattern that you want to see should be optimized quite well.

like image 42
Jens Gustedt Avatar answered Nov 15 '22 06:11

Jens Gustedt


What is that for? Depending on usage, the following scenario might work: you initialize one memory page (that's several KB) to all 1's. Then you map that page into the virtual address space as many times as needed with a copy-on-write flag. This way, on reading you'll get all ones from all those virtual pages, on writing the system will allocate more physical pages as needed.

like image 30
Seva Alekseyev Avatar answered Nov 15 '22 05:11

Seva Alekseyev