Skip to content
This repository was archived by the owner on Mar 21, 2024. It is now read-only.

Implement ptx_dispatch for if-target compatible target specialization - #521

Closed
alliepiper wants to merge 10 commits into
NVIDIA:mainfrom
alliepiper:if_target
Closed

alliepiper wants to merge 10 commits into
NVIDIA:mainfrom
alliepiper:if_target

Conversation

@alliepiper

Copy link
Copy Markdown
Contributor

No description provided.

@alliepiper alliepiper added this to the 2.0.0 milestone Jul 1, 2022
@alliepiper
alliepiper marked this pull request as draft July 1, 2022 03:06
@alliepiper alliepiper modified the milestones: 2.0.0, 2.1.0 Aug 4, 2022
@alliepiper
alliepiper force-pushed the if_target branch 5 times, most recently from c5ff0a7 to d11e27e Compare September 13, 2022 22:05
@alliepiper
alliepiper marked this pull request as ready for review September 14, 2022 20:24
@alliepiper alliepiper added type: enhancement New feature or request. testing: gpuCI in progress Started gpuCI testing. P0: must have Absolutely necessary. Critical issue, major blocker, etc. release: breaking change Include in "Breaking Changes" section of release notes. labels Sep 14, 2022
Comment thread cmake/CubCudaConfig.cmake Resolved
@gevtushenko

Copy link
Copy Markdown
Collaborator

I can see a lot of compilation errors when build with CTK 11.0, could you take a look?

cub/cub/cmake/../../cub/device/dispatch/dispatch_scan.cuh:169:57: error: unused parameter ‘d_in’ [-Werror=unused-parameter]
  169 | __global__ void DeviceScanKernel(InputIteratorT d_in,
      |                                         ~~~~~~~~~~~~~   ^   
cub/cub/cmake/../../cub/device/dispatch/dispatch_scan.cuh:170:1: error: unused parameter ‘d_out’ [-Werror=unused-parameter]
  169 | __global__ void DeviceScanKernel(InputIteratorT d_in,
      |                                                      
  170 |                                  OutputIteratorT d_out,
      | ^    
cub/cub/cmake/../../cub/device/dispatch/dispatch_scan.cuh:171:1: error: unused parameter ‘tile_state’ [-Werror=unused-parameter]
  170 |                                  OutputIteratorT d_out,
      |                                  ~~~~~~~~~~~~~~~~~~~~~~
  171 |                                  ScanTileStateT tile_state,
      | ^         
cub/cub/cmake/../../cub/device/dispatch/dispatch_scan.cuh:172:1: error: unused parameter ‘start_tile’ [-Werror=unused-parameter]
  171 |                                  ScanTileStateT tile_state,
      |                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~
  172 |                                  int start_tile,
      | ^         
cub/cmake/../../cub/device/dispatch/dispatch_scan.cuh:173:1: error: unused parameter ‘scan_op’ [-Werror=unused-parameter]
  172 |                                  int start_tile,
      |                                  ~~~~~~~~~~~~~~~
  173 |                                  ScanOpT scan_op,
      | ^      
cub/cub/cmake/../../cub/device/dispatch/dispatch_scan.cuh:174:1: error: unused parameter ‘init_value’ [-Werror=unused-parameter]
  173 |                                  ScanOpT scan_op,
      |                                  ~~~~~~~~~~~~~~~~
  174 |                                  InitValueT init_value,
      | ^         
cub/cub/cmake/../../cub/device/dispatch/dispatch_scan.cuh:175:1: error: unused parameter ‘num_items’ [-Werror=unused-parameter]
  174 |                                  InitValueT init_value,
      |                                  ~~~~~~~~~~~~~~~~~~~~~~
  175 |                                  OffsetT num_items)
      | ^     

@gevtushenko gevtushenko left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The code looks much better now, thanks! A few minor notes below:

template <typename T>
struct type_wrapper
{
using type = T;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Policy might be a user defined type. This type wrapper implementation doesn't isolate from ADL:

#include <iostream>

namespace ns {
  struct A{};

  template <class T>
  void foo(T&&) {
    std::cout << "ns" << std::endl;
  }
}

template <class T>
struct type_wrapper_1 {
  using type = T;
};

template <class T>
struct type_wrapper_2_impl {
  using type = struct t {
    using type = T;
  };
};

template <class T>
using type_wrapper_2 = typename type_wrapper_2_impl<T>::type;

template <class T>
struct library_type {
};

int main() {
  using wrapped_type_1 = type_wrapper_1<ns::A>;
  using wrapped_type_2 = type_wrapper_2<ns::A>;
  library_type<wrapped_type_1> library_obj_1;
  library_type<wrapped_type_2> library_obj_2;

  foo(library_obj_1);
  // foo(library_obj_2); // does isolate from ADL
} 

This might be worth it to reproduce @ericniebler implementation from the beginning.

@alliepiper alliepiper Sep 22, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This isn't intended to interact with ADL, and ADL shouldn't be a concern in ptx_dispatch. type_wrapper just prevents PtxTypes from requiring default constructability.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

e.g. this way, the ptx_dispatch takes a functor that defines operator()(type_wrapper<T>) instead of operator()(T).

I've run into issues with similar dispatch mechanisms when using types that required ctor arguments.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is there anything blocking us from using the suggested implementation? Otherwise, we'd have to implement a second type wrapper at some point.

Comment thread cmake/CubCudaConfig.cmake
@@ -3,9 +3,9 @@ enable_language(CUDA)
#
# Architecture options:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm not sure if this is related to this PR, but I bumped into the following issue when tested it:

	57 - cub.cpp17.test.device_radix_sort.cdp_1.bytes_2.pairs_3 (SEGFAULT)

When I launched the test separately, there were no issue.

@alliepiper alliepiper Sep 22, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I haven't seen this, and it hasn't popped up on CI. Still worrying though...

When this failed, were you running the tests in parallel (ctest -j XX)? Maybe there was a spurious OOM -> failed malloc -> nullptr -> segfault?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I haven't run tests in parallel.

Comment thread test/test_device_segmented_sort.cu Resolved
#elif defined(NV_TARGET_SM_INTEGER_LIST)

// clang-format off
#define CUB_DETAIL_PTX_TARGETS_NAME1(P1) \

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

optional: we might potentially abstract this pattern by providing suffix and separator parameters.

Comment thread cub/detail/type_list_impl.cuh Resolved
@alliepiper

Copy link
Copy Markdown
Contributor Author

I can see a lot of compilation errors when build with CTK 11.0, could you take a look?

Ack. I had an ugly WAR for these, but ended up upstreaming the fix to nvcc (NVBug 2431416). You're right, though, we still need it for older CTK -- I'll put the WAR back in.

@gevtushenko gevtushenko left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I've built our tests to compare new codegen with the previous one. There's quite a difference there:

:wc -l asm_old/cub.cpp17.test.device_reduce.cdp_0.types_0.asm
3389546 asm_old/cub.cpp17.test.device_reduce.cdp_0.types_0.asm
:wc -l asm_new/cub.cpp17.test.device_reduce.cdp_0.types_0.asm
34674 asm_new/cub.cpp17.test.device_reduce.cdp_0.types_0.asm

Am I missing something or there was some sort of a bug there? The build is for all supported architectures. Then:

for f in $(ls bin); do cuobjdump --dump-sass bin/$f > asm/$f.asm &; done

Could you take a look on your side?

CUB_HOST_COMPILER == CUB_HOST_COMPILER_CLANG

// Clang honors GCC pragmas
#define CUB_KERNEL_BEGIN \

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do you intend these macro set as a public API? Otherwise we'd have to name them as CUB_DETAIL_KERNEL_*. Also, if the issue was fixed in 11.4, I think there's a value in defining these macro only for CTK < 11.4.

@miscco miscco left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

So my knowledge about the dispatch mechanisms is limited, so everything here is with a grain of salt.

I believe the new implementation manages to be less eldritchy than the previous. one.

I am fine with not trying to do ADL isolation right now, as that adds quite a bit to compile times due to the additional instantiations required. It is only a problem when the target functions are overloaded, which might be a problem for copy(in, in , out) but I would wager we are fine here.

I am fine with being strict about the ordering and uniqueness of the archs, that is on our plate and we should take care there.

#include <cub/thread/thread_search.cuh>
#include <cub/util_type.cuh>

#include <nv/target>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is there a certain order this include must go in? Should we add <nv/ as another include block in the clang-format file?

// clang-format off
NV_IF_TARGET(NV_PROVIDES_SM_52,
(
auto a = wd_values + tile_start_coord.y + nonzero_idx;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is there a reason we need those computations outside of the if?

and obligatory hiss at one letter variable names

{
this->impl(exec_space_tag<ExecSpace>{},
policy_wrapper,
std::forward<DeviceAlgorithmDispatch>(dispatch),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should those be cuda::std::forward? Because I am not sure whether std::forward has __host__ __device__ on it

* using FooList = cub::detail::type_list<Foo800, Foo600>;
* ```
*
* \tparam PtxArch 3-digit PTX architecture version (e.g. 800 for Ampere)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

That comment might become stale quickly should we drop the "3-digit"

template <typename Functor, typename... Args>
__host__ __forceinline__ static void exec(int /* target_ptx_arch */, Functor &&, Args &&...)
{
// Didn't find a matching arch.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

should there be an assert here or something?


#include <cuda/std/type_traits>

#include <iterator>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should that be

Suggested change
#include <iterator>
#include <cuda/std/iterator>

using policies_t = typename DispatchAlgorithm::Policies;

// The execution spaces supported by the invokable
// (ie. host/device/host_device). `runtime_exec_space` is `host_device` when

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could we move the second sentence to its own line

@jrhemstad

Copy link
Copy Markdown
Collaborator

@allisonvacanti and I talked and we agreed to close this PR for now as this work has been de-prioritized. We will pick it back up if/when it becomes a priority again.

@jrhemstad jrhemstad closed this May 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

P0: must have Absolutely necessary. Critical issue, major blocker, etc. release: breaking change Include in "Breaking Changes" section of release notes. testing: gpuCI in progress Started gpuCI testing. type: enhancement New feature or request.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants