From 31ec0915a17920cc9c4e3865d19536f5e856fd74 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 23 Oct 2009 22:12:03 +0000 Subject: limit thread number to cpu number --- weasel.c | 53 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/weasel.c b/weasel.c index fb86a2a..f683d70 100644 --- a/weasel.c +++ b/weasel.c @@ -15,6 +15,8 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include +#include #include #include #include @@ -29,7 +31,7 @@ pthread_attr_t attr; int population = 1000; int mutationrate = 100; /* 1/n */ -int display = 1000; +int display = 100; #if 0 const char defaim[] = "METHINKS IT IS LIKE A WEASEL"; @@ -51,6 +53,21 @@ struct creature { int locked; } *cp, **generation; +int +ncpu(void) +{ + int mib[2], n; + size_t len; + + mib[0] = CTL_HW; + mib[1] = HW_NCPU; + len = sizeof(n); + + if (sysctl(mib, 2, &n, &len, NULL, 0) == -1) + return 1; + + return n; +} char rndchar() { @@ -88,15 +105,15 @@ initpopulation(int length) cp->genom[i] = '\0'; cp->length = length; cp->fitness = calculatefitness(cp->genom, cp->length); - cp->locked = 0; generation[n] = cp; + cp->locked = 0; } } void printcreature(struct creature *c) { - printw("%1.3f\t%s\n", c->fitness, c->genom); + printw("%1.3f\t%s\t%d\n", c->fitness, c->genom, c->locked); } void @@ -124,8 +141,8 @@ pickrandom() int hasone = 0; while (!hasone) { - cp = generation[random() % population]; pthread_mutex_lock(&mutexsum); + cp = generation[random() % population]; if (!cp->locked) { cp->locked = 1; hasone = 1; @@ -212,12 +229,13 @@ int main(int argc, char **argv) { struct creature *best; - pthread_t dummy; - int i, c, n; + pthread_t *threads; + int i, c, n, nthreads; alphabet = defalpha; + nthreads = ncpu(); - while ((c = getopt(argc, argv, "a:m:p:d:")) != -1) + while ((c = getopt(argc, argv, "a:m:p:d:t:")) != -1) switch (c) { case 'a': alphabet = strdup(optarg); @@ -240,6 +258,12 @@ main(int argc, char **argv) usage(); display = i; break; + case 't': + i = atoi(optarg); + if (i <= 0) + usage(); + nthreads = i; + break; default: usage(); /* NOTREACHED */ @@ -250,6 +274,8 @@ main(int argc, char **argv) aim = argc ? strdup(*argv) : defaim; alphalen = strlen(alphabet); + threads = calloc(nthreads, sizeof(pthread_t)); + assert(threads); initscr(); srandom(time(NULL)); @@ -257,12 +283,17 @@ main(int argc, char **argv) pthread_mutex_init(&mutexsum, NULL); pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + /* pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); */ for (i = 0; !success(&best); i++) { - for (n = 0; n < display; n++) - pthread_create(&dummy, &attr, intercourse, NULL); - printpopulation(best); + for (n = 0; n < nthreads; n++) + pthread_create(&threads[n], &attr, intercourse, NULL); + for (n = 0; n < nthreads; n++) + pthread_join(threads[n], NULL); + if (i > display) { + display += i; + printpopulation(best); + } } pthread_attr_destroy(&attr); -- cgit v1.2.3