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.
This commit is contained in:
Brecht Van Lommel 2013-05-08 13:23:17 +00:00
parent 3e763d7e4d
commit a07dcd67eb
12 changed files with 75 additions and 59 deletions

@ -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

@ -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");

@ -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);
}

@ -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--;

@ -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
*

@ -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)

@ -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

@ -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)
{

@ -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");
}

@ -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);
}
if (rd->mode & R_FIXED_THREADS)
return rd->threads;
static int rna_RenderSettings_threads_mode_get(PointerRNA *ptr)
{
RenderData *rd = (RenderData *)ptr->data;
int override = BLI_system_num_threads_override_get();
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);

@ -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

@ -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);
}