aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2009-10-23 22:12:03 +0000
committerDimitri Sokolyuk <demon@dim13.org>2009-10-23 22:12:03 +0000
commit31ec0915a17920cc9c4e3865d19536f5e856fd74 (patch)
tree65c1c8305f29dcaf18d91d8b96b0074377cab3d1
parentbeecc86c2b9c1f6ac2d399b24cf87a8418f0f043 (diff)
limit thread number to cpu number
-rw-r--r--weasel.c53
1 files 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 <sys/param.h>
+#include <sys/sysctl.h>
#include <assert.h>
#include <curses.h>
#include <pthread.h>
@@ -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);