Fix #35240: command line -t number of threads option did not work for cycles.
authorBrecht Van Lommel <brechtvanlommel@pandora.be>
Wed, 8 May 2013 13:23:17 +0000 (13:23 +0000)
committerBrecht Van Lommel <brechtvanlommel@pandora.be>
Wed, 8 May 2013 13:23:17 +0000 (13:23 +0000)
Now it works for blender internal, cycles and other multithreading code in
Blender in both background and UI mode.

12 files changed:
source/blender/blenkernel/BKE_scene.h
source/blender/blenkernel/intern/particle_system.c
source/blender/blenkernel/intern/scene.c
source/blender/blenkernel/intern/softbody.c
source/blender/blenlib/BLI_threads.h
source/blender/blenlib/intern/threads.c
source/blender/editors/armature/editarmature_retarget.c
source/blender/editors/object/object_bake.c
source/blender/editors/sculpt_paint/paint_image_proj.c
source/blender/makesrna/intern/rna_scene.c
source/blender/render/intern/source/pipeline.c
source/creator/creator.c

index 1171e42cad7252513ca6ab215a4638c5a6015672..74a847a01d4d8beb00ab49fd0ab95f5a80856eee 100644 (file)
@@ -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
index 74a50389cd58658c13f2a906375a0de98f078d44..a2ba09dd6af0d0bd9e7099faae46eb72a34f3803 100644 (file)
@@ -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");
index c7a8c96a2321431ec78abb8a9c6749b35db5a712..e93c32ddf645c761508b381f77c0936a6ddf2fe6 100644 (file)
@@ -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);
+}
+
index d600b82522d2e2326377da15084b3e13d27ed23e..5011080234e8170f53b79635053399b6dc7f35ea 100644 (file)
@@ -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--;
index 614cd4ee59dfc7eba1185832229880eb272ef133..331cac3ed7686e1e588dcbe6725cf35f07b6c635 100644 (file)
@@ -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
  * 
index 8b1ba38a35a8dffb18438761c99ae962b79102c2..84a8af71dd58305138770b58b4a2c3991d19dc03 100644 (file)
@@ -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)
index a6ee2a2acad8d1d86775d6ea458c868d1f017bb1..687455495a12459dab36af1ecf00c32b9fa16b00 100644 (file)
@@ -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
index 01e614fef41a512729547a61a982f2f7c614de49..d02e58913e4773cd6241c0128b59966af5dd2206 100644 (file)
@@ -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)
        {
index 3dff3c7846ad37f72315bdc917ea161894813541..7523d10996aea93bcdf08dc171499144392fe856 100644 (file)
@@ -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");
        }
index 0d224469b4e473fe74905afcd8727880834cae02..6cbfe7398986dbda8008ce5691b189a5ba84c968 100644 (file)
@@ -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);
        
index e2d6633fffce64b109bd926dae5306f2d6ec89cf..c21196392dc36cf9358aa082667199376d973fad 100644 (file)
 /* 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
index 5c6f85c918df028f5b96d4119879a837564facdf..418c05e22ce69ce679b39e6425255226faf4bbed 100644 (file)
@@ -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", "<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", "<threads>\n\tUse amount of <threads> 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", "<threads>\n\tUse amount of <threads> 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", "<bool>\n\tSet option to add the file extension to the end of the file", set_extension, C);
 
 }