Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reduce_each example #697

Open
jaredhoberock opened this issue Jan 29, 2015 · 1 comment
Open

Add reduce_each example #697

jaredhoberock opened this issue Jan 29, 2015 · 1 comment
Labels
thrust For all items related to Thrust.

Comments

@jaredhoberock
Copy link

This code for summing matrix rows is appropriate and simple:

#include <thrust/tabulate.h>
#include <thrust/reduce.h>
#include <thrust/tabulate.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h> // needed for thrust::device
#include <thrust/copy.h>
#include <thrust/sequence.h>
#include <iterator>

struct reduce_functor
{
  thrust::device_ptr<float> matrix;
  int width;

  __host__ __device__
  float operator()(int row_index)
  {
    auto begin = matrix + width * row_index;
    return thrust::reduce(thrust::device, begin, begin + width, 0.0f);
  }
};

template<class Vector>
void print_matrix(const Vector& mtx, int width)
{
  auto num_rows = mtx.size() / width;
  for(auto row = 0; row < num_rows; ++row)
  {
    std::cout << "|";
    auto begin = mtx.begin() + row * width;
    thrust::copy(begin, begin + width, std::ostream_iterator<typename Vector::value_type>(std::cout, " "));
    std::cout << "|" << std::endl;
  }
}

int main()
{
  int width = 10;
  thrust::device_vector<float> matrix(width * width);

  // fill the matrix with ascending values
  thrust::sequence(matrix.begin(), matrix.end());

  auto f = reduce_functor{matrix.data(), width};

  thrust::device_vector<float> row_sums(width);

  // call the reduce_functor on the indices of the rows of matrix
  // this reduces each row of the matrix to a single float containing the row's sum
  thrust::tabulate(row_sums.begin(), row_sums.end(), f);

  std::cout << "matrix: " << std::endl;
  print_matrix(matrix, width);
  std::cout << std::endl;

  std::cout << "row sums:" << std::endl;
  print_matrix(row_sums, 1);
  std::cout << std::endl;

  return 0;
}
@judfs
Copy link

judfs commented May 31, 2019

This seems simpler than the thrust solutions at http://www.orangeowlsolutions.com/archives/1239 -> https://github.com/OrangeOwlSolutions/Thrust/blob/master/Reduce_rows.cu . (ie, what you get googling "cuda matrix reduction"). Shame this tabulate approach is buried.
(Though it's a shame there is no native convince function for reducing matrix rows. Also for "sum" reduction, a blas vector multiply is still 10x faster.)

@jrhemstad jrhemstad added the thrust For all items related to Thrust. label Feb 22, 2023
@miscco miscco self-assigned this Feb 23, 2023
@jarmak-nv jarmak-nv transferred this issue from NVIDIA/thrust Nov 8, 2023
@miscco miscco removed their assignment Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
thrust For all items related to Thrust.
Projects
Status: No status
Development

No branches or pull requests

4 participants