CHAI icon indicating copy to clipboard operation
CHAI copied to clipboard

ManagedArrays copy constructed outside of a lambda capture are broken

Open adayton1 opened this issue 6 years ago • 4 comments

EDIT: See my comment below for an even simpler test case.

Data movement is triggered by the copy constructor, right? I have a case where a ManagedArray is a member of a class. The copy constructor of the class is called, which calls the copy constructor of the ManagedArray. However, the following test case fails.

class TestClass { public: TestClass(chai::ManagedArray values) : m_values(values) {} TestClass(const TestClass& other) : m_values(other.m_values) {} CHAI_HOST_DEVICE int getValue(const int i) const { return m_values[i]; } private: chai::ManagedArray m_values; };

CUDA_TEST(managed_ptr, cuda_inner_ManagedArray) { const int expectedValue = rand();

chai::ManagedArray array(1, chai::CPU); array[0] = expectedValue;

TestClass temp(array); chai::ManagedArray results(1, chai::GPU);

forall(cuda(), 0, 1, [=] device (int i) { results[i] = temp.getValue(i); });

results.move(chai::CPU); ASSERT_EQ(results[0], expectedValue); }

adayton1 avatar Feb 15 '19 23:02 adayton1

Here's an even simpler case.

CUDA_TEST(managed_ptr, cuda_inner_ManagedArray) { const int expectedValue = rand();

chai::ManagedArray array(1, chai::CPU); array[0] = expectedValue;

chai::ManagedArray array2 = array;

chai::ManagedArray results(1, chai::GPU);

forall(cuda(), 0, 1, [=] device (int i) { results[i] = array2[i]; });

results.move(chai::CPU); ASSERT_EQ(results[0], expectedValue); }

adayton1 avatar Feb 15 '19 23:02 adayton1

Hey @adayton1 your interpretation of how the copy is triggered is correct. I wonder if the array is being "moved" rather than copied.

davidbeckingsale avatar Feb 16 '19 00:02 davidbeckingsale

Does there need to be a call to cudaDeviceSynchronize? I thought the forall called it.

adayton1 avatar Feb 22 '19 20:02 adayton1

It does: https://github.com/LLNL/CHAI/blob/develop/src/util/forall.hpp#L114

davidbeckingsale avatar Feb 22 '19 20:02 davidbeckingsale