diff --git a/figure1_7/figure1_7.c b/figure1_7/figure1_7.c index 02b2835..5e69786 100644 --- a/figure1_7/figure1_7.c +++ b/figure1_7/figure1_7.c @@ -46,7 +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 SIZE; /** * @brief Iterates through a chunk of the A array and counts the number of @@ -86,6 +86,25 @@ void *count3s(void *idx) } } + // When we reach the final index we will calculate the remaining values in the array + // So that we can add them to the result. + // This can become quite high once you go past the 50% mark of the array size + // but our tests will never go that far so this is a pretty good solution. + if ((myend < SIZE) && (*index == NUMOFTHREADS - 1)) { + // int remain = SIZE - myend; + // printf("Remainder: %d \n", remain); + // printf("ACTIVE \n"); + for (int i = myend; i < SIZE; i++) { + if (A[i] == 3) { + COUNT++; // increment count + } + } + if (DEBUG) { + printf("The count is now %d threads %d index %d SIZE %d end %d \n", + COUNT, NUMOFTHREADS, *index, SIZE, myend); + } + } + return (void *)0; } @@ -114,7 +133,6 @@ 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)); @@ -134,7 +152,6 @@ int count3s_parallel() (void *)&t_indices[i]); } - // wait for all the treads to finish. for (int i = 0; i < NUMOFTHREADS; i++) { pthread_join(t_idents[i], NULL); @@ -158,8 +175,6 @@ int count3s_parallel() int main(int argc, char const *argv[]) { - int size; - time_t t; srand((unsigned)time(&t)); @@ -171,17 +186,17 @@ int main(int argc, char const *argv[]) exit(1); } - size = atoi(argv[1]); + SIZE = atoi(argv[1]); NUMOFTHREADS = atoi(argv[2]); - SEGSIZE = size / NUMOFTHREADS; + SEGSIZE = SIZE / NUMOFTHREADS; if (DEBUG) { printf("number is %s \n", argv[1]); - printf("number is %d \n", size); + printf("number is %d \n", SIZE); printf("NumThr is %d \n", NUMOFTHREADS); printf("Seg Size is %d \n", SEGSIZE); } - A = (int *)(malloc(sizeof(int) * size)); + A = (int *)(malloc(sizeof(int) * SIZE)); // if the array is null barf if (A == NULL) { @@ -189,10 +204,9 @@ int main(int argc, char const *argv[]) 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++) { + // the numbers are between 1 and 3 inclusive + for (int i = 0; i < SIZE; i++) { A[i] = rand() % 4; // printf("E%d \n", A[i]); @@ -211,7 +225,7 @@ int main(int argc, char const *argv[]) clock_gettime(CLOCK_REALTIME, &starttime); // start the timer // Count the threes in serial - for (int i = 0; i < size; i++) { + for (int i = 0; i < SIZE; i++) { if (A[i] == 3) { local_count++; } diff --git a/withmutex/withmutex.c b/withmutex/withmutex.c index 4570fab..ba15991 100644 --- a/withmutex/withmutex.c +++ b/withmutex/withmutex.c @@ -46,8 +46,9 @@ 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 SIZE; -pthread_mutex_t mtex = PTHREAD_MUTEX_INITIALIZER; // this is our mutex variable +pthread_mutex_t mtex = PTHREAD_MUTEX_INITIALIZER; // this is our mutex variable /** * @brief Iterates through a chunk of the A array and counts the number of @@ -85,19 +86,46 @@ void *count3s(void *idx) // printf("Found a three\n"); // lock the mutex thereby waiting until other threads - // are done with this section of the code before + // 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 + // unlock the mutex to let the OS know we are done // with this section of code. pthread_mutex_unlock(&mtex); } } + // When we reach the final index we will calculate the remaining values in the array + // So that we can add them to the result. + // This can become quite high once you go past the 50% mark of the array size + // but our tests will never go that far so this is a pretty good solution. + if ((myend < SIZE) && (*index == NUMOFTHREADS - 1)) { + // int remain = SIZE - myend; + // printf("Remainder: %d \n", remain); + // printf("ACTIVE \n"); + for (int i = myend; i < SIZE; i++) { + if (A[i] == 3) { + // 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); + } + } + if (DEBUG) { + printf("The count is now %d threads %d index %d SIZE %d end %d \n", + COUNT, NUMOFTHREADS, *index, SIZE, myend); + } + } + return (void *)0; } @@ -142,7 +170,6 @@ int count3s_parallel() (void *)&t_indices[i]); } - // wait for all the treads to finish. for (int i = 0; i < NUMOFTHREADS; i++) { pthread_join(t_idents[i], NULL); @@ -166,30 +193,28 @@ int count3s_parallel() int main(int argc, char const *argv[]) { - int size; - time_t t; srand((unsigned)time(&t)); if (argc != 3) { printf("Not correct arguments \n"); - printf("Usage %s \n", + printf("Usage %s r of threads> \n", argv[0]); exit(1); } - size = atoi(argv[1]); + SIZE = atoi(argv[1]); NUMOFTHREADS = atoi(argv[2]); - SEGSIZE = size / NUMOFTHREADS; + SEGSIZE = SIZE / NUMOFTHREADS; if (DEBUG) { printf("number is %s \n", argv[1]); - printf("number is %d \n", size); + printf("number is %d \n", SIZE); printf("NumThr is %d \n", NUMOFTHREADS); printf("Seg Size is %d \n", SEGSIZE); } - A = (int *)(malloc(sizeof(int) * size)); + A = (int *)(malloc(sizeof(int) * SIZE)); // if the array is null barf if (A == NULL) { @@ -198,7 +223,7 @@ int main(int argc, char const *argv[]) exit(1); } - for (int i = 0; i < size; i++) { + for (int i = 0; i < SIZE; i++) { A[i] = rand() % 4; // printf("E%d \n", A[i]); @@ -217,7 +242,7 @@ int main(int argc, char const *argv[]) clock_gettime(CLOCK_REALTIME, &starttime); // start the timer // Count the threes in serial - for (int i = 0; i < size; i++) { + for (int i = 0; i < SIZE; i++) { if (A[i] == 3) { local_count++; }