diff options
author | Simon Quigley <tsimonq2@ubuntu.com> | 2017-06-19 15:19:47 -0500 |
---|---|---|
committer | Simon Quigley <tsimonq2@ubuntu.com> | 2017-06-19 15:19:47 -0500 |
commit | 7aacc9f2510901c9e97b30fa9bcb550bb7f99c03 (patch) | |
tree | 16908948750c11da8332d80d8bb9b339399ee4d7 /modules/benchmark/nqueens.c | |
parent | 7c47b5b9584f5011aeba18d7e1b26b3d3124825f (diff) |
New upstream version 0.5.1+git20170605
Diffstat (limited to 'modules/benchmark/nqueens.c')
-rw-r--r-- | modules/benchmark/nqueens.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/modules/benchmark/nqueens.c b/modules/benchmark/nqueens.c new file mode 100644 index 00000000..a32ed8c1 --- /dev/null +++ b/modules/benchmark/nqueens.c @@ -0,0 +1,66 @@ +/* + * N-Queens Problem Solver + * Found somewhere on the Internet; can't remember where. Possibly Wikipedia. + */ +#include <stdio.h> +#include <stdbool.h> +#include <stdlib.h> + +#include "hardinfo.h" +#include "benchmark.h" + +#define QUEENS 11 + +int row[QUEENS]; + +bool safe(int x, int y) +{ + int i; + for (i = 1; i <= y; i++) + if (row[y - i] == x || row[y - i] == x - i || row[y - i] == x + i) + return false; + return true; +} + +int nqueens(int y) +{ + int x; + + for (x = 0; x < QUEENS; x++) { + if (safe((row[y - 1] = x), y - 1)) { + if (y < QUEENS) { + nqueens(y + 1); + } else { + break; + } + } + } + + return 0; +} + +static gpointer nqueens_for(unsigned int start, unsigned int end, void *data, gint thread_number) +{ + unsigned int i; + + for (i = start; i <= end; i++) { + nqueens(0); + } + + return NULL; +} + +void +benchmark_nqueens(void) +{ + gdouble elapsed = 0; + + shell_view_set_enabled(FALSE); + shell_status_update("Running N-Queens benchmark..."); + + elapsed = benchmark_parallel_for(0, 10, nqueens_for, NULL); + + bench_results[BENCHMARK_NQUEENS] = elapsed; +} + + |