Skip to content
Merged
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
9 changes: 9 additions & 0 deletions 1706-4/dobrohotov_vn/Lab 1/Lab 1/Lab 1.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>C:\Program Files %28x86%29\Microsoft SDKs\MPI\Include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>C:\Program Files %28x86%29\Microsoft SDKs\MPI\Lib\x64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>msmpi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
Expand All @@ -108,13 +113,17 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>C:\Program Files %28x86%29\Microsoft SDKs\MPI\Include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>C:\Program Files %28x86%29\Microsoft SDKs\MPI\Lib\x64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>msmpi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Source.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
5 changes: 5 additions & 0 deletions 1706-4/dobrohotov_vn/Lab 1/Lab 1/Lab 1.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Source.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
201 changes: 201 additions & 0 deletions 1706-4/dobrohotov_vn/Lab 1/Lab 1/Source.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
#include <iostream>
#include <iomanip>
#include <mpi.h>

using namespace std;

int* InitMatrix(int rows, int cols)
{
int* vec = new int[rows*cols];
if (rows < 2 && cols < 2)
for (int i = 0; i < rows*cols; i++)
cin >> vec[i];
else
for (int i = 0; i < rows*cols; i++)
vec[i] = rand() % 10000;
return vec;
}
void PrintVector(int *x, int size)
{
if (size < 50)
for (int i = 0; i < size; i++)
cout << x[i] << " ";
else
cout << "Vector too large" << endl;
}
void PrintInfo(int rank, int size, int block, int rows, int cols, int *tmp, int *local_max)
{
cout << "\nInformation\nProcess " << rank << "\n" << "Size " << size << "\n" << "block " << block << "\n" << "rows " << rows << "\n" << "cols " << cols << endl;

for (int i = 0; i < rows*block; i++)
cout << " tmp " << rank << " " << tmp[i] << " ";
for (int i = 0; i < block; i++)
cout << "local max " << rank << " " << local_max[i] << " ";
}
void PrintMatrix(int *vec, int rows, int cols)
{
if (rows < 100 && cols < 100) {
cout << vec[0] << setw(2) << " ";
for (int i = 1; i < rows*cols; i++)
{
cout << setw(2) << vec[i];
(i % (cols)) == cols - 1 ? cout << endl : cout << " ";
}
cout << endl;
}
else
cout << "Matrix too large" << endl;
}
void Tranpose(int* vec, int rows, int cols)
{
int* copy = new int[rows*cols];
for (int i = 0; i < rows*cols; i++)
copy[i] = vec[i];
int ind = 0;
for (int i = 0; i < cols; i++) {
int n = 0;
for (int j = 0; j < rows; j++) {
vec[ind] = copy[i + n];
ind++;
n += cols;
}
}
delete[] copy;
}

void Max(int *mas, int *res, int cols, int pos)
{
int max = mas[0];
for (int i = 1; i < cols; i++)
{
if (max < mas[i])
max = mas[i];
}
res[pos] = max;
}

int main(int argc, char ** argv)
{
int size; //��������� ���������
int rank; //�������������� ��������
int rows, cols; //������� �������
double start_linear = 0, end_linear = 0,
start_pp = 0, end_pp = 0;
int* vec = NULL; //������� � ���� �������
int* resl = NULL; //�������������� ������ ���������� ��� �����������������
int *resp;//=NULL; //= NULL; //�������������� ������ ���������� ��� ������������
int *tmp = NULL; //��������� ������ ��� �����
int *local_max = NULL; //����� ����� ��������� ��������� ���������
int block; //���������� �������� � �����
int left; //���������� �����
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (size < 1) {
cout << "Error count process" << endl;
return -2;
}
if (rank == 0) {
//Input Matrix
while (1) {
cout << "Input rows and cols" << endl;
cin >> rows;
cin >> cols;
if (cols < 1 || rows < 1)
{
system("cls");
cout << "rows and cols must be >0" << endl;
}
else
break;
}
//Initialize
if (cols < size) //���� ������� ������ ��� ���������, �� ������������� ��� ��������, � ������� ������� �����
size = cols;
vec = InitMatrix(rows, cols); //��������������� ������� � ���� ������� ��������
PrintMatrix(vec, rows, cols);
Tranpose(vec, rows, cols); //���������������� ������� (������������� �������)
block = cols / size; //� ����� n-�� ��� �� ��������
left = cols % size; //���������� ������� ������� �� ������� �� ��������
tmp = new int[rows*block]; // ������������� ������ ��� ������ �������
local_max = new int[block];

//Linear algorithm
resl = new int[cols]; // ������ � ������� ����� �������� ������������ �������� ��������� ���������
start_linear = MPI_Wtime();
int max = vec[0];
int k = 0;
for (int i = 1; i < rows*cols; i++)
{
if (max < vec[i])
max = vec[i];
if (i % rows == rows - 1) {
resl[k] = max;
k++;
max = vec[++i];
}
}
end_linear = MPI_Wtime();
}
//parellel algorithm
start_pp = MPI_Wtime();
MPI_Bcast(&size, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&block, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&rows, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&cols, 1, MPI_INT, 0, MPI_COMM_WORLD);
resp = new int[cols]; // ������ � ������� ����� �������� ������������ ��������
if (rank > 0)
{
tmp = new int[rows*block]; // ������������� ������ ��� ������ �������
local_max = new int[block];
}
MPI_Scatter(vec, rows*block, MPI_INT, tmp, rows*block, MPI_INT, 0, MPI_COMM_WORLD);
//����� ��������� �� �������� � ��������� ��� � ������ ��������� ����
int c = 0; //��������
for (int i = 0; i < block; i++)
{
int max = tmp[0 + c];
for (int j = 1; j < rows; j++)
{
if (tmp[j + c] > max)
max = tmp[j + c];
}
c += rows;
local_max[i] = max;
}
MPI_Gather(local_max, block, MPI_INT, resp, block, MPI_INT, 0, MPI_COMM_WORLD);
//��������� ������� ������� �� ������ �� ��������
if (rank == 0)
if (left != 0)
{
cout << "i am in left" << endl;
int c = 0; //��������
for (int i = 0; i < left; i++)
{
int max = vec[rows*size*block + c];
for (int j = 1; j < rows; j++)
{
if (vec[j + rows * size * block + c] > max)
max = vec[j + rows * size *block + c];
}
c += rows;
resp[i + size*block] = max;
}
}
end_pp = MPI_Wtime();
if (rank == 0)
{
PrintVector(resl,cols);
cout << endl;
PrintVector(resp, cols);
cout << endl;
cout << "time linear " << (end_linear - start_linear) * 1000 << endl;
cout << "time parallel " << (end_pp - start_pp) * 1000 << endl;
}
delete[]vec;
delete[]resl;
//delete[]resp;
delete[] tmp;
delete[] local_max;
MPI_Finalize();
}