Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 108 additions & 11 deletions CompMath1/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,127 @@ double acceleration(double t)

void calc(double* trace, uint32_t traceSize, double t0, double dt, double y0, double y1, int rank, int size)
{
int count, pre_count;
int mod;
double v, u;
double start_t;

// Sighting shot
double v0 = 0;
MPI_Bcast(&dt, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
if (rank == 0 && size > 0)
{
trace[0] = y0;
trace[1] = y0 + dt*v0;
for (uint32_t i = 2; i < traceSize; i++)
count = traceSize / size;
mod = traceSize % size;

++count;
for(int i = 0; i < mod; ++i)
{
start_t = t0 + dt * ((count - 1) + i * count);
MPI_Send(&count, 1 , MPI_INT, i + 1, 0, MPI_COMM_WORLD);
MPI_Send(&start_t, 1 , MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD);
}

--count;
for(int i = mod; i < size - 1; ++i)
{
trace[i] = dt*dt*acceleration(t0 + (i - 1)*dt) + 2*trace[i - 1] - trace[i - 2];
start_t = t0 + dt * (count + mod + i * count);
MPI_Send(&count, 1 , MPI_INT, i + 1, 0, MPI_COMM_WORLD);
MPI_Send(&start_t, 1 , MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD);
}
start_t = t0;

}
if (rank != 0 && size > 0)
{
MPI_Recv(&count, 1 , MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(&start_t, 1 , MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}

// The final shot
v = 0;
double* local_array = new double[count + 1];
if (rank != 0 && size > 0)
local_array[0] = 0;
if (rank == 0 && size > 0)
local_array[0] = y0;
local_array[1] = local_array[0] + dt * v;
for (int i = 2; i < (count + 1); i++)
local_array[i] = dt*dt*acceleration(start_t + (i - 1)*dt) + 2*local_array[i - 1] - local_array[i - 2];
u = (local_array[count] - local_array[count - 1]) / dt; // b = local_array[count]

double tmp_a, tmp_b, tmp_u, tmp_v;

if (rank != 0 && size > 0)
{
MPI_Recv(&pre_count, 1 , MPI_INT, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(&tmp_a, 1 , MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(&tmp_b, 1 , MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(&tmp_u, 1 , MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(&tmp_v, 1 , MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
local_array[0] = tmp_a + tmp_b + dt * pre_count * tmp_v;
v = tmp_u + tmp_v;
}
if ((rank != size - 1) && size > 1)
{
MPI_Send(&count, 1 , MPI_INT, rank + 1, 0, MPI_COMM_WORLD);
MPI_Send(&local_array[0], 1 , MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD);
MPI_Send(&local_array[count], 1 , MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD);
MPI_Send(&u, 1 , MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD);
MPI_Send(&v, 1 , MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD);
}
if (rank == size - 1)
{
v0 = (y1 - trace[traceSize - 1])/(dt*traceSize);
trace[0] = y0;
trace[1] = y0 + dt*v0;
for (uint32_t i = 2; i < traceSize; i++)
if (size > 1)
{
trace[i] = dt*dt*acceleration(t0 + (i - 1)*dt) + 2*trace[i - 1] - trace[i - 2];
double tmp_y1_miss = local_array[0] + local_array[count - 1] + dt * (count - 1) * v; //because we dont need local_array[count]
MPI_Send(&tmp_y1_miss, 1 , MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
}
}


// The final shot
if (rank == 0 && size > 0)
{
double y1_miss;
if (size > 1)
MPI_Recv(&y1_miss, 1 , MPI_DOUBLE, size - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
else
y1_miss = local_array[0] + local_array[count - 1] + dt * (count - 1) * v;
printf("y1_miss: %f\n", y1_miss);
v = (y1 - y1_miss) / (dt * traceSize);
local_array[0] = y0;
}
if (rank != 0 && size > 0)
{
double tmp_a_2, tmp_v_2;
MPI_Recv(&tmp_a_2, 1 , MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(&tmp_v_2, 1 , MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
local_array[0] = tmp_b + tmp_a_2 + dt * pre_count * tmp_v_2;
v = tmp_u + tmp_v_2;
}
if ((rank != size - 1) && size > 0)
{
MPI_Send(&local_array[0], 1 , MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD);
MPI_Send(&v, 1 , MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD);
}
local_array[1] = local_array[0] + dt * v;
for(int i = 2; i < count; ++i)
local_array[i] = dt*dt*acceleration(start_t + (i - 1)*dt) + 2*local_array[i - 1] - local_array[i - 2];

if (rank != 0 && size > 0)
MPI_Send(local_array, count , MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
if (rank == 0 && size > 0)
{
for(int i = 0; i < count; ++i)
trace[i] = local_array[i];
++count;
for(int i = 0; i < mod; ++i)
MPI_Recv(&trace[(count - 1) + i * count], count , MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
--count;
for(int i = mod; i < size - 1; ++i)
MPI_Recv(&trace[count + mod + i * count], count , MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

}
delete local_array;
}

int main(int argc, char** argv)
Expand Down
128 changes: 87 additions & 41 deletions CompMath2/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,108 @@

void calc(double* frame, uint32_t ySize, uint32_t xSize, double delta, int rank, int size)
{
int count, mod;
double* local_array;
MPI_Bcast(&xSize, 1, MPI_INT, 0, MPI_COMM_WORLD);

if (rank == 0 && size > 0)
{
double diff = 0;
double* tmpFrame = new double[ySize * xSize];
// Prepare tmpFrame
for (uint32_t y = 0; y < ySize; y++)
count = (ySize - 2) / size;
mod = (ySize - 2) % size;

++count;
for(int i = 0; i < mod; ++i)
{
tmpFrame[y*xSize] = frame[y*xSize];
tmpFrame[y*xSize + xSize - 1] = frame[y*xSize + xSize - 1];
MPI_Send(&count, 1 , MPI_INT, i + 1, 0, MPI_COMM_WORLD);
MPI_Send(&frame[(1 + (count - 1) + i * count - 1) * xSize], (count + 2) * xSize , MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD);
}
for (uint32_t x = 1; x < xSize - 1; x++)

--count;
for(int i = mod; i < size - 1; ++i)
{
tmpFrame[x] = frame[x];
tmpFrame[(ySize - 1)*xSize + x] = frame[(ySize - 1)*xSize + x];
}
// Calculate first iteration
for (uint32_t y = 1; y < ySize - 1; y++)
{
for (uint32_t x = 1; x < xSize - 1; x++)
{
tmpFrame[y*xSize + x] = (frame[(y + 1)*xSize + x] + frame[(y - 1)*xSize + x] +\
frame[y*xSize + x + 1] + frame[y*xSize + x - 1])/4.0;
diff += std::abs(tmpFrame[y*xSize + x] - frame[y*xSize + x]);
}
MPI_Send(&count, 1 , MPI_INT, i + 1, 0, MPI_COMM_WORLD);
MPI_Send(&frame[(1 + count + mod + i * count - 1) * xSize], (count + 2) * xSize , MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD);
}

double* currFrame = tmpFrame;
double* nextFrame = frame;
uint32_t iteration = 1;
// Calculate frames
while (diff > delta)
local_array = frame;
}

if (rank != 0 && size > 0)
{
MPI_Recv(&count, 1 , MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
local_array = new double[(count + 2) * xSize];
MPI_Recv(local_array, (count + 2) * xSize , MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}

double* up_string = new double[xSize - 2]; //up_string is what was upper
double* tmp_string = new double[xSize - 1]; //tmp_string[0] is what was lefter
int is_calculate = 1;
double diff;
double diff_sum;
double* diff_recv;
if (rank == 0 && size > 0)
diff_recv = new double[size];
while(is_calculate)
{
diff = 0;
for(uint32_t x = 1; x < xSize - 1; ++x)
up_string[x - 1] = local_array[x];
for(int y = 1; y < count + 1; ++y)
{
diff = 0;
for (uint32_t y = 1; y < ySize - 1; y++)
tmp_string[0] = local_array[y * xSize];
for(uint32_t x = 1; x < xSize - 1; ++x)
{
for (uint32_t x = 1; x < xSize - 1; x++)
{
nextFrame[y*xSize + x] = (currFrame[(y + 1)*xSize + x] + currFrame[(y - 1)*xSize + x] +\
currFrame[y*xSize + x + 1] + currFrame[y*xSize + x - 1])/4.0;
diff += std::abs(nextFrame[y*xSize + x] - currFrame[y*xSize + x]);
}
tmp_string[x] = local_array[y*xSize + x];
local_array[y*xSize + x] = (up_string[x - 1] + local_array[(y + 1)*xSize + x] +\
local_array[y*xSize + x + 1] + tmp_string[x - 1])/4.0;
up_string[x - 1] = tmp_string[x];
diff += std::abs(local_array[y*xSize + x] - tmp_string[x]);
}
std::swap(currFrame, nextFrame);
iteration++;
}

// Copy result from tmp
if (iteration % 2 == 1)
//0--->ySize
if (rank != 0 && size > 0)
MPI_Recv(&local_array[1], xSize - 2 , MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if ((rank != size - 1) && size > 0)
MPI_Send(&local_array[count * xSize + 1], xSize - 2 , MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD);
//0<---ySize
if ((rank != size - 1) && size > 0)
MPI_Recv(&local_array[(count + 1) * xSize + 1], xSize - 2 , MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (rank != 0 && size > 0)
MPI_Send(&local_array[1 * xSize + 1], xSize - 2 , MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD);

MPI_Gather(&diff, 1, MPI_DOUBLE, diff_recv, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
if (rank == 0 && size > 0)
{
for (uint32_t i = 0; i < xSize*ySize; i++)
{
frame[i] = tmpFrame[i];
}
diff_sum = 0;
for(int i = 0; i < size; ++i)
diff_sum += diff_recv[i];
if (diff_sum <= delta)
is_calculate = 0;
}
delete tmpFrame;
MPI_Bcast(&is_calculate, 1, MPI_INT, 0, MPI_COMM_WORLD);

}
if (rank != 0 && size > 0)
{
MPI_Send(&local_array[1 * xSize], xSize * count , MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
}
if (rank == 0 && size > 0)
{
++count;
for(int i = 0; i < mod; ++i)
MPI_Recv(&frame[(1 + (count - 1) + i * count) * xSize], count * xSize , MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
--count;
for(int i = mod; i < size - 1; ++i)
MPI_Recv(&frame[(1 + count + mod + i * count) * xSize], count * xSize, MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}

if (rank == 0 && size > 0)
delete diff_recv;
if (rank != 0 && size > 0)
delete local_array;
delete up_string;
delete tmp_string;
}

int main(int argc, char** argv)
Expand Down
Binary file added HeatMap/HeatMap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions HeatMap/heatTable.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
20
31 198 731 3648 1011 3895 1003 1505 3436 5708 5575 1976 8865 2671 6557 6471 3060 4438 8459 1477
346 5352 11736 398 7494 708 4300 963 1133 6373 8475 1442 7438 1801 2078 2188 2805 2612 6445 2944
10818 8034 8029 4536 6274 1732 8252 13360 8665 5327 2470 2398 8017 2939 15942 6134 8930 4149 5846 7492
23766 405 649 18906 4963 24127 10445 17795 14682 19266 14095 3769 5068 21338 28790 18090 6962 21272 8195 9011
8124 396 782 1102 1633 2201 2510 3109 3477 3980 4463 4872 5318 6002 7154 6594 6445 8290 8324 9220
469 792 1033 1434 1765 2587 2649 3605 4049 4500 5839 5471 6858 7297 7009 8888 9119 9208 10303 10391
1485 627 1229 2079 2148 3131 3464 5134 5395 5761 5852 6829 7778 7099 8375 10947 10161 10248 11622 13279
278 705 1464 2130 2677 3850 3734 4576 5590 7335 7339 7917 9369 10698 9974 11534 11427 12512 11933 15186
635 844 1762 2371 3704 4052 4793 5485 6791 7309 7692 8676 11193 10083 13067 12791 13603 14201 15126 14959
454 1690 1952 2936 3741 3918 5817 7170 6532 8341 9256 9934 10487 11588 13339 13755 15170 14416 16875 17437
1788 1048 2131 3289 4117 5830 6097 7116 7702 9438 10021 11780 15343 17228 14928 16738 15380 19516 19116 19515
936 1246 2181 3441 4353 5444 6357 7756 8099 10237 10897 12335 13945 14255 16192 16348 17530 19667 21263 20902
1085 1574 2262 3904 5133 5257 7822 8176 10062 11145 12811 12844 14524 15079 17572 16432 21341 20594 22159 22960
1387 1555 2727 3970 5057 6648 8134 8780 8979 12353 13642 12450 16476 16438 18175 19565 20487 23751 24442 24206
1754 1697 2794 4079 5202 7710 8314 9654 11377 12675 13937 15338 17062 18702 20160 20176 23363 23697 24503 25357
1625 1814 2906 4423 6252 7488 8995 10806 12119 12953 15484 15556 18054 20370 19738 21299 25678 25434 26377 29035
821 1781 3229 4816 6435 7936 9117 11509 13764 14647 16420 15889 20705 21097 22463 23801 34761 27085 28386 29914
645 2486 3389 5013 6631 8569 9919 12519 12134 15579 17534 18479 18981 20165 23039 25437 25876 31150 28725 32798
898 2271 3929 5242 7372 8370 10788 12644 14565 16055 18651 19608 21929 23051 24408 27094 28413 29583 33296 33799
2439 2085 4009 5593 7193 9305 11484 13168 15106 18024 18288 20107 21020 26085 26508 28744 30182 32361 32827 36708
44 changes: 44 additions & 0 deletions HeatMap/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
#include <iomanip>
#include <fstream>
#include <omp.h>
#include <ctime>
#include <unistd.h>

#define TABLE_SIZE 20


int main(int argc, char** argv)
{
omp_set_nested(1);

clock_t start_time, end_time;
clock_t arr_time[TABLE_SIZE][TABLE_SIZE];

std::ofstream fout("heatTable.txt");
fout << TABLE_SIZE << '\n';

for(int i = 1; i <= TABLE_SIZE; ++i)
for(int j = 1; j <= TABLE_SIZE; ++j)
{
start_time = clock();
#pragma omp parallel num_threads(i)
{
#pragma omp parallel num_threads(j)
{
usleep(10);
}
}
end_time = clock();
arr_time[i - 1][j - 1] = end_time - start_time;
}

for(int i = 0; i < TABLE_SIZE; ++i)
{
for(int j = 0; j < TABLE_SIZE; ++j)
fout << arr_time[i][j] << ' ';
fout << '\n';
}
fout.close();
return 0;
}
8 changes: 8 additions & 0 deletions HeatMap/map_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import matplotlib.pyplot as plt
import numpy as np

with open('heatTable.txt', 'r') as f:
table_size = f.readline()
data = np.loadtxt('heatTable.txt', skiprows=1)
plt.imshow(data, cmap='hot', interpolation='nearest')
plt.show()
Loading