Informe 2
Informe 2
Informe 2
0.1 Ejercicio 2
0.1.1 A
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <omp.h>
1
j--;
if (i >= j)
break;
t = B[i];
B[i] = B[j];
B[j] = t;
}
quicksort(B, i);
quicksort(B + i, n - i);
}
int x = 1;
2
scan_left(C + x, n); // acumula los valores de elementos de C mas
// cout << "Scan left" << endl;
// print_arrays(A, B, C, n);
return res;
}
int main()
{
int n = 100000;
int A[n], B[n], C[n];
srand(time(NULL));
for (int i = 0; i < n; ++i)
{
A[i] = rand() % 10;
}
3
for (int i = 0; i < n; ++i)
{
B[i] = rand() % 10;
}
4
x = B[n / 2];
for (i = 0, j = n - 1;; i++, j--)
{
while (B[i] < x)
i++;
while (x < B[j])
j--;
if (i >= j)
break;
t = B[i];
B[i] = B[j];
B[j] = t;
}
quicksort(B, i);
quicksort(B + i, n - i);
}
int x = 1;
5
}
return res;
}
int main()
{
int n = 100000;
int A[n], B[n], C[n];
6
srand(time(NULL));
for (int i = 0; i < n; ++i)
{
A[i] = rand() % 10;
}
7
0.1.2 DAG
0.2 B
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <omp.h>
8
{
int i, j;
int x, t;
if (n < 2)
return;
x = B[n / 2];
for (i = 0, j = n - 1;; i++, j--)
{
while (B[i] < x)
i++;
while (x < B[j])
j--;
if (i >= j)
break;
t = B[i];
B[i] = B[j];
B[j] = t;
}
quicksort(B, i);
quicksort(B + i, n - i);
}
int x = 1;
9
#pragma omp task depend(out : A[0], B[0], C[0])
{
suma_de_prefijos(A, n); // obtiene el array de suma de prefijos de A
// cout << "Prefix sum" << endl;
// print_arrays(A, B, C, n);
}
10
#pragma omp parallel for reduction(+ : res) private(i)
for (i = 0; i < n; i++)
res += a * C[i];
return res;
}
int main()
{
int n = 100000;
int A[n], B[n], C[n];
srand(time(NULL));
for (int i = 0; i < n; ++i)
{
A[i] = rand() % 10;
}
omp_set_num_threads(64);
double start, end;
start = omp_get_wtime();
#pragma omp parallel
{
#pragma omp single
{
cout << OpArr(A, B, C, n) << endl;
}
}
end = omp_get_wtime();
11
// print_arrays((int *)A, (int *)B, (int *)C, n);
}
0.2.1 C
[5]: import matplotlib.pyplot as plt
import math
n = 1000000
times_ = []
ps= [1, 2, 4, 8, 16, 32]
for p in ps:
times_.append(n* math.log(n) / p)
plt.xlabel("# procesadores")
plt.ylabel("tiempo (s)")
plt.legend()
plt.show()
12
[3]: import numpy as np
import matplotlib.pyplot as plt
13
plt.xlabel('Número de Hilos (p)')
plt.ylabel('Speedup')
plt.title('Speedup vs. Número de Hilos')
plt.legend()
plt.grid(True)
plt.show()
def sequential_time(n):
return algorithm_complexity(n)
n = 1000000
p_values = [1, 2, 4, 8, 16, 32] # Números de hilos a evaluar
14
speedup = theoretical_speedup(n, p)
print(f"Speedup teórico para p = {p}: {speedup:.2f}")
15