seems nice
This commit is contained in:
@@ -5,6 +5,9 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <ctime>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -15,36 +18,35 @@ struct SharedData {
|
|||||||
int arr[MAX];
|
int arr[MAX];
|
||||||
};
|
};
|
||||||
|
|
||||||
// ================= TIME =================
|
string now() {
|
||||||
double now() {
|
timeval tv;
|
||||||
timeval t;
|
gettimeofday(&tv, NULL);
|
||||||
gettimeofday(&t, NULL);
|
|
||||||
return t.tv_sec + t.tv_usec / 1e6;
|
ostringstream oss;
|
||||||
|
oss << tv.tv_sec << "."
|
||||||
|
<< setfill('0') << setw(6) << tv.tv_usec;
|
||||||
|
|
||||||
|
return oss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= MERGE =================
|
|
||||||
void merge(int *arr, int l, int m, int r) {
|
void merge(int *arr, int l, int m, int r) {
|
||||||
int *temp = new int[r - l + 1];
|
int *temp = new int[r - l + 1];
|
||||||
|
|
||||||
int i = l, j = m + 1, k = 0;
|
int i = l, j = m + 1, k = 0;
|
||||||
|
|
||||||
while (i <= m && j <= r) {
|
while (i <= m && j <= r) {
|
||||||
if (arr[i] < arr[j])
|
if (arr[i] < arr[j]) temp[k++] = arr[i++];
|
||||||
temp[k++] = arr[i++];
|
else temp[k++] = arr[j++];
|
||||||
else
|
|
||||||
temp[k++] = arr[j++];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (i <= m) temp[k++] = arr[i++];
|
while (i <= m) temp[k++] = arr[i++];
|
||||||
while (j <= r) temp[k++] = arr[j++];
|
while (j <= r) temp[k++] = arr[j++];
|
||||||
|
|
||||||
for (int x = 0; x < k; x++)
|
for (int x = 0; x < k; x++) arr[l + x] = temp[x];
|
||||||
arr[l + x] = temp[x];
|
|
||||||
|
|
||||||
delete[] temp;
|
delete[] temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= LOCAL SORT =================
|
|
||||||
void local_sort(int *arr, int l, int r) {
|
void local_sort(int *arr, int l, int r) {
|
||||||
if (l >= r) return;
|
if (l >= r) return;
|
||||||
int m = (l + r) / 2;
|
int m = (l + r) / 2;
|
||||||
@@ -53,45 +55,53 @@ void local_sort(int *arr, int l, int r) {
|
|||||||
merge(arr, l, m, r);
|
merge(arr, l, m, r);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= PARALLEL SORT =================
|
void parallel_sort(int l, int r, int depth, int shmid) {
|
||||||
void parallel_sort(int *arr, int l, int r, int depth) {
|
SharedData *data = (SharedData*) shmat(shmid, NULL, 0);
|
||||||
if (l >= r) return;
|
int *arr = data->arr;
|
||||||
|
|
||||||
double start = now();
|
string start_time = now();
|
||||||
|
|
||||||
pid_t pid = getpid();
|
cout << "START PID=" << getpid()
|
||||||
pid_t ppid = getppid();
|
<< " PPID=" << getppid()
|
||||||
|
|
||||||
cout << "[START] t=" << start
|
|
||||||
<< " PID=" << pid
|
|
||||||
<< " PPID=" << ppid
|
|
||||||
<< " depth=" << depth
|
<< " depth=" << depth
|
||||||
<< " range=[" << l << "," << r << "]\n";
|
<< " range=[" << l << "," << r << "]"
|
||||||
|
<< " time=" << start_time << "\n";
|
||||||
|
|
||||||
|
if (l >= r) {
|
||||||
|
string end_time = now();
|
||||||
|
cout << "END PID=" << getpid()
|
||||||
|
<< " range=[" << l << "," << r << "]"
|
||||||
|
<< " time=" << end_time << "\n";
|
||||||
|
shmdt(data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
int m = (l + r) / 2;
|
int m = (l + r) / 2;
|
||||||
|
|
||||||
if (depth >= MAX_DEPTH) {
|
if (depth >= MAX_DEPTH) {
|
||||||
local_sort(arr, l, r);
|
local_sort(arr, l, r);
|
||||||
|
|
||||||
double end = now();
|
string end_time = now();
|
||||||
cout << "[END] t=" << end
|
cout << "END PID=" << getpid()
|
||||||
<< " PID=" << pid
|
<< " depth=" << depth
|
||||||
<< " duration=" << (end - start)
|
<< " range=[" << l << "," << r << "]"
|
||||||
<< " sec\n";
|
<< " time=" << end_time << "\n";
|
||||||
|
|
||||||
|
shmdt(data);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
pid_t left = fork();
|
pid_t left = fork();
|
||||||
|
|
||||||
if (left == 0) {
|
if (left == 0) {
|
||||||
parallel_sort(arr, l, m, depth + 1);
|
parallel_sort(l, m, depth + 1, shmid);
|
||||||
_exit(0);
|
_exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
pid_t right = fork();
|
pid_t right = fork();
|
||||||
|
|
||||||
if (right == 0) {
|
if (right == 0) {
|
||||||
parallel_sort(arr, m + 1, r, depth + 1);
|
parallel_sort(m + 1, r, depth + 1, shmid);
|
||||||
_exit(0);
|
_exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,20 +110,21 @@ void parallel_sort(int *arr, int l, int r, int depth) {
|
|||||||
|
|
||||||
merge(arr, l, m, r);
|
merge(arr, l, m, r);
|
||||||
|
|
||||||
double end = now();
|
string end_time = now();
|
||||||
|
|
||||||
cout << "[END] t=" << end
|
cout << "END PID=" << getpid()
|
||||||
<< " PID=" << pid
|
<< " depth=" << depth
|
||||||
<< " duration=" << (end - start)
|
<< " range=[" << l << "," << r << "]"
|
||||||
<< " sec\n";
|
<< " time=" << end_time << "\n";
|
||||||
|
|
||||||
|
shmdt(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= MAIN =================
|
|
||||||
int main() {
|
int main() {
|
||||||
key_t key = ftok("shmfile", 65);
|
key_t key = ftok("shmfile", 65);
|
||||||
int shmid = shmget(key, sizeof(SharedData), 0666 | IPC_CREAT);
|
int shmid = shmget(key, sizeof(SharedData), 0666 | IPC_CREAT);
|
||||||
|
|
||||||
SharedData *data = (SharedData*) shmat(shmid, (void*)0, 0);
|
SharedData *data = (SharedData*) shmat(shmid, NULL, 0);
|
||||||
|
|
||||||
int n = 20000;
|
int n = 20000;
|
||||||
|
|
||||||
@@ -122,27 +133,17 @@ int main() {
|
|||||||
for (int i = 0; i < n; i++)
|
for (int i = 0; i < n; i++)
|
||||||
data->arr[i] = rand() % 100000;
|
data->arr[i] = rand() % 100000;
|
||||||
|
|
||||||
double global_start = now();
|
cout << "Before:\n";
|
||||||
|
for (int i = 0; i < 20; i++)
|
||||||
|
cout << data->arr[i] << " ";
|
||||||
|
cout << "\n\n";
|
||||||
|
|
||||||
cout << "Main PID=" << getpid() << "\n\n";
|
parallel_sort(0, n - 1, 0, shmid);
|
||||||
|
|
||||||
parallel_sort(data->arr, 0, n - 1, 0);
|
cout << "\nAfter:\n";
|
||||||
|
for (int i = 0; i < 20; i++)
|
||||||
double global_end = now();
|
cout << data->arr[i] << " ";
|
||||||
|
cout << "\n";
|
||||||
cout << "\nExecution time: "
|
|
||||||
<< (global_end - global_start)
|
|
||||||
<< " sec\n";
|
|
||||||
|
|
||||||
// Проверка
|
|
||||||
for (int i = 1; i < n; i++) {
|
|
||||||
if (data->arr[i - 1] > data->arr[i]) {
|
|
||||||
cout << "ERROR: not sorted!\n";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cout << "Sorted correctly\n";
|
|
||||||
|
|
||||||
shmdt(data);
|
shmdt(data);
|
||||||
shmctl(shmid, IPC_RMID, NULL);
|
shmctl(shmid, IPC_RMID, NULL);
|
||||||
|
|||||||
Reference in New Issue
Block a user