From a07dcd67ebf63fad08536b1e78bbb61e18fa51e6 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Wed, 8 May 2013 13:23:17 +0000 Subject: Fix #35240: command line -t number of threads option did not work for cycles. Now it works for blender internal, cycles and other multithreading code in Blender in both background and UI mode. --- source/blender/blenkernel/BKE_scene.h | 3 +++ source/blender/blenkernel/intern/particle_system.c | 7 +----- source/blender/blenkernel/intern/scene.c | 26 ++++++++++++++++++++++ source/blender/blenkernel/intern/softbody.c | 11 +++------ source/blender/blenlib/BLI_threads.h | 2 ++ source/blender/blenlib/intern/threads.c | 14 ++++++++++++ .../editors/armature/editarmature_retarget.c | 9 ++------ source/blender/editors/object/object_bake.c | 5 +++-- .../editors/sculpt_paint/paint_image_proj.c | 8 ++----- source/blender/makesrna/intern/rna_scene.c | 14 +++++++++--- source/blender/render/intern/source/pipeline.c | 25 ++------------------- source/creator/creator.c | 10 +++++---- 12 files changed, 75 insertions(+), 59 deletions(-) diff --git a/source/blender/blenkernel/BKE_scene.h b/source/blender/blenkernel/BKE_scene.h index 1171e42cad7..74a847a01d4 100644 --- a/source/blender/blenkernel/BKE_scene.h +++ b/source/blender/blenkernel/BKE_scene.h @@ -119,6 +119,9 @@ void BKE_scene_disable_color_management(struct Scene *scene); int BKE_scene_check_color_management_enabled(const struct Scene *scene); int BKE_scene_check_rigidbody_active(const struct Scene *scene); +int BKE_scene_num_threads(const struct Scene *scene); +int BKE_render_num_threads(const struct RenderData *r); + #ifdef __cplusplus } #endif diff --git a/source/blender/blenkernel/intern/particle_system.c b/source/blender/blenkernel/intern/particle_system.c index 74a50389cd5..a2ba09dd6af 100644 --- a/source/blender/blenkernel/intern/particle_system.c +++ b/source/blender/blenkernel/intern/particle_system.c @@ -1439,12 +1439,7 @@ ParticleThread *psys_threads_create(ParticleSimulationData *sim) { ParticleThread *threads; ParticleThreadContext *ctx; - int i, totthread; - - if (sim->scene->r.mode & R_FIXED_THREADS) - totthread= sim->scene->r.threads; - else - totthread= BLI_system_thread_count(); + int i, totthread = BKE_scene_num_threads(sim->scene); threads= MEM_callocN(sizeof(ParticleThread)*totthread, "ParticleThread"); ctx= MEM_callocN(sizeof(ParticleThreadContext), "ParticleThreadContext"); diff --git a/source/blender/blenkernel/intern/scene.c b/source/blender/blenkernel/intern/scene.c index c7a8c96a232..e93c32ddf64 100644 --- a/source/blender/blenkernel/intern/scene.c +++ b/source/blender/blenkernel/intern/scene.c @@ -56,6 +56,7 @@ #include "BLI_utildefines.h" #include "BLI_callbacks.h" #include "BLI_string.h" +#include "BLI_threads.h" #include "BLF_translation.h" @@ -1469,3 +1470,28 @@ int BKE_scene_check_rigidbody_active(const Scene *scene) { return scene && scene->rigidbody_world && scene->rigidbody_world->group && !(scene->rigidbody_world->flag & RBW_FLAG_MUTED); } + +int BKE_render_num_threads(const RenderData *rd) +{ + int threads; + + /* override set from command line? */ + threads = BLI_system_num_threads_override_get(); + + if (threads > 0) + return threads; + + /* fixed number of threads specified in scene? */ + if (rd->mode & R_FIXED_THREADS) + threads = rd->threads; + else + threads = BLI_system_thread_count(); + + return max_ii(threads, 1); +} + +int BKE_scene_num_threads(const Scene *scene) +{ + return BKE_render_num_threads(&scene->r); +} + diff --git a/source/blender/blenkernel/intern/softbody.c b/source/blender/blenkernel/intern/softbody.c index d600b82522d..5011080234e 100644 --- a/source/blender/blenkernel/intern/softbody.c +++ b/source/blender/blenkernel/intern/softbody.c @@ -79,6 +79,7 @@ variables on the UI for now #include "BKE_pointcache.h" #include "BKE_deform.h" #include "BKE_mesh.h" +#include "BKE_scene.h" #include "PIL_time.h" // #include "ONL_opennl.h" remove linking to ONL for now @@ -1664,10 +1665,7 @@ static void sb_sfesf_threads_run(Scene *scene, struct Object *ob, float timenow, do_effector= pdInitEffectors(scene, ob, NULL, ob->soft->effector_weights); /* figure the number of threads while preventing pretty pointless threading overhead */ - if (scene->r.mode & R_FIXED_THREADS) - totthread= scene->r.threads; - else - totthread= BLI_system_thread_count(); + totthread= BKE_scene_num_threads(scene); /* what if we got zillions of CPUs running but less to spread*/ while ((totsprings/totthread < lowsprings) && (totthread > 1)) { totthread--; @@ -2395,10 +2393,7 @@ static void sb_cf_threads_run(Scene *scene, Object *ob, float forcetime, float t int lowpoints =100; /* wild guess .. may increase with better thread management 'above' or even be UI option sb->spawn_cf_threads_nopts */ /* figure the number of threads while preventing pretty pointless threading overhead */ - if (scene->r.mode & R_FIXED_THREADS) - totthread= scene->r.threads; - else - totthread= BLI_system_thread_count(); + totthread= BKE_scene_num_threads(scene); /* what if we got zillions of CPUs running but less to spread*/ while ((totpoint/totthread < lowpoints) && (totthread > 1)) { totthread--; diff --git a/source/blender/blenlib/BLI_threads.h b/source/blender/blenlib/BLI_threads.h index 614cd4ee59d..331cac3ed76 100644 --- a/source/blender/blenlib/BLI_threads.h +++ b/source/blender/blenlib/BLI_threads.h @@ -68,6 +68,8 @@ void BLI_end_threaded_malloc(void); /* System Information */ int BLI_system_thread_count(void); /* gets the number of threads the system can make use of */ +void BLI_system_num_threads_override_set(int num); +int BLI_system_num_threads_override_get(void); /* Global Mutex Locks * diff --git a/source/blender/blenlib/intern/threads.c b/source/blender/blenlib/intern/threads.c index 8b1ba38a35a..84a8af71dd5 100644 --- a/source/blender/blenlib/intern/threads.c +++ b/source/blender/blenlib/intern/threads.c @@ -115,6 +115,7 @@ static pthread_mutex_t _movieclip_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t _colormanage_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_t mainid; static int thread_levels = 0; /* threads can be invoked inside threads */ +static int num_threads_override = 0; /* just a max for security reasons */ #define RE_MAX_THREAD BLENDER_MAX_THREADS @@ -322,6 +323,9 @@ int BLI_system_thread_count(void) t = (int)sysconf(_SC_NPROCESSORS_ONLN); # endif #endif + + if (num_threads_override > 0) + return num_threads_override; if (t > RE_MAX_THREAD) return RE_MAX_THREAD; @@ -331,6 +335,16 @@ int BLI_system_thread_count(void) return t; } +void BLI_system_num_threads_override_set(int num) +{ + num_threads_override = num; +} + +int BLI_system_num_threads_override_get(void) +{ + return num_threads_override; +} + /* Global Mutex Locks */ void BLI_lock_thread(int type) diff --git a/source/blender/editors/armature/editarmature_retarget.c b/source/blender/editors/armature/editarmature_retarget.c index a6ee2a2acad..687455495a1 100644 --- a/source/blender/editors/armature/editarmature_retarget.c +++ b/source/blender/editors/armature/editarmature_retarget.c @@ -40,6 +40,7 @@ #include "BKE_constraint.h" #include "BKE_armature.h" #include "BKE_context.h" +#include "BKE_scene.h" #include "ED_armature.h" #include "ED_util.h" @@ -283,14 +284,8 @@ static RigGraph *newRigGraph(void) rg->free_node = NULL; #ifdef USE_THREADS -// if (G.scene->r.mode & R_FIXED_THREADS) -// { -// totthread = G.scene->r.threads; -// } -// else -// { + //totthread = BKE_scene_num_threads(G.scene); totthread = BLI_system_thread_count(); -// } rg->worker = BLI_create_worker(exec_retargetArctoArc, totthread, 20); /* fix number of threads */ #endif diff --git a/source/blender/editors/object/object_bake.c b/source/blender/editors/object/object_bake.c index 01e614fef41..d02e58913e4 100644 --- a/source/blender/editors/object/object_bake.c +++ b/source/blender/editors/object/object_bake.c @@ -63,6 +63,7 @@ #include "BKE_subsurf.h" #include "BKE_depsgraph.h" #include "BKE_mesh.h" +#include "BKE_scene.h" #include "RE_pipeline.h" #include "RE_shader_ext.h" @@ -339,7 +340,7 @@ static int multiresbake_image_exec_locked(bContext *C, wmOperator *op) bkr.number_of_rays = scene->r.bake_samples; bkr.raytrace_structure = scene->r.raytrace_structure; bkr.octree_resolution = scene->r.ocres; - bkr.threads = scene->r.mode & R_FIXED_THREADS ? scene->r.threads : 0; + bkr.threads = BKE_scene_num_threads(scene); /* create low-resolution DM (to bake to) and hi-resolution DM (to bake from) */ bkr.hires_dm = multiresbake_create_hiresdm(scene, ob, &bkr.tot_lvl, &bkr.simple); @@ -377,7 +378,7 @@ static void init_multiresbake_job(bContext *C, MultiresBakeJob *bkj) bkj->number_of_rays = scene->r.bake_samples; bkj->raytrace_structure = scene->r.raytrace_structure; bkj->octree_resolution = scene->r.ocres; - bkj->threads = scene->r.mode & R_FIXED_THREADS ? scene->r.threads : 0; + bkj->threads = BKE_scene_num_threads(scene); CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases) { diff --git a/source/blender/editors/sculpt_paint/paint_image_proj.c b/source/blender/editors/sculpt_paint/paint_image_proj.c index 3dff3c7846a..7523d10996a 100644 --- a/source/blender/editors/sculpt_paint/paint_image_proj.c +++ b/source/blender/editors/sculpt_paint/paint_image_proj.c @@ -3047,12 +3047,8 @@ static void project_paint_begin(ProjPaintState *ps) * threads is being able to fill in multiple buckets at once. * Only use threads for bigger brushes. */ - if (ps->scene->r.mode & R_FIXED_THREADS) { - ps->thread_tot = ps->scene->r.threads; - } - else { - ps->thread_tot = BLI_system_thread_count(); - } + ps->thread_tot = BKE_scene_num_threads(ps->scene); + for (a = 0; a < ps->thread_tot; a++) { ps->arena_mt[a] = BLI_memarena_new(1 << 16, "project paint arena"); } diff --git a/source/blender/makesrna/intern/rna_scene.c b/source/blender/makesrna/intern/rna_scene.c index 0d224469b4e..6cbfe739898 100644 --- a/source/blender/makesrna/intern/rna_scene.c +++ b/source/blender/makesrna/intern/rna_scene.c @@ -671,11 +671,18 @@ static char *rna_RenderSettings_path(PointerRNA *UNUSED(ptr)) static int rna_RenderSettings_threads_get(PointerRNA *ptr) { RenderData *rd = (RenderData *)ptr->data; + return BKE_render_num_threads(rd); +} + +static int rna_RenderSettings_threads_mode_get(PointerRNA *ptr) +{ + RenderData *rd = (RenderData *)ptr->data; + int override = BLI_system_num_threads_override_get(); - if (rd->mode & R_FIXED_THREADS) - return rd->threads; + if (override > 0) + return R_FIXED_THREADS; else - return BLI_system_thread_count(); + return (rd->mode & R_FIXED_THREADS); } static int rna_RenderSettings_is_movie_fomat_get(PointerRNA *ptr) @@ -4327,6 +4334,7 @@ static void rna_def_scene_render_data(BlenderRNA *brna) prop = RNA_def_property(srna, "threads_mode", PROP_ENUM, PROP_NONE); RNA_def_property_enum_bitflag_sdna(prop, NULL, "mode"); RNA_def_property_enum_items(prop, threads_mode_items); + RNA_def_property_enum_funcs(prop, "rna_RenderSettings_threads_mode_get", NULL, NULL); RNA_def_property_ui_text(prop, "Threads Mode", "Determine the amount of render threads used"); RNA_def_property_update(prop, NC_SCENE | ND_RENDER_OPTIONS, NULL); diff --git a/source/blender/render/intern/source/pipeline.c b/source/blender/render/intern/source/pipeline.c index e2d6633fffc..c21196392dc 100644 --- a/source/blender/render/intern/source/pipeline.c +++ b/source/blender/render/intern/source/pipeline.c @@ -124,10 +124,7 @@ /* here we store all renders */ static struct { ListBase renderlist; - - /* commandline thread override */ - int threads; -} RenderGlobal = {{NULL, NULL}, -1}; +} RenderGlobal = {{NULL, NULL}}; /* hardcopy of current render, used while rendering for speed */ Render R; @@ -2748,27 +2745,9 @@ int RE_ReadRenderResult(Scene *scene, Scene *scenode) return success; } -void RE_set_max_threads(int threads) -{ - if (threads == 0) { - RenderGlobal.threads = BLI_system_thread_count(); - } - else if (threads >= 1 && threads <= BLENDER_MAX_THREADS) { - RenderGlobal.threads = threads; - } - else { - printf("Error, threads has to be in range 0-%d\n", BLENDER_MAX_THREADS); - } -} - void RE_init_threadcount(Render *re) { - if (RenderGlobal.threads >= 1) { /* only set as an arg in background mode */ - re->r.threads = MIN2(RenderGlobal.threads, BLENDER_MAX_THREADS); - } - else if ((re->r.mode & R_FIXED_THREADS) == 0 || RenderGlobal.threads == 0) { /* Automatic threads */ - re->r.threads = BLI_system_thread_count(); - } + re->r.threads = BKE_render_num_threads(&re->r); } /* loads in image into a result, size must match diff --git a/source/creator/creator.c b/source/creator/creator.c index 5c6f85c918d..418c05e22ce 100644 --- a/source/creator/creator.c +++ b/source/creator/creator.c @@ -821,11 +821,13 @@ static int set_image_type(int argc, const char **argv, void *data) static int set_threads(int argc, const char **argv, void *UNUSED(data)) { if (argc > 1) { - if (G.background) { - RE_set_max_threads(atoi(argv[1])); + int threads = atoi(argv[1]); + + if (threads >= 0 && threads <= BLENDER_MAX_THREADS) { + BLI_system_num_threads_override_set(threads); } else { - printf("Warning: threads can only be set in background mode\n"); + printf("Error, threads has to be in range 0-%d\n", BLENDER_MAX_THREADS); } return 1; } @@ -1406,7 +1408,7 @@ static void setupArguments(bContext *C, bArgs *ba, SYS_SystemHandle *syshandle) BLI_argsAdd(ba, 4, "-E", "--engine", "\n\tSpecify the render engine\n\tuse -E help to list available engines", set_engine, C); BLI_argsAdd(ba, 4, "-F", "--render-format", format_doc, set_image_type, C); - BLI_argsAdd(ba, 4, "-t", "--threads", "\n\tUse amount of for rendering in background\n\t[1-" STRINGIFY(BLENDER_MAX_THREADS) "], 0 for systems processor count.", set_threads, NULL); + BLI_argsAdd(ba, 4, "-t", "--threads", "\n\tUse amount of for rendering and other operations\n\t[1-" STRINGIFY(BLENDER_MAX_THREADS) "], 0 for systems processor count.", set_threads, NULL); BLI_argsAdd(ba, 4, "-x", "--use-extension", "\n\tSet option to add the file extension to the end of the file", set_extension, C); } -- cgit v1.2.3