Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9148303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:14:11+00:00 2026-06-17T11:14:11+00:00

I am using gcc 4.7.2 on a 64-bit Linux box. I have 20 large

  • 0

I am using gcc 4.7.2 on a 64-bit Linux box.

I have 20 large sorted binary POD files that I need to read as a part of the final merge in an external merge-sort.

Normally, I would mmap all the files for reading and use a multiset<T,LessThan> to manage the merge sort from small to large, before doing a mmap write out to disk.

However, I realised that if I keep a std::mutex on each of these files, I can create a second thread which reads the file backwards, and sort from large to small at the same time. If I decide, beforehand, that the first thread will take exactly n/2 elements and the second thread will take the rest, I will have no need for a mutex on the output end of things.

Reading lock contentions, can be expected to occur, on average, maybe 1 in 20 in this particular case, so that’s acceptable.

Now, here’s my question. In the first case, it is obvious that I should call madvise with MADV_SEQUENTIAL, but I have no idea what I should do for the second case, where I’m reading the file backwards.

I see no MADV_REVERSE in the man pages. Should I use MADV_NORMAL or maybe don’t call madvise at all?

Recall that an external sort is needed when the volume of data is so large that it will not fit into memory. So we are left with a more complex algorithm to use disk as a temporary store. Divide-and-conquer algorithms will usually involve breaking up the data, doing partial sorts, and then merging the partial sorts.

My steps for an external merge-sort

  1. Take n=1 billion random numbers and break them into 20 shards of equal sizes.
  2. Sort each shard individually from small to large, and write each out into its own file.
  3. Open 40 mmap’s, 2 for each file, one for going forward, one for going backwards, associate a mutex with each file.
  4. Instantiate a std::multiset<T,LessThan> buff_fwd; for the forward thread and a std::multiset<T,GreaterThan> buff_rev for the reverse thread. Some people prefer to use priority queues here, but essentially, and sort-on-insert container will work here.
  5. I like to call the two buffers surface and rockbottom, representing the smallest and largest numbers not yet added to the final sort.
  6. Add items from the shards until n/2 is used up, and flush the shards to one output file using mmap from beginning towards the middle, and from the end towards to middle in the other thread. You can basically flush at will, but at least one should do it before either buffer uses up too much memory.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:14:12+00:00Added an answer on June 17, 2026 at 11:14 am

    I would suggest:

    MADV_RANDOM
    

    To prevent useless read-ahead (which is in the wrong direction).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While trying to compile a 64 bit linux kernel using gcc, I see the
gcc 4.5.1 Fedora 14 Linux I have this static library that was build from
I have built GCC 4.7 on my x86 32-bit linux system. When I try
I'm using GCC 4.3.3 on Ubuntu 9.04 64-bit and was getting errors using C++-style
I'm attempting to assemble some 32-bit code using NASM and GCC on a 64-bit
I'm using gcc 4.3.2. I have the following code (simplified): #include <cstdlib> template<int SIZE>
Hi I'm using Ubuntu release 12.10 (quantal) 32-bit with Linux Kernel 3.5.0-21-generic. I'm trying
I have been using a socket library for C++. Some other info: 32 bit
I have been using a socket library for C++. Some other info: 32 bit
I have recently migrated from Windows to Linux (Debian, 64-bit) and am trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.