[Bf-blender-cvs] [fcc844f8fbd] master: BLI: use explicit task isolation, no longer part of parallel operations

Brecht Van Lommel noreply at git.blender.org
Tue Jun 15 18:26:27 CEST 2021


Commit: fcc844f8fbd0d10aeb5012c0b25babe76c278e9e
Author: Brecht Van Lommel
Date:   Mon Jun 14 23:50:24 2021 +1000
Branches: master
https://developer.blender.org/rBfcc844f8fbd0d10aeb5012c0b25babe76c278e9e

BLI: use explicit task isolation, no longer part of parallel operations

After looking into task isolation issues with Sergey, we couldn't find the
reason behind the deadlocks that we are getting in T87938 and a Sprite Fright
file involving motion blur renders.

There is no apparent place where we adding or waiting on tasks in a task group
from different isolation regions, which is what is known to cause problems. Yet
it still hangs. Either we do not understand some limitation of TBB isolation,
or there is a bug in TBB, but we could not figure it out.

Instead the idea is to use isolation only where we know we need it: when
holding a mutex lock and then doing some multithreaded operation within that
locked region. Three places where we do this now:
* Generated images
* Cached BVH tree building
* OpenVDB lazy grid loading

Compared to the more automatic approach previously used, there is the downside
that it is easy to miss places where we need isolation. Yet doing it more
automatically is also causing unexpected issue and bugs that we found no
solution for, so this seems better.

Patch implemented by Sergey and me.

Differential Revision: https://developer.blender.org/D11603

===================================================================

M	intern/cycles/blender/blender_python.cpp
M	source/blender/blenkernel/intern/bvhutils.c
M	source/blender/blenkernel/intern/editmesh_tangent.c
M	source/blender/blenkernel/intern/image.c
M	source/blender/blenkernel/intern/lib_override.c
M	source/blender/blenkernel/intern/mesh_evaluate.c
M	source/blender/blenkernel/intern/mesh_tangent.c
M	source/blender/blenkernel/intern/ocean.c
M	source/blender/blenkernel/intern/particle.c
M	source/blender/blenkernel/intern/particle_distribute.c
M	source/blender/blenkernel/intern/volume.cc
M	source/blender/blenlib/BLI_task.h
M	source/blender/blenlib/intern/task_graph.cc
M	source/blender/blenlib/intern/task_iterator.c
M	source/blender/blenlib/intern/task_pool.cc
M	source/blender/blenlib/intern/task_range.cc
M	source/blender/blenlib/intern/task_scheduler.cc
M	source/blender/blenlib/tests/BLI_linklist_lockfree_test.cc
M	source/blender/compositor/intern/COM_WorkScheduler.cc
M	source/blender/depsgraph/intern/eval/deg_eval.cc
M	source/blender/editors/mesh/editmesh_undo.c
M	source/blender/editors/render/render_opengl.c
M	source/blender/editors/sculpt_paint/paint_image_proj.c
M	source/blender/editors/space_clip/clip_editor.c
M	source/blender/editors/space_clip/clip_ops.c
M	source/blender/editors/space_file/filelist.c
M	source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c
M	source/blender/imbuf/intern/imageprocess.c
M	source/blender/modifiers/intern/MOD_nodes_evaluator.cc

===================================================================

diff --git a/intern/cycles/blender/blender_python.cpp b/intern/cycles/blender/blender_python.cpp
index 785ca6b4e01..c58d2eb6e04 100644
--- a/intern/cycles/blender/blender_python.cpp
+++ b/intern/cycles/blender/blender_python.cpp
@@ -289,11 +289,10 @@ static PyObject *render_func(PyObject * /*self*/, PyObject *args)
   RNA_pointer_create(NULL, &RNA_Depsgraph, (ID *)PyLong_AsVoidPtr(pydepsgraph), &depsgraphptr);
   BL::Depsgraph b_depsgraph(depsgraphptr);
 
-  /* Allow Blender to execute other Python scripts, and isolate TBB tasks so we
-   * don't get deadlocks with Blender threads accessing shared data like images. */
+  /* Allow Blender to execute other Python scripts. */
   python_thread_state_save(&session->python_thread_state);
 
-  tbb::this_task_arena::isolate([&] { session->render(b_depsgraph); });
+  session->render(b_depsgraph);
 
   python_thread_state_restore(&session->python_thread_state);
 
@@ -330,8 +329,7 @@ static PyObject *bake_func(PyObject * /*self*/, PyObject *args)
 
   python_thread_state_save(&session->python_thread_state);
 
-  tbb::this_task_arena::isolate(
-      [&] { session->bake(b_depsgraph, b_object, pass_type, pass_filter, width, height); });
+  session->bake(b_depsgraph, b_object, pass_type, pass_filter, width, height);
 
   python_thread_state_restore(&session->python_thread_state);
 
@@ -377,7 +375,7 @@ static PyObject *reset_func(PyObject * /*self*/, PyObject *args)
 
   python_thread_state_save(&session->python_thread_state);
 
-  tbb::this_task_arena::isolate([&] { session->reset_session(b_data, b_depsgraph); });
+  session->reset_session(b_data, b_depsgraph);
 
   python_thread_state_restore(&session->python_thread_state);
 
@@ -399,7 +397,7 @@ static PyObject *sync_func(PyObject * /*self*/, PyObject *args)
 
   python_thread_state_save(&session->python_thread_state);
 
-  tbb::this_task_arena::isolate([&] { session->synchronize(b_depsgraph); });
+  session->synchronize(b_depsgraph);
 
   python_thread_state_restore(&session->python_thread_state);
 
diff --git a/source/blender/blenkernel/intern/bvhutils.c b/source/blender/blenkernel/intern/bvhutils.c
index bc63e423c09..116e6279657 100644
--- a/source/blender/blenkernel/intern/bvhutils.c
+++ b/source/blender/blenkernel/intern/bvhutils.c
@@ -31,6 +31,7 @@
 
 #include "BLI_linklist.h"
 #include "BLI_math.h"
+#include "BLI_task.h"
 #include "BLI_threads.h"
 #include "BLI_utildefines.h"
 
@@ -160,6 +161,26 @@ void bvhcache_free(BVHCache *bvh_cache)
   MEM_freeN(bvh_cache);
 }
 
+/* BVH tree balancing inside a mutex lock must be run in isolation. Balancing
+ * is multithreaded, and we do not want the current thread to start another task
+ * that may involve acquiring the same mutex lock that it is waiting for. */
+static void bvhtree_balance_isolated(void *userdata)
+{
+  BLI_bvhtree_balance((BVHTree *)userdata);
+}
+
+static void bvhtree_balance(BVHTree *tree, const bool isolate)
+{
+  if (tree) {
+    if (isolate) {
+      BLI_task_isolate(bvhtree_balance_isolated, tree);
+    }
+    else {
+      BLI_bvhtree_balance(tree);
+    }
+  }
+}
+
 /** \} */
 /* -------------------------------------------------------------------- */
 /** \name Local Callbacks
@@ -566,7 +587,6 @@ static BVHTree *bvhtree_from_editmesh_verts_create_tree(float epsilon,
       BLI_bvhtree_insert(tree, i, eve->co, 1);
     }
     BLI_assert(BLI_bvhtree_get_len(tree) == verts_num_active);
-    BLI_bvhtree_balance(tree);
   }
 
   return tree;
@@ -600,7 +620,6 @@ static BVHTree *bvhtree_from_mesh_verts_create_tree(float epsilon,
         BLI_bvhtree_insert(tree, i, vert[i].co, 1);
       }
       BLI_assert(BLI_bvhtree_get_len(tree) == verts_num_active);
-      BLI_bvhtree_balance(tree);
     }
   }
 
@@ -649,6 +668,7 @@ BVHTree *bvhtree_from_editmesh_verts_ex(BVHTreeFromEditMesh *data,
     if (data->cached == false) {
       tree = bvhtree_from_editmesh_verts_create_tree(
           epsilon, tree_type, axis, em, verts_mask, verts_num_active);
+      bvhtree_balance(tree, true);
 
       /* Save on cache for later use */
       /* printf("BVHTree built and saved on cache\n"); */
@@ -660,6 +680,7 @@ BVHTree *bvhtree_from_editmesh_verts_ex(BVHTreeFromEditMesh *data,
   else {
     tree = bvhtree_from_editmesh_verts_create_tree(
         epsilon, tree_type, axis, em, verts_mask, verts_num_active);
+    bvhtree_balance(tree, false);
   }
 
   if (tree) {
@@ -711,6 +732,7 @@ BVHTree *bvhtree_from_mesh_verts_ex(BVHTreeFromMesh *data,
   if (in_cache == false) {
     tree = bvhtree_from_mesh_verts_create_tree(
         epsilon, tree_type, axis, vert, verts_num, verts_mask, verts_num_active);
+    bvhtree_balance(tree, bvh_cache_p != NULL);
 
     if (bvh_cache_p) {
       /* Save on cache for later use */
@@ -771,7 +793,6 @@ static BVHTree *bvhtree_from_editmesh_edges_create_tree(float epsilon,
       BLI_bvhtree_insert(tree, i, co[0], 2);
     }
     BLI_assert(BLI_bvhtree_get_len(tree) == edges_num_active);
-    BLI_bvhtree_balance(tree);
   }
 
   return tree;
@@ -809,7 +830,6 @@ static BVHTree *bvhtree_from_mesh_edges_create_tree(const MVert *vert,
 
         BLI_bvhtree_insert(tree, i, co[0], 2);
       }
-      BLI_bvhtree_balance(tree);
     }
   }
 
@@ -861,7 +881,7 @@ BVHTree *bvhtree_from_editmesh_edges_ex(BVHTreeFromEditMesh *data,
     if (data->cached == false) {
       tree = bvhtree_from_editmesh_edges_create_tree(
           epsilon, tree_type, axis, em, edges_mask, edges_num_active);
-
+      bvhtree_balance(tree, true);
       /* Save on cache for later use */
       /* printf("BVHTree built and saved on cache\n"); */
       bvhcache_insert(bvh_cache, tree, bvh_cache_type);
@@ -872,6 +892,7 @@ BVHTree *bvhtree_from_editmesh_edges_ex(BVHTreeFromEditMesh *data,
   else {
     tree = bvhtree_from_editmesh_edges_create_tree(
         epsilon, tree_type, axis, em, edges_mask, edges_num_active);
+    bvhtree_balance(tree, false);
   }
 
   if (tree) {
@@ -928,12 +949,17 @@ BVHTree *bvhtree_from_mesh_edges_ex(BVHTreeFromMesh *data,
         vert, edge, edges_num, edges_mask, edges_num_active, epsilon, tree_type, axis);
 
     if (bvh_cache_p) {
+      bvhtree_balance(tree, true);
+
       BVHCache *bvh_cache = *bvh_cache_p;
       /* Save on cache for later use */
       /* printf("BVHTree built and saved on cache\n"); */
       bvhcache_insert(bvh_cache, tree, bvh_cache_type);
       in_cache = true;
     }
+    else {
+      bvhtree_balance(tree, false);
+    }
   }
 
   if (bvh_cache_p) {
@@ -994,7 +1020,6 @@ static BVHTree *bvhtree_from_mesh_faces_create_tree(float epsilon,
         }
       }
       BLI_assert(BLI_bvhtree_get_len(tree) == faces_num_active);
-      BLI_bvhtree_balance(tree);
     }
   }
 
@@ -1057,6 +1082,7 @@ BVHTree *bvhtree_from_mesh_faces_ex(BVHTreeFromMesh *data,
   if (in_cache == false) {
     tree = bvhtree_from_mesh_faces_create_tree(
         epsilon, tree_type, axis, vert, face, numFaces, faces_mask, faces_num_active);
+    bvhtree_balance(tree, bvh_cache_p != NULL);
 
     if (bvh_cache_p) {
       /* Save on cache for later use */
@@ -1127,7 +1153,6 @@ static BVHTree *bvhtree_from_editmesh_looptri_create_tree(float epsilon,
         }
       }
       BLI_assert(BLI_bvhtree_get_len(tree) == looptri_num_active);
-      BLI_bvhtree_balance(tree);
     }
   }
 
@@ -1173,7 +1198,6 @@ static BVHTree *bvhtree_from_mesh_looptri_create_tree(float epsilon,
         }
       }
       BLI_assert(BLI_bvhtree_get_len(tree) == looptri_num_active);
-      BLI_bvhtree_balance(tree);
     }
   }
 
@@ -1229,6 +1253,7 @@ BVHTree *bvhtree_from_editmesh_looptri_ex(BVHTreeFromEditMesh *data,
     bool in_cache = bvhcache_find(
         bvh_cache_p, bvh_cache_type, &tree, &lock_started, mesh_eval_mutex);
     BVHCache *bvh_cache = *bvh_cache_p;
+    bvhtree_balance(tree, true);
 
     if (in_cache == false) {
       tree = bvhtree_from_editmesh_looptri_create_tree(
@@ -1243,6 +1268,7 @@ BVHTree *bvhtree_from_editmesh_looptri_ex(BVHTreeFromEditMesh *data,
   else {
     tree = bvhtree_from_editmesh_looptri_create_tree(
         epsilon, tree_type, axis, em, looptri_mask, looptri_num_active);
+    bvhtree_balance(tree, false);
   }
 
   if (tree) {
@@ -1303,6 +1329,8 @@ BVHTree *bvhtree_from_mesh_looptri_ex(BVHTreeFromMesh *data,
                                                  looptri_mask,
                                                  looptri_num_active);
 
+    bvhtree_balance(tree, bvh_cache_p != NULL);
+
     if (bvh_cache_p) {
       BVHCache *bvh_cache = *bvh_cache_p;
       bvhcache_insert(bvh_cache, tree, bvh_cache_type);
@@ -1742,7 +1770,7 @@ BVHTree *BKE_bvhtree_from_pointcloud_get(BVHTreeFromPointCloud *data,
     BLI_bvhtree_insert(tree, i, pointcloud->co[i], 1);
   }
   BLI_assert(BLI_bvhtree_get_len(tree) == pointcloud->totpoint);
-  BLI_bvhtree_balance(tree);
+  bvhtree_balance(tree, false);
 
   data->coords = pointcloud->co;
   data->tree = tree;
diff --git a/source/blender/blenkernel/intern/editmesh_tangent.c b/source/blender/blenkernel/intern/editmesh_tangent.c
index 088a2087a96..d849f4ab37d 100644
--- a/source/blender/blenkernel/intern/editmesh_tangent.c
+++ b/source/blender/blenkernel/intern/editmesh_tangent.c
@@ -362,7 +362,7 @@ void BKE_editmesh_loop_tangent_calc(BMEditMesh *em,
     /* Calculation */
     if (em->tottri != 0) {
       TaskPool *task_pool;
-      task_pool = BLI_task_pool_create(NULL, TASK_PRIORITY_LOW, TASK_ISOLATION_ON);
+      task_pool = BLI_task_pool_create(NULL, TASK_PRIORITY_LOW);
 
       tangent_mask_curr = 0;
       /* Calculate tangent layers */
diff --git a/source/blender/blenkernel/intern/image.c b/source/blender/blenkernel/intern/image.c
index 2f7e2b41a73..dad518ec696 100644
--- a/source/blender/blenkernel/intern/image.c
+++ b/source/blender/blenkernel/intern/image.c
@@ -68,6 +68,7 @@
 #include "BLI_math_vector.h"
 #include "BLI_mempool.h"
 #include "BLI_system.h"
+#include "BLI_task.h"
 #include "BLI_threads.h"
 #include "BLI_timecode.h" /* For stamp time-code format. */
 #include "BLI_utildefines.h"
@@ -882,6 +883,39 @@ Image *BKE_image_load_exists(Main *bmain, const char *filepath)
   return BKE_image_load_exists_ex(bmain, filepath, NULL);
 }
 
+typedef struct ImageFillData {
+  short gen_type;
+  uint width;
+  uint

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list