Skip to content

Commit

Permalink
Merge Pull Request #10913 from rppawlo/Trilinos/phalanx-fix-cuda-warn…
Browse files Browse the repository at this point in the history
…ings

Automatically Merged using Trilinos Pull Request AutoTester
PR Title: Phalanx: remove cuda compiler warnings and add test for new use case 
PR Author: rppawlo
  • Loading branch information
trilinos-autotester authored Aug 25, 2022
2 parents 2187f52 + 2c21f07 commit 37d8075
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/phalanx/src/Phalanx_KokkosViewOfViews.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,17 @@ namespace PHX {
check_use_count_(true)
{}

// Making this a kokkos function eliminates cuda compiler warnings
// in objects that contain ViewOfViews3 that are copied to device.
KOKKOS_INLINE_FUNCTION
~ViewOfViews3()
{
// Make sure there is not another object pointing to device view
// since the host view will delete the inner views on exit.
if ( check_use_count_ && (view_device_.impl_track().use_count() != use_count_) )
Kokkos::abort("\n ERROR - PHX::ViewOfViews - please free all instances of device ViewOfView \n before deleting the host ViewOfView!\n\n");
KOKKOS_IF_ON_HOST((
if ( check_use_count_ && (view_device_.impl_track().use_count() != use_count_) )
Kokkos::abort("\n ERROR - PHX::ViewOfViews - please free all instances of device ViewOfView \n before deleting the host ViewOfView!\n\n");
))
}

/// Enable safety check in dtor for external references.
Expand Down
7 changes: 7 additions & 0 deletions packages/phalanx/test/Kokkos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ TRIBITS_ADD_EXECUTABLE_AND_TEST(
TESTONLYLIBS phalanx_unit_test_main phalanx_test_utilities
NUM_MPI_PROCS 1
)

TRIBITS_ADD_EXECUTABLE_AND_TEST(
tKokkosClassOnDevice
SOURCES tKokkosClassOnDevice.cpp
TESTONLYLIBS phalanx_unit_test_main phalanx_test_utilities
NUM_MPI_PROCS 1
)
108 changes: 108 additions & 0 deletions packages/phalanx/test/Kokkos/tKokkosClassOnDevice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include "Kokkos_Core.hpp"
#include "Kokkos_View.hpp"

#include "Teuchos_Assert.hpp"
#include "Teuchos_UnitTestHarness.hpp"
#include "Phalanx_KokkosViewOfViews.hpp"

template<typename T,int dim>
class Vector {
T values_[dim];
public:

template<typename INDEX_I>
KOKKOS_INLINE_FUNCTION
T& operator[](const INDEX_I& index){return values_[index];}

template<typename INDEX_I>
KOKKOS_INLINE_FUNCTION
const T& operator[](const INDEX_I& index)const{return values_[index];}

template<typename INDEX_I>
KOKKOS_INLINE_FUNCTION
volatile T& operator[](const INDEX_I& index)volatile{return values_[index];}

template<typename INDEX_I>
KOKKOS_INLINE_FUNCTION
const volatile T& operator[](const INDEX_I& index)const volatile{return values_[index];}
};

class MyClass {
Kokkos::View<double*> a_;
double b_[3];
Kokkos::View<double*> c_;
Vector<double,3> d_;
// To test for cuda warnings when MyClass is lambda captured to
// device
PHX::ViewOfViews3<1,Kokkos::View<double*>> e_;

public:
MyClass() :
a_("a",3),
c_("c",3)
{
Kokkos::deep_copy(a_,1.0);
b_[0] = 1.0;
b_[1] = 2.0;
b_[2] = 3.0;
Kokkos::deep_copy(c_,2.0);
d_[0] = 1.0;
d_[1] = 2.0;
d_[2] = 3.0;
}

void KOKKOS_FUNCTION checkInternalMethod1() const
{ this->callInternalMethod1(); }

void KOKKOS_FUNCTION
callInternalMethod1() const
{
printf("b_[0]=%f\n",b_[0]);
printf("b_[1]=%f\n",b_[1]);
printf("b_[2]=%f\n",b_[2]);
a_(0)=b_[0];
a_(1)=b_[1];
a_(2)=b_[2];
}

void KOKKOS_FUNCTION checkInternalMethod2() const
{ this->callInternalMethod2(); }

void KOKKOS_FUNCTION
callInternalMethod2() const
{
a_(0)=c_(0);
a_(1)=c_(1);
a_(2)=c_(2);
}

void KOKKOS_FUNCTION checkInternalMethod3() const
{ this->callInternalMethod3(); }

void KOKKOS_FUNCTION
callInternalMethod3() const
{
a_(0)=d_[0];
a_(1)=d_[1];
a_(2)=d_[2];
}
};

TEUCHOS_UNIT_TEST(KokkosClassOnDevice, One)
{
MyClass my_class;

Kokkos::parallel_for("test 1",1,KOKKOS_LAMBDA (const int ) {
my_class.checkInternalMethod1();
});

Kokkos::parallel_for("test 2",1,KOKKOS_LAMBDA (const int ) {
my_class.checkInternalMethod2();
});

Kokkos::parallel_for("test 3",1,KOKKOS_LAMBDA (const int ) {
my_class.checkInternalMethod3();
});

Kokkos::fence();
}

0 comments on commit 37d8075

Please sign in to comment.