2

I am trying to use a reduction algorithm like thrust::reduce for a sequence of matrices. Let's say I want to do the product of N matrices: A1A2....*AN. I think a reduction algorithm would be great because the product is associative. Is it possible to use thrust::reduce for this?

4
  • 2
    This is certainly possible, but whether this will be efficient is another story. Indeed each matrices is stored contiguously and threads should do coalesced load for good performance. Besides, IDK what thrust uses internally but a log2 reduction tree should be inefficient for matrices that are not very small. If the matrices are pretty big, it is better to just execute N BLAS kernels (though it is certainly a bit less numerically stable). If the matrices are neither big nor pretty small, then doing this efficiently is complicated. More information is needed for more details about this. Commented Feb 21 at 18:02
  • @JérômeRichard thanks for the response. The matrices I am handling are between 100 - 1000 size. Do you think it would be better to just use cuBLAS for each product? How would you implement it? Commented Feb 21 at 18:11
  • I think this is typically the use case where it is "complicated" unfortunately. In this case, the matrices are certainly not big enough for cuBlas to be efficient and too big for thrust reduction to be also efficient. I expect cuBlas to be faster than a naive thrust reduction. You can start with cuBlas and if this is not efficient enough (you can profile this to be sure), then I advise to write your own kernel for that. AFAIK, CuBlas fortunately supports batched matrix multiplication so you can do the first reduction step efficiently with it. For the last, maybe it does not matter much. Commented Feb 21 at 19:39
  • "I think a reduction algorithm would be great because the product is associative." Generally reductions assume commutativity. See this issue which is still open. Commented Feb 21 at 21:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.