From f1d5b74cec415fe572c61ee5213a111434142826 Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 22 Sep 2014 07:04:31 -0400 Subject: [PATCH] Update the threading chapter --- atomic_factors.c | 1 + c_factors.c | 14 +++++++++----- c_primes.c | 4 ++++ mapreduce_wc.c | 1 + openmp_atoms.c | 3 +-- openmp_wc.c | 1 + pthread_factors.c | 5 +++++ 7 files changed, 22 insertions(+), 7 deletions(-) diff --git a/atomic_factors.c b/atomic_factors.c index 351e9f2..c009f65 100644 --- a/atomic_factors.c +++ b/atomic_factors.c @@ -1,5 +1,6 @@ #include #include +#include //malloc #include //memset #include "openmp_getmax.c" diff --git a/c_factors.c b/c_factors.c index a9c4b38..db6c365 100644 --- a/c_factors.c +++ b/c_factors.c @@ -1,3 +1,7 @@ +/* Compile with: +export CFLAGS="-g -Wall -O3 --std=c11 -pthread -latomic" +make c_factors +*/ #include #include #include //malloc @@ -44,7 +48,7 @@ void *mark_factors(void *vin){ } int main(){ - long int max = 1e4; + long int max = 1e7; _Atomic(int) *factor_ct = malloc(sizeof(_Atomic(int))*max); int thread_ct = 4; @@ -58,7 +62,7 @@ int main(){ one_factor_s x[thread_ct]; for (long int i=2; i<= max/2; i+=thread_ct){ for (int t=0; t < thread_ct && t+i <= max/2; t++){ - x[t] = (one_factor_s){.i=i+t, .max=max, + x[t] = (one_factor_s){.i=i+t, .max=max, .factor_ct=factor_ct}; pthread_create(&threads[t], NULL, mark_factors, x+t); } @@ -67,8 +71,8 @@ int main(){ } int max_factors = get_max_factors(factor_ct, max); - _Atomic(long int) tally[max_factors]; - memset(tally, 0, sizeof(long int)*max_factors); + _Atomic(long int) tally[max_factors+1]; + memset(tally, 0, sizeof(long int)*(max_factors+1)); tally_s thread_info[thread_ct]; for (int i=0; i< thread_ct; i++){ @@ -80,6 +84,6 @@ int main(){ for (int t=0; t< thread_ct; t++) pthread_join(threads[t], NULL); - for (int i=0; i #include #include //malloc diff --git a/mapreduce_wc.c b/mapreduce_wc.c index 30d2148..27cd35a 100644 --- a/mapreduce_wc.c +++ b/mapreduce_wc.c @@ -16,6 +16,7 @@ int main(int argc, char **argv){ argv++; Stopif(!argc, return 0, "Please give some file names on the command line."); long int total_wc = 0; + #pragma omp parallel for \ reduction(+:total_wc) for (int i=0; i< argc; i++){ diff --git a/openmp_atoms.c b/openmp_atoms.c index 5464d16..5471ed6 100644 --- a/openmp_atoms.c +++ b/openmp_atoms.c @@ -1,8 +1,7 @@ /* Suggested makefile: ---------- P=openmp_atoms -CFLAGS=`pkg-config --cflags glib-2.0` -g -Wall -std=gnu99 -O3 -fopenmp -LDLIBS=`pkg-config --libs glib-2.0` -fopenmp +CFLAGS=-g -Wall -std=gnu99 -O3 -fopenmp $(P): ---------- diff --git a/openmp_wc.c b/openmp_wc.c index e65eb6b..5dff2af 100644 --- a/openmp_wc.c +++ b/openmp_wc.c @@ -16,6 +16,7 @@ int main(int argc, char **argv){ argv++; Stopif(!argc, return 0, "Please give some file names on the command line."); int count[argc]; + #pragma omp parallel for for (int i=0; i< argc; i++){ count[i] = wc(argv[i]); diff --git a/pthread_factors.c b/pthread_factors.c index e0fed81..ea68ce8 100644 --- a/pthread_factors.c +++ b/pthread_factors.c @@ -1,6 +1,11 @@ +/* Compile with: +export CFLAGS="-g -Wall -O3 --std=c99 -pthread -fopenmp" +make pthread_factors +*/ #include //get_max is still OpenMP #include #include +#include //malloc #include //memset #include "openmp_getmax.c" -- GitLab