Skip to content

Multithreading Support and fixed critical heap corruption - #371

Merged
Oceania2018 merged 20 commits into
masterfrom
multithreading
Aug 31, 2019
Merged

Oceania2018 merged 20 commits into
masterfrom
multithreading

Conversation

@Nucs

@Nucs Nucs commented Aug 31, 2019 •

Copy link
Copy Markdown
Member

Multithreading

The multithreading architecture that was decided to implement can be summarized in making get_default_graph and get_default_session to be different for every thread by using ThreadLocal<T>.
It is now possible (and well unit-tested) to initialize two models and call session.run in different threads without interference.
These changes are entirely behind the scenes and require no changes from the users.
Most of c_api provided by TF is completely thread-safe but some are not and these are not documented. So in the future there might rise some problems which are easily
fixed by performing lock(Locks.ProcessWide).

Heap Corruption

With @henon collaboration, I discovered a serious heap corruption that occurs after Tensor is created and moved by the GC (ususally when scoping out of sess.run), causing the DeallocationArgs to be moved with Tensor (it was a field in Tensor) while TF still has the old memory location. When GC decides the Tensor is no longer referenced, invalid DeallocationArgs is passed to the Deallocator method to perform disposal which caused heap corruption.
With that I ended up rewriting Tensor.Creation which is now pretty faster.

Nucs added 20 commits August 30, 2019 19:35
TensorTest: Added Unit tests for more coverage, reformatted file.
c_api.tensor: Added overloads for TF_NewTensor that does not have deallocator parameters.
BaseSession: Removed disposal immediately after TF_SessionRun call.
c_api.DeallocatorArgs: Added DeallocatorArgs.Empty
…F_NewTensor

- Added other missing AllocationType setting in different cases.
- Ignored all unit tests related to CSession as it does not use TF.NET's api directly and unable to be tested with other tests parallely.
@Nucs
Nucs requested a review from Oceania2018 August 31, 2019 14:09

@Oceania2018 Oceania2018 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great work. @Nucs @henon

@Oceania2018
Oceania2018 merged commit 641a105 into master Aug 31, 2019
@Oceania2018
Oceania2018 deleted the multithreading branch October 6, 2019 04:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants