topic Block sparse matrix with sparse matrix in IntelĀ® oneAPI Math Kernel Library
https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Block-sparse-matrix-with-sparse-matrix/m-p/1179199#M29197
<P>I have a sparse matrix M, and a sparse matrix K, both of the sparse matrices obtain the same non-zeros location, how to form a sparse matrix with following format:</P><P>A= [ M, 0.2K; 0.1M, 0.1K];</P><P>This was used to solve linear equation with Ax = b.</P><P>Thanks a lot.</P><P> </P>Wed, 19 Dec 2018 13:06:02 GMTLi__Yapeng2018-12-19T13:06:02ZBlock sparse matrix with sparse matrix
https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Block-sparse-matrix-with-sparse-matrix/m-p/1179199#M29197
<P>I have a sparse matrix M, and a sparse matrix K, both of the sparse matrices obtain the same non-zeros location, how to form a sparse matrix with following format:</P><P>A= [ M, 0.2K; 0.1M, 0.1K];</P><P>This was used to solve linear equation with Ax = b.</P><P>Thanks a lot.</P><P> </P>Wed, 19 Dec 2018 13:06:02 GMThttps://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Block-sparse-matrix-with-sparse-matrix/m-p/1179199#M29197Li__Yapeng2018-12-19T13:06:02ZYapeng,
https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Block-sparse-matrix-with-sparse-matrix/m-p/1179200#M29198
<P>Yapeng,</P><P>Please explain "<EM>obtain the same non-zeros location</EM>". Obtain them from where? Or do you mean the matrices have non-zero values in the same row,column positions?</P><P>Also, please identify or explain the format you are using.</P><BLOCKQUOTE><P>A= [ M, 0.2K; 0.1M, 0.1K];</P></BLOCKQUOTE><P>Pamela</P>Thu, 20 Dec 2018 18:29:21 GMThttps://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Block-sparse-matrix-with-sparse-matrix/m-p/1179200#M29198Pamela_H_Intel2018-12-20T18:29:21ZSo, this is a sort of tricky
https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Block-sparse-matrix-with-sparse-matrix/m-p/1179201#M29199
<P>So, this is a sort of a tricky case to do with existing functionality, you could probably use the mkl_sparse_?_add functionality, but that would require you to call it 3 times and adjust some arrays as you go with their offsets. I would probably instead write if from scratch. Maybe something like the following:</P><P>Two steps to do full computation. I will assume you are using CSR format to represent the matrices. For this to make sense, M and K must have the same number of rows, but columns can be different, so suppose M is nxm and K is nxk., then your new sparse matrix has global dimensions (nrows=2n) x (ncols=m+k). Let M be represented by Mrowptr, Mcolumns and Mvalues arrays, and K be represented by Krowptr, Kcolumns, Kvalues arrays. Finally let rowptr, columns and values arrays be the CSR representation for the new A matrix.</P><P>1. Construct new rowptr array and initialize memory for new matrix. </P><P> Let us consider the number of sparse elements of first row (same as the m+1 row). We will use 0 based (c-style) indexing in our arrays. The the first row of new matrix has Mrowptr[1]-Mrowptr[0] + Krowptr[1]-Krowptr[0] nonzero elements. then rowptr[0] = 0 and rowptr[1] = Mrowptr[1]-Mrowptr[0] + Krowptr[1]-Krowptr[0]</P><P>You can go through and calculate new row lengths independently of each other. So if using parallelism, this is a simple parallel for loop (OMP or TBB) otherwise it is just a for loop. I would recommend filling in each value with the number of nonzero elements on that row. Then you do a second pass to calculate the prefix sum (partial sums increasing up to the end) of this rowptr array. Once this is done, you know how big the columns arrays need to be and where each row can start. Now allocate memory for the new columns and values arrays.</P><P>2. Now you can fill in proper values of the column and values arrays. </P><P>Again this can be done independently on each row so a simple (parallel) for loop will handle the computations. For columns of M being copied, there are no offsets, it can be copied as is, but for the columns of K, you need to offset (read this as add) by m (the number of columns in M) to shift it to the right blocks. The values for each row can be copied and scaled as desired to match your 4 blocks, one row at a time...</P><P>I hope that was detailed enough for you to see how to do it. Best of luck!</P><P>Spencer</P><P> </P>Thu, 20 Dec 2018 19:01:59 GMThttps://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Block-sparse-matrix-with-sparse-matrix/m-p/1179201#M29199Spencer_P_Intel2018-12-20T19:01:59Z