pangeo-data / xESMF

Universal Regridder for Geospatial Data
http://xesmf.readthedocs.io/
MIT License
189 stars 34 forks source link

Running xESMF with parallel throws multiprocessing error #324

Closed guigrpa closed 8 months ago

guigrpa commented 8 months ago

I'm trying to run the following snippet from https://github.com/pangeo-data/xESMF/issues/299#issuecomment-1714333695:

import xarray as xr
import xesmf as xe

ds = xr.tutorial.open_dataset("air_temperature")
ds_out = xe.util.grid_global(2.5, 2.5)
ds_out = ds_out.assign(mask=ds_out.lon.notnull() & ds_out.lat.notnull())

# Chunked, parallel
reg = xe.Regridder(ds, ds_out.chunk(), "bilinear", periodic=True, parallel=True)

I get the following error (full stack below):

RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

@aulemahal Any idea? Could you please provide details on the environment (OS and dependencies) you're using to run the snippet? Thanks a lot.

Environment and OSs I used for the test

I created a brand new environment on MacOS and Ubuntu, with the following dependencies:

name: env-xxx
channels:
  - conda-forge
  - defaults
dependencies:
  - python=3.9

  # My dependencies
  - dask~=2023.10.1
  - netCDF4~=1.6
  - numpy~=1.26.3
  - pooch
  - xarray~=2023.10.1
  - xesmf~=0.8.1

Full error stack

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/multiprocessing/spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/multiprocessing/spawn.py", line 125, in _main
    prepare(preparation_data)
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/multiprocessing/spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/multiprocessing/spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/runpy.py", line 288, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/eouser/test-xesmf-0.8-parallel/test.py", line 16, in <module>
    reg = xe.Regridder(ds, ds_out.chunk(), "bilinear", periodic=True, parallel=True)
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/site-packages/xesmf/frontend.py", line 958, in __init__
    self._init_para_regrid(ds_in, ds_out, kwargs)
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/site-packages/xesmf/frontend.py", line 1054, in _init_para_regrid
    w = w.compute(scheduler='processes')
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/site-packages/xarray/core/dataarray.py", line 1148, in compute
    return new.load(**kwargs)
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/site-packages/xarray/core/dataarray.py", line 1122, in load
    ds = self._to_temp_dataset().load(**kwargs)
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/site-packages/xarray/core/dataset.py", line 847, in load
    evaluated_data = chunkmanager.compute(*lazy_data.values(), **kwargs)
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/site-packages/xarray/core/daskmanager.py", line 70, in compute
    return compute(*data, **kwargs)
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/site-packages/dask/base.py", line 628, in compute
    results = schedule(dsk, keys, **kwargs)
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/multiprocessing/context.py", line 284, in _Popen
    return Popen(process_obj)
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/multiprocessing/popen_spawn_posix.py", line 42, in _launch
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/multiprocessing/spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "/home/eouser/micromamba/envs/env-admp-regridder-weights/lib/python3.9/multiprocessing/spawn.py", line 134, in _check_not_importing_main
    raise RuntimeError('''
aulemahal commented 8 months ago

I was using a Linux machine (Fedora 38) with python 3.10 or 3.11. Dask and xarray were probably at their 2023.09 version as this comment was written in september.

Are you putting your snippet in a text file and running it with python thefile.py ? If yes, then you might be having an error I don't fully understand that involves the initialisation of the python processes. And in that case, the error message actually tells you what to do (even though its wording as nothing to do with dask): put the code after if __name__ == '__main__'.

Your text file should look like:

import xarray as xr
import xesmf as xe

if __name__ == '__main__':
    ds = xr.tutorial.open_dataset("air_temperature")
    ds_out = xe.util.grid_global(2.5, 2.5)
    ds_out = ds_out.assign(mask=ds_out.lon.notnull() & ds_out.lat.notnull())

    # Chunked, parallel
    reg = xe.Regridder(ds, ds_out.chunk(), "bilinear", periodic=True, parallel=True)

My previous comment was either simplified for readability, or I was putting all this in a IPython/jupyter console, which is already "fully initialized" on the first line.

guigrpa commented 8 months ago

Oops, that should have been obvious to me. Thanks for your advice, it fixed the problem!