It seems the examples in one of the CUDA books (CUDA by Example: An Introduction to General-Purpose GPU Programming) never nullify pointers when initializing them.
Two questions:
Does GPU use 0x0 (or integer 0) as NULL pointer? Should we follow the C/C++ pointer practice with device pointers (e.g. nullify them when initializing)
Do we need to check whether the pointer is NULL before cudaFree it ? if (devPtr) HANDLE_ERROR(cudaFree(devPtr));
Someone said that for Fermi architecture, 0x0 is used for on-chip shared memory, it seems that it's still okay for use to assume that 0x0 should not be pointed by a used pointer.
http://forums.thedailywtf.com/forums/p/25369/273567.aspx
What about Kepler architecture? What does GPU do with 0x0 address?
Thank you!
Generally, you can treat NULL pointers just like you do with host pointers. To answer your questions.
Yes. Feel free to use the same practice you use on the host (nullify at will).
It's safe to check for NULL before you free it, but not necessary, since cudaFree(0)
will not actually attempt to free any memory. (In fact cudaFree(0)
is commonly used to initialize the CUDA context!) I believe that most modern malloc
implementations do not attempt to free zero-valued pointers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With