We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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; }
The text was updated successfully, but these errors were encountered:
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.)
Sorry, something went wrong.
No branches or pull requests
This code for summing matrix rows is appropriate and simple:
The text was updated successfully, but these errors were encountered: