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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sarah - added in set_values function #94

Merged
merged 3 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ if (KOKKOS)
add_definitions(-DHAVE_THREADS=1)
endif()

add_executable(testsetval test_set_values.cpp)
target_link_libraries(testsetval ${LINKING_LIBRARIES})

add_executable(mtestkokkos main_kokkos.cpp)
target_link_libraries(mtestkokkos ${LINKING_LIBRARIES})

Expand Down
83 changes: 83 additions & 0 deletions examples/test_set_values.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**********************************************************************************************
� 2020. Triad National Security, LLC. All rights reserved.
This program was produced under U.S. Government contract 89233218CNA000001 for Los Alamos
National Laboratory (LANL), which is operated by Triad National Security, LLC for the U.S.
Department of Energy/National Nuclear Security Administration. All rights in the program are
reserved by Triad National Security, LLC, and the U.S. Department of Energy/National Nuclear
Security Administration. The Government is granted for itself and others acting on its behalf a
nonexclusive, paid-up, irrevocable worldwide license in this material to reproduce, prepare
derivative works, distribute copies to the public, perform publicly and display publicly, and
to permit others to do so.
This program is open source under the BSD-3 License.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used
to endorse or promote products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**********************************************************************************************/
#include <stdio.h>
#include "matar.h"

using namespace mtr; // matar namespace

int main(int argc, char* argv[])
{
// Test RaggedRightArrayKokkos
Kokkos::initialize(argc, argv);
{
// Create a CArrayKokkos for strides
CArrayKokkos<size_t, Kokkos::LayoutRight, Kokkos::DefaultExecutionSpace, Kokkos::MemoryTraits<0>> strides(3);

// Set up strides (this is just an example, adjust as needed)
Kokkos::parallel_for("SetStrides", 1, KOKKOS_LAMBDA(const int&) {
strides(0) = 3; // dim0
strides(1) = 9; // total elements in dim1
strides(2) = 18; // total elements overall
});

// Create RaggedRightArrayKokkos using the new constructor
RaggedRightArrayKokkos<double, Kokkos::LayoutRight, Kokkos::DefaultExecutionSpace, Kokkos::MemoryTraits<0>, Kokkos::LayoutRight> ragged_array(strides, "MyRaggedArray");

// Use the ragged array...
ragged_array.set_values(3.14);
ragged_array.print();
}

// Test RaggedDownArrayKokkos
{
// Create a CArrayKokkos for strides
CArrayKokkos<size_t, Kokkos::LayoutRight, Kokkos::DefaultExecutionSpace, Kokkos::MemoryTraits<0>> strides(3);

// Set up strides (this is just an example, adjust as needed)
Kokkos::parallel_for("SetStrides", 1, KOKKOS_LAMBDA(const int&) {
strides(0) = 2; // dim0
strides(1) = 6; // total elements in dim1
strides(2) = 12; // total elements overall
});

// Create RaggedRightArrayKokkos using the new constructor
RaggedDownArrayKokkos<double, Kokkos::LayoutRight, Kokkos::DefaultExecutionSpace, Kokkos::MemoryTraits<0>, Kokkos::LayoutRight> ragged_array(strides, "MyRaggedArray");

// Use the ragged array...
ragged_array.set_values(5.67);
ragged_array.print();
}
Kokkos::finalize();
return 0;
}
103 changes: 103 additions & 0 deletions src/include/kokkos_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -6402,12 +6402,20 @@ class RaggedRightArrayKokkos {
//setup start indices
void data_setup(const std::string& tag_string);

//return pointer
KOKKOS_INLINE_FUNCTION
T* pointer();

//return the view
KOKKOS_INLINE_FUNCTION
TArray1D get_kokkos_view();

//print values
void print() const;

//set values to input
KOKKOS_INLINE_FUNCTION
void set_values(T val);

// Kokkos views of strides and start indices
Strides1D mystrides_;
Expand Down Expand Up @@ -6728,6 +6736,36 @@ Kokkos::View<T*, Layout, ExecSpace, MemoryTraits> RaggedRightArrayKokkos<T,Layou
return array_;
}

//set values to input
template <typename T, typename Layout, typename ExecSpace, typename MemoryTraits, typename ILayout>
KOKKOS_INLINE_FUNCTION
void RaggedRightArrayKokkos<T,Layout,ExecSpace,MemoryTraits,ILayout>::set_values(T val) {
Kokkos::parallel_for("SetValues_RaggedRightArrayKokkos", length_, KOKKOS_CLASS_LAMBDA(const int i) {
array_(i) = val;
});
}

// print method implementation
template <typename T, typename Layout, typename ExecSpace, typename MemoryTraits, typename ILayout>
void RaggedRightArrayKokkos<T, Layout, ExecSpace, MemoryTraits, ILayout>::print() const {
auto host_array = Kokkos::create_mirror_view(array_);
Kokkos::deep_copy(host_array, array_);

int dim0 = mystrides_(0);
int dim1 = mystrides_(1) / dim0; // Average dim1 size
int dim2 = mystrides_(2) / mystrides_(1); // Average dim2 size

for (int i = 0; i < dim0; i++) {
for (int j = 0; j < dim1; j++) {
for (int k = 0; k < dim2; k++) {
printf("%.2f ", host_array(i * dim1 * dim2 + j * dim2 + k));
}
printf("\n");
}
printf("\n");
}
}

// Destructor
template <typename T, typename Layout, typename ExecSpace, typename MemoryTraits, typename ILayout>
KOKKOS_INLINE_FUNCTION
Expand Down Expand Up @@ -7127,6 +7165,7 @@ class RaggedDownArrayKokkos {
KOKKOS_INLINE_FUNCTION
T& operator()(size_t i, size_t j) const;

//return pointer
KOKKOS_INLINE_FUNCTION
T* pointer();

Expand All @@ -7136,6 +7175,13 @@ class RaggedDownArrayKokkos {

KOKKOS_INLINE_FUNCTION
RaggedDownArrayKokkos& operator= (const RaggedDownArrayKokkos &temp);

//print values
void print() const;

//set values to input
KOKKOS_INLINE_FUNCTION
void set_values(T val);

// Kokkos views of strides and start indices
Strides1D mystrides_;
Expand Down Expand Up @@ -7421,6 +7467,37 @@ Kokkos::View<T*, Layout, ExecSpace, MemoryTraits> RaggedDownArrayKokkos<T,Layout
return array_;
}

//set values to input
template <typename T, typename Layout, typename ExecSpace, typename MemoryTraits, typename ILayout>
KOKKOS_INLINE_FUNCTION
void RaggedDownArrayKokkos<T,Layout,ExecSpace,MemoryTraits,ILayout>::set_values(T val) {
Kokkos::parallel_for("SetValues_RaggedDownArrayKokkos", length_, KOKKOS_CLASS_LAMBDA(const int i) {
array_(i) = val;
});
}

// print method implementation
template <typename T, typename Layout, typename ExecSpace, typename MemoryTraits, typename ILayout>
void RaggedDownArrayKokkos<T, Layout, ExecSpace, MemoryTraits, ILayout>::print() const {
auto host_array = Kokkos::create_mirror_view(array_);
Kokkos::deep_copy(host_array, array_);

int dim0 = mystrides_(0);
int dim1 = mystrides_(1) / dim0; // Average dim1 size
int dim2 = mystrides_(2) / mystrides_(1); // Average dim2 size

for (int i = 0; i < dim0; i++) {
for (int j = 0; j < dim1; j++) {
for (int k = 0; k < dim2; k++) {
printf("%.2f ", host_array(i * dim1 * dim2 + j * dim2 + k));
}
printf("\n");
}
printf("\n");
}
}


// Destructor
template <typename T, typename Layout, typename ExecSpace, typename MemoryTraits, typename ILayout>
KOKKOS_INLINE_FUNCTION
Expand Down Expand Up @@ -7467,6 +7544,10 @@ class DynamicRaggedRightArrayKokkos {
KOKKOS_INLINE_FUNCTION
TArray1D get_kokkos_view();

// set values to input
KOKKOS_INLINE_FUNCTION
void set_values(T val);

// Overload operator() to access data as array(i,j),
// where i=[0:N-1], j=[stride(i)]
KOKKOS_INLINE_FUNCTION
Expand Down Expand Up @@ -7596,6 +7677,15 @@ Kokkos::View<T*, Layout, ExecSpace, MemoryTraits> DynamicRaggedRightArrayKokkos<
return array_;
}

//set values to input
template <typename T, typename Layout, typename ExecSpace, typename MemoryTraits>
KOKKOS_INLINE_FUNCTION
void DynamicRaggedRightArrayKokkos<T,Layout,ExecSpace,MemoryTraits>::set_values(T val) {
Kokkos::parallel_for("SetValues_DynamicRaggedRightArrayKokkos", length_, KOKKOS_CLASS_LAMBDA(const int i) {
array_(i) = val;
});
}

// Destructor
template <typename T, typename Layout, typename ExecSpace, typename MemoryTraits>
KOKKOS_INLINE_FUNCTION
Expand Down Expand Up @@ -7645,6 +7735,10 @@ class DynamicRaggedDownArrayKokkos {
KOKKOS_INLINE_FUNCTION
TArray1D get_kokkos_view();

//set values to input
KOKKOS_INLINE_FUNCTION
void set_values(T val);

// Overload operator() to access data as array(i,j),
// where i=[stride(j)], j=[0:N-1]
KOKKOS_INLINE_FUNCTION
Expand Down Expand Up @@ -7774,6 +7868,15 @@ Kokkos::View<T*, Layout, ExecSpace, MemoryTraits> DynamicRaggedDownArrayKokkos<T
return array_;
}

//set values to input
template <typename T, typename Layout, typename ExecSpace, typename MemoryTraits>
KOKKOS_INLINE_FUNCTION
void DynamicRaggedDownArrayKokkos<T,Layout,ExecSpace,MemoryTraits>::set_values(T val) {
Kokkos::parallel_for("SetValues_DynamicRaggedDownArrayKokkos", length_, KOKKOS_CLASS_LAMBDA(const int i) {
array_(i) = val;
});
}

// Destructor
template <typename T, typename Layout, typename ExecSpace, typename MemoryTraits>
KOKKOS_INLINE_FUNCTION
Expand Down
Loading