Skip to content

Commit

Permalink
Update all arrays in compute sources to be a function of sources.size…
Browse files Browse the repository at this point in the history
…() and not nspec.
  • Loading branch information
lsawade committed Jan 24, 2025
1 parent 764ac3c commit fa6cef6
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 101 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ add_library(

target_link_libraries(
compute
enumerations
quadrature
mesh
# material_class
Expand Down
22 changes: 13 additions & 9 deletions include/compute/sources/source_medium.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ struct source_medium {

template <typename IteratorIndexType, typename PointSourceType>
KOKKOS_INLINE_FUNCTION void
store_on_device(const int timestep, const IteratorIndexType &iterator_index,
store_on_device(const int timestep, const IteratorIndexType iterator_index,
const PointSourceType &point_source) const {
/* For the source it is important to remember that we are using the
* mapped index to access the element and source indices
Expand All @@ -135,24 +135,28 @@ struct source_medium {
}
}

template <typename IndexType, typename PointSourceType>
void load_on_host(const int timestep, const IndexType index,
template <typename IteratorIndexType, typename PointSourceType>
void load_on_host(const int timestep, const IteratorIndexType iterator_index,
PointSourceType &point_source) const {
const auto index = iterator_index.index;
const auto isource = iterator_index.imap;
for (int component = 0; component < components; component++) {
point_source.stf(component) =
h_source_time_function(timestep, index.ispec, component);
h_source_time_function(timestep, isource, component);
point_source.lagrange_interpolant(component) =
h_source_array(index.ispec, component, index.iz, index.ix);
h_source_array(isource, component, index.iz, index.ix);
}
}

template <typename IndexType, typename PointSourceType>
void store_on_host(const int timestep, const IndexType index,
template <typename IteratorIndexType, typename PointSourceType>
void store_on_host(const int timestep, const IteratorIndexType iterator_index,
const PointSourceType &point_source) const {
const auto index = iterator_index.index;
const auto isource = iterator_index.imap;
for (int component = 0; component < components; component++) {
h_source_time_function(timestep, index.ispec, component) =
h_source_time_function(timestep, isource, component) =
point_source.stf(component);
h_source_array(index.ispec, component, index.iz, index.ix) =
h_source_array(isource, component, index.iz, index.ix) =
point_source.lagrange_interpolant(component);
}
}
Expand Down
138 changes: 84 additions & 54 deletions include/compute/sources/sources.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "compute/properties/properties.hpp"
#include "enumerations/dimension.hpp"
#include "enumerations/material_definitions.hpp"
#include "enumerations/medium.hpp"
#include "enumerations/wavefield.hpp"
#include "kokkos_abstractions.h"
#include "point/sources.hpp"
Expand Down Expand Up @@ -214,24 +215,25 @@ struct sources {

#undef SOURCE_INDICES_VARIABLES_NAME

template <typename IndexType, typename PointSourceType>
template <typename IteratorIndexType, typename PointSourceType>
friend KOKKOS_INLINE_FUNCTION void
load_on_device(const IndexType index,
load_on_device(const IteratorIndexType iterator_index,
const specfem::compute::sources &sources,
PointSourceType &point_source);

template <typename IndexType, typename PointSourceType>
friend void load_on_host(const IndexType index,
template <typename IteratorIndexType, typename PointSourceType>
friend void load_on_host(const IteratorIndexType iterator_index,
const specfem::compute::sources &sources,
PointSourceType &point_source);

template <typename IndexType, typename PointSourceType>
template <typename IteratorIndexType, typename PointSourceType>
friend KOKKOS_INLINE_FUNCTION void
store_on_device(const IndexType index, const PointSourceType &point_source,
store_on_device(const IteratorIndexType iterator_index,
const PointSourceType &point_source,
const specfem::compute::sources &sources);

template <typename IndexType, typename PointSourceType>
friend void store_on_host(const IndexType index,
template <typename IteratorIndexType, typename PointSourceType>
friend void store_on_host(const IteratorIndexType iterator_index,
const PointSourceType &point_source,
const specfem::compute::sources &sources);
}; // namespace compute
Expand Down Expand Up @@ -278,22 +280,28 @@ load_on_device(const IteratorIndexType iterator_index,
"PointSourceType must be an acoustic or elastic point source");

#ifndef NDEBUG

const int isource = iterator_index.imap;

// Checks if the spectral element index is out of bounds

if (index.ispec >= sources.nspec) {
Kokkos::abort("Invalid spectral element index detected in source");
}

// Checks if the spectral element has a source associated with it
if (sources.source_domain_index_mapping(index.ispec) < 0) {
Kokkos::abort("Invalid spectral element index detected in source");
}

if (sources.medium_types(index.ispec) != PointSourceType::medium_tag) {
if (sources.medium_types(isource) != PointSourceType::medium_tag) {
std::cout << "compute::sources::load_on_device" << std::endl;
std::cout << " Medium type: "
<< specfem::element::to_string(sources.medium_types(isource))
<< std::endl;
std::cout << " PointSourceType medium tag: "
<< specfem::element::to_string(PointSourceType::medium_tag)
<< std::endl;
std::cout << " File: " << __FILE__ << std::endl;
std::cout << " Line: " << __LINE__ << std::endl;
Kokkos::abort("Invalid medium detected in source");
}

if (sources.wavefield_types(index.ispec) != PointSourceType::wavefield_tag) {
if (sources.wavefield_types(isource) != PointSourceType::wavefield_tag) {
Kokkos::abort("Invalid wavefield type detected in source");
}
#endif
Expand Down Expand Up @@ -331,12 +339,15 @@ load_on_device(const IteratorIndexType iterator_index,
* @param sources Source information for the domain
* @param point_source Point source object to load source information into
*/
template <typename IndexType, typename PointSourceType>
void load_on_host(const IndexType index,
template <typename IteratorIndexType, typename PointSourceType>
void load_on_host(const IteratorIndexType iterator_index,
const specfem::compute::sources &sources,
PointSourceType &point_source) {

static_assert(IndexType::using_simd == false,
// Get the mapping from the iterator index
const auto index = iterator_index.index;

static_assert(index.using_simd == false,
"IndexType must not use SIMD when loading sources");

static_assert(
Expand All @@ -346,7 +357,7 @@ void load_on_host(const IndexType index,
static_assert(PointSourceType::dimension == specfem::dimension::type::dim2,
"PointSourceType must be a 2D point source type");

static_assert(IndexType::dimension == specfem::dimension::type::dim2,
static_assert(index.dimension == specfem::dimension::type::dim2,
"IndexType must be a 2D index type");

static_assert(
Expand All @@ -356,36 +367,38 @@ void load_on_host(const IndexType index,
"PointSourceType must be an acoustic or elastic point source");

#ifndef NDEBUG
// Checks if the spectral element index is out of bounds
if (index.ispec >= sources.nspec) {
Kokkos::abort("Invalid spectral element index detected in source");
}
const int isource = iterator_index.imap;

// Checks if the spectral element has a source associated with it
if (sources.h_source_domain_index_mapping(index.ispec) < 0) {
// Checks if the spectral element index is out of bounds
if ((index.ispec < 0) || (sources.nspec <= index.ispec)) {
Kokkos::abort("Invalid spectral element index detected in source");
}

if (sources.h_medium_types(index.ispec) != PointSourceType::medium_tag) {
if (sources.h_medium_types(isource) != PointSourceType::medium_tag) {
std::cout << "compute::sources::load_on_host" << std::endl;
std::cout << " Medium type: "
<< specfem::element::to_string(sources.medium_types(isource))
<< std::endl;
std::cout << " PointSourceType medium tag: "
<< specfem::element::to_string(PointSourceType::medium_tag)
<< std::endl;
std::cout << " File: " << __FILE__ << std::endl;
std::cout << " Line: " << __LINE__ << std::endl;
Kokkos::abort("Invalid medium detected in source");
}

if (sources.h_wavefield_types(index.ispec) !=
PointSourceType::wavefield_tag) {
if (sources.h_wavefield_types(isource) != PointSourceType::wavefield_tag) {
Kokkos::abort("Invalid wavefield type detected in source");
}
#endif

IndexType lcoord = index;
lcoord.ispec = sources.h_source_domain_index_mapping(index.ispec);

#define SOURCE_MEDIUM_LOAD_ON_HOST(DIMENSION_TAG, MEDIUM_TAG) \
if constexpr (GET_TAG(DIMENSION_TAG) == specfem::dimension::type::dim2) { \
if constexpr (GET_TAG(MEDIUM_TAG) == PointSourceType::medium_tag) { \
sources \
.CREATE_VARIABLE_NAME(source, GET_NAME(DIMENSION_TAG), \
GET_NAME(MEDIUM_TAG)) \
.load_on_host(sources.timestep, lcoord, point_source); \
.load_on_host(sources.timestep, iterator_index, point_source); \
} \
}

Expand Down Expand Up @@ -440,19 +453,28 @@ store_on_device(const IteratorIndexType iterator_index,
"PointSourceType must be an acoustic or elastic point source");

#ifndef NDEBUG
if (index.ispec >= sources.nspec) {
Kokkos::abort("Invalid spectral element index detected in source");
}
const int isource = iterator_index.imap;

if (sources.source_domain_index_mapping(index.ispec) < 0) {
if ((index.ispec < 0) || (sources.nspec <= index.ispec)) {
Kokkos::abort("Invalid spectral element index detected in source");
}

if (sources.medium_types(index.ispec) != PointSourceType::medium_tag) {
if (sources.medium_types(isource) != PointSourceType::medium_tag) {
std::cout << "compute::sources::store_on_device" << std::endl;
std::cout << " isource: " << isource << std::endl;
std::cout << " ispec: " << index.ispec << std::endl;
std::cout << " Medium type: "
<< specfem::element::to_string(sources.medium_types(isource))
<< std::endl;
std::cout << " PointSourceType medium tag: "
<< specfem::element::to_string(PointSourceType::medium_tag)
<< std::endl;
std::cout << " File: " << __FILE__ << std::endl;
std::cout << " Line: " << __LINE__ << std::endl;
Kokkos::abort("Invalid medium detected in source");
}

if (sources.wavefield_types(index.ispec) != PointSourceType::wavefield_tag) {
if (sources.wavefield_types(isource) != PointSourceType::wavefield_tag) {
Kokkos::abort("Invalid wavefield type detected in source");
}
#endif
Expand Down Expand Up @@ -490,11 +512,14 @@ store_on_device(const IteratorIndexType iterator_index,
* @param point_source Point source object to load source information into
* @param sources Source information for the domain
*/
template <typename IndexType, typename PointSourceType>
void store_on_host(const IndexType index, const PointSourceType &point_source,
template <typename IteratorIndexType, typename PointSourceType>
void store_on_host(const IteratorIndexType iterator_index,
const PointSourceType &point_source,
const specfem::compute::sources &sources) {

static_assert(IndexType::using_simd == false,
const auto index = iterator_index.index;

static_assert(index.using_simd == false,
"IndexType must not use SIMD when storing sources");

static_assert(
Expand All @@ -504,7 +529,7 @@ void store_on_host(const IndexType index, const PointSourceType &point_source,
static_assert(PointSourceType::dimension == specfem::dimension::type::dim2,
"PointSourceType must be a 2D point source type");

static_assert(IndexType::dimension == specfem::dimension::type::dim2,
static_assert(index.dimension == specfem::dimension::type::dim2,
"IndexType must be a 2D index type");

static_assert(
Expand All @@ -514,34 +539,39 @@ void store_on_host(const IndexType index, const PointSourceType &point_source,
"PointSourceType must be an acoustic or elastic point source");

#ifndef NDEBUG
if (index.ispec >= sources.nspec) {
Kokkos::abort("Invalid spectral element index detected in source");
}
const int isource = iterator_index.imap;

if (sources.h_source_domain_index_mapping(index.ispec) < 0) {
if ((index.ispec < 0) || (sources.nspec <= index.ispec)) {
Kokkos::abort("Invalid spectral element index detected in source");
}

if (sources.h_medium_types(index.ispec) != PointSourceType::medium_tag) {
if (sources.h_medium_types(isource) != PointSourceType::medium_tag) {
std::cout << "compute::sources::store_on_host" << std::endl;
std::cout << " isource: " << isource << std::endl;
std::cout << " ispec: " << index.ispec << std::endl;
std::cout << " Medium type: "
<< specfem::element::to_string(sources.h_medium_types(isource))
<< std::endl;
std::cout << " PointSourceType medium tag: "
<< specfem::element::to_string(PointSourceType::medium_tag)
<< std::endl;
std::cout << " File: " << __FILE__ << std::endl;
std::cout << " Line: " << __LINE__ << std::endl;
Kokkos::abort("Invalid medium detected in source");
}

if (sources.h_wavefield_types(index.ispec) !=
PointSourceType::wavefield_tag) {
if (sources.h_wavefield_types(isource) != PointSourceType::wavefield_tag) {
Kokkos::abort("Invalid wavefield type detected in source");
}
#endif

IndexType lcoord = index;
lcoord.ispec = sources.h_source_domain_index_mapping(index.ispec);

#define SOURCE_MEDIUM_STORE_ON_HOST(DIMENSION_TAG, MEDIUM_TAG) \
if constexpr (GET_TAG(DIMENSION_TAG) == specfem::dimension::type::dim2) { \
if constexpr (GET_TAG(MEDIUM_TAG) == PointSourceType::medium_tag) { \
sources \
.CREATE_VARIABLE_NAME(source, GET_NAME(DIMENSION_TAG), \
GET_NAME(MEDIUM_TAG)) \
.store_on_host(sources.timestep, lcoord, point_source); \
.store_on_host(sources.timestep, iterator_index, point_source); \
} \
}

Expand Down
Loading

0 comments on commit fa6cef6

Please sign in to comment.