Re: Read external file

From: Nathan Goldbaum <>
Date: Mon, 15 Aug 2016 18:10:26 -0500

On Mon, Aug 15, 2016 at 4:37 PM, Jesus <> wrote:

> Hello,
> I am trying to modify Gadget2 to read an external file that contains a
> grid of velocities to simulate turbulence. In summary, I did the following:
> -I defined an array (static double rgrid[grid_size][grid_size][grid_size],
> where int grid_size = 128, and the array is static double to avoid problems
> with memory ) in the file allvars.h
> -I set up a function to read a file and store the values in the 3D array
> 'rgrid' in a file called "read_file.h". The function is called
> "read_turb()" and in it I have a printf statement that says "File read"
> when the process is completed.
> -I include "read_file.h" in the file "main.c", and I wrote the function
> "read_file()" at the beginning of the main function.
> Gadget2 compiles fine (it tells me that the rgrid is defined but not used,
> but whatever, it compiles), however when I run the program, the statement
> "File read" appears 8 times. I supposed is has something to do with the
> paralelization (I have 8 cores), so the file is being read 8 times and thus
> takes a long time. Is there a way to avoid this problem? i.e. to read the
> file just once. I wrote the function before the MPI_Init command in the
> main file so I assumed that the paralelization would not have started then,
> but I guess I was wrong.

Likely your filesystem is deadlocking as 8 threads try to read from the
same file at the same time. If you wait long enough and depending on how
you implemented the I/O, it should eventually complete, albeit it will take
at least 8 times longer than if you read in the file on one core.

Placing the I/O before MPI_Init won't change this, you will still have 8
processes executing the same code, they just won't be initialized as a
single MPI job in which the individual processes that make up the job can
talk to each other.

It might be a better idea to read in the file on one core (e.g. the core
with MPI rank 0) and then use an MPI broadcast to share the data with the
other cores.


Nathan Goldbaum

Thank you for your help
> -Jesus
> -----------------------------------------------------------
> If you wish to unsubscribe from this mailing, send mail to
> with a subject of: unsubscribe gadget-list
> A web-archive of this mailing list is available here:
Received on 2016-08-16 01:10:50

This archive was generated by hypermail 2.3.0 : 2023-01-10 10:01:32 CET