This repository was archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 761
introduce get_iterator_value #783
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3b79f66
get_iterator_value: safe pointer/iterator dereferencing
3gx 718c996
bug fix
3gx 2b8e2f2
get_iterator_value does not belong in thryst/system/detail/generic be…
3gx 10c409d
add {min,max,minmax}_element testing with transform_iterator
3gx 5b848c5
add {min,max,minmax}_element testing with raw device pointer and thru…
3gx 7ee3fc9
remove duplicate #pragma once
3gx f9c9daa
typo fix
3gx c73d64a
invoke get_iterator_value via thrust::detail::get_iterator_value
3gx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #pragma once | ||
| /* | ||
| * Copyright 2008-2016 NVIDIA Corporation | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include <thrust/detail/config.h> | ||
|
|
||
| namespace thrust { | ||
| namespace detail { | ||
|
|
||
| // get_iterator_value specialization on iterators | ||
| // -------------------------------------------------- | ||
| // it is okay to dereference iterator in usual way | ||
| template<typename DerivedPolicy, typename Iterator> | ||
| __host__ __device__ | ||
| typename thrust::iterator_traits<Iterator>::value_type | ||
| get_iterator_value(thrust::execution_policy<DerivedPolicy> &, Iterator it) | ||
| { | ||
| return *it; | ||
| } // get_iterator_value(exec,Iterator); | ||
|
|
||
| // get_iterator_value specialization on pointer | ||
| // ---------------------------------------------- | ||
| // we can't just dereference a pointer in usual way, because | ||
| // it may point to a location in the device memory. | ||
| // we use get_value(exec,pointer*) function | ||
| // to perform a dereferencing consistent with the execution policy | ||
| template<typename DerivedPolicy, typename Pointer> | ||
| __host__ __device__ | ||
| typename thrust::detail::pointer_traits<Pointer*>::element_type | ||
| get_iterator_value(thrust::execution_policy<DerivedPolicy> &exec, Pointer* ptr) | ||
| { | ||
| return get_value(derived_cast(exec),ptr); | ||
| } // get_iterator_value(exec,Pointer*) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this implementation only works for raw pointers. What will happen with types like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dereferencing device_ptr is safe on the host. Upon dereferencing on the host data is copied from the device, and so it is treated as an iterator. This specialisation is meant for raw pointers only that might be unsafe to dereference on the host. |
||
|
|
||
| } // namespace detail | ||
| } // namespace thrust | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this implementation will work as intended because it ignores the execution policy. For example, if the user provides a CUDA stream in the execution policy, that will be ignored, and this dereference will use the default stream.
Wouldn't it be more correct for the body of
get_iterator_valueto do something like callthrust::copy?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why wouldn't it, aren't true iterators supposed to be safe for usual derlerencing? If not, what is the interface to deference an iterator with an execution policy? This passes all the tests.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we intend for
get_iterator_value()itself to be the interface to dereference an iterator with an execution policy. So, we have to build the implementation and ensure that it handles all cases correctly. I'm concerned that this implementation doesn't handle all cases correctly, namely, those cases where the user has provided a custom execution policy which is different from whatever is tagged in the iterator.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not following. What would be an example of dereferencing an iterator with execution policy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To add to this: it will be a non-issue because nothing nothing changes for iterators: they are still � dereference via 'operator*', as it was earlier. If users were hitting bugs with such dereferencing, we would have already known. We need a counter example to show it is unsafe now and worked before this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suppose I call
max_element(par.on(my_stream), vec.begin(), vec.end())and look at the visual profiler timeline of my application. What sort of synchronization behavior would I observe with this implementation?I believe I would see that all CUDA streams in my application synchronize at the call to
max_elementbecause the iterator dereference inside ofget_iterator_value()will use the default stream instead of the stream contained inside of my execution policy.Remember that in addition to preventing a crash due to dereferencing a raw device pointer on the host, we also have to ensure that the user's execution policy is used for this dereference. The one overload's use of
get_value()ensures both of those things happen, but the other overload's plain iterator dereference does not.I think that using
thrust::copywill solve this problem.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand. However, this fix doesn't change the previously existed behaviour, but fixes a bug when a raw pointer is passed to max_element.
That being said, replacing
*itwithmakes the reproducer to die with
when used with transform_iterator and I wasn't able to quickly nail down the issue. I agree the get_value_iterator needs to be enhanced to respect customer user policy, but it may take more time.
I suggest either accepting this fix, or reverting #777 to make sure the user codes are not broken and then continue working on this PR to make get_iterator_value to respect user policy.