Entry-wise multiplication of a sparse matrix on GPU

4 ビュー (過去 30 日間)
Ben Ward
Ben Ward 2017 年 8 月 8 日
コメント済み: Joss Knight 2017 年 8 月 12 日
Using a GPU, I need to multiply sparse matrices using the entrywise ' times' (i.e. .*) operation. As far as I can tell, this is not currently supported on a gpuArray.
For example,
>> rand(5).*sparse(rand(5))
and
>> rand(5,'gpuArray').*rand(5,'gpuArray')
both work, but
>> rand(5,'gpuArray').*sparse(rand(5,'gpuArray'))
Error using .*
Sparse gpuArrays are not supported for this function.
Is there any way I can get around this without converting matrices back to full (which would negate most/all of the advantage of using the GPU).
Thanks

回答 (1 件)

Joss Knight
Joss Knight 2017 年 8 月 8 日
Is the sparsity the same for both matrices?
[I, J, VA] = find(Asparse);
[~, ~, VB] = find(Bsparse);
C = sparse(I, J, VA.*VB);
If you just want to multiply it by itself you can use SPFUN:
Asqr = spfun(@(x)x.^2, Asparse);
If the sparsity is different then you have to merge the indices unfortunately. It's a horrible sequence of operations involving sorting and indexing, and not very efficient which is why it hasn't been implemented. I'm curious as to what your application is - maybe there's another solution that doesn't involve element-wise operations.
  9 件のコメント
Ben Ward
Ben Ward 2017 年 8 月 10 日
編集済み: Ben Ward 2017 年 8 月 10 日
Hi Joss and Jan, Thanks both for your replies.
Joss, I've edited my initial reply to make it clearer for future readers. For reference, 'a real use case' can be found here...
https://en.wikipedia.org/wiki/Matrix_population_models
The core of these models is the mtimes operation, but entry-wise operations will frequently be essential as well. Adding this functionality would therefore be very useful in supporting this kind of research.
thanks again.
Ben
Joss Knight
Joss Knight 2017 年 8 月 12 日
Thanks Ben. Just to reiterate, there's no guarantee a GPU implementation will be faster than the CPU. Don't convert your sparse matrices to dense to work around this issue, gather them to the CPU.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGPU Computing in MATLAB についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by