rapidsai/dask-cuda

Is the use of `__main__` necessary for `LocalCUDACluster`?

trivialfis opened this issue · 3 comments

Hi, I just read the new quickstart here: https://docs.rapids.ai/api/dask-cuda/stable/quickstart.html#localcudacluster thanks to dmlc/xgboost#9008 .

My question is, in the Python script environment instead of a notebook, is wrapping code under:

if __name__ == "__main__":
    ...

necessary for CUDA cluster?

Yes. If you do not do this, you get an error that will look similar to the following:

/home/wence/Documents/src/rapids/third-party/distributed/distributed/node.py:182: UserWarning: Port 8787 is already in use.
Perhaps you already have a cluster running?
Hosting the HTTP server on port 39273 instead
  warnings.warn(
/home/wence/Documents/src/rapids/third-party/distributed/distributed/node.py:182: UserWarning: Port 8787 is already in use.
Perhaps you already have a cluster running?
Hosting the HTTP server on port 39353 instead
  warnings.warn(
2023-04-04 11:05:35,220 - distributed.nanny - ERROR - Failed to start process
Traceback (most recent call last):
  File "/home/wence/Documents/src/rapids/third-party/distributed/distributed/nanny.py", line 442, in instantiate
    result = await self.process.start()
  File "/home/wence/Documents/src/rapids/third-party/distributed/distributed/nanny.py", line 711, in start
    await self.process.start()
  File "/home/wence/Documents/src/rapids/third-party/distributed/distributed/process.py", line 55, in _call_and_set_future
    res = func(*args, **kwargs)
  File "/home/wence/Documents/src/rapids/third-party/distributed/distributed/process.py", line 215, in _start
    process.start()
  File "/home/wence/Documents/apps/mambaforge/envs/test-weakref/lib/python3.10/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
  File "/home/wence/Documents/apps/mambaforge/envs/test-weakref/lib/python3.10/multiprocessing/context.py", line 288, in _Popen
    return Popen(process_obj)
  File "/home/wence/Documents/apps/mambaforge/envs/test-weakref/lib/python3.10/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/home/wence/Documents/apps/mambaforge/envs/test-weakref/lib/python3.10/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/home/wence/Documents/apps/mambaforge/envs/test-weakref/lib/python3.10/multiprocessing/popen_spawn_posix.py", line 42, in _launch
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "/home/wence/Documents/apps/mambaforge/envs/test-weakref/lib/python3.10/multiprocessing/spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "/home/wence/Documents/apps/mambaforge/envs/test-weakref/lib/python3.10/multiprocessing/spawn.py", line 134, in _check_not_importing_main
    raise RuntimeError('''
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.

The reason being, that the booting of the cluster spwans a number of new processes, this can only be done once the main process has finished loading (which is exactly what the if __name__ == "__main__" guard ensures).

Thank you for confirming! Would you like me to open a PR for document to reflect this requirement?

Oh, yes please! Please target the 23.06 branch.