Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deadlock with multiprocessing (using fork) and OpenMP #1552

Closed
district10 opened this issue Feb 29, 2020 · 5 comments
Closed

Deadlock with multiprocessing (using fork) and OpenMP #1552

district10 opened this issue Feb 29, 2020 · 5 comments

Comments

@district10
Copy link

district10 commented Feb 29, 2020

Python code not working properly in another process.

To reproduce, use these code:

import open3d as o3d
import numpy as np
from multiprocessing import Process, set_start_method


def test():
    all_pts = np.array([[108.46383020247207, 350.5646727751972, 116.3624462382868],
                     [112.25361397099844, 347.0114607994641, 116.26706010755152],
                     [113.18366122221072, 361.2549821304686, 116.27959668822587],
                     [111.91097601143456, 360.8370105069897, 116.30449797399342]])
    point_cloud = o3d.geometry.PointCloud()
    point_cloud.points = o3d.utility.Vector3dVector(all_pts)
    print("before")
    point_cloud.estimate_normals(o3d.geometry.KDTreeSearchParamHybrid(radius=100, max_nn=22))
    print("after")


if __name__ == '__main__':
    print('open3d version:', o3d.__version__)

    # set_start_method('spawn') # spawn, fork (default on Unix), forkserver

    process = Process(target=test, args=())
    process.start()
    process.join()

    test()

    process = Process(target=test, args=())
    process.start()
    process.join()

Output on unix:

open3d version: 0.9.0.0
before
after
before
after
before
(hangs there)

If uncomment set_start_method('spawn'), it will finish properly.

Related issue: pytorch/pytorch#17199

It's caused by GNU OpenMP. Maybe compile againt Intel OpenMP?

@district10 district10 added the bug label Feb 29, 2020
@chrisranderson
Copy link

chrisranderson commented Apr 14, 2020

I ran into a similar problem with open3d.geometry.TriangleMesh.create_from_point_cloud_poisson. I thought it was hanging indefinitely, but it was just taking a long time, for some reason at 100% CPU and ~10% RAM. This was using multiprocessing.Pool.

Time per mesh construction with 2 processes was about 65 seconds. With a single process, it was less than a second.

@ssheorey
Copy link
Member

@district10 I think this is a common issue with fork. The advice for Python multiprocessing is to use forkserver for multithreaded programs on Linux.

https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods

@FranzEricSchneider
Copy link

I just wanted to post a slightly different use case because I found this thread helpful, and maybe it'll help other googlers. I was trying to run a series of open3d operations on a set of files using multiprocessing.Pool

pool = multiprocessing.Pool(multiprocessing.cpu_count() - 1)
for file in path.glob("*"):
    pool.apply_async(process_file, args=(file, settings))
pool.close()
pool.join()

And the code was freezing on remove_statistical_outlier

...
cloud = open3d.io.read_point_cloud(file)
cloud, _ = color_filter(cloud)
cloud, _ = median_radius_filter(cloud, radius)
# Would always get stuck here at 0-5% CPU on all cores
cloud, _ = cloud.remove_statistical_outlier(
    nb_neighbors=100,
    std_ratio=0.8,
)
...

The trick, as suggested here, was to set forkserver as the start method at the top. Thanks!

@Siltyx
Copy link

Siltyx commented Jul 14, 2023

Had the same problem with cluster_connected_triangles. Weirdly, it was working in Debug mode most of the time.
multiprocessing.set_start_method('spawn') fixed it for me aswell 👍

@shannon112
Copy link

  • Same issue on my side, when I'm using point cloud estimate_normals(), it's stocked
  • As the solutions provided above, I added a line multiprocessing.set_start_method('forkserver') before claiming multiprocessing.Pool.
  • then it's resolved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants