Index: source/blender/editors/sculpt_paint/sculpt.c =================================================================== --- source/blender/editors/sculpt_paint/sculpt.c (revision 46566) +++ source/blender/editors/sculpt_paint/sculpt.c (working copy) @@ -212,6 +212,10 @@ float clip_tolerance[3]; float initial_mouse[2]; + int num_threads; + float (**tmpgrid_co)[3], (**tmprow_co)[3]; + float **tmpgrid_mask, **tmprow_mask; + /* Variants */ float radius; float radius_squared; @@ -1127,18 +1131,27 @@ NULL, &gridsize, &griddata, &gridadj); BLI_pbvh_get_grid_key(ss->pbvh, &key); - #pragma omp critical +#ifdef _OPENMP + if (sd->flags & SCULPT_USE_OPENMP) { - if (smooth_mask) { - tmpgrid_mask = MEM_mallocN(sizeof(float) * gridsize * gridsize, "tmpgrid_mask"); - tmprow_mask = MEM_mallocN(sizeof(float) * gridsize, "tmprow_mask"); - } - else { - tmpgrid_co = MEM_mallocN(sizeof(float) * 3 * gridsize * gridsize, "tmpgrid_co"); - tmprow_co = MEM_mallocN(sizeof(float) * 3 * gridsize, "tmprow_co"); - } + int thread_num = omp_get_thread_num(); + + tmpgrid_co = ss->cache->tmpgrid_co [thread_num]; + tmprow_co = ss->cache->tmprow_co [thread_num]; + + tmpgrid_mask = ss->cache->tmpgrid_mask [thread_num]; + tmprow_mask = ss->cache->tmprow_mask [thread_num]; } + else +#endif + { + tmpgrid_co = ss->cache->tmpgrid_co[0]; + tmprow_co = ss->cache->tmprow_co[0]; + tmpgrid_mask = ss->cache->tmpgrid_mask [0]; + tmprow_mask = ss->cache->tmprow_mask [0]; + } + for (i = 0; i < totgrid; ++i) { data = griddata[grid_indices[i]]; adj = &gridadj[grid_indices[i]]; @@ -1253,18 +1266,6 @@ } } } - - #pragma omp critical - { - if (smooth_mask) { - MEM_freeN(tmpgrid_mask); - MEM_freeN(tmprow_mask); - } - else { - MEM_freeN(tmpgrid_co); - MEM_freeN(tmprow_co); - } - } } static void smooth(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode, @@ -3094,6 +3095,69 @@ } } +static void sculpt_omp_start(Sculpt *sd, SculptSession *ss) +{ + int array_mem_size; + int gridsize; + int i; + StrokeCache *cache = ss->cache; + +#ifdef _OPENMP + /* If using OpenMP then create a number of threads two times the + * number of processor cores. + * Justification: Empirically I've found that two threads per + * processor gives higher throughput. */ + if (sd->flags & SCULPT_USE_OPENMP) { + cache->num_threads = 2 * omp_get_num_procs(); + omp_set_num_threads(cache->num_threads); + } + else +#endif + { + cache->num_threads = 1; + } + + if (ss->multires) { + BLI_pbvh_node_get_grids(ss->pbvh, NULL, NULL, NULL, NULL, &gridsize, NULL, NULL); + + array_mem_size = cache->num_threads * sizeof(void*); + + cache->tmpgrid_co = MEM_mallocN(array_mem_size, "tmpgrid_co array"); + cache->tmprow_co = MEM_mallocN(array_mem_size, "tmprow_co array"); + cache->tmpgrid_mask = MEM_mallocN(array_mem_size, "tmpgrid_mask array"); + cache->tmprow_mask = MEM_mallocN(array_mem_size, "tmprow_mask array"); + + for (i = 0; i < cache->num_threads; i++) { + const size_t row_size = sizeof(float) * gridsize; + const size_t co_row_size = 3 * row_size; + + cache->tmprow_co [i] = MEM_mallocN(co_row_size, "tmprow_co"); + cache->tmpgrid_co [i] = MEM_mallocN(co_row_size * gridsize, "tmpgrid_co"); + cache->tmprow_mask [i] = MEM_mallocN(row_size, "tmprow_mask"); + cache->tmpgrid_mask [i] = MEM_mallocN(row_size * gridsize, "tmpgrid_mask"); + } + } +} + +static void sculpt_omp_done(SculptSession *ss) +{ + if (ss->multires) { + int i; + + for (i = 0; i < ss->cache->num_threads; i++) { + MEM_freeN(ss->cache->tmpgrid_co [i]); + MEM_freeN(ss->cache->tmprow_co [i]); + MEM_freeN(ss->cache->tmpgrid_mask [i]); + MEM_freeN(ss->cache->tmprow_mask [i]); + } + + MEM_freeN(ss->cache->tmpgrid_co); + MEM_freeN(ss->cache->tmprow_co); + MEM_freeN(ss->cache->tmpgrid_mask); + MEM_freeN(ss->cache->tmprow_mask); + } +} + /* Initialize the stroke cache invariants from operator properties */ static void sculpt_update_cache_invariants(bContext *C, Sculpt *sd, SculptSession *ss, wmOperator *op, wmEvent *event) { @@ -3214,6 +3278,8 @@ cache->first_time = 1; cache->vertex_rotation = 0; + + sculpt_omp_start(sd, ss); } static void sculpt_update_brush_delta(Sculpt *sd, Object *ob, Brush *brush) @@ -3653,19 +3719,6 @@ sculpt_undo_push_begin(sculpt_tool_name(sd)); -#ifdef _OPENMP - /* If using OpenMP then create a number of threads two times the - * number of processor cores. - * Justification: Empirically I've found that two threads per - * processor gives higher throughput. */ - if (sd->flags & SCULPT_USE_OPENMP) { - int num_procs; - - num_procs = omp_get_num_procs(); - omp_set_num_threads(2 * num_procs); - } -#endif - return 1; } else @@ -3702,6 +3755,8 @@ SculptSession *ss = ob->sculpt; Sculpt *sd = CTX_data_tool_settings(C)->sculpt; + sculpt_omp_done(ss); + /* reset values used to draw brush after completing the stroke */ sd->draw_anchored = 0; sd->draw_pressure = 0;