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
216 changes: 138 additions & 78 deletions Loop1/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,98 +4,158 @@
#include <mpi.h>
#include <unistd.h>
#include <cmath>
#include <algorithm>

void calc(double* arr, uint32_t ySize, uint32_t xSize, int rank, int size)
{
if (rank == 0 && size > 0)
{
for (uint32_t y = 0; y < ySize; y++)
{
for (uint32_t x = 0; x < xSize; x++)
{
arr[y*xSize + x] = sin(0.00001*arr[y*xSize + x]);
}
}
}
}
#include <string.h>
#include <cstdlib>

int main(int argc, char** argv)
{
int rank = 0, size = 0, buf = 0;
uint32_t ySize = 0, xSize = 0;
double* arr = 0;

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

if (rank == 0)
{
// Check arguments
if (argc != 3)
{
std::cout << "[Error] Usage <inputfile> <output file>\n";
buf = 1;
MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD);
return 1;
}
#define ROOT 0

// Prepare input file
std::ifstream input(argv[1]);
if (!input.is_open())
{
std::cout << "[Error] Can't open " << argv[1] << " for write\n";
buf = 1;
MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD);
return 1;
#define S_TAG 1

#define coord(x, y, xSize) ((y)*(xSize) + x)

// arr[y*xSize + x] = sin(0.00001*arr[y*xSize + x]);
void calc(double* arr, uint32_t ySize, uint32_t xSize, int rank, int size) {
int32_t step = 0;
int32_t* range = NULL;
int32_t* displace = NULL;

MPI_Bcast(&xSize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD);
MPI_Bcast(&ySize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD);

if(rank == ROOT) {
uint32_t y_step = ceil(ySize / (1.0 * size));
uint32_t prev_range = 0;

range = (int32_t*)calloc(size, sizeof(int32_t));
displace = (int32_t*)calloc(size, sizeof(int32_t));

for(int send_rank = 0; send_rank < size; ++send_rank) {
// We will send start_y, step, xSize and part of arr
uint32_t real_step = std::min(y_step, ySize - prev_range);
range[send_rank] = real_step * xSize;
displace[send_rank] = prev_range * xSize;
prev_range += y_step;
}
}
MPI_Scatter(range, 1, MPI_INT,
&step, 1, MPI_INT,
ROOT, MPI_COMM_WORLD);

// Read arguments from input
input >> ySize >> xSize;
MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD);

arr = new double[ySize * xSize];
double* my_copy = (double*) calloc(step, sizeof(double));
MPI_Scatterv(arr, range, displace, MPI_DOUBLE,
my_copy, step, MPI_DOUBLE,
ROOT, MPI_COMM_WORLD);

for (uint32_t y = 0; y < ySize; y++)
{
for (uint32_t x = 0; x < xSize; x++)
{
input >> arr[y*xSize + x];
}

for(int32_t x = 0; x < step; ++x) {
my_copy[x] = sin(0.00001*my_copy[x]);
}
input.close();
} else {
MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (buf != 0)
{
return 1;
/*
if(size > 1 && rank == 1) {
for(int i = 0; i < step; ++i) {
printf("%lf \n", my_copy[i]);
}
}*/
/*
if(rank == ROOT) {
for(int i = 0; i < size; ++i) {
printf("rang[i] = %d, dit[i] = %d\n", range[i], displace[i]);
}
}
}*/

//printf("[%d] xSize = %u, step = %u, my_copy = %p\n", rank, xSize, step, my_copy);

MPI_Gatherv(my_copy, step, MPI_DOUBLE,
arr, range, displace, MPI_DOUBLE,
ROOT, MPI_COMM_WORLD);

if(rank == ROOT) {

free(range);
free(displace);
}
free(my_copy);
}

int main(int argc, char** argv)
{
int rank = 0, size = 0, buf = 0;
uint32_t ySize = 0, xSize = 0;
double* arr = 0;

calc(arr, ySize, xSize, rank, size);
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

if (rank == 0)
{
// Prepare output file
std::ofstream output(argv[2]);
if (!output.is_open())
if (rank == 0)
{
std::cout << "[Error] Can't open " << argv[2] << " for read\n";
delete arr;
return 1;
// Check arguments
if (argc != 3)
{
std::cout << "[Error] Usage <inputfile> <output file>\n";
buf = 1;
MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD);
return 1;
}

// Prepare input file
std::ifstream input(argv[1]);
if (!input.is_open())
{
std::cout << "[Error] Can't open " << argv[1] << " for write\n";
buf = 1;
MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD);
return 1;
}

// Read arguments from input
input >> ySize >> xSize;
MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD);

arr = new double[ySize * xSize];

for (uint32_t y = 0; y < ySize; y++)
{
for (uint32_t x = 0; x < xSize; x++)
{
input >> arr[y*xSize + x];
}
}
input.close();
} else {
MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (buf != 0)
{
return 1;
}
}
for (uint32_t y = 0; y < ySize; y++)
calc(arr, ySize, xSize, rank, size);

if (rank == 0)
{
for (uint32_t x = 0; x < xSize; x++)
{
output << " " << arr[y*xSize + x];
}
output << std::endl;
// Prepare output file
std::ofstream output(argv[2]);
if (!output.is_open())
{
std::cout << "[Error] Can't open " << argv[2] << " for read\n";
delete arr;
return 1;
}
for (uint32_t y = 0; y < ySize; y++)
{
for (uint32_t x = 0; x < xSize; x++)
{
output << " " << arr[y*xSize + x];
}
output << std::endl;
}
output.close();
delete arr;
}
output.close();
delete arr;
}

MPI_Finalize();
return 0;
MPI_Finalize();
return 0;
}
95 changes: 84 additions & 11 deletions Loop2/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,91 @@
#include <unistd.h>
#include <cmath>

void calc(double* arr, uint32_t ySize, uint32_t xSize, int rank, int size)
{
if (rank == 0 && size > 0)
{
for (uint32_t y = 0; y < ySize - 1; y++)
{
for (uint32_t x = 3; x < xSize; x++)
{
arr[y*xSize + x] = sin(0.00001*arr[(y + 1)*xSize + x - 3]);
}
#include <cstdlib>

#define ROOT 0
// auntidep
// y = 1; y < ySize;
// x = 0; y < xSize - 3
//arr[(y - 1)*xSize + (x + 3)] = sin(0.00001*arr[y*xSize + x]);

void calc(double* arr, uint32_t ySize, uint32_t xSize, int rank, int size) {
int* send_dist = NULL;
int* send_range = NULL;

int* recv_dist = NULL;
int* recv_range = NULL;

MPI_Bcast(&ySize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD);
MPI_Bcast(&xSize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD);
int real_rank_size = 0;

if(rank == ROOT) {
size_t my_calc_size = ceil((ySize) * 1.0 / size);
// if proc_rank > ceil(ySize / my_calc_size) it will do nothing
real_rank_size = ceil((ySize) * 1.0 / my_calc_size);
size_t now_size = 0;

recv_dist = (int*)calloc(size, sizeof(int));
recv_range = (int*)calloc(size, sizeof(int));

send_dist = (int*)calloc(size, sizeof(int));
send_range = (int*)calloc(size, sizeof(int));

for(int i = 0; i < real_rank_size; ++i) {
recv_dist[i] = i * xSize * my_calc_size;
recv_range[i] = xSize * std::min(my_calc_size, ySize - now_size);
now_size += std::min(my_calc_size, ySize - now_size);
}

for(int i = 0; i < real_rank_size; ++i) {
send_range[i] = (i == real_rank_size - 1)? recv_range[i] : recv_range[i] + xSize;
send_dist[i] = recv_dist[i];
}
}
int send_step = 0;
int recv_step = 0;


// Bug with MPI <- i hate it! really!
MPI_Scatter(
send_range, 1, MPI_INT,
&send_step, size, MPI_INT,
ROOT, MPI_COMM_WORLD);
MPI_Scatter(
recv_range, 1, MPI_INT,
&recv_step, size, MPI_INT,
ROOT, MPI_COMM_WORLD);

double* my_calc = NULL;
my_calc = (double*)calloc(send_step, sizeof(double));

MPI_Scatterv(
arr, send_range, send_dist, MPI_DOUBLE,
my_calc, send_step, MPI_DOUBLE,
ROOT, MPI_COMM_WORLD);

if((uint32_t)send_step > xSize) {
for(size_t y = 0; y < send_step / xSize - 1; ++y) {
for(size_t x = 3; x < xSize; ++x) {
my_calc[y*xSize + x] = sin(0.00001*my_calc[(y + 1) * xSize + (x - 3)]);
}
}
}

MPI_Gatherv(
my_calc, recv_step, MPI_DOUBLE,
arr, recv_range, recv_dist, MPI_DOUBLE,
ROOT, MPI_COMM_WORLD);


free(my_calc);
if(rank == ROOT) {
free(recv_dist);
free(recv_range);
free(send_dist);
free(send_range);
}
}
}

int main(int argc, char** argv)
Expand Down
Loading