# Write array file fails when running a parallel pool of FreeFEM processes

**URL:** <https://community.freefem.org/t/write-array-file-fails-when-running-a-parallel-pool-of-freefem-processes/3015>\
**Category:** General Discussion\
**Created:** [March 6, 2024, 3:37pm UTC](https://community.freefem.org/t/write-array-file-fails-when-running-a-parallel-pool-of-freefem-processes/3015 "2024-03-06T15:37:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ffeppon](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/ffeppon/32/1011_2.png) [@ffeppon](https://community.freefem.org/u/ffeppon)\
**Post date:** [March 6, 2024, 3:37pm UTC](https://community.freefem.org/t/write-array-file-fails-when-running-a-parallel-pool-of-freefem-processes/3015/1 "2024-03-06T15:37:24Z")

</div>

Dear community,

I noticed a weird bug when trying to export arrays to files when running several FreeFem++ processes with a Python parallel for loop. The bug seems to be related to the interpretation of macros imported from external files.

I am using the following Python code calling [pyfreefem](https://pyfreefem.readthedocs.io/en/latest/) for exporting an array in multiple output folders:

```python
# This code does not work !
from pyfreefem import FreeFemRunner
from multiprocessing.pool import ThreadPool as Pool
    
code = """  
IMPORT "io.edp"
    
real[int] x = [1,2,3,4,5];  
exportArray(x); 
"""

def process_data(index):    
    FreeFemRunner(code, config={"FILE": str(index)},    
                  run_dir="test/output_"+str(index), debug=1).execute()
        
indices=range(100)
with Pool(processes=8) as pool: 
    pool.map(process_data, indices) 

```

The code is supposed to save hundred times the file test/output\_{i}/ffexport/array\_x where i is an integer between 0 and 99.  
For running this code, pyfreefem needs to be installed with `pip install pyfreefem`.

The macro `exportArray` is defined [here](https://gitlab.com/florian.feppon/pyfreefem/-/blob/master/pyfreefem/edp/io.edp?ref_type=heads#L195): it does

```auto
func int saveArray(string fileName, real[int] &value){
	ofstream file(fileName);
	file.precision(16);
	file << value;
	dispDebug("Saved "+fileName,2,"blue");
}

macro exportArray(a) saveArray("$FFEXPORTDIR/array_"+Stringification(a),a)//

```

When executed, the line `IMPORT "io.edp"` is substituted into a regular import of a local copy of the file [io.edp](https://gitlab.com/florian.feppon/pyfreefem/-/blob/master/pyfreefem/edp/io.edp?ref_type=heads) and `$FFEXPORTDIR` is replaced with `test/output_{i}/ffexport`.

Running this code with Python fails for some of the processes (not all) with the message

```auto
    4 : real[int] x = [1,2,3,4,5];  
    5 : exportArray(x) saveArray("test/output_68/ffexport/array_"+Stringification((xx)),x); sizestack + 1024 =4568 ( 3544 )

 Error opening file test/output_68/ffexport/array_x
  current line = 85
 call saveArray at line 5
Exec error : Error opening file
   -- number :1

```

The problem seems to be related to the interpretation of the macro `exportArray` which saves the file. Curiously, if we replace `exportArray(x)` with `exportArray2(x)` where `exportArray2` is a copy of the macro `exportArray` that we insert directly in the running file , then it works:

```python
# THIS CODE WORKS !
from pyfreefem import FreeFemRunner
from multiprocessing.pool import ThreadPool as Pool
    
code = """  
IMPORT "io.edp"
    
real[int] x = [1,2,3,4,5];  

macro exportArray2(a) saveArray("$FFEXPORTDIR/array_"+Stringification(a),a)//
exportArray2(x); 
"""

def process_data(index):    
    FreeFemRunner(code, config={"FILE": str(index)},    
                  run_dir="test/output_"+str(index), debug=1).execute()
        
indices=range(100)
with Pool(processes=8) as pool: 
    pool.map(process_data, indices) 

```

Would you have an idea about why the original version fails ?

PS : Sorry for not providing a more minimal code which would not rely on pyfreefem, but I hope my message and the instructions are sufficiently clear.
