The more efficient way to compute XSX^T and XSy


The more efficient way to compute XSX^T and XSy



I need to compute the following matrices:
M = XSX^T
and
V = XSy



what I'd like to know is the more efficient implementation using blas, knowing that S is a symmetric and definite positive matrix of dimension n, X has m rows and n columns while y is a vector of length n.



My implementation is the following:
I compute A = XS using dsymm and then with dgemm is obtained M=AX^T while dgemv is used to obtain V=Ay.



I think that at least M can be computed in a more efficient way since I know that M is symmetric and definite positive.




2 Answers
2



Your code is the best BLAS can do for you. There is no BLAS operation, that can exploit the fact that M is symmetric.



You are right though you'd technically only need to compute the upper diagonal part of the gemm product and then copy the strictly upper diagonal part to the lower diagonal part. But there is no routine for that.


gemm



May I inquire about the sizes? And may I also inspire some other sources for performance gains: Own build of your BLAS implementation, comparison with MKL, ACML, OpenBLAS, ATLAS. You could obviously code your own version that would use AVX, FMA intrinsics. You should be able to do better that some generalised library. Also what is the precision of your floating point variable?


AVX


FMA



I seriously doubt that you might gain too much by coding it yourself anyway. But what I would definitely suggest is converting everything to floats and testing if float precision is not giving you the same result with significant speed up in compute time. Very seldom have I seen such cases, which were more in the ODE solving domain and numeric integration of nasty functions.



But you did not address my question regarding the BLAS implementation and machine type.



Again, the optimisation beyond this point is not possible without more skills :(. But seriously, don't be to worried about this. There is a reason, why BLAS does not the optimisation you ask for. It might not be worth the hassle. Go with your solution.



And don't forget to investgate the use of floats rather than double. On R convert everything to float. For the Lapack commands use only sgemX


sgemX





I added a reply to the other answer
– niandra82
Jul 2 at 7:23





I updated my answer :)
– Kaveh Vahedipour
Jul 2 at 11:55





Sorry. As I said I'm not really "skilled" and I'm not sure how to reply. Moreover I don't think it is a useful information because I'm writing a statistical model that is part of an "R" library. Then, there will be different users, with probably different machines and BLAS implementation
– niandra82
Jul 2 at 11:59





Thank you for your answer(s)!
– niandra82
Jul 2 at 12:10



Without knowing the detail of your problem, it can be useful to recognize the zeros in the matrices. Partitioning the matrices to achieve this can provide significant benefits. Is M the sum of many XSX' sub matrices ?



For V = XSy, where y is a vector and X and S are matrices, calculating S.y then X.(Sy) should be better, unless X.S is a necessary calculation for M.





both m and n are around 100, that I know is not much, but S and M must be computed hundreds of thousands of times. X,S and y contains doubles. The elements in general have not a partucular structure, but some times X is on the form x_1 0 0 0 x_2 0 0 0 x_3 where x_i has dimension (1,m_i). P.S. I'm not so skilled to write my own implementation that is better then BLAS
– niandra82
Jul 2 at 7:20







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter