# Error collecting data using mpiexec

**URL:** <https://fenicsproject.discourse.group/t/error-collecting-data-using-mpiexec/9130>\
**Category:** Uncategorized\
**Created:** [August 25, 2022, 6:17pm UTC](https://fenicsproject.discourse.group/t/error-collecting-data-using-mpiexec/9130 "2022-08-25T18:17:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafael\_Ferro](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/rafael_ferro/32/1157_2.png) [@Rafael\_Ferro](https://fenicsproject.discourse.group/u/Rafael_Ferro)\
**Post date:** [August 25, 2022, 6:17pm UTC](https://fenicsproject.discourse.group/t/error-collecting-data-using-mpiexec/9130/1 "2022-08-25T18:17:00Z")

</div>

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:

```auto
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:

```auto
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [August 25, 2022, 6:33pm UTC](https://fenicsproject.discourse.group/t/error-collecting-data-using-mpiexec/9130/2 "2022-08-25T18:33:51Z")

</div>

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](https://mpi4py.readthedocs.io/en/stable/tutorial.html)  
or

> [@How to output the results in each iteration in parallel computation?](https://fenicsproject.discourse.group/t/how-to-output-the-results-in-each-iteration-in-parallel-computation/6148):
>
> Hi all, in order to observe the progress of the running code, I want to output the computational results from FEniCS/FEniCSX in each iteration. And here is a minimal example: from mpi4py import MPI import time Iter = 0 while Iter \< 3: Iter += 1 if MPI.COMM\_WORLD.rank == 0: print('Iter=%d: some results obtained in FEniCS/FEniCSX' %Iter) else: pass time.sleep(1) When we run the code in serial, it can give us the result in each iteration immediately: Iter 1: Ite…
