Data Structure in C++ Implement a multithreaded version of Quicksort, where the
ID: 3842308 • Letter: D
Question
Data Structure in C++
Implement a multithreaded version of Quicksort, where the recursive calls will be replaced by other threads.
Parallelization
I tried this homework and there are errors.
Please check my code and fix errors.
:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <boost/thread.hpp>
using namespace std;
struct qsort_thread {
unsigned int* data;
unsigned int left, right;
unsigned int size;
unsigned int p;
void operator()() {
int pivotValue = data[p];
swap(data[p], data[right]);
int storeIndex = left;
for (int i = left ; i < right ; i++) {
if (data[i] <= pivotValue){
swap(data[i], data[storeIndex]);
storeIndex++;
}
}
swap(data[storeIndex], data[right]);
return storeIndex;
}
};
void seq_qsort(int* data, int left, int right) {
if (right > left) {
int pivotIndex = left + (right - left)/2;
pivotIndex = qsort_thread(data, left, right, pivotIndex);
seq_qsort(data, left, pivotIndex-1);
seq_qsort(data, pivotIndex+1, right);
}
}
void par_qsort(int* data, int left, int right, int size){
if (right > left) {
int pivotIndex = left + (right - left)/2;
pivotIndex = qsort_thread(data, left, right, pivotIndex);
if (size-- > 0) {
struct qsort_thread arg = {data, left, pivotIndex-1, size};
thread_group threads;
int ret = threads.create_thread(&thread, NULL, quicksort_thread, &arg);
par_qsort(data, pivotIndex+1, right, size);
}
else {
seq_qsort(data, left, pivotIndex-1);
seq_qsort(data, pivotIndex+1, right);
}
}
}
/*
int main() {
int data[1000000];
for(int i = 0; i < 1000000; i++)
data[i] = rand();
return 0;
}
*/
Explanation / Answer
please find the below code:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <boost/thread.hpp>
using namespace std;
struct qsort_thread {
unsigned int* data;
unsigned int left, right;
unsigned int size;
unsigned int p;
void operator()() {
int pivotValue = data[p];
swap(data[p], data[right]);
int storeIndex = left;
for (int i = left ; i < right ; i++) {
if (data[i] <= pivotValue){
swap(data[i], data[storeIndex]);
storeIndex++;
}
}
swap(data[storeIndex], data[right]);
return storeIndex;
}
};
void seq_qsort(int* data, int left, int right) {
if (right > left) {
int pivotIndex = left + (right - left)/2;
pivotIndex = qsort_thread(data, left, right, pivotIndex);
seq_qsort(data, left, pivotIndex-1);
seq_qsort(data, pivotIndex+1, right);
}
}
void par_qsort(int* data, int left, int right, int size){
if (right > left) {
int pivotIndex = left + (right - left)/2;
pivotIndex = qsort_thread(data, left, right, pivotIndex);
if (size-- > 0) {
struct qsort_thread arg = {data, left, pivotIndex-1, size};
thread_group threads;
int ret = threads.create_thread(&thread, NULL, quicksort_thread, &arg);
par_qsort(data, pivotIndex+1, right, size);
}
else {
seq_qsort(data, left, pivotIndex-1);
seq_qsort(data, pivotIndex+1, right);
}
}
}
int main() {
int data[1000000];
for(int i = 0; i < 1000000; i++)
data[i] = rand();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.