Error collecting data using mpiexec

Dear,

I need to extract data from my code, but when the code is executed using multiple processors, the data ends up in the .txt file with a “copy” of the number of cores used or “overwrites” the data, depending on the type of function module open.

See an example below:

from fenics import *
import numpy as np

frequency = np.arange(0., 10., 1.)
frequencylist = open("testfrequency.txt", 'a')
frequency_list = []
Nsteps = len(frequency)

for i in range(Nsteps): 
    freqlist = frequency[i]
    frequencylist.write(str(freqlist))
    frequencylist.write('\n')
    frequency_list.append(freqlist)

Considering the command:
mpiexec -n 4 python3 print_frequency.py

And in the open line using “a”, the .txt file is as follows:

0.0
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
0.0
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
0.0
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
0.0
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0

If I use the open line using “w”, the .txt file looks like this:

0.0
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0

But my code using the last method using “w”, which would be correct, the new values are overwritten in the old values, it doesn’t create a new value in the line below keeping the old value, and then the final file has only a single value, the last processed.

Could someone help me with this type of command:
mpiexec -n 4 python3 print_frequency.py

This question is not related to FEniCS (as illustrated by the code example). This relates to how to use an MPI communicator to decide which process should write to file. See for instance

https://mpi4py.readthedocs.io/en/stable/tutorial.html
or

1 Like