finished cleaning up and commenting paddded array

This commit is contained in:
Ziad Arafat committed 2021-10-06 22:39:14 -06:00
1 parent 781d9cf85c
commit bf9e0ec509
2 files changed
+120 -45

No files matched your search

+83 -28
View File
@@ -27,7 +27,12 @@ using the shared memory get method to allocate
- BUGFIX: Shared memory was not going away because I wasnt calling shmctl I - BUGFIX: Shared memory was not going away because I wasnt calling shmctl I
mistakenly tought shmdt was all I needed. mistakenly tought shmdt was all I needed.
- http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/shm/shmdt.html - http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/shm/shmdt.html
- - BUGFIX: I could not initialize the shared memory to a size based on
NUMOFTHREADS because I was doing this initialization before I even initialized
NUMOFTHREADS so I had to move thie initialization to below that point.
Somehow... the code was working regardless. Weird. Maybe the OS dynamically
resizes the shared memory to the needs of the program?
*/ */
#define DEBUG 0 // flag to print debug values #define DEBUG 0 // flag to print debug values
@@ -48,24 +53,25 @@ mistakenly tought shmdt was all I needed.
int *A; // Pointer to our array in heap int *A; // Pointer to our array in heap
// struct to store shared memory count // struct to store the final counts.
struct counts { struct counts {
int count_proc; int count_proc;
int count_thrd; int count_thrd;
}; };
// This is a struct that will hold our data in the array for padding. // This is a struct that will hold our data in the array for padding in shared memory
struct padded_int { struct padded_int {
// We need 64B of padding. Because our processor has 64B cache lines // We need 64B of padding. Because our processor has 64B cache lines
int value; // an int is 4 bytes int value; // an int is 4 bytes
char padding[60]; // now we need 64-4 more bytes so we make 60 chars char padding[60]; // now we need 64-4 more bytes so we make 60 chars
}; };
// initialize memory // initialize the final counts.
struct counts COUNT; struct counts COUNT;
struct padded_int *thrd_results; struct padded_int *thrd_results; // the thread results will be local.
// The process results will be in shared memory so static.
static struct padded_int *results; static struct padded_int *results;
int SEGSIZE; // the size of each chunk per tread int SEGSIZE; // the size of each chunk per tread
@@ -74,41 +80,77 @@ int SIZE; // The size of the input array
/** /**
* @brief Iterates through a chunk of the A array and counts the number of * @brief Iterates through a chunk of the A array and counts the number of
* threes by updating the COUNT variable * threes by updating the shared results array. If there is a remainder in
* the end of the array we will iterate through the remainder as well but
* only on the last call.
* *
* *
* @param index The index of the array chunks re are working with * @param idx The index of the array chunks re are working with
* @return void* returns a void pointer to work with pthread *
* @return void* returns a void pointer for no reason
* *
* @pre The global A variable must be a pointer to an array in heap containing numbers between * @pre The global A variable must be a pointer to an array in heap containing numbers between
* 1 and 3. index must be a number referring to the index of the chunk we want * 1 and 3. index must be a number referring to the index of the chunk we want
* to work through starting at 0. The global COUNT variable must contain the * to work through starting at 0. The shared memory array results corresponding
* number of threes we have already counted * to the fork must exist.
* *
* @post When done the COUNT variable must be updated to have the number of * @post When done the shared memory array must be updated to have the number of
* threes found in our chunk of the array * threes found in our chunk of the array
*/ */
void *count3s(void *idx); void *count3s_proc(void *idx);
/**
* @brief Iterates through a chunk of the array and counts the number of threes.
* We then update the local thrd_results array with our result. If there is a remainder
* chunk in the array we will finish it off in a second loop.
*
* @return a void pointer to work with the p_thread library.
*
* @pre The global A variable must be a pointer to an array in heap containing
* numbers between 1 and 3. Idx must be a number referring to the index of the
* chunk we want to work through starting at 0. thrd_results array must be defined
* globally.
*
* @post when done the global thrd_results array element corresponding to idx
* must have the count of threes we found in our chunk of the array.
*/
void *count3s_thrd(void *idx); void *count3s_thrd(void *idx);
/** /**
* @brief This function is used to spawn the appropriate number of threads as * @brief This function is used to spawn the appropriate number of forks as
* well as keep track of their progress. * well as keep track of their completion and results.
* *
* @return int containing the time it took * @return int containing the time it took
* *
* @pre The global COUNT variable is 0. NUMOFTHREADS is greater than 0. * @pre The results shared memory array must be defined.
* SEGSIZE is the size of the array divided by NUMOFTHREADS. the A variable
* points to an array in heap.
* *
* @post The global COUNT contains the correct three count * @post The global thrd_results must be defined.
* *
*/ */
int count3s_parallel_proc(); int count3s_parallel_proc();
/**
* @brief This function is used to spawn the appropriate number of threads as
* well as keep track of their completion and results.
*
* @return int containing the time it took
*
* @pre The thrd_results array must be defined.
*
* @post The global thrd_results must be defined.
*
*/
int count3s_parallel(); int count3s_parallel();
/**
* @brief this main function will:
* - initialize the shared memory
* - Get the command line args and use them to initialize the global vars
* - check for errors.
* - execute the parallel code functions
* - print the results out
* - delete and detach the shared memory.
*/
int main(int argc, char const *argv[]) int main(int argc, char const *argv[])
{ {
int shmid; // The shared memory id int shmid; // The shared memory id
@@ -116,8 +158,6 @@ int main(int argc, char const *argv[])
char *shmadd_ptr; // The address of the shared memory char *shmadd_ptr; // The address of the shared memory
shmadd_ptr = (char *)0; shmadd_ptr = (char *)0;
// this variable stores the time for generating the random numbers. // this variable stores the time for generating the random numbers.
time_t t; time_t t;
@@ -136,7 +176,11 @@ int main(int argc, char const *argv[])
// Convert the user input to integers // Convert the user input to integers
SIZE = atoi(argv[1]); SIZE = atoi(argv[1]);
NUMOFTHREADS = atoi(argv[2]); NUMOFTHREADS = atoi(argv[2]);
if ((shmid = shmget(SHMKEY,
// Ask the OS for shared memory of size equal to the struct times
// the number of threads we want. There's probably a lot of redundant
// casting in this but it works now.
if ((shmid = shmget(SHMKEY,
(size_t)(sizeof(struct padded_int) * (size_t)(sizeof(struct padded_int) *
((size_t)NUMOFTHREADS)), ((size_t)NUMOFTHREADS)),
IPC_CREAT | 0666)) < 0) { IPC_CREAT | 0666)) < 0) {
@@ -178,6 +222,7 @@ int main(int argc, char const *argv[])
// printf("E%d \n", A[i]); // printf("E%d \n", A[i]);
} }
// Initialize the counts to 0 to avoid garbage.
COUNT.count_proc = 0; COUNT.count_proc = 0;
COUNT.count_thrd = 0; COUNT.count_thrd = 0;
@@ -231,6 +276,7 @@ int main(int argc, char const *argv[])
exit(-1); exit(-1);
} }
// Delete the shared memory. Barf if it fails to do so.
if (shmctl(shmid, IPC_RMID, NULL) < 0) { if (shmctl(shmid, IPC_RMID, NULL) < 0) {
perror("Cant delete shared memory"); perror("Cant delete shared memory");
exit(1); exit(1);
@@ -239,7 +285,7 @@ int main(int argc, char const *argv[])
return 0; return 0;
} }
void *count3s(void *idx) void *count3s_proc(void *idx)
{ {
// if (DEBUG) // if (DEBUG)
// printf("I ran once %d\n", index); // printf("I ran once %d\n", index);
@@ -256,6 +302,7 @@ void *count3s(void *idx)
results[*index].value = 0; results[*index].value = 0;
// iterate through our section of the array and count the threes // iterate through our section of the array and count the threes
for (int i = mystart; i < myend; i++) { for (int i = mystart; i < myend; i++) {
// if we find a 3 increment
if (A[i] == 3) { if (A[i] == 3) {
if (DEBUG) if (DEBUG)
printf("Found a threeeeee\n"); printf("Found a threeeeee\n");
@@ -331,6 +378,7 @@ int count3s_parallel()
pthread_join(t_idents[i], NULL); pthread_join(t_idents[i], NULL);
} }
// add up the results in the array
for (int i = 0; i < NUMOFTHREADS; i++) { for (int i = 0; i < NUMOFTHREADS; i++) {
COUNT.count_thrd += thrd_results[i].value; COUNT.count_thrd += thrd_results[i].value;
// printf("this oneeeeeee is %d \n", results[i]); // printf("this oneeeeeee is %d \n", results[i]);
@@ -376,26 +424,32 @@ int count3s_parallel_proc()
int childcnt = 0; int childcnt = 0;
// fork until we reach the number of threads specified.
while (childcnt < NUMOFTHREADS) { while (childcnt < NUMOFTHREADS) {
// If we are inside the child process run the program
if ((t_idents[childcnt] = fork()) == 0) { if ((t_idents[childcnt] = fork()) == 0) {
t_indices[childcnt] = childcnt; t_indices[childcnt] = childcnt;
count3s((void *)&childcnt); count3s_proc((void *)&childcnt);
exit(0); exit(0); // exit the child before we go further
} }
childcnt++; childcnt++; // increment the number of children
} }
int pid; int pid; // the pid of the waited child
// keep waiting for children until they have all exited.
while (childcnt > 0) { while (childcnt > 0) {
// wait for a child and store their pid for debugging
pid = wait(NULL); pid = wait(NULL);
if (DEBUG) { if (DEBUG) {
printf("Process 1 PID %d finished\n", pid); printf("Process 1 PID %d finished\n", pid);
} }
childcnt--; childcnt--; // decrement the number of remaining children
} }
// add up all the results from the children into one variable
for (int i = 0; i < NUMOFTHREADS; i++) { for (int i = 0; i < NUMOFTHREADS; i++) {
COUNT.count_proc += results[i].value; COUNT.count_proc += results[i].value;
// printf("this oneeeeeee is %d \n", results[i]); // printf("this oneeeeeee is %d \n", results[i]);
@@ -432,7 +486,8 @@ void *count3s_thrd(void *idx)
printf("Count is....... %d \n", COUNT.count_proc); printf("Count is....... %d \n", COUNT.count_proc);
} }
thrd_results[*index].value = 0; thrd_results[*index].value = 0; // initialize to 0 to avoid garbage
// iterate through our section of the array and count the threes // iterate through our section of the array and count the threes
for (int i = mystart; i < myend; i++) { for (int i = mystart; i < myend; i++) {
if (A[i] == 3) { if (A[i] == 3) {
+37 -17
View File
@@ -81,7 +81,7 @@ int SIZE; // The size of the input array
* @post When done the COUNT variable must be updated to have the number of * @post When done the COUNT variable must be updated to have the number of
* threes found in our chunk of the array * threes found in our chunk of the array
*/ */
void *count3s(void *idx); void *count3s_proc(void *idx);
void *count3s_thrd(void *idx); void *count3s_thrd(void *idx);
@@ -110,17 +110,6 @@ int main(int argc, char const *argv[])
char *shmadd_ptr; // The address of the shared memory char *shmadd_ptr; // The address of the shared memory
shmadd_ptr = (char *)0; shmadd_ptr = (char *)0;
if ((shmid = shmget(SHMKEY, sizeof(int), IPC_CREAT | 0666)) < 0) {
perror("uh oh, shmget failed");
exit(1);
}
// check if the shared mem attached correctly.
if ((results = (int *)shmat(shmid, shmadd_ptr, 0)) == (int *)-1) {
perror("shmat failed");
exit(1);
}
// this variable stores the time for generating the random numbers. // this variable stores the time for generating the random numbers.
time_t t; time_t t;
@@ -144,6 +133,21 @@ int main(int argc, char const *argv[])
// number of threads // number of threads
SEGSIZE = SIZE / NUMOFTHREADS; SEGSIZE = SIZE / NUMOFTHREADS;
// ask the OS for shared memory and set it to the size of the array
// we want to make which is the size of an int times the number of threads.
// Barf if we cant get shared memory.
if ((shmid = shmget(SHMKEY, sizeof(int) * NUMOFTHREADS,
IPC_CREAT | 0666)) < 0) {
perror("uh oh, shmget failed");
exit(1);
}
// check if the shared mem attached correctly.
if ((results = (int *)shmat(shmid, shmadd_ptr, 0)) == (int *)-1) {
perror("shmat failed");
exit(1);
}
if (DEBUG) { if (DEBUG) {
printf("number is %s \n", argv[1]); printf("number is %s \n", argv[1]);
printf("number is %d \n", SIZE); printf("number is %d \n", SIZE);
@@ -167,6 +171,7 @@ int main(int argc, char const *argv[])
// printf("E%d \n", A[i]); // printf("E%d \n", A[i]);
} }
// initialize counts to 0 to avoid garbage
COUNT.count_proc = 0; COUNT.count_proc = 0;
COUNT.count_thrd = 0; COUNT.count_thrd = 0;
@@ -220,6 +225,7 @@ int main(int argc, char const *argv[])
exit(-1); exit(-1);
} }
// delete the shared memory and barf if it fails to do so
if (shmctl(shmid, IPC_RMID, NULL) < 0) { if (shmctl(shmid, IPC_RMID, NULL) < 0) {
perror("Cant delete shared memory"); perror("Cant delete shared memory");
exit(1); exit(1);
@@ -228,7 +234,7 @@ int main(int argc, char const *argv[])
return 0; return 0;
} }
void *count3s(void *idx) void *count3s_proc(void *idx)
{ {
// if (DEBUG) // if (DEBUG)
// printf("I ran once %d\n", index); // printf("I ran once %d\n", index);
@@ -242,7 +248,10 @@ void *count3s(void *idx)
printf("end is %d\n", myend); printf("end is %d\n", myend);
printf("Count is....... %d \n", COUNT.count_proc); printf("Count is....... %d \n", COUNT.count_proc);
} }
// initialize the element to 0 to avoid garbage.
results[*index] = 0; results[*index] = 0;
// iterate through our section of the array and count the threes // iterate through our section of the array and count the threes
for (int i = mystart; i < myend; i++) { for (int i = mystart; i < myend; i++) {
if (A[i] == 3) { if (A[i] == 3) {
@@ -320,6 +329,7 @@ int count3s_parallel()
pthread_join(t_idents[i], NULL); pthread_join(t_idents[i], NULL);
} }
// add up the results from the array.
for (int i = 0; i < NUMOFTHREADS; i++) { for (int i = 0; i < NUMOFTHREADS; i++) {
COUNT.count_thrd += thrd_results[i]; COUNT.count_thrd += thrd_results[i];
// printf("this oneeeeeee is %d \n", results[i]); // printf("this oneeeeeee is %d \n", results[i]);
@@ -365,17 +375,26 @@ int count3s_parallel_proc()
int childcnt = 0; int childcnt = 0;
// Fork children until we reach the number of threads.
while (childcnt < NUMOFTHREADS) { while (childcnt < NUMOFTHREADS) {
// if we are in the child run the program
if ((t_idents[childcnt] = fork()) == 0) { if ((t_idents[childcnt] = fork()) == 0) {
t_indices[childcnt] = childcnt; t_indices[childcnt] =
count3s((void *)&childcnt); childcnt; // create the index to pass
exit(0);
count3s_proc((
void *)&childcnt); // call the function with the index
exit(0); // exit the child before continuing.
} }
childcnt++; childcnt++;
} }
int pid; int pid; // pid of the child waited
// keep waiting on children until they are all exited
while (childcnt > 0) { while (childcnt > 0) {
// wait till a child exits then store it's pid for debugging
pid = wait(NULL); pid = wait(NULL);
if (DEBUG) { if (DEBUG) {
@@ -385,6 +404,7 @@ int count3s_parallel_proc()
childcnt--; childcnt--;
} }
// add up the results from the shared mem array
for (int i = 0; i < NUMOFTHREADS; i++) { for (int i = 0; i < NUMOFTHREADS; i++) {
COUNT.count_proc += results[i]; COUNT.count_proc += results[i];
// printf("this oneeeeeee is %d \n", results[i]); // printf("this oneeeeeee is %d \n", results[i]);