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 7002101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:53:10+00:00 2026-05-27T20:53:10+00:00

In my program I am simulating a N-body system for a large number of

  • 0

In my program I am simulating a N-body system for a large number of iterations. For each iteration I produce a set of 6N coordinates which I need to append to a file and then use for executing the next iteration. The code is written in C++ and currently makes use of ofstream‘s method write() to write the data in binary format at each iteration.

I am not an expert in this field, but I would like to improve this part of the program, since I am in the process of optimizing the whole code. I feel that the latency associated with writing the result of the computation at each cycle significantly slows down the performance of the software.

I’m confused because I have no experience in actual parallel programming and low level file I/O. I thought of some abstract techniques that I imagined I could implement, since I am programming for modern (possibly multi-core) machines with Unix OSes:

  • Writing the data in the file in chunks of n iterations (there seem to be better ways to proceed…)
  • Parallelizing the code with OpenMP (how to actually implement a buffer so that the threads are synchronized appropriately, and do not overlap?)
  • Using mmap (the file size could be huge, on the order of GBs, is this approach robust enough?)

However, I don’t know how to best implement them and combine them appropriately.

  • 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-05-27T20:53:11+00:00Added an answer on May 27, 2026 at 8:53 pm

    Of course writing into a file at each iteration is inefficient and most likely slow down your computing. (as a rule of thumb, depends on your actuel case)

    You have to use a producer -> consumer design pattern. They will be linked by a queue, like a conveyor belt.

    • The producer will try to produce as fast as it can, only slowing if the consumer can’t handle it.
    • The consumer will try to “consume” as fast as it can.

    By splitting the two, you can increase performance more easily because each process is simpler and has less interferences from the other.

    • If the producer is faster, you need to improve the consumer, in your case by writing into file in the most efficient way, chunk by chunk most likely (as you said)
    • If the consumer is faster, you need to improve the producer, most likely by parallelizing it as you said.

    There is no need to optimize both. Only optimize the slowest (the bottleneck).

    Practically, you use threads and a synchronized queue between them. For implementation hints, have a look here, especially §18.12 “The Producer-Consumer Pattern”.

    About flow management, you’ll have to add a little bit more complexity by selecting a “max queue size” and making the producer(s) wait if the queue has not enough space. Beware of deadlocks then, code it carefully. (see the wikipedia link I gave about that)

    Note : It’s a good idea to use boost threads because threads are not very portable. (well, they are since C++0x but C++0x availability is not yet good)

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

Sidebar

Related Questions

I want to write a program for simulating a motion of high number (N
Right now I am writing a simulation program which output is formatted according to
I'm writing a program in C++ to perform a simulation of particular system. For
A program receives a list of Messages (base type). Each message in the list
In a c# program for simulating a lan messenger, i have a callback function
In order to run a python script on a large deployment, I need to
I have a program which performs a Monte Carlo-type simulation. Currently I have written
I have been working on a program, simulating a small database where I could
I'm trying to setup a simulation program. The simulation runs for a number of
In some simulation program we generate object surfaces in terms of points, each point

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.