mirror of
https://github.com/RustyRaptor/parallel-programming.git
synced 2026-12-03 15:55:15 -07:00
implemented shared memory with array program
This commit is contained in:
1 parent
8287fec1a9
commit
abb48deb32
1 file changed
+242
-226
@@ -15,14 +15,13 @@
|
||||
|
||||
/**
|
||||
Changelog
|
||||
09/30
|
||||
- Copied code from Program 1
|
||||
- Copied and refactored shared memory code from program 1.5
|
||||
- Modified the thread to use the shared memory
|
||||
- Implemented process forks using two loops
|
||||
- added separate counts for threads and processes
|
||||
- implemented separate functions for threads and processes
|
||||
- BUGFIX: The values in the struct were not starting at 0
|
||||
10/01
|
||||
- Copied code from witharray.c in program 1
|
||||
- copied shared memory code from figure1_7 in Program2
|
||||
- Added shared memory dynamically allocated array for results
|
||||
- BUGFIX: array was not actually shared because i was using malloc instaed of
|
||||
using the shared memory get method to allocate
|
||||
-
|
||||
*/
|
||||
|
||||
#define DEBUG 0 // flag to print debug values
|
||||
@@ -39,7 +38,7 @@ Changelog
|
||||
#include <semaphore.h>
|
||||
|
||||
// create key for shared memory
|
||||
#define SHMKEY ((key_t)1497)
|
||||
#define SHMKEY ((key_t)1578)
|
||||
|
||||
int *A; // Pointer to our array in heap
|
||||
|
||||
@@ -52,6 +51,8 @@ struct counts {
|
||||
// initialize memory
|
||||
struct counts COUNT;
|
||||
|
||||
int *thrd_results;
|
||||
|
||||
static int *results;
|
||||
|
||||
int SEGSIZE; // the size of each chunk per tread
|
||||
@@ -74,113 +75,9 @@ int SIZE; // The size of the input array
|
||||
* @post When done the COUNT variable must be updated to have the number of
|
||||
* threes found in our chunk of the array
|
||||
*/
|
||||
void *count3s(void *idx)
|
||||
{
|
||||
// if (DEBUG)
|
||||
// printf("I ran once %d\n", index);
|
||||
void *count3s(void *idx);
|
||||
|
||||
int *index = (int *)idx; // cast the void star input to an int ptr
|
||||
int mystart = *index * SEGSIZE; // set the starting point for the thread
|
||||
int myend = mystart + SEGSIZE; // set the end point for the thread
|
||||
|
||||
if (DEBUG) {
|
||||
printf("start is %d\n", mystart);
|
||||
printf("end is %d\n", myend);
|
||||
printf("Count is....... %d \n", COUNT.count_proc);
|
||||
}
|
||||
results[*index] = 0;
|
||||
// iterate through our section of the array and count the threes
|
||||
for (int i = mystart; i < myend; i++) {
|
||||
if (A[i] == 3) {
|
||||
if (DEBUG)
|
||||
printf("Found a threeeeee\n");
|
||||
results[*index]++;
|
||||
// printf("this one is %d \n", results[*index]);
|
||||
}
|
||||
}
|
||||
|
||||
// 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)) {
|
||||
if (DEBUG) {
|
||||
int remain = SIZE - myend;
|
||||
printf("Remainder: %d \n", remain);
|
||||
}
|
||||
|
||||
// printf("ACTIVE \n");
|
||||
|
||||
// iterate through the remainder of the array and count the 3s
|
||||
for (int i = myend; i < SIZE; i++) {
|
||||
if (A[i] == 3) {
|
||||
results[*index]++; // increment count
|
||||
}
|
||||
}
|
||||
|
||||
if (DEBUG) {
|
||||
printf("The count is now %d threads %d index %d SIZE %d end %d \n",
|
||||
COUNT.count_proc, NUMOFTHREADS, *index, SIZE,
|
||||
myend);
|
||||
}
|
||||
}
|
||||
|
||||
return (void *)0;
|
||||
}
|
||||
|
||||
void *count3s_thrd(void *idx)
|
||||
{
|
||||
// if (DEBUG)
|
||||
// printf("I ran once %d\n", index);
|
||||
|
||||
int *index = (int *)idx; // cast the void star input to an int ptr
|
||||
int mystart = *index * SEGSIZE; // set the starting point for the thread
|
||||
int myend = mystart + SEGSIZE; // set the end point for the thread
|
||||
|
||||
if (DEBUG) {
|
||||
printf("start is %d\n", mystart);
|
||||
printf("end is %d\n", myend);
|
||||
printf("Count is....... %d \n", COUNT.count_proc);
|
||||
}
|
||||
|
||||
// iterate through our section of the array and count the threes
|
||||
for (int i = mystart; i < myend; i++) {
|
||||
if (A[i] == 3) {
|
||||
if (DEBUG)
|
||||
printf("Found a three\n");
|
||||
COUNT.count_thrd++;
|
||||
}
|
||||
}
|
||||
|
||||
// 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)) {
|
||||
if (DEBUG) {
|
||||
int remain = SIZE - myend;
|
||||
printf("Remainder: %d \n", remain);
|
||||
}
|
||||
|
||||
// printf("ACTIVE \n");
|
||||
|
||||
// iterate through the remainder of the array and count the 3s
|
||||
for (int i = myend; i < SIZE; i++) {
|
||||
if (A[i] == 3) {
|
||||
COUNT.count_thrd++; // increment count
|
||||
}
|
||||
}
|
||||
|
||||
if (DEBUG) {
|
||||
printf("The count is now %d threads %d index %d SIZE %d end %d \n",
|
||||
COUNT.count_proc, NUMOFTHREADS, *index, SIZE,
|
||||
myend);
|
||||
}
|
||||
}
|
||||
|
||||
return (void *)0;
|
||||
}
|
||||
void *count3s_thrd(void *idx);
|
||||
|
||||
/**
|
||||
* @brief This function is used to spawn the appropriate number of threads as
|
||||
@@ -196,116 +93,9 @@ void *count3s_thrd(void *idx)
|
||||
*
|
||||
*/
|
||||
|
||||
int count3s_parallel_proc()
|
||||
{
|
||||
// initialize the timer for the parallel work
|
||||
struct timespec starttime, endtime;
|
||||
int j, k;
|
||||
int count3s_parallel_proc();
|
||||
|
||||
// get the start time
|
||||
clock_gettime(CLOCK_REALTIME, &starttime); // start the timer
|
||||
|
||||
// Created an array allocated at runtime to hold the identifiers for
|
||||
// our threads
|
||||
int *t_idents;
|
||||
int *t_indices;
|
||||
|
||||
// Allocates the space needed for the thread IDs
|
||||
t_idents = (int *)(malloc(sizeof(int) * NUMOFTHREADS));
|
||||
|
||||
// in order to fix the "integer to pointer conversion" warning
|
||||
// i made an array to store the indices for each thread that way
|
||||
// i can pass it as a pointer in the for loop
|
||||
t_indices = (int *)(malloc(sizeof(int) * NUMOFTHREADS));
|
||||
|
||||
int childcnt = 0;
|
||||
|
||||
while (childcnt < NUMOFTHREADS) {
|
||||
if ((t_idents[childcnt] = fork()) == 0) {
|
||||
t_indices[childcnt] = childcnt;
|
||||
count3s((void *)&childcnt);
|
||||
exit(0);
|
||||
}
|
||||
childcnt++;
|
||||
}
|
||||
int pid;
|
||||
while (childcnt > 0) {
|
||||
pid = wait(NULL);
|
||||
if (DEBUG)
|
||||
printf("Process 1 PID %d finished\n", pid);
|
||||
childcnt--;
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUMOFTHREADS; i++) {
|
||||
COUNT.count_proc += results[i];
|
||||
// printf("this oneeeeeee is %d \n", results[i]);
|
||||
}
|
||||
|
||||
// free the ids and indexes in memory so we dont leak
|
||||
free(t_idents);
|
||||
free(t_indices);
|
||||
|
||||
// get the end time
|
||||
clock_gettime(CLOCK_REALTIME, &endtime);
|
||||
|
||||
// calculate duration
|
||||
j = endtime.tv_sec - starttime.tv_sec;
|
||||
k = endtime.tv_nsec - starttime.tv_nsec;
|
||||
j = j * 1000000000 + k;
|
||||
|
||||
// return the duration
|
||||
return j;
|
||||
}
|
||||
|
||||
int count3s_parallel()
|
||||
{
|
||||
// initialize the timer for the parallel work
|
||||
struct timespec starttime, endtime;
|
||||
int j, k;
|
||||
|
||||
// get the start time
|
||||
clock_gettime(CLOCK_REALTIME, &starttime); // start the timer
|
||||
|
||||
// Created an array allocated at runtime to hold the identifiers for
|
||||
// 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));
|
||||
|
||||
// in order to fix the "integer to pointer conversion" warning
|
||||
// i made an array to store the indices for each thread that way
|
||||
// i can pass it as a pointer in the for loop
|
||||
t_indices = (int *)(malloc(sizeof(int) * NUMOFTHREADS));
|
||||
|
||||
// Create the threads
|
||||
for (int i = 0; i < NUMOFTHREADS; i++) {
|
||||
t_indices[i] = i;
|
||||
pthread_create(&t_idents[i], NULL, count3s_thrd,
|
||||
(void *)&t_indices[i]);
|
||||
}
|
||||
|
||||
// wait for all the treads to finish.
|
||||
for (int i = 0; i < NUMOFTHREADS; i++) {
|
||||
pthread_join(t_idents[i], NULL);
|
||||
}
|
||||
|
||||
// free the ids and indexes in memory so we dont leak
|
||||
free(t_idents);
|
||||
free(t_indices);
|
||||
|
||||
// get the end time
|
||||
clock_gettime(CLOCK_REALTIME, &endtime);
|
||||
|
||||
// calculate duration
|
||||
j = endtime.tv_sec - starttime.tv_sec;
|
||||
k = endtime.tv_nsec - starttime.tv_nsec;
|
||||
j = j * 1000000000 + k;
|
||||
|
||||
// return the duration
|
||||
return j;
|
||||
}
|
||||
int count3s_parallel();
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
@@ -314,8 +104,7 @@ int main(int argc, char const *argv[])
|
||||
char *shmadd_ptr; // The address of the shared memory
|
||||
shmadd_ptr = (char *)0;
|
||||
|
||||
if ((shmid = shmget(SHMKEY, sizeof(int) * NUMOFTHREADS,
|
||||
IPC_CREAT | 0666)) < 0) {
|
||||
if ((shmid = shmget(SHMKEY, sizeof(int), IPC_CREAT | 0666)) < 0) {
|
||||
perror("uh oh, shmget failed");
|
||||
exit(1);
|
||||
}
|
||||
@@ -427,3 +216,230 @@ int main(int argc, char const *argv[])
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *count3s(void *idx)
|
||||
{
|
||||
// if (DEBUG)
|
||||
// printf("I ran once %d\n", index);
|
||||
|
||||
int *index = (int *)idx; // cast the void star input to an int ptr
|
||||
int mystart = *index * SEGSIZE; // set the starting point for the thread
|
||||
int myend = mystart + SEGSIZE; // set the end point for the thread
|
||||
|
||||
if (DEBUG) {
|
||||
printf("start is %d\n", mystart);
|
||||
printf("end is %d\n", myend);
|
||||
printf("Count is....... %d \n", COUNT.count_proc);
|
||||
}
|
||||
results[*index] = 0;
|
||||
// iterate through our section of the array and count the threes
|
||||
for (int i = mystart; i < myend; i++) {
|
||||
if (A[i] == 3) {
|
||||
if (DEBUG)
|
||||
printf("Found a threeeeee\n");
|
||||
results[*index]++;
|
||||
// printf("this one is %d \n", results[*index]);
|
||||
}
|
||||
}
|
||||
|
||||
// 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)) {
|
||||
if (DEBUG) {
|
||||
int remain = SIZE - myend;
|
||||
printf("Remainder: %d \n", remain);
|
||||
}
|
||||
|
||||
// printf("ACTIVE \n");
|
||||
|
||||
// iterate through the remainder of the array and count the 3s
|
||||
for (int i = myend; i < SIZE; i++) {
|
||||
if (A[i] == 3) {
|
||||
results[*index]++; // increment count
|
||||
}
|
||||
}
|
||||
|
||||
if (DEBUG) {
|
||||
printf("The count is now %d threads %d index %d SIZE %d end %d \n",
|
||||
COUNT.count_proc, NUMOFTHREADS, *index, SIZE,
|
||||
myend);
|
||||
}
|
||||
}
|
||||
|
||||
return (void *)0;
|
||||
}
|
||||
|
||||
int count3s_parallel()
|
||||
{
|
||||
// initialize the timer for the parallel work
|
||||
struct timespec starttime, endtime;
|
||||
int j, k;
|
||||
|
||||
// get the start time
|
||||
clock_gettime(CLOCK_REALTIME, &starttime); // start the timer
|
||||
|
||||
// Created an array allocated at runtime to hold the identifiers for
|
||||
// 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));
|
||||
|
||||
// in order to fix the "integer to pointer conversion" warning
|
||||
// i made an array to store the indices for each thread that way
|
||||
// i can pass it as a pointer in the for loop
|
||||
t_indices = (int *)(malloc(sizeof(int) * NUMOFTHREADS));
|
||||
|
||||
thrd_results = (int *)(calloc(sizeof(int) * NUMOFTHREADS,
|
||||
sizeof(int) * NUMOFTHREADS));
|
||||
|
||||
// Create the threads
|
||||
for (int i = 0; i < NUMOFTHREADS; i++) {
|
||||
t_indices[i] = i;
|
||||
pthread_create(&t_idents[i], NULL, count3s_thrd,
|
||||
(void *)&t_indices[i]);
|
||||
}
|
||||
|
||||
// wait for all the treads to finish.
|
||||
for (int i = 0; i < NUMOFTHREADS; i++) {
|
||||
pthread_join(t_idents[i], NULL);
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUMOFTHREADS; i++) {
|
||||
COUNT.count_thrd += thrd_results[i];
|
||||
// printf("this oneeeeeee is %d \n", results[i]);
|
||||
}
|
||||
|
||||
// free the ids and indexes in memory so we dont leak
|
||||
free(t_idents);
|
||||
free(t_indices);
|
||||
|
||||
// get the end time
|
||||
clock_gettime(CLOCK_REALTIME, &endtime);
|
||||
|
||||
// calculate duration
|
||||
j = endtime.tv_sec - starttime.tv_sec;
|
||||
k = endtime.tv_nsec - starttime.tv_nsec;
|
||||
j = j * 1000000000 + k;
|
||||
|
||||
// return the duration
|
||||
return j;
|
||||
}
|
||||
|
||||
int count3s_parallel_proc()
|
||||
{
|
||||
// initialize the timer for the parallel work
|
||||
struct timespec starttime, endtime;
|
||||
int j, k;
|
||||
|
||||
// get the start time
|
||||
clock_gettime(CLOCK_REALTIME, &starttime); // start the timer
|
||||
|
||||
// Created an array allocated at runtime to hold the identifiers for
|
||||
// our threads
|
||||
int *t_idents;
|
||||
int *t_indices;
|
||||
|
||||
// Allocates the space needed for the thread IDs
|
||||
t_idents = (int *)(malloc(sizeof(int) * NUMOFTHREADS));
|
||||
|
||||
// in order to fix the "integer to pointer conversion" warning
|
||||
// i made an array to store the indices for each thread that way
|
||||
// i can pass it as a pointer in the for loop
|
||||
t_indices = (int *)(malloc(sizeof(int) * NUMOFTHREADS));
|
||||
|
||||
int childcnt = 0;
|
||||
|
||||
while (childcnt < NUMOFTHREADS) {
|
||||
if ((t_idents[childcnt] = fork()) == 0) {
|
||||
t_indices[childcnt] = childcnt;
|
||||
count3s((void *)&childcnt);
|
||||
exit(0);
|
||||
}
|
||||
childcnt++;
|
||||
}
|
||||
int pid;
|
||||
while (childcnt > 0) {
|
||||
pid = wait(NULL);
|
||||
if (DEBUG)
|
||||
printf("Process 1 PID %d finished\n", pid);
|
||||
childcnt--;
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUMOFTHREADS; i++) {
|
||||
COUNT.count_proc += results[i];
|
||||
// printf("this oneeeeeee is %d \n", results[i]);
|
||||
}
|
||||
|
||||
// free the ids and indexes in memory so we dont leak
|
||||
free(t_idents);
|
||||
free(t_indices);
|
||||
|
||||
// get the end time
|
||||
clock_gettime(CLOCK_REALTIME, &endtime);
|
||||
|
||||
// calculate duration
|
||||
j = endtime.tv_sec - starttime.tv_sec;
|
||||
k = endtime.tv_nsec - starttime.tv_nsec;
|
||||
j = j * 1000000000 + k;
|
||||
|
||||
// return the duration
|
||||
return j;
|
||||
}
|
||||
|
||||
void *count3s_thrd(void *idx)
|
||||
{
|
||||
// if (DEBUG)
|
||||
// printf("I ran once %d\n", index);
|
||||
|
||||
int *index = (int *)idx; // cast the void star input to an int ptr
|
||||
int mystart = *index * SEGSIZE; // set the starting point for the thread
|
||||
int myend = mystart + SEGSIZE; // set the end point for the thread
|
||||
|
||||
if (DEBUG) {
|
||||
printf("start is %d\n", mystart);
|
||||
printf("end is %d\n", myend);
|
||||
printf("Count is....... %d \n", COUNT.count_proc);
|
||||
}
|
||||
|
||||
// iterate through our section of the array and count the threes
|
||||
for (int i = mystart; i < myend; i++) {
|
||||
if (A[i] == 3) {
|
||||
if (DEBUG)
|
||||
printf("Found a three\n");
|
||||
thrd_results[*index]++;
|
||||
}
|
||||
}
|
||||
|
||||
// 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)) {
|
||||
if (DEBUG) {
|
||||
int remain = SIZE - myend;
|
||||
printf("Remainder: %d \n", remain);
|
||||
}
|
||||
|
||||
// printf("ACTIVE \n");
|
||||
|
||||
// iterate through the remainder of the array and count the 3s
|
||||
for (int i = myend; i < SIZE; i++) {
|
||||
if (A[i] == 3) {
|
||||
thrd_results[*index]++; // increment count
|
||||
}
|
||||
}
|
||||
|
||||
if (DEBUG) {
|
||||
printf("The count is now %d threads %d index %d SIZE %d end %d \n",
|
||||
COUNT.count_proc, NUMOFTHREADS, *index, SIZE,
|
||||
myend);
|
||||
}
|
||||
}
|
||||
|
||||
return (void *)0;
|
||||
}
|
||||
Reference in new issue
Block a user