hcc icon indicating copy to clipboard operation
hcc copied to clipboard

Kernel can use raw copy of object after its destruction

Open misos1 opened this issue 7 years ago • 0 comments

First I had some difficulties with hc::printf_buffer. According code in mcwamp_hsa.cpp it should be initialized but in my case it is always null. So first line in following example initializes it (I had to use ::printf because seems something is sometimes shadowing printf with hc::printf even when I do not add "using namespace hc" or "using hc::printf").

#include <hc.hpp>
#define HCC_ENABLE_ACCELERATOR_PRINTF
#include <hc_printf.hpp>

#ifdef __KALMAR_CPU__
#define IAM "CPU"
#else
#define IAM "GPU"
#endif

int main()
{
	::printf("%p\n", hc::printf_buffer);
	hc::printf_buffer = hc::createPrintfBuffer(2048);
	struct Test
	{
		int x;
		Test() [[cpu, hc]] {hc::printf("%s constructor %p\n", IAM, this);}
		Test(const Test&) [[cpu, hc]] {hc::printf("%s copy constructor %p\n", IAM, this);}
		~Test() [[cpu, hc]] {hc::printf("%s destructor %p\n", IAM, this);}
		void func() const [[cpu, hc]] {hc::printf("%s func %p\n", IAM, this);}
	};
	{
		hc::completion_future fut;
		{
			Test test;
			fut = parallel_for_each(hc::extent<1>(1), [=](hc::index<1> i) [[hc]]
			{
				test.func();
			});
			hc::printf("1\n");
		}
		hc::printf("2\n");
		fut.wait();
		hc::printf("3\n");
	}
	hc::printf("4\n");
	hc::processPrintfBuffer(hc::printf_buffer);
	return 0;
}

This prints:

(nil)
CPU constructor 0x7ffe83d3eb10
CPU copy constructor 0x7ffe83d3eae8
CPU destructor 0x7ffe83d3eae8
1
CPU destructor 0x7ffe83d3eb10
2
GPU func 0x2000000000008
3
4

But all constructed and copy constructed instances are already destructed. GPU side did not call any constructor or destructor so it probably uses raw copy of object which was already destructed.

Lets compare this with std::async:

...
		std::future<void> fut;
		{
			Test test;
			fut = std::async(std::launch::async, [=]()
			{
				test.func();
			});
			hc::printf("1\n");
		}
...

Now I got:

(nil)
CPU constructor 0x7ffff79f72e8
CPU copy constructor 0x7ffff79f7330
CPU copy constructor 0x7ffff79f72a0
CPU copy constructor 0xaf9908
CPU destructor 0x7ffff79f72a0
CPU destructor 0x7ffff79f7330
1
CPU destructor 0x7ffff79f72e8
2
CPU func 0xaf9908
3
CPU destructor 0xaf9908
4

Which looks more correct as object on which was called func was somewhere copy constructed and destructed only after used.

Due to this strange behaviour is little difficult to create some types of smart pointers like for example smart pointer with host coherent system memory (what is missing when using hc::array or hc::array_view and this probably affects also hc::array_view).

misos1 avatar Sep 09 '18 18:09 misos1