From eb4691098767935c1ffd10f7da46796c11eefcfa Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 28 Jun 2010 15:57:54 -0600 Subject: Use sched_getaffinity() rather than sysconf(_SC_NPROCESSORS_ONLN). Also, abstract cpu counting into dedicated dmnsn_ncpus() function. --- libdimension/Makefile.am | 6 ++++-- libdimension/dimension_impl.h | 2 ++ libdimension/platform.c | 35 +++++++++++++++++++++++++++++++++++ libdimension/platform.h | 29 +++++++++++++++++++++++++++++ libdimension/scene.c | 10 ++-------- 5 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 libdimension/platform.c create mode 100644 libdimension/platform.h (limited to 'libdimension') diff --git a/libdimension/Makefile.am b/libdimension/Makefile.am index 732233e..905606d 100644 --- a/libdimension/Makefile.am +++ b/libdimension/Makefile.am @@ -49,8 +49,6 @@ lib_LTLIBRARIES = libdimension.la libdimension_la_SOURCES = $(nobase_include_HEADERS) \ ambient.c \ - prtree.c \ - prtree.h \ camera.c \ canvas.c \ canvas_pigment.c \ @@ -71,8 +69,12 @@ libdimension_la_SOURCES = $(nobase_include_HEADERS) \ perspective.c \ phong.c \ plane.c \ + platform.c \ + platform.h \ point_light.c \ progress.c \ + prtree.c \ + prtree.h \ raytrace.c \ reflective.c \ scene.c \ diff --git a/libdimension/dimension_impl.h b/libdimension/dimension_impl.h index 3a37386..cf1671e 100644 --- a/libdimension/dimension_impl.h +++ b/libdimension/dimension_impl.h @@ -21,7 +21,9 @@ #ifndef DIMENSION_IMPL_H #define DIMENSION_IMPL_H +#define _GNU_SOURCE #include "dimension.h" +#include "platform.h" #include "threads.h" #include "prtree.h" diff --git a/libdimension/platform.c b/libdimension/platform.c new file mode 100644 index 0000000..6ac15c0 --- /dev/null +++ b/libdimension/platform.c @@ -0,0 +1,35 @@ +/************************************************************************* + * Copyright (C) 2010 Tavian Barnes * + * * + * This file is part of The Dimension Library. * + * * + * The Dimension Library is free software; you can redistribute it and/ * + * or modify it under the terms of the GNU Lesser General Public License * + * as published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Library is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this program. If not, see * + * . * + *************************************************************************/ + +#include "dimension_impl.h" +#include /* For sysconf() */ +#include /* For sched_getaffinity() */ + +size_t +dmnsn_ncpus() +{ + cpu_set_t cpuset; + if (sched_getaffinity(0, sizeof(cpuset), &cpuset) == 0) { + return CPU_COUNT(&cpuset); + } else { + dmnsn_error(DMNSN_SEVERITY_MEDIUM, "sched_getaffinity() failed."); + return 1; + } +} diff --git a/libdimension/platform.h b/libdimension/platform.h new file mode 100644 index 0000000..bda83dd --- /dev/null +++ b/libdimension/platform.h @@ -0,0 +1,29 @@ +/************************************************************************* + * Copyright (C) 2010 Tavian Barnes * + * * + * This file is part of The Dimension Library. * + * * + * The Dimension Library is free software; you can redistribute it and/ * + * or modify it under the terms of the GNU Lesser General Public License * + * as published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Library is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this program. If not, see * + * . * + *************************************************************************/ + +#ifndef DIMENSION_IMPL_UTILITIES_H +#define DIMENSION_IMPL_UTILITIES_H + +#include + +/* Return the number of CPUs available to dimension */ +size_t dmnsn_ncpus(); + +#endif /* DIMENSION_IMPL_UTILITIES_H */ diff --git a/libdimension/scene.c b/libdimension/scene.c index 10af1b2..c81f936 100644 --- a/libdimension/scene.c +++ b/libdimension/scene.c @@ -18,9 +18,8 @@ * . * *************************************************************************/ -#include "dimension.h" +#include "dimension_impl.h" #include -#include /* For sysconf */ /* Allocate an empty scene */ dmnsn_scene * @@ -35,12 +34,7 @@ dmnsn_new_scene() scene->lights = dmnsn_new_array(sizeof(dmnsn_light *)); scene->quality = DMNSN_RENDER_FULL; scene->reclimit = 5; - - /* 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; + scene->nthreads = dmnsn_ncpus(); return scene; } -- cgit v1.2.3