From 8830fbbb2e0c8fc4ae6f5d9be9037d950f646b8b Mon Sep 17 00:00:00 2001 From: Jerome Kelleher Date: Thu, 15 Dec 2022 15:42:20 +0000 Subject: [PATCH] Simple pthread example --- c/examples/multichrom_wright_fisher.c | 73 ++++++++++++++++++++++----- c/meson.build | 4 +- 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/c/examples/multichrom_wright_fisher.c b/c/examples/multichrom_wright_fisher.c index b64bad53e1..e4de79a1e8 100644 --- a/c/examples/multichrom_wright_fisher.c +++ b/c/examples/multichrom_wright_fisher.c @@ -4,6 +4,7 @@ #include #include +#include #include #define check_tsk_error(val) \ @@ -51,6 +52,63 @@ dump_tables(tsk_table_collection_t *tcs, int num_chroms, const char *prefix) } } +struct chrom_work { + int chrom; + tsk_table_collection_t *tc; + int *samples; + int N; +}; + +void * +simplify_chrom(void *arg) +{ + int ret; + struct chrom_work *work = (struct chrom_work *) arg; + tsk_size_t edges_before = work->tc->edges.num_rows; + + ret = tsk_table_collection_sort(work->tc, NULL, 0); + check_tsk_error(ret); + ret = tsk_table_collection_simplify(work->tc, work->samples, work->N, + TSK_SIMPLIFY_NO_FILTER_NODES | TSK_SIMPLIFY_NO_UPDATE_SAMPLE_FLAGS, NULL); + check_tsk_error(ret); + /* NOTE: this printf makes helgrind complain */ + printf("\tchr %d: %lld -> %lld\n", work->chrom, (long long) edges_before, + (long long) work->tc->edges.num_rows); + + return NULL; +} + +void +sort_and_simplify_all(tsk_table_collection_t *tcs, int num_chroms, int *samples, int N) +{ + int j, ret; + struct chrom_work work[num_chroms]; + pthread_t threads[num_chroms]; + + for (j = 1; j < num_chroms; j++) { + tcs[j].nodes = tcs[0].nodes; + } + + for (j = 0; j < num_chroms; j++) { + work[j].chrom = j; + work[j].tc = &tcs[j]; + work[j].samples = samples; + work[j].N = N; + + ret = pthread_create(&threads[j], NULL, simplify_chrom, (void *) &work[j]); + if (ret != 0) { + errx(EXIT_FAILURE, "Pthread create failed"); + } + /* simplify_chrom((void *) &work[j]); */ + } + for (j = 0; j < num_chroms; j++) { + ret = pthread_join(threads[j], NULL); + if (ret != 0) { + errx(EXIT_FAILURE, "Pthread join failed"); + } + } +} + void simplify_tables(tsk_table_collection_t *tcs, int num_chroms, int *samples, int N) { @@ -65,20 +123,7 @@ simplify_tables(tsk_table_collection_t *tcs, int num_chroms, int *samples, int N } printf("Simplify %lld nodes\n", (long long) tcs[0].nodes.num_rows); - for (j = 1; j < num_chroms; j++) { - tcs[j].nodes = tcs[0].nodes; - } - - for (j = 0; j < num_chroms; j++) { - printf("\tchr %d: %lld", j, (long long) tcs[j].edges.num_rows); - ret = tsk_table_collection_sort(&tcs[j], NULL, 0); - /* tsk_table_collection_print_state(&tcs[j], stdout); */ - check_tsk_error(ret); - ret = tsk_table_collection_simplify(&tcs[j], samples, N, - TSK_SIMPLIFY_NO_FILTER_NODES | TSK_SIMPLIFY_NO_UPDATE_SAMPLE_FLAGS, NULL); - check_tsk_error(ret); - printf(" -> %lld\n", (long long) tcs[j].edges.num_rows); - } + sort_and_simplify_all(tcs, num_chroms, samples, N); for (j = 0; j < num_nodes; j++) { delete_nodes[j] = true; diff --git a/c/meson.build b/c/meson.build index 77aea329a2..fba8e3caae 100644 --- a/c/meson.build +++ b/c/meson.build @@ -117,8 +117,10 @@ if not meson.is_subproject() executable('haploid_wright_fisher', sources: ['examples/haploid_wright_fisher.c'], link_with: [tskit_lib], dependencies: lib_deps) + + thread_dep = dependency('threads') executable('multichrom_wright_fisher', sources: ['examples/multichrom_wright_fisher.c'], - link_with: [tskit_lib], dependencies: lib_deps) + link_with: [tskit_lib], dependencies: [m_dep, kastore_dep, thread_dep]) endif endif