Skip to content

Commit

Permalink
the reverse walk is mostly done, there is a small bug
Browse files Browse the repository at this point in the history
  • Loading branch information
dwa012 committed Mar 18, 2011
1 parent f5e5534 commit 806ac16
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 54 deletions.
6 changes: 0 additions & 6 deletions adjacents.c
Expand Up @@ -36,11 +36,7 @@ long double delta_energy(int to_remove_index, particle new_particle)

//get old cube energies
number_of_old_cubes = adjacents(old_cube_indices, old_particle.myCube);
for(i = 0; i < number_of_old_cubes; i++)
old_cube_energies[i] = cubes[old_cube_indices[i]].energy;

//get initial energy
energy_at_old_initial = calculate_cube_list_energy(cubes,old_cube_indices, number_of_old_cubes);

//change the energies of the old cubes
neighbor_particles_of_random = get_particles_from_cubes(old_cube_indices, number_of_old_cubes, cubes, &number_of_neighbors);
Expand All @@ -54,7 +50,6 @@ long double delta_energy(int to_remove_index, particle new_particle)
}
else{
long double energy = calculate_pair_energy(distance(old_particle, neighbor_particles_of_random[i]));
cubes[old_particle.myCube].energy -= energy;
energy_change_at_old += energy;
}
}
Expand Down Expand Up @@ -87,7 +82,6 @@ long double delta_energy(int to_remove_index, particle new_particle)
}
else{
long double energy = calculate_pair_energy(distance(new_particle, neighbor_particles_at_new[i]));
cubes[new_particle.myCube].energy -= energy;
energy_change_at_new += energy;
}
}
Expand Down
11 changes: 6 additions & 5 deletions parallel.c
Expand Up @@ -132,13 +132,14 @@ void perturb(double *current_perturbing, long double * result){
double x,y,z;

int particle_index = (int)current_perturbing[0];
particle_array[particle_index].x = x = current_perturbing[1];
particle_array[particle_index].y = y = current_perturbing[2];
particle_array[particle_index].z = z = current_perturbing[3];
particle_array[particle_index].myCube = belongs_to_cube((int) x/10, (int) y/10, (int) z/10);
particle new_particle;
new_particle.x = x = current_perturbing[1];
new_particle.y = y = current_perturbing[2];
new_particle.z = z = current_perturbing[3];
new_particle.myCube = belongs_to_cube((int) x/10, (int) y/10, (int) z/10);


long double delta = delta_energy(particle_index,particle_array[particle_index]);
long double delta = delta_energy(particle_index,new_particle);

result[1] = delta;

Expand Down
136 changes: 93 additions & 43 deletions pib.c
Expand Up @@ -126,70 +126,120 @@ int main(int argc, char** argv){
calculate_cube_energy(cubes, i);
}
///////////////////////////////////////////////////
int max_buff_size = 4;
int childrens_max_buff_size = 4;
if (id != 1 && id != 0){
max_buff_size = floor(log2(id))*4; // There's 4 double elements
childrens_max_buff_size = max_buff_size+4;
}
double current_peturbing[4];
double previous_state[max_buff_size];
double accepted_state[childrens_max_buff_size];
double rejected_state[childrens_max_buff_size];

setup_tree(max_buff_size, childrens_max_buff_size, previous_state, current_peturbing, accepted_state, rejected_state);

if (id != 0){
//MPI_Send(&temp, 1, MPI_INT, parent, id, MPI_COMM_WORLD);

printf("Updateing state in process: %d\n",id);
update_state(cubes, particle_array, previous_state, current_peturbing, max_buff_size);

long double result[2];
int finished = 1;
int max_buff_size = 4;
int childrens_max_buff_size = 4;
if (id != 1 && id != 0){
max_buff_size = floor(log2(id))*4; // There's 4 double elements
childrens_max_buff_size = max_buff_size+4;
}
double current_peturbing[4];
double previous_state[max_buff_size];
double accepted_state[childrens_max_buff_size];
double rejected_state[childrens_max_buff_size];

setup_tree(max_buff_size, childrens_max_buff_size, previous_state, current_peturbing, accepted_state, rejected_state);

if (id != 0){
//MPI_Send(&temp, 1, MPI_INT, parent, id, MPI_COMM_WORLD);

printf("Perturbing in process: %d\n",id);
//printf("Updateing state in process: %d\n",id);

update_state(cubes, particle_array, previous_state, current_peturbing, max_buff_size);

int children_result_length = 6 * (ceil(log2(nprocs)) - ceil(log2(id)));

int result_length = children_result_length + 6;

long double result[result_length];
long double finished_left[children_result_length];
long double finished_right[children_result_length];


//printf("Perturbing in process: %d\n",id);

perturb(current_peturbing, result);

printf("Perturbing complete in process: %d\n",id);
//printf("Delta energy: %Lf\n",result[1]);

for(i = 2; i < 6; i++)
result[i] = current_peturbing[i-2];

//printf("Perturbing complete in process: %d\n",id);

int left_child = 2*id;
int right_child = (2*id)+1;
int right_child = (2*id)+1;
int parent = id/2;

// busy wait for children to finish
if (left_child < nprocs){

// busy wait for children to finish
if (left_child < nprocs){

printf("Wating for left child: %d\n",left_child);
//printf("Wating for left child: %d\n",left_child);

MPI_Recv(&finished, 1, MPI_INT, left_child, left_child, MPI_COMM_WORLD, &status);
MPI_Recv(&finished_left, children_result_length, MPI_LONG_DOUBLE, left_child, left_child, MPI_COMM_WORLD, &status);

}
if (right_child < nprocs){
if (right_child < nprocs){

printf("Waiting for right child: %d\n",right_child);
//printf("Waiting for right child: %d\n",right_child);

MPI_Recv(&finished_right, children_result_length, MPI_LONG_DOUBLE, right_child, right_child, MPI_COMM_WORLD, &status);
}

//if i fail pick the left childs data
if(result[0] == 0 && (left_child < nprocs))
{
for(i = 0; i < children_result_length; i++)
result[i+6] = finished_left[i];

}

MPI_Recv(&finished, 1, MPI_INT, right_child, right_child, MPI_COMM_WORLD, &status);
//if i suceed then pick the right child data
if(result[0] == 1 && (right_child < nprocs))
{
for(i = 0; i < children_result_length; i++)
result[i+6] = finished_right[i];
}
// Tell parent that the child is done
int temp = 1;
// Tell parent that the child is done
int temp = 1;

printf("Sending to parent %d from process: %d\n",parent,id);
MPI_Send(&temp, 1, MPI_INT, parent, id, MPI_COMM_WORLD);
printf("Sending to process %d from process: %d\n",0,id);
//if not the root node then send my data to my parent
if(id != 1){
//printf("Sending to parent %d from process: %d\n",parent,id);
MPI_Send(&result, result_length, MPI_LONG_DOUBLE, parent, id, MPI_COMM_WORLD);
}

//if process 1 then print the data received
if(id == 1)
{
for(i = 0; i < result_length; i++)
{
printf("Data from level %f\n",floor(log2(nprocs)));
printf("\tresult: %Lf\n", result[i]);
printf("\tdelta energy: %Lf\n", result[i+1]);
printf("\tparticle index removed: %Lf\n", result[i+2]);
printf("\tnew particle x: : %Lf\n", result[i+3]);
printf("\tnew particle y: : %Lf\n", result[i+4]);
printf("\tnew particle z: : %Lf\n", result[i+5]);
i += 5;
}
}

//printf("Sending to process %d from process: %d\n",0,id);
MPI_Send(&temp, 1, MPI_INT, 0, id, MPI_COMM_WORLD);

}
else{
int temp;
for (i=1; i<nprocs;i++){
MPI_Recv(&temp, 1, MPI_INT, i, i, MPI_COMM_WORLD, &status);
printf("Process %d done\n",i);
else{
int temp;
for (i=2; i<nprocs;i++){
MPI_Recv(&temp, 1, MPI_INT, i, i, MPI_COMM_WORLD, &status);
//printf("Process %d done\n",i);
}
}
}

/*if (id==0){
//start the simualtion
printf("Starting the simulation with %d trials\n", NUMBER_OF_TRIALS);
Expand Down

0 comments on commit 806ac16

Please sign in to comment.