Commit 47ddcd03 authored by Peter W. Draper's avatar Peter W. Draper
Browse files

Merge branch 'threadpool_task_plots2' into 'master'

Fixes to the threadpool

Don't free the `tp->threads` struct when not allocated (1 thread).
Also set the function when logging one thread (otherwise we don't 
get the function names in log).

More testing to check 1 thread behaviour and different numbers of
threads. Tweak output to make progress more obvious.

See merge request !390
parents a590b4d9 0dfdf3aa
......@@ -268,6 +268,7 @@ void threadpool_map(struct threadpool *tp, threadpool_map_function map_function,
if (tp->num_threads == 1) {
map_function(map_data, N, extra_data);
#ifdef SWIFT_DEBUG_THREADPOOL
tp->map_function = map_function;
threadpool_log(tp, 0, N, tic, getticks());
#endif
return;
......@@ -330,10 +331,11 @@ void threadpool_clean(struct threadpool *tp) {
if (pthread_barrier_destroy(&tp->wait_barrier) != 0 ||
pthread_barrier_destroy(&tp->run_barrier) != 0)
error("Failed to destroy threadpool barriers.");
/* Clean up memory. */
free(tp->threads);
}
/* Clean up memory. */
free(tp->threads);
#ifdef SWIFT_DEBUG_THREADPOOL
for (int k = 0; k < tp->num_threads; k++) {
free(tp->logs[k].log);
......
......@@ -33,7 +33,7 @@ void map_function_first(void *map_data, int num_elements, void *extra_data) {
for (int ind = 0; ind < num_elements; ind++) {
int input = inputs[ind];
usleep(rand() % 1000000);
printf("map_function_first: got input %i.\n", input);
printf(" map_function_first: got input %i.\n", input);
fflush(stdout);
}
}
......@@ -43,7 +43,7 @@ void map_function_second(void *map_data, int num_elements, void *extra_data) {
for (int ind = 0; ind < num_elements; ind++) {
int input = inputs[ind];
usleep(rand() % 1000000);
printf("map_function_second: got input %i.\n", input);
printf(" map_function_second: got input %i.\n", input);
fflush(stdout);
}
}
......@@ -51,42 +51,49 @@ void map_function_second(void *map_data, int num_elements, void *extra_data) {
int main(int argc, char *argv[]) {
// Some constants for this test.
const int num_threads = 16;
const int N = 20;
const int num_runs = 2;
// Create a threadpool with 8 threads.
struct threadpool tp;
threadpool_init(&tp, num_threads);
// Create threadpools with different numbers of threads.
for (int num_thread = 1; num_thread <= 16; num_thread *= 4) {
printf("# Creating threadpool with %d threads\n", num_thread);
struct threadpool tp;
threadpool_init(&tp, num_thread);
// Main loop.
for (int run = 0; run < num_runs; run++) {
// Main loop.
for (int run = 0; run < num_runs; run++) {
// Run over a set of integers and print them.
int data[N];
for (int k = 0; k < N; k++) data[k] = k;
printf("processing integers from 0..%i.\n", N);
fflush(stdout);
threadpool_map(&tp, map_function_first, data, N, sizeof(int), 1, NULL);
// Run over a set of integers and print them.
int data[N];
for (int k = 0; k < N; k++) data[k] = k;
printf("1..processing integers from 0..%i.\n", N);
fflush(stdout);
threadpool_map(&tp, map_function_first, data, N, sizeof(int), 1, NULL);
// Do the same thing again, with less jobs than threads.
printf("processing integers from 0..%i.\n", N / 2);
fflush(stdout);
threadpool_map(&tp, map_function_second, data, N / 2, sizeof(int), 1, NULL);
// Do the same thing again, with less jobs than threads.
printf("2..processing integers from 0..%i.\n", N / 2);
fflush(stdout);
threadpool_map(&tp, map_function_second, data, N / 2, sizeof(int), 1,
NULL);
// Do the same thing again, with a chunk size of two.
printf("processing integers from 0..%i.\n", N);
fflush(stdout);
threadpool_map(&tp, map_function_first, data, N, sizeof(int), 2, NULL);
}
// Do the same thing again, with a chunk size of two.
printf("3..processing integers from 0..%i.\n", N);
fflush(stdout);
threadpool_map(&tp, map_function_first, data, N, sizeof(int), 2, NULL);
}
/* If logging was enabled, dump the log. */
/* If logging was enabled, dump the log. */
#ifdef SWIFT_DEBUG_THREADPOOL
threadpool_dump_log(&tp, "threadpool_log.txt", 1);
char filename[80];
sprintf(filename, "threadpool_log-%d.txt", num_thread);
printf("# Dumping log\n");
threadpool_dump_log(&tp, filename, 1);
#endif
/* Be clean */
threadpool_clean(&tp);
/* Be clean */
threadpool_clean(&tp);
printf("\n");
}
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment