Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] SENPAI Parallel computing #9 -- MPI implementation #19

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
8 changes: 2 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@

.PHONY: all clean fclean re

$(CC) := gcc

SRC_DIR := ./sources
HDR_DIR := ./headers

SRCS := $(shell find $(SRC_DIR) -name "*.c")
OBJS := $(shell find $(SRC_DIR) -name "*.o")
NAME := senpai

WARNINGS := -Wall -Wextra -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline -Wno-long-long -Wuninitialized -Wstrict-prototypes
OPTIONS := -std=c99 -O2 -g -U__STRICT_ANSI__
LIBS := -lm
CFLAGS := -I$(HDR_DIR) $(WARNINGS) $(OPTIONS) $(LIBS) -o $(NAME).bin
CFLAGS := -I$(HDR_DIR) $(WARNINGS) $(OPTIONS) $(LIBS) -o $(NAME)mpi.bin

all:
$(CC) $(SRCS) $(CFLAGS)
mpicc $(SRCS) $(CFLAGS)

clean:
$(RM) $(OBJS)
Expand Down
128 changes: 126 additions & 2 deletions sources/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,38 @@
#include "text.h"
#include "util.h"

#include <mpi.h>

#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
#define MODE_NUMERICAL 1


int main(int argc, char **argv)
{
// Initializare MPI
MPI_Init(&argc, &argv);

args_t args; /* Program arguments (from argv) */
universe_t universe; /* The universe itself (wow) */

/* We don't need a perfectly random generator */
// srand((unsigned int)time(NULL));


// Get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);

// Get the rank of the process
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);



/* That's the welcome message */
puts(TEXT_START);
if(world_rank == 0) {
puts(TEXT_START);
}

/* Parse the arguments */
args_init(&args);
Expand All @@ -44,5 +66,107 @@ int main(int argc, char **argv)
return (retstri(EXIT_FAILURE, TEXT_MAIN_FAILURE, __FILE__, __LINE__));

/* Let's roll */
return (universe_simulate(&universe, &args));

// universe_simulate(&universe, &args);
uint64_t frame_nb; /* Used for frameskipping */

/* Tell the user the simulation is starting */
puts(TEXT_SIMSTART);

/* While we haven't reached the target time, we iterate the universe */
frame_nb = 0;


// Impartim numarul la cate procese avem
int ratio = universe.atom_nb/world_size;
int start = world_rank * ratio;
int end = (world_rank + 1) * ratio;

// Daca e ultimul proces, atunci merge pana la final
if (world_rank == world_size - 1)
end = universe.atom_nb;

while (universe.time < args.max_time)
{
if(world_rank == 0) {
/* Print the state to the .xyz file, if required */
if (!frame_nb)
{
universe_printstate(&universe);
frame_nb = (args.frameskip);
}
else
--frame_nb;
}
/* Iterate */
//
// universe_iterate(universe, args)
size_t i; /* Iterator */

/* We update the position vector first, as part of the Velocity-Verley integration */
for (i=0; i<(universe.atom_nb); ++i)
if (atom_update_pos(&universe, &args, i) == NULL)
return (retstr(NULL, TEXT_UNIVERSE_ITERATE_FAILURE, __FILE__, __LINE__));

/* We enforce the periodic boundary conditions */

for (i=0; i<(universe.atom_nb); ++i)
if (atom_enforce_pbc(&universe, i) == NULL)
return (retstr(NULL, TEXT_UNIVERSE_ITERATE_FAILURE, __FILE__, __LINE__));

/* Update the force vectors */
/* By numerically differentiating the potential energy... */
if (args.numerical == MODE_NUMERICAL)
{
for (i=0; i<(universe.atom_nb); ++i)
if (atom_update_frc_numerical(&universe, i) == NULL)
return (retstr(NULL, TEXT_UNIVERSE_ITERATE_FAILURE, __FILE__, __LINE__));
}

/* Or analytically solving for force */
else
{

for (i=start; i<end; ++i)
if (atom_update_frc_analytical(&universe, i) == NULL)
return (retstr(NULL, TEXT_UNIVERSE_ITERATE_FAILURE, __FILE__, __LINE__));
}

/* Update the acceleration vectors */
for (i=0; i<(universe.atom_nb); ++i)
if (atom_update_acc(&universe, i) == NULL)
return (retstr(NULL, TEXT_UNIVERSE_ITERATE_FAILURE, __FILE__, __LINE__));

/* Update the speed vectors */
for (i=0; i<(universe.atom_nb); ++i)
if (atom_update_vel(&universe, &args, i) == NULL)
return (retstr(NULL, TEXT_UNIVERSE_ITERATE_FAILURE, __FILE__, __LINE__));

// return (universe);

//TODO broadcast la universe.atom->pos - pentru toti atomii modificati de proces

double buf[3];
for (i=0; i<(universe.atom_nb); ++i){
buf[0] = universe.atom[i].pos.x;
buf[1] = universe.atom[i].pos.y;
buf[2] = universe.atom[i].pos.z;
MPI_Bcast(&buf, 3, MPI_DOUBLE, MIN(i/ratio, world_size - 1), MPI_COMM_WORLD);
universe.atom[i].pos.x = buf[0];
universe.atom[i].pos.y = buf[1];
universe.atom[i].pos.z = buf[2];
}
universe.time += args.timestep;
++(universe.iterations);
}

/* End of simulation */
puts(TEXT_SIMEND);
universe_clean(&universe);

// Finalizare MPI
MPI_Finalize();

return 0;

}
81 changes: 0 additions & 81 deletions sources/universe.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,87 +335,6 @@ void universe_clean(universe_t *universe)
free(universe->atom);
}

/* Main loop of the simulator. Iterates until the target time is reached */
int universe_simulate(universe_t *universe, const args_t *args)
{
uint64_t frame_nb; /* Used for frameskipping */

/* Tell the user the simulation is starting */
puts(TEXT_SIMSTART);

/* While we haven't reached the target time, we iterate the universe */
frame_nb = 0;
while (universe->time < args->max_time)
{
/* Print the state to the .xyz file, if required */
if (!frame_nb)
{
if (universe_printstate(universe) == NULL)
return (retstri(EXIT_FAILURE, TEXT_UNIVERSE_SIMULATE_FAILURE, __FILE__, __LINE__));
frame_nb = (args->frameskip);
}
else
--frame_nb;

/* Iterate */
if (universe_iterate(universe, args) == NULL)
return (retstri(EXIT_FAILURE, TEXT_UNIVERSE_SIMULATE_FAILURE, __FILE__, __LINE__));

universe->time += args->timestep;
++(universe->iterations);
}

/* End of simulation */
puts(TEXT_SIMEND);
universe_clean(universe);

return (EXIT_SUCCESS);
}

universe_t *universe_iterate(universe_t *universe, const args_t *args)
{
size_t i; /* Iterator */

/* We update the position vector first, as part of the Velocity-Verley integration */
for (i=0; i<(universe->atom_nb); ++i)
if (atom_update_pos(universe, args, i) == NULL)
return (retstr(NULL, TEXT_UNIVERSE_ITERATE_FAILURE, __FILE__, __LINE__));

/* We enforce the periodic boundary conditions */

for (i=0; i<(universe->atom_nb); ++i)
if (atom_enforce_pbc(universe, i) == NULL)
return (retstr(NULL, TEXT_UNIVERSE_ITERATE_FAILURE, __FILE__, __LINE__));

/* Update the force vectors */
/* By numerically differentiating the potential energy... */
if (args->numerical == MODE_NUMERICAL)
{
for (i=0; i<(universe->atom_nb); ++i)
if (atom_update_frc_numerical(universe, i) == NULL)
return (retstr(NULL, TEXT_UNIVERSE_ITERATE_FAILURE, __FILE__, __LINE__));
}

/* Or analytically solving for force */
else
{
for (i=0; i<(universe->atom_nb); ++i)
if (atom_update_frc_analytical(universe, i) == NULL)
return (retstr(NULL, TEXT_UNIVERSE_ITERATE_FAILURE, __FILE__, __LINE__));
}

/* Update the acceleration vectors */
for (i=0; i<(universe->atom_nb); ++i)
if (atom_update_acc(universe, i) == NULL)
return (retstr(NULL, TEXT_UNIVERSE_ITERATE_FAILURE, __FILE__, __LINE__));

/* Update the speed vectors */
for (i=0; i<(universe->atom_nb); ++i)
if (atom_update_vel(universe, args, i) == NULL)
return (retstr(NULL, TEXT_UNIVERSE_ITERATE_FAILURE, __FILE__, __LINE__));

return (universe);
}

/* Print the system's state to the .xyz file */
universe_t *universe_printstate(universe_t *universe)
Expand Down