add timers
This commit is contained in:
@@ -15,6 +15,13 @@ struct SharedData {
|
|||||||
int arr[MAX];
|
int arr[MAX];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// ================= TIME =================
|
||||||
|
double now() {
|
||||||
|
timeval t;
|
||||||
|
gettimeofday(&t, NULL);
|
||||||
|
return t.tv_sec + t.tv_usec / 1e6;
|
||||||
|
}
|
||||||
|
|
||||||
// ================= MERGE =================
|
// ================= 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];
|
||||||
@@ -50,14 +57,27 @@ void local_sort(int *arr, int l, int r) {
|
|||||||
void parallel_sort(int *arr, int l, int r, int depth) {
|
void parallel_sort(int *arr, int l, int r, int depth) {
|
||||||
if (l >= r) return;
|
if (l >= r) return;
|
||||||
|
|
||||||
int m = (l + r) / 2;
|
double start = now();
|
||||||
|
|
||||||
cout << "PID " << getpid()
|
pid_t pid = getpid();
|
||||||
|
pid_t ppid = getppid();
|
||||||
|
|
||||||
|
cout << "[START] t=" << start
|
||||||
|
<< " PID=" << pid
|
||||||
|
<< " PPID=" << ppid
|
||||||
<< " depth=" << depth
|
<< " depth=" << depth
|
||||||
<< " range=[" << l << "," << r << "]\n";
|
<< " range=[" << l << "," << r << "]\n";
|
||||||
|
|
||||||
|
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();
|
||||||
|
cout << "[END] t=" << end
|
||||||
|
<< " PID=" << pid
|
||||||
|
<< " duration=" << (end - start)
|
||||||
|
<< " sec\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,6 +99,13 @@ void parallel_sort(int *arr, int l, int r, int depth) {
|
|||||||
waitpid(right, NULL, 0);
|
waitpid(right, NULL, 0);
|
||||||
|
|
||||||
merge(arr, l, m, r);
|
merge(arr, l, m, r);
|
||||||
|
|
||||||
|
double end = now();
|
||||||
|
|
||||||
|
cout << "[END] t=" << end
|
||||||
|
<< " PID=" << pid
|
||||||
|
<< " duration=" << (end - start)
|
||||||
|
<< " sec\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= MAIN =================
|
// ================= MAIN =================
|
||||||
@@ -95,19 +122,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;
|
||||||
|
|
||||||
timeval start, end;
|
double global_start = now();
|
||||||
|
|
||||||
gettimeofday(&start, NULL);
|
cout << "Main PID=" << getpid() << "\n\n";
|
||||||
|
|
||||||
parallel_sort(data->arr, 0, n - 1, 0);
|
parallel_sort(data->arr, 0, n - 1, 0);
|
||||||
|
|
||||||
gettimeofday(&end, NULL);
|
double global_end = now();
|
||||||
|
|
||||||
double time_spent =
|
cout << "\nExecution time: "
|
||||||
(end.tv_sec - start.tv_sec) +
|
<< (global_end - global_start)
|
||||||
(end.tv_usec - start.tv_usec) / 1e6;
|
<< " sec\n";
|
||||||
|
|
||||||
cout << "\nExecution time: " << time_spent << " sec\n";
|
|
||||||
|
|
||||||
// Проверка
|
// Проверка
|
||||||
for (int i = 1; i < n; i++) {
|
for (int i = 1; i < n; i++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user