CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
177 lines
5.6 KiB
Plaintext
177 lines
5.6 KiB
Plaintext
// Copyright (c) 2020-2025 Advanced Micro Devices, Inc. All rights reserved.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
// copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
|
|
#include <thrust/device_vector.h>
|
|
#include <thrust/iterator/discard_iterator.h>
|
|
#include <thrust/merge.h>
|
|
#include <thrust/set_operations.h>
|
|
|
|
#include <iostream>
|
|
|
|
// This example illustrates use of the set operation algorithms
|
|
// - merge
|
|
// - set_union
|
|
// - set_intersection
|
|
// - set_difference
|
|
// - set_symmetric_difference
|
|
//
|
|
// In this context a "set" is simply a sequence of sorted values,
|
|
// allowing the standard set operations to be performed more efficiently
|
|
// than on unsorted data. Since the output of a set operation is a valid
|
|
// set (i.e. a sorted sequence) it is possible to apply the set operations
|
|
// in a nested fashion to compute arbitrary set expressions.
|
|
//
|
|
// Set operation usage notes:
|
|
// - The output set size is variable (except for thrust::merge),
|
|
// so the return value is important.
|
|
// - Generally one would conservatively allocate storage for the output
|
|
// and then resize or shrink an output container as necessary.
|
|
// Alternatively, one can compute the exact output size by
|
|
// outputting to a discard_iterator. This approach is more computationally
|
|
// expensive (approximately 2x), but conserves memory capacity.
|
|
// Refer to the SetIntersectionSize function for implementation details.
|
|
// - Sets are allowed to have duplicate elements, which are carried
|
|
// through to the output in a algorithm-specific manner. Refer
|
|
// to the full documentation for precise semantics.
|
|
|
|
// helper routine
|
|
template <typename String, typename Vector>
|
|
void print(const String& s, const Vector& v)
|
|
{
|
|
std::cout << s << " [";
|
|
for (size_t i = 0; i < v.size(); i++)
|
|
{
|
|
std::cout << " " << v[i];
|
|
}
|
|
std::cout << " ]\n";
|
|
}
|
|
|
|
template <typename Vector>
|
|
void Merge(const Vector& A, const Vector& B)
|
|
{
|
|
// merged output is always exactly A.size() + B.size()
|
|
Vector C(A.size() + B.size());
|
|
|
|
thrust::merge(A.begin(), A.end(), B.begin(), B.end(), C.begin());
|
|
|
|
print("Merge(A,B)", C);
|
|
}
|
|
|
|
template <typename Vector>
|
|
void SetUnion(const Vector& A, const Vector& B)
|
|
{
|
|
// union output is at most A.size() + B.size()
|
|
Vector C(A.size() + B.size());
|
|
|
|
// set_union returns an iterator C_end denoting the end of input
|
|
typename Vector::iterator C_end;
|
|
|
|
C_end = thrust::set_union(A.begin(), A.end(), B.begin(), B.end(), C.begin());
|
|
|
|
// shrink C to exactly fit output
|
|
C.erase(C_end, C.end());
|
|
|
|
print("Union(A,B)", C);
|
|
}
|
|
|
|
template <typename Vector>
|
|
void SetIntersection(const Vector& A, const Vector& B)
|
|
{
|
|
// intersection output is at most min(A.size(), B.size())
|
|
Vector C(thrust::min(A.size(), B.size()));
|
|
|
|
// set_union returns an iterator C_end denoting the end of input
|
|
typename Vector::iterator C_end;
|
|
|
|
C_end = thrust::set_intersection(A.begin(), A.end(), B.begin(), B.end(), C.begin());
|
|
|
|
// shrink C to exactly fit output
|
|
C.erase(C_end, C.end());
|
|
|
|
print("Intersection(A,B)", C);
|
|
}
|
|
|
|
template <typename Vector>
|
|
void SetDifference(const Vector& A, const Vector& B)
|
|
{
|
|
// difference output is at most A.size()
|
|
Vector C(A.size());
|
|
|
|
// set_union returns an iterator C_end denoting the end of input
|
|
typename Vector::iterator C_end;
|
|
|
|
C_end = thrust::set_difference(A.begin(), A.end(), B.begin(), B.end(), C.begin());
|
|
|
|
// shrink C to exactly fit output
|
|
C.erase(C_end, C.end());
|
|
|
|
print("Difference(A,B)", C);
|
|
}
|
|
|
|
template <typename Vector>
|
|
void SetSymmetricDifference(const Vector& A, const Vector& B)
|
|
{
|
|
// symmetric difference output is at most A.size() + B.size()
|
|
Vector C(A.size() + B.size());
|
|
|
|
// set_union returns an iterator C_end denoting the end of input
|
|
typename Vector::iterator C_end;
|
|
|
|
C_end = thrust::set_symmetric_difference(A.begin(), A.end(), B.begin(), B.end(), C.begin());
|
|
|
|
// shrink C to exactly fit output
|
|
C.erase(C_end, C.end());
|
|
|
|
print("SymmetricDifference(A,B)", C);
|
|
}
|
|
|
|
template <typename Vector>
|
|
void SetIntersectionSize(const Vector& A, const Vector& B)
|
|
{
|
|
// computes the exact size of the intersection without allocating output
|
|
thrust::discard_iterator<> C_begin, C_end;
|
|
|
|
C_end = thrust::set_intersection(A.begin(), A.end(), B.begin(), B.end(), C_begin);
|
|
|
|
std::cout << "SetIntersectionSize(A,B) " << (C_end - C_begin) << std::endl;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
int a[] = {0, 2, 4, 5, 6, 8, 9};
|
|
int b[] = {0, 1, 2, 3, 5, 7, 8};
|
|
|
|
thrust::device_vector<int> A(a, a + sizeof(a) / sizeof(int));
|
|
thrust::device_vector<int> B(b, b + sizeof(b) / sizeof(int));
|
|
|
|
print("Set A", A);
|
|
print("Set B", B);
|
|
|
|
Merge(A, B);
|
|
SetUnion(A, B);
|
|
SetIntersection(A, B);
|
|
SetDifference(A, B);
|
|
SetSymmetricDifference(A, B);
|
|
|
|
SetIntersectionSize(A, B);
|
|
|
|
return 0;
|
|
}
|