Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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 CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ endif()
set(ENABLE_COPY_HEADERS On CACHE BOOL "")
set(BLT_CXX_STD c++11 CACHE STRING "")

if( CMAKE_BUILD_TYPE STREQUAL "Debug" )
set( CHAI_ENABLE_BOUNDS_CHECK ON CACHE BOOL "Enable Bounds Checking for chai::ManagedArray<>::operator[]" FORCE )
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an option at the top of the CMakeLists file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

endif()

################################
# BLT
Expand Down
1 change: 1 addition & 0 deletions cmake/ChaiBasics.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@

set (CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --expt-extended-lambda")
include(${CMAKE_SOURCE_DIR}/cmake/thirdparty/SetupChaiThirdparty.cmake)

5 changes: 5 additions & 0 deletions src/ManagedArray.inl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
#include "ManagedArray.hpp"
#include "ArrayManager.hpp"

#include <cassert>

namespace chai {

template<typename T>
Expand Down Expand Up @@ -206,6 +208,9 @@ template<typename T>
template<typename Idx>
CHAI_INLINE
CHAI_HOST_DEVICE T& ManagedArray<T>::operator[](const Idx i) const {
#if defined(CHAI_ENABLE_BOUNDS_CHECK)
assert( i>=0 && static_cast<size_t>(i) < m_elems );
#endif
return m_active_pointer[i];
}

Expand Down
1 change: 1 addition & 0 deletions src/config.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@
#cmakedefine CHAI_ENABLE_IMPLICIT_CONVERSIONS
#cmakedefine CHAI_DISABLE_RM
#cmakedefine CHAI_ENABLE_UM
#cmakedefine CHAI_ENABLE_BOUNDS_CHECK

#endif // CHAI_config_HPP
42 changes: 33 additions & 9 deletions src/tests/managed_array_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,20 +393,44 @@ CUDA_TEST(ManagedArray, UserCallback)
#endif
#endif

#if defined(CHAI_ENABLE_BOUNDS_CHECK)
TEST(ManagedArray, UpperOutOfRangeAccess)
{
chai::ManagedArray<float> array(20);

#if defined(CHAI_ENABLE_CUDA)
CUDA_TEST(ManagedArray, Move)
array[19] = 0.0;
EXPECT_DEATH_IF_SUPPORTED( array[20] = 0.0, ".*" );
}

TEST(ManagedArray, LowerOutOfRangeAccess)
{
chai::ManagedArray<float> array(10, chai::GPU);
chai::ManagedArray<float> array(20);

forall(cuda(), 0, 10, [=] __device__ (int i) {
array[i] = i;
});
array[0] = 0.0;
EXPECT_DEATH_IF_SUPPORTED( array[-1] = 0.0, ".*" );
}

array.move(chai::CPU);
#if defined(CHAI_ENABLE_CUDA)
CUDA_TEST(ManagedArray, UpperOutOfRangeAccessGPU)
{
chai::ManagedArray<float> array(20);

ASSERT_EQ(array[5], 5);
EXPECT_DEATH(
forall(cuda(), 19, 20, [=] __device__ (int i) {
array[i] = 0.0f;
});,
"i > m_elems");
}

Copy link
Contributor

@rrsettgast rrsettgast Jun 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@artv3 I think that the intention was to use @davidbeckingsale tests. Can you revert this file to his commit if that is a better idea?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I remove yours? Or keep them as well?

Copy link
Contributor

@rrsettgast rrsettgast Jun 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think David's tests cover the space better...right? well at any rate, there needs to be a cuda test.

array.free();
CUDA_TEST(ManagedArray, LowerOutOfRangeAccessGPU)
{
chai::ManagedArray<float> array(20);

EXPECT_DEATH(
forall(cuda(), -1, 0, [=] __device__ (int i) {
array[i] = 0.0f;
});,
"i < 0");
}
#endif // defined(CHAI_ENABLE_CUDA)
#endif // !defined(NDEBUG)
2 changes: 1 addition & 1 deletion src/util/forall.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ template <typename LOOP_BODY>
void forall_kernel_cpu(int begin, int end, LOOP_BODY body)
{
for (int i = 0; i < (end - begin); ++i) {
body(i);
body(begin+i);
}
}

Expand Down