Skip to content

Commit

Permalink
add thread name for dataloader (PaddlePaddle#46990)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiqiu authored Oct 13, 2022
1 parent f856fc8 commit 770501b
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions paddle/fluid/platform/os_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ limitations under the License. */
#else
#include <unistd.h>
#endif
#include "glog/logging.h"
#include "paddle/fluid/framework/new_executor/workqueue/thread_data_registry.h"
#include "paddle/fluid/platform/macros.h" // import DISABLE_COPY_AND_ASSIGN

Expand Down Expand Up @@ -115,6 +116,7 @@ bool SetCurrentThreadName(const std::string& name) {
return false;
}
instance.SetCurrentThreadData(name);
VLOG(4) << __func__ << " " << name;
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/platform/os_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ ThreadId GetCurrentThreadId();
// create/destory when using it.
std::unordered_map<uint64_t, ThreadId> GetAllThreadIds();

static constexpr const char* kDefaultThreadName = "unset";
static constexpr const char* kDefaultThreadName = "unnamed";
// Returns kDefaultThreadName if SetCurrentThreadName is never called.
std::string GetCurrentThreadName();

Expand Down
2 changes: 2 additions & 0 deletions paddle/fluid/pybind/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,8 @@ PYBIND11_MODULE(libpaddle, m) {

m.def("_set_paddle_lib_path", &paddle::platform::dynload::SetPaddleLibPath);

m.def("set_current_thread_name", &paddle::platform::SetCurrentThreadName);

m.def("_promote_types_if_complex_exists",
&paddle::framework::PromoteTypesIfComplexExists);

Expand Down
2 changes: 2 additions & 0 deletions python/paddle/fluid/dataloader/dataloader_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def _thread_loop(self, legacy_expected_place):
# If we do not set cudaDeviceId in new thread, the default cudaDeviceId will be 0,
# Which may cost hundreds of MB of GPU memory on CUDAPlace(0) if calling some cuda
# APIs in this thread.
core.set_current_thread_name("Dataloader_" + str(id(self)))
_set_expected_place(legacy_expected_place)

while not self._thread_done_event.is_set():
Expand Down Expand Up @@ -530,6 +531,7 @@ def _thread_loop(self, legacy_expected_place):
# If we do not set cudaDeviceId in new thread, the default cudaDeviceId will be 0,
# Which may cost hundreds of MB of GPU memory on CUDAPlace(0) if calling some cuda
# APIs in this thread.
core.set_current_thread_name("Dataloader_" + str(id(self)))
_set_expected_place(legacy_expected_place)

while not self._thread_done_event.is_set():
Expand Down
1 change: 1 addition & 0 deletions python/paddle/fluid/layers/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ def start_provide_thread(func):
def __provider_thread__(legacy_expected_place):
try:
# See _DataLoaderIterSingleProcess._thread_loop() for why set expected place here.

_set_expected_place(legacy_expected_place)

for tensors in func():
Expand Down
3 changes: 3 additions & 0 deletions python/paddle/fluid/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,7 @@ def _exit_thread_unexpectedly(self):

def _reader_thread_loop_for_multiprocess(self, legacy_expected_place):
# See _DataLoaderIterSingleProcess._thread_loop() for why set expected place here.
core.set_current_thread_name("Dataloader_" + str(id(self)))
_set_expected_place(legacy_expected_place)

while not self._thread_done_event.is_set():
Expand Down Expand Up @@ -1169,6 +1170,7 @@ def _reader_thread_loop_for_multiprocess(self, legacy_expected_place):
def _reader_thread_loop_for_singleprocess(self, legacy_expected_place):
try:
# See _DataLoaderIterSingleProcess._thread_loop() for why set expected place here.
core.set_current_thread_name("Dataloader_" + str(id(self)))
_set_expected_place(legacy_expected_place)

for sample in self._batch_reader():
Expand Down Expand Up @@ -1419,6 +1421,7 @@ def _start(self):
def __thread_main__(legacy_expected_place):
try:
# See _DataLoaderIterSingleProcess._thread_loop() for why set expected place here.
core.set_current_thread_name("Dataloader_" + str(id(self)))
_set_expected_place(legacy_expected_place)

while not self._queue.wait_for_inited(1):
Expand Down

0 comments on commit 770501b

Please sign in to comment.