Here are my instructions: This assignment uses pthreads to perform parallel processing –…
- User
Here are my instructions: This assignment uses pthreads to perform parallel processing – allowing you to speed up your sort program substantially. You should use 10 threads in your program. Your program will read in 1 million unsorted numbers from numbers.dat into an array. Then you will logically split the array into 10 sections – each section will have 100,000 numbers. You will call pthread_create( ) 10 times and pass each section of the array to the thread to sort using the sort algorithm you used in the previous assignment. Once all the threads have returned, you will merge 2 adjacent sections into a sorted super section, and continue to do that until all the smaller sorted sections are in one large sorted section – which will be the entire array of 1 million numbers. Then print the array to a file. Then you will call sort -c to verify the output file is sorted.
Now please check my code below for errors according to those instructions: #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include #include #include <unistd.h> #include using namespace std;
unsigned int NUM_THREADS = 8; unsigned int NUM_SIZE = 1000000;
struct printStruct{ int *pstart; // pointer to the starting array element int size; }
void *bubblesort(void *ptr){ printStruct *arg = (printStruct *) ptr; int i, j; for(int i = 0; i < (elements - 1); i++) { for (int j = 0; j < (elements - 1 - i); j++) { if (sortArray[j] > sortArray[j+1]) { temp = sortArray[j]; sortArray[j] = sortArray[j+1]; sortArray[j+1] = temp; } } } return NULL; }
void merge(int* left, int* right, int leftSize, int rightSize){ int leftIndex = 0, rightIndex = 0, mergedIndex = 0; int* temp = new int[leftSize + rightSize]; while(leftIndex < leftSize && rightIndex < rightSize){ /* this part is sus temp[merged++] = left[left] < right[right] */ } while(leftIndex < leftSize){ temp[mergedIndex++] = left[leftIndex++]; } while(rightIndex < rightSize){ temp[mergedIndex++] = right[rightIndex++] } delete[] temp; }
int main(int argc, char*argv[]) { ifstream numFile(argv[1]); int elements = 0; int sortArray[1000000]; int size; // size of the array = size of the elements int temp;
while (numFile >> size) { sortArray[elements] = size; elements++; } numFile.close(); printStruct s1; s1.pstart = s1.size = 100000; printStruct s2; s2.pstart = s2.size = 100000; printStruct s3; s3.pstart = s3.size = 100000; printStruct s4; s4.pstart = s4.size = 100000; printStruct s5; s5.pstart = s5.size = 100000; printStruct s6; s6.pstart = s6.size = 100000; printStruct s7; s7.pstart = s7.size = 100000; printStruct s8; s8.pstart = s8.size = 100000; printStruct s9; s9.pstart = s9.size = 100000; printStruct s10; s10.pstart = s10.size = 100000; pthread_t thread0, thread1, thread2, thread3, thread4, thread5, thread6, thread7, thread8, thread9; int iret0, iret1, iret2, iret3, iret4, iret5, iret6, iret7, iret8, iret9; iret0 = pthread_create( &thread0, NULL, bubblesort, (void*) &s1); iret1 = pthread_create( &thread1, NULL, bubblesort, (void*) &s2); iret2 = pthread_create( &thread2, NULL, bubblesort, (void*) &s3); iret3 = pthread_create( &thread3, NULL, bubblesort, (void*) &s4); iret4 = pthread_create( &thread0, NULL, bubblesort, (void*) &s5); iret5 = pthread_create( &thread1, NULL, bubblesort, (void*) &s6); iret6 = pthread_create( &thread2, NULL, bubblesort, (void*) &s7); iret7 = pthread_create( &thread3, NULL, bubblesort, (void*) &s8); iret8 = pthread_create( &thread0, NULL, bubblesort, (void*) &s9); iret9 = pthread_create( &thread1, NULL, bubblesort, (void*) &s10); pthread_join( thread0, NULL); pthread_join( thread1, NULL); pthread_join( thread2, NULL); pthread_join( thread3, NULL); pthread_join( thread4, NULL); pthread_join( thread5, NULL); pthread_join( thread6, NULL); pthread_join( thread7, NULL); pthread_join( thread8, NULL); pthread_join( thread9, NULL); ofstream outFile(argv[2]); for(int i = 0; i < elements; i++) { outFile << sortArray[i] << endl; } outFile.close(); return 0; - Assistant
- User
Undo this loop:
for (int i = 0; i < NUM_THREADS; i++) { section[i].pstart = &sortArray[i * sectionSize]; section[i].size = sectionSize; pthread_t thread; pthread_create(&thread, NULL, bubblesort, (void*)§ion[i]); pthread_join(thread, NULL); } I want to see the longer code - Assistant
Sure, here is the code without the loop:
// Create the threads and start sorting each section pthread_t thread0, thread1, thread2, thread3, thread4, thread5, thread6, thread7, thread8, thread9; section[0].pstart = &sortArray[0]; section[0].size = sectionSize; pthread_create(&thread0, NULL, bubblesort, (void*)§ion[0]); section[1].pstart = &sortArray[sectionSize]; section[1].size = sectionSize; pthread_create(&thread1, NULL, bubblesort, (void*)§ion[1]); section[2].pstart = &sortArray[sectionSize * 2]; section[2].size = sectionSize; pthread_create(&thread2, NULL, bubblesort, (void*)§ion[2]); section[3].pstart = &sortArray[sectionSize * 3]; section[3].size = sectionSize; pthread_create(&thread3, NULL, bubblesort, (void*)§ion[3]); section[4].pstart = &sortArray[sectionSize * 4]; section[4].size = sectionSize; pthread_create(&thread4, NULL, bubblesort, (void*)§ion[4]); section[5].pstart = &sortArray[sectionSize * 5]; section[5].size = sectionSize; pthread_create(&thread5, NULL, bubblesort, (void*)§ion[5]); section[6].pstart = &sortArray[sectionSize * 6]; section[6].size = sectionSize; pthread_create(&thread6, NULL, bubblesort, (void*)§ion[6]); section[7].pstart = &sortArray[sectionSize * 7]; section[7].size = sectionSize; pthread_create(&thread7, NULL, bubblesort, (void*)§ion[7]); section[8].pstart = &sortArray[sectionSize * 8]; section[8].size = sectionSize; pthread_create(&thread8, NULL, bubblesort, (void*)§ion[8]); section[9].pstart = &sortArray[sectionSize * 9]; section[9].size = sectionSize + remainingSize; // Add the remaining size to the last section pthread_create(&thread9, NULL, bubblesort, (void*)§ion[9]); // Wait for all the threads to finish pthread_join(thread0, NULL); pthread_join(thread1, NULL); pthread_join(thread2, NULL); pthread_join(thread3, NULL); pthread_join(thread4, NULL); pthread_join(thread5, NULL); pthread_join(thread6, NULL); pthread_join(thread7, NULL); pthread_join(thread8, NULL); pthread_join(thread9, NULL);