Skip to content

Commit 3a01630

Browse files
committed
Skip powder grain ID init if no cells in powder layer are assigned
1 parent 992c0e8 commit 3a01630

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

src/CAcelldata.hpp

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -578,53 +578,57 @@ struct CellData {
578578
int powder_layer_cells = grid.nx * grid.ny * powder_layer_height;
579579
int powder_layer_assigned_cells =
580580
Kokkos::round(static_cast<double>(powder_layer_cells) * _inputs.powder_active_fraction);
581-
// Associate powder grain IDs with CA cells in the powder layer
582581
MPI_Barrier(MPI_COMM_WORLD);
583582
if (id == 0)
584583
std::cout << "Initializing powder layer for Z = " << powder_bottom_z << " through " << powder_top_z - 1
585584
<< " (" << grid.nx * grid.ny * powder_layer_height << " cells): powder layer has "
586585
<< powder_layer_assigned_cells << " cells assigned new grain ID values" << std::endl;
587-
std::vector<int> powder_grain_ids(powder_layer_cells, 0);
588-
for (int n = 0; n < powder_layer_assigned_cells; n++) {
589-
powder_grain_ids[n] = n + next_layer_first_epitaxial_grain_id; // assigned a nonzero GrainID
590-
}
591-
std::shuffle(powder_grain_ids.begin(), powder_grain_ids.end(), gen);
592-
// Wrap powder layer GrainIDs into an unmanaged view, then copy to the device
593-
view_type_int_unmanaged powder_grain_ids_host(powder_grain_ids.data(), powder_layer_cells);
594-
auto powder_grain_ids_device = Kokkos::create_mirror_view_and_copy(memory_space(), powder_grain_ids_host);
595586

596-
int powder_start = grid.nx * grid.ny * powder_bottom_z;
597-
if (id == 0)
598-
std::cout << "Powder layer grain ID values range from " << next_layer_first_epitaxial_grain_id
599-
<< " through " << next_layer_first_epitaxial_grain_id + powder_layer_assigned_cells - 1
600-
<< std::endl;
587+
// Associate powder grain IDs with CA cells in the powder layer, if nonzero number of powder cells
588+
if (powder_layer_assigned_cells > 0) {
601589

602-
// Iterate over all cells in the powder layer, on each rank loading the powder grain ID data for local cell
603-
// locations
604-
auto grain_id_all_layers_local = grain_id_all_layers;
605-
auto powder_policy =
606-
Kokkos::MDRangePolicy<execution_space, Kokkos::Rank<3, Kokkos::Iterate::Right, Kokkos::Iterate::Right>>(
607-
{powder_bottom_z, 0, 0}, {powder_top_z, grid.nx, grid.ny});
608-
Kokkos::parallel_for(
609-
"PowderGrainInit", powder_policy,
610-
KOKKOS_LAMBDA(const int coord_z_all_layers, const int coord_x, const int coord_y_global) {
611-
// Is this powder coordinate in X and Y in bounds for this rank? Is the grain id of this site
612-
// unassigned (wasn't captured during solidification of the previous layer)?
613-
if ((coord_y_global >= grid.y_offset) && (coord_y_global < grid.y_offset + grid.ny_local)) {
614-
int coord_y = coord_y_global - grid.y_offset;
615-
int index_all_layers = grid.get1DIndex(coord_x, coord_y, coord_z_all_layers);
616-
if (grain_id_all_layers_local(index_all_layers) == 0) {
617-
int index_all_ranks_all_layers =
618-
coord_z_all_layers * grid.nx * grid.ny + coord_x * grid.ny + coord_y_global;
619-
grain_id_all_layers_local(index_all_layers) =
620-
powder_grain_ids_device(index_all_ranks_all_layers - powder_start);
590+
std::vector<int> powder_grain_ids(powder_layer_cells, 0);
591+
for (int n = 0; n < powder_layer_assigned_cells; n++) {
592+
powder_grain_ids[n] = n + next_layer_first_epitaxial_grain_id; // assigned a nonzero GrainID
593+
}
594+
std::shuffle(powder_grain_ids.begin(), powder_grain_ids.end(), gen);
595+
// Wrap powder layer GrainIDs into an unmanaged view, then copy to the device
596+
view_type_int_unmanaged powder_grain_ids_host(powder_grain_ids.data(), powder_layer_cells);
597+
auto powder_grain_ids_device = Kokkos::create_mirror_view_and_copy(memory_space(), powder_grain_ids_host);
598+
599+
int powder_start = grid.nx * grid.ny * powder_bottom_z;
600+
if (id == 0)
601+
std::cout << "Powder layer grain ID values range from " << next_layer_first_epitaxial_grain_id
602+
<< " through " << next_layer_first_epitaxial_grain_id + powder_layer_assigned_cells - 1
603+
<< std::endl;
604+
605+
// Iterate over all cells in the powder layer, on each rank loading the powder grain ID data for local cell
606+
// locations
607+
auto grain_id_all_layers_local = grain_id_all_layers;
608+
auto powder_policy =
609+
Kokkos::MDRangePolicy<execution_space, Kokkos::Rank<3, Kokkos::Iterate::Right, Kokkos::Iterate::Right>>(
610+
{powder_bottom_z, 0, 0}, {powder_top_z, grid.nx, grid.ny});
611+
Kokkos::parallel_for(
612+
"PowderGrainInit", powder_policy,
613+
KOKKOS_LAMBDA(const int coord_z_all_layers, const int coord_x, const int coord_y_global) {
614+
// Is this powder coordinate in X and Y in bounds for this rank? Is the grain id of this site
615+
// unassigned (wasn't captured during solidification of the previous layer)?
616+
if ((coord_y_global >= grid.y_offset) && (coord_y_global < grid.y_offset + grid.ny_local)) {
617+
int coord_y = coord_y_global - grid.y_offset;
618+
int index_all_layers = grid.get1DIndex(coord_x, coord_y, coord_z_all_layers);
619+
if (grain_id_all_layers_local(index_all_layers) == 0) {
620+
int index_all_ranks_all_layers =
621+
coord_z_all_layers * grid.nx * grid.ny + coord_x * grid.ny + coord_y_global;
622+
grain_id_all_layers_local(index_all_layers) =
623+
powder_grain_ids_device(index_all_ranks_all_layers - powder_start);
624+
}
621625
}
622-
}
623-
});
624-
Kokkos::fence();
626+
});
627+
Kokkos::fence();
625628

626-
// Update next_layer_first_epitaxial_grain_id for next layer
627-
next_layer_first_epitaxial_grain_id += powder_layer_assigned_cells;
629+
// Update next_layer_first_epitaxial_grain_id for next layer
630+
next_layer_first_epitaxial_grain_id += powder_layer_assigned_cells;
631+
}
628632
MPI_Barrier(MPI_COMM_WORLD);
629633
if (id == 0)
630634
std::cout << "Initialized powder grain structure for layer " << layernumber << std::endl;

0 commit comments

Comments
 (0)