<html><body><div><div>Hi all,<br></div><div><br></div><div>I am using the PETSc and SLEPc to solve the Faddeev equation of baryons. I encounter a problem of function MatCreateDense when changing from CPU to CPU-GPU computations.<br></div><div>At first, I write the codes in purely CPU computation in the following way and it works.<br></div><div>```<br></div></div><div style="background-color:#131314;color:#ebebeb"><pre style="font-family:'DejaVu Sans Mono',monospace;font-size:10.9pt;"><span style="color:#b5b6e3;">Eigen</span>::<span style="color:#b9bcd1;">MatrixXcd </span><span style="color:#ffffff;">H_KER</span><span style="color: rgb(237, 134, 74);"><b>;<br></b></span><span style="color:#b5b6e3;">Eigen</span>::<span style="color:#b9bcd1;">MatrixXcd </span><span style="color:#ffffff;">G0</span><span style="color: rgb(237, 134, 74);"><b>;</b></span></pre><pre style="font-family:'DejaVu Sans Mono',monospace;font-size:10.9pt;">printf(<span style="color:#54b33e;">"</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">Compute the propagator matrix.</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">"</span>)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>prop_matrix_nucleon_sc_av(<span style="color:#ffffff;">Mn</span><span style="color: rgb(237, 134, 74);"><b>, </b></span>pp_nodes<span style="color: rgb(237, 134, 74);"><b>, </b></span>cos1_nodes)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>printf(<span style="color:#54b33e;">"</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">Compute the propagator matrix done.</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">"</span>)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>printf(<span style="color:#54b33e;">"</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">Compute the kernel matrix.</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">"</span>)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>bse_kernel_nucleon_sc_av(<span style="color:#ffffff;">Mn</span><span style="color: rgb(237, 134, 74);"><b>, </b></span>pp_nodes<span style="color: rgb(237, 134, 74);"><b>, </b></span>pp_weights<span style="color: rgb(237, 134, 74);"><b>, </b></span>cos1_nodes<span style="color: rgb(237, 134, 74);"><b>, </b></span>cos1_weights)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>printf(<span style="color:#54b33e;">"</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">Compute the kernel matrix done.</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">"</span>)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>printf(<span style="color:#54b33e;">"</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">Compute the full kernel matrix by multiplying kernel and propagator matrix.</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">"</span>)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>MatrixXcd <span style="color:#ffffff;">kernel_temp </span>= H_KER * G0<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>printf(<span style="color:#54b33e;">"</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">Compute the full kernel matrix done.</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">"</span>)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span><span style="color: rgb(237, 134, 74);"><b><br></b></span><span style="color:#7ec3e6;">// Solve the eigen system with SLEPc<br></span>printf(<span style="color:#54b33e;">"</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">Solve the eigen system in the rest frame.</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">"</span>)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span><span style="color:#7ec3e6;">// Get the size of the Eigen matrix<br></span><span style="color:#ed864a;">int </span><span style="color:#ffffff;">nRows </span>= (<span style="color:#ed864a;">int</span>) kernel_temp.<span style="color:#ffffff;">rows</span>()<span style="color: rgb(237, 134, 74);"><b>;<br></b></span><span style="color:#ed864a;">int </span><span style="color:#ffffff;">nCols </span>= (<span style="color:#ed864a;">int</span>) kernel_temp.<span style="color:#ffffff;">cols</span>()<span style="color: rgb(237, 134, 74);"><b>;</b></span></pre><pre style="font-family:'DejaVu Sans Mono',monospace;font-size:10.9pt;"><span style="color:#7ec3e6;">// Create PETSc matrix and share the data of kernel_temp<br></span>Mat <span style="color:#ffffff;">kernel</span><span style="color: rgb(237, 134, 74);"><b>;<br></b></span>PetscCall(MatCreateDense(PETSC_COMM_WORLD<span style="color: rgb(237, 134, 74);"><b>, </b></span>PETSC_DECIDE<span style="color: rgb(237, 134, 74);"><b>, </b></span>PETSC_DECIDE<span style="color: rgb(237, 134, 74);"><b>, </b></span>nRows<span style="color: rgb(237, 134, 74);"><b>, </b></span>nCols<span style="color: rgb(237, 134, 74);"><b>, </b></span>kernel_temp.data()<span style="color: rgb(237, 134, 74);"><b>, </b></span>&kernel))<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>PetscCall(MatAssemblyBegin(kernel<span style="color: rgb(237, 134, 74);"><b>, </b></span>MAT_FINAL_ASSEMBLY))<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>PetscCall(MatAssemblyEnd(kernel<span style="color: rgb(237, 134, 74);"><b>, </b></span>MAT_FINAL_ASSEMBLY))<span style="color: rgb(237, 134, 74);"><b>;</b></span></pre></div><div>```<br></div><div>Now I change to compute the propagator and kernel matrices in GPU and then compute the largest eigenvalues in CPU using SLEPc in the ways below.<br></div><div>```<br></div><div style="background-color:#131314;color:#ebebeb"><pre style="font-family:'DejaVu Sans Mono',monospace;font-size:10.9pt;"><span style="color:#b9bcd1;">cuDoubleComplex </span>*<span style="color:#ffffff;">h_propmat</span><span style="color: rgb(237, 134, 74);"><b>;<br></b></span><span style="color:#b9bcd1;">cuDoubleComplex </span>*<span style="color:#ffffff;">h_kernelmat</span><span style="color: rgb(237, 134, 74);"><b>;</b></span></pre><pre style="font-family:'DejaVu Sans Mono',monospace;font-size:10.9pt;"><span style="color:#ed864a;">int </span><span style="color:#ffffff;">dim </span>= <span style="color:#908b25;">EIGHT </span>* <span style="color:#ffffff;">NP </span>* <span style="color:#ffffff;">NZ</span><span style="color: rgb(237, 134, 74);"><b>;<br></b></span>printf(<span style="color:#54b33e;">"</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">Compute the propagator matrix.</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">"</span>)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>prop_matrix_nucleon_sc_av_cuda(<span style="color:#ffffff;">Mn</span><span style="color: rgb(237, 134, 74);"><b>, </b></span><span style="color:#ffffff;">pp_nodes</span>.data()<span style="color: rgb(237, 134, 74);"><b>, </b></span><span style="color:#ffffff;">cos1_nodes</span>.data())<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>printf(<span style="color:#54b33e;">"</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">Compute the propagator matrix done.</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">"</span>)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>printf(<span style="color:#54b33e;">"</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">Compute the kernel matrix.</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">"</span>)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>kernel_matrix_nucleon_sc_av_cuda(<span style="color:#ffffff;">Mn</span><span style="color: rgb(237, 134, 74);"><b>, </b></span><span style="color:#ffffff;">pp_nodes</span>.data()<span style="color: rgb(237, 134, 74);"><b>, </b></span><span style="color:#ffffff;">pp_weights</span>.data()<span style="color: rgb(237, 134, 74);"><b>, </b></span><span style="color:#ffffff;">cos1_nodes</span>.data()<span style="color: rgb(237, 134, 74);"><b>, </b></span><span style="color:#ffffff;">cos1_weights</span>.data())<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>printf(<span style="color:#54b33e;">"</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">Compute the kernel matrix done.</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">"</span>)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>printf(<span style="color:#54b33e;">"</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">Compute the full kernel matrix by multiplying kernel and propagator matrix.</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">"</span>)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span><span style="color:#7ec3e6;">// Map the raw arrays to Eigen matrices (column-major order)<br></span><span style="color:#ed864a;">auto </span>*<span style="color:#ffffff;">h_kernel_temp </span>= <span style="color:#ed864a;">new </span><span style="color:#b9bcd1;">cuDoubleComplex </span>[<span style="color:#ffffff;">dim</span>*<span style="color:#ffffff;">dim</span>]<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>matmul_cublas_cuDoubleComplex(<span style="color:#ffffff;">h_kernelmat</span><span style="color: rgb(237, 134, 74);"><b>,</b></span><span style="color:#ffffff;">h_propmat</span><span style="color: rgb(237, 134, 74);"><b>,</b></span><span style="color:#ffffff;">h_kernel_temp</span><span style="color: rgb(237, 134, 74);"><b>,</b></span><span style="color:#ffffff;">dim</span><span style="color: rgb(237, 134, 74);"><b>,</b></span><span style="color:#ffffff;">dim</span><span style="color: rgb(237, 134, 74);"><b>,</b></span><span style="color:#ffffff;">dim</span>)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span>printf(<span style="color:#54b33e;">"</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">Compute the full kernel matrix done.</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">"</span>)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span><span style="color: rgb(237, 134, 74);"><b><br></b></span><span style="color:#7ec3e6;">// Solve the eigen system with SLEPc<br></span>printf(<span style="color:#54b33e;">"</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">Solve the eigen system in the rest frame.</span><span style="color:#ed864a;">\n</span><span style="color:#54b33e;">"</span>)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span><span style="color:#ed864a;">int </span><span style="color:#ffffff;">nRows </span>= <span style="color:#ffffff;">dim</span><span style="color: rgb(237, 134, 74);"><b>;<br></b></span><span style="color:#ed864a;">int </span><span style="color:#ffffff;">nCols </span>= <span style="color:#ffffff;">dim</span><span style="color: rgb(237, 134, 74);"><b>;</b></span></pre><pre style="font-family:'DejaVu Sans Mono',monospace;font-size:10.9pt;"><span style="color:#7ec3e6;">// Create PETSc matrix and share the data of kernel_temp<br></span><span style="color:#b9bcd1;">Mat </span>kernel<span style="color: rgb(237, 134, 74);"><b>;<br></b></span><span style="color:#ed864a;">auto</span>* <span style="color:#ffffff;">h_kernel </span>= (<span style="color:#b5b6e3;">std</span>::<span style="color:#b5b6e3;">complex</span><<span style="color:#ed864a;">double</span>>*)(<span style="color:#ffffff;">h_kernel_temp</span>)<span style="color: rgb(237, 134, 74);"><b>;<br></b></span><span style="color:#908b25;">PetscCall</span>(MatCreateDense(<span style="color:#ffffff;">PETSC_COMM_WORLD</span><span style="color: rgb(237, 134, 74);"><b>, </b></span>PETSC_DECIDE<span style="color: rgb(237, 134, 74);"><b>, </b></span>PETSC_DECIDE<span style="color: rgb(237, 134, 74);"><b>, </b></span><span style="color:#ffffff;">nRows</span><span style="color: rgb(237, 134, 74);"><b>, </b></span><span style="color:#ffffff;">nCols</span><span style="color: rgb(237, 134, 74);"><b>, </b></span><span style="color:#ffffff;">h_kernel_temp</span><span style="color: rgb(237, 134, 74);"><b>, </b></span>&<span style="color:#ffffff;">kernel</span>))<span style="color: rgb(237, 134, 74);"><b>;<br></b></span><span style="color:#908b25;">PetscCall</span>(MatAssemblyBegin(<span style="color:#ffffff;">kernel</span><span style="color: rgb(237, 134, 74);"><b>, </b></span><span style="color: rgb(237, 148, 255);"><i>MAT_FINAL_ASSEMBLY</i></span>))<span style="color: rgb(237, 134, 74);"><b>;<br></b></span><span style="color:#908b25;">PetscCall</span>(MatAssemblyEnd(<span style="color:#ffffff;">kernel</span><span style="color: rgb(237, 134, 74);"><b>, </b></span><span style="color: rgb(237, 148, 255);"><i>MAT_FINAL_ASSEMBLY</i></span>))<span style="color: rgb(237, 134, 74);"><b>;</b></span></pre></div><div>But in this case, the compiler told me that the MatCreateDense function uses the data pointer as type of "thrust::complex<double>" instead of "std::complex<double>".<br></div><div>I am sure I only configured and install PETSc in purely CPU without GPU and this codes are written in the host function.<br></div><div>Why the function changes its behavior? Did you also meet this problem when writing the cuda codes and how to solve this problem.<br></div><div>I tried to copy the data to a new thrust::complex<double> matrix but this is very time consuming since my matrix is very big. Is there a way to create the Mat from the original data without changing the data type to thrust::complex<double> in the cuda applications? Any response will be appreciated. Thank you!<br></div><div><br></div><div>Best wishes,<br></div><div>Langtian Liu<br></div><div><br></div><div>------<br></div><div>Institute for Theorectical Physics, Justus-Liebig-University Giessen<br></div><div> Heinrich-Buff-Ring 16, 35392 Giessen Germany<br></div><div><br></div></body></html>