public
Last active

  • Download Gist
parallel.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
for (t = 0; t < iteratii; ++t) {
n_res0 = n_res1 = 0;
p_res0 = p_res1 = 0;
// pentru fiecare negustor
#pragma omp parallel for private(i, j, _c, _v, k, l) schedule(dynamic)
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
// calculele
_c = pMax;
_v = pMax;
for (k = 0; k < n; ++k) {
for (l = 0; l < n; ++l) {
if (res[i][j] != res[k][l]) {
// resursa complementara
_c = min(_c, pret[k][l] + dist(i, j, k, l));
} else {
// aceeasi resursa
_v = min(_v, pret[k][l] + dist(i, j, k, l));
}
}
}
cmin[i][j] = _c;
cmin_res[i][j] = _v;
}
}
 
#pragma omp parallel for private(i, j) schedule(dynamic)
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
// Actualizare preturi si bugete pentru anul urmator
if (cmin[i][j] < buget[i][j]) {
pret[i][j] += (cmin[i][j] - buget[i][j]) / 2;
buget[i][j] = cmin[i][j];
pret[i][j] = max(pret[i][j], pMin);
} else {
if (cmin[i][j] > buget[i][j]) {
pret[i][j] += (cmin[i][j] - buget[i][j]);
buget[i][j] = cmin[i][j];
} else {
buget[i][j] = cmin[i][j];
pret[i][j] = cmin_res[i][j] + 1;
}
 
if (pret[i][j] > pMax) {
res[i][j] = 1 - res[i][j];
buget[i][j] = pMax;
pret[i][j] = (pMin + pMax) / 2;
}
}
if (res[i][j]) {
#pragma omp critical
{
n_res1++;
p_res1 = max(p_res1, pret[i][j]);
}
} else {
#pragma omp critical
{
n_res0++;
p_res0 = max(p_res0, pret[i][j]);
}
}
}
}
fprintf(out, "%d %d %d %d\n", n_res0, p_res0, n_res1, p_res1);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.