From 300558df75955a141f3f3f0e4c79b56a010f155d Mon Sep 17 00:00:00 2001 From: RustyRaptor Date: Tue, 14 Sep 2021 15:48:40 -0600 Subject: [PATCH] added some comments and got the array version working --- figure1_7/figure1_7.c | 9 +++++++++ witharray/witharray | Bin 17288 -> 0 bytes witharray/witharray.c | 24 +++++++++--------------- withmutex/withmutex.c | 4 +++- 4 files changed, 21 insertions(+), 16 deletions(-) delete mode 100755 witharray/witharray diff --git a/figure1_7/figure1_7.c b/figure1_7/figure1_7.c index 304de1d..02b2835 100644 --- a/figure1_7/figure1_7.c +++ b/figure1_7/figure1_7.c @@ -47,6 +47,7 @@ int COUNT = 0; // The count of threes we found for the parallel code int SEGSIZE; // the size of each chunk per tread int NUMOFTHREADS; // the number of threads to spawn + /** * @brief Iterates through a chunk of the A array and counts the number of * threes by updating the COUNT variable @@ -113,6 +114,7 @@ int count3s_parallel() // our threads pthread_t *t_idents; int *t_indices; + // Allocates the space needed for the thread IDs t_idents = (pthread_t *)(malloc(sizeof(pthread_t) * NUMOFTHREADS)); @@ -122,6 +124,9 @@ int count3s_parallel() // i can pass it as a pointer in the for loop t_indices = (int *)(malloc(sizeof(int) * NUMOFTHREADS)); + // allocate an array to store the individual results of the threads + // Then we can add them up afterwards + // Create the threads for (int i = 0; i < NUMOFTHREADS; i++) { t_indices[i] = i; @@ -178,11 +183,15 @@ int main(int argc, char const *argv[]) } A = (int *)(malloc(sizeof(int) * size)); + // if the array is null barf if (A == NULL) { printf("Austin, we have a problem"); exit(1); } + + // generate random numbers and fill them in the array + // the numbers are between 1 and 3 inclusive for (int i = 0; i < size; i++) { A[i] = rand() % 4; diff --git a/witharray/witharray b/witharray/witharray deleted file mode 100755 index 459e6bc807e5325ce74c940008c3e572af9ecf1a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 17288 zcmeHOYjj-Ibv}2lMv^UiG?r~_1Hl(;2lJSbY{1Cx(PQip$PaAE5Q-U%XRf3P(u|mg z9~6Q?nB|Eu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~q?^Nm;a;Qm9yJSrLm|{z=V;9<%9?+s8nB1M);-o{1_id)A9iFl5))W&2DuA zb;qE~oTXtcB3K%nhT%wW9E%lQN!UWM3yaRtC(QC|VKNY48Hijw?$ z$99^5eT=<8*5`TMr~{ThIe+J&J&&&UP%2C^tAJ_ovG5>vsKF_mX znQ2MzzBbD{>wi?~v;X;ha!Bd_j1?7i_TMuOeg58=zk_ae=sU}Q-l5O)`!m>6qEP1; zu+H)aAw#0-cFC^;@ORNpdFGkYe5=}~&+7$aYKqNr5Rrj-re}cC*x~Z?>k5}x@}sK$ zYB=P_ANS)M5F-)R=kBsrTC~i(%QbK>hVoRalQNDwf^HwKDnolgT^l2Vx4-Qt;10c-lzxyw@ zv_9gFQASvwX_L~omqF=0YNED|1s(b3=fsaIusO_$-BkYM?Fx qOTYCzZh2hL^Ucvjsd)N2NyI`&rlUESQx?~Mr{2=P&7t66#eV|<#ifJ* diff --git a/witharray/witharray.c b/witharray/witharray.c index c11a018..d6e632e 100644 --- a/witharray/witharray.c +++ b/witharray/witharray.c @@ -46,6 +46,7 @@ int *A; // Pointer to our array in heap int COUNT = 0; // The count of threes we found for the parallel code int SEGSIZE; // the size of each chunk per tread int NUMOFTHREADS; // the number of threads to spawn +int *t_results; // an array to store the results from each thread pthread_mutex_t mtex = PTHREAD_MUTEX_INITIALIZER; // this is our mutex variable @@ -81,20 +82,7 @@ void *count3s(void *idx) for (int i = mystart; i < myend; i++) { if (A[i] == 3) { - // if (DEBUG) - // printf("Found a three\n"); - - // lock the mutex thereby waiting until other threads - // are done with this section of the code before - // locking it. - pthread_mutex_lock(&mtex); // lock the mutex - - - COUNT++; // increment count - - // unlock the mutex to let the OS know we are done - // with this section of code. - pthread_mutex_unlock(&mtex); + t_results[*index]++; // increment result } } @@ -135,6 +123,8 @@ int count3s_parallel() // i can pass it as a pointer in the for loop t_indices = (int *)(malloc(sizeof(int) * NUMOFTHREADS)); + t_results = (int *)(calloc(NUMOFTHREADS, sizeof(int) * NUMOFTHREADS)); + // Create the threads for (int i = 0; i < NUMOFTHREADS; i++) { t_indices[i] = i; @@ -146,6 +136,7 @@ int count3s_parallel() // wait for all the treads to finish. for (int i = 0; i < NUMOFTHREADS; i++) { pthread_join(t_idents[i], NULL); + COUNT+=t_results[i]; } // free the ids and indexes in memory so we dont leak @@ -191,8 +182,11 @@ int main(int argc, char const *argv[]) } A = (int *)(malloc(sizeof(int) * size)); + // if the array is null barf if (A == NULL) { - printf("Austin, we have a problem"); + + // printf("Austin, we have a problem"); + perror("Austin, we have a problem"); exit(1); } diff --git a/withmutex/withmutex.c b/withmutex/withmutex.c index c11a018..4570fab 100644 --- a/withmutex/withmutex.c +++ b/withmutex/withmutex.c @@ -191,8 +191,10 @@ int main(int argc, char const *argv[]) } A = (int *)(malloc(sizeof(int) * size)); + // if the array is null barf if (A == NULL) { - printf("Austin, we have a problem"); + perror("Austin, we have a problem"); + // printf("Austin, we have a problem"); exit(1); }