From c76c0f017fa884c4b9a04ff7fa3b43d331c82d84 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 23 Feb 2010 14:21:48 -0500 Subject: Determine nthreads in dmnsn_new_scene(). --- dimension/main.c | 26 ++++++++++++++------------ libdimension/raytrace.c | 15 +++++---------- libdimension/scene.c | 8 +++++++- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/dimension/main.c b/dimension/main.c index 00c0fdf..fa7f5a6 100644 --- a/dimension/main.c +++ b/dimension/main.c @@ -39,18 +39,18 @@ main(int argc, char **argv) { /* Long-only option codes */ enum { - DMNSN_NTHREADS = 256 + DMNSN_OPT_THREADS = 256 }; static struct option long_options[] = { - { "output", required_argument, NULL, 'o' }, - { "input", required_argument, NULL, 'i' }, - { "width", required_argument, NULL, 'w' }, - { "height", required_argument, NULL, 'h' }, - { "threads", required_argument, NULL, DMNSN_NTHREADS }, - { "tokenize", no_argument, &tokenize, 1 }, - { "parse", no_argument, &parse, 1 }, - { 0, 0, 0, 0 } + { "output", required_argument, NULL, 'o' }, + { "input", required_argument, NULL, 'i' }, + { "width", required_argument, NULL, 'w' }, + { "height", required_argument, NULL, 'h' }, + { "threads", required_argument, NULL, DMNSN_OPT_THREADS }, + { "tokenize", no_argument, &tokenize, 1 }, + { "parse", no_argument, &parse, 1 }, + { 0, 0, 0, 0 } }; int opt, opt_index; @@ -105,12 +105,12 @@ main(int argc, char **argv) { break; } - case DMNSN_NTHREADS: + case DMNSN_OPT_THREADS: { char *endptr; nthreads = strtoul(optarg, &endptr, 10); if (*endptr != '\0' || endptr == optarg) { - fprintf(stderr, "Invalid argument to --nthreads!\n"); + fprintf(stderr, "Invalid argument to --threads!\n"); return EXIT_FAILURE; } break; @@ -216,7 +216,9 @@ main(int argc, char **argv) { return EXIT_FAILURE; } - scene->nthreads = nthreads; + /* Set the new number of threads if --threads changed it */ + if (nthreads) + scene->nthreads = nthreads; /* * Now we render the scene diff --git a/libdimension/raytrace.c b/libdimension/raytrace.c index 6721a9b..3fa873c 100644 --- a/libdimension/raytrace.c +++ b/libdimension/raytrace.c @@ -19,7 +19,6 @@ *************************************************************************/ #include "dimension_impl.h" -#include /* For sysconf */ /* * Boilerplate for multithreading @@ -109,20 +108,16 @@ static void *dmnsn_raytrace_scene_multithread_thread(void *ptr); static int dmnsn_raytrace_scene_multithread(dmnsn_raytrace_payload *payload) { - int i, j, nthreads = payload->scene->nthreads; + int i, j; void *ptr; int retval = 0; dmnsn_raytrace_payload *payloads; pthread_t *threads; - if (!nthreads) { - /* Find the number of processors/cores running (TODO: do this portably) */ - nthreads = sysconf(_SC_NPROCESSORS_ONLN); - if (nthreads < 1) { - nthreads = 1; - } - /* End non-portable section */ - } + unsigned int nthreads = payload->scene->nthreads; + /* Sanity check */ + if (nthreads < 1) + nthreads = 1; payloads = malloc(nthreads*sizeof(dmnsn_raytrace_payload)); if (!payloads) { diff --git a/libdimension/scene.c b/libdimension/scene.c index 8973005..8ebf706 100644 --- a/libdimension/scene.c +++ b/libdimension/scene.c @@ -20,6 +20,7 @@ #include "dimension.h" #include /* For malloc */ +#include /* For sysconf */ /* Allocate an empty scene */ dmnsn_scene * @@ -39,7 +40,12 @@ dmnsn_new_scene() scene->lights = dmnsn_new_array(sizeof(dmnsn_light *)); scene->quality = DMNSN_RENDER_FULL; scene->reclimit = 5; - scene->nthreads = 0; + + /* Find the number of processors/cores running (TODO: do this portably) */ + int nprocs = sysconf(_SC_NPROCESSORS_ONLN); + if (nprocs < 1) + nprocs = 1; + scene->nthreads = nprocs; } return scene; } -- cgit v1.2.3