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 459e6bc..0000000 Binary files a/witharray/witharray and /dev/null differ 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); }