Skip to content

Commit

Permalink
Simple pthread example
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromekelleher committed Jan 10, 2023
1 parent 1ecd671 commit 8830fbb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
73 changes: 59 additions & 14 deletions c/examples/multichrom_wright_fisher.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <err.h>
#include <string.h>

#include <pthread.h>
#include <tskit/tables.h>

#define check_tsk_error(val) \
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion c/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8830fbb

Please sign in to comment.