
    `i6                         d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ 	 	 ddZ	 	 dd	Z	 	 dd
Zd Zd Zd ZdS )    N)cublas)_dtype)device)_csr)
_interfaceh㈵>c                    t          | |||          \  } }}}| j        }	|j        }
| j        d         }||dz  }|dk    rt          j        |          dfS t          j                            |          }|dk    r|dfS ||t          |          z  }n-t          t          |          |t          |          z            }| |	|          z
  }d}d}||k     r |
|          }|}t          j
        ||          }|dk    r|}n||z  }|||z  z   } |	|          }|t          j
        ||          z  }|||z  z   }|||z  z
  }|dz  }| ||           t          j        |          }||k    rn||k     d}||k    r||k    s|}||fS )a  Uses Conjugate Gradient iteration to solve ``Ax = b``.

    Args:
        A (ndarray, spmatrix or LinearOperator): The real or complex matrix of
            the linear system with shape ``(n, n)``. ``A`` must be a hermitian,
            positive definitive matrix with type of :class:`cupy.ndarray`,
            :class:`cupyx.scipy.sparse.spmatrix` or
            :class:`cupyx.scipy.sparse.linalg.LinearOperator`.
        b (cupy.ndarray): Right hand side of the linear system with shape
            ``(n,)`` or ``(n, 1)``.
        x0 (cupy.ndarray): Starting guess for the solution.
        tol (float): Tolerance for convergence.
        maxiter (int): Maximum number of iterations.
        M (ndarray, spmatrix or LinearOperator): Preconditioner for ``A``.
            The preconditioner should approximate the inverse of ``A``.
            ``M`` must be :class:`cupy.ndarray`,
            :class:`cupyx.scipy.sparse.spmatrix` or
            :class:`cupyx.scipy.sparse.linalg.LinearOperator`.
        callback (function): User-specified function to call after each
            iteration. It is called as ``callback(xk)``, where ``xk`` is the
            current solution vector.
        atol (float): Tolerance for convergence.

    Returns:
        tuple:
            It returns ``x`` (cupy.ndarray) and ``info`` (int) where ``x`` is
            the converged solution and ``info`` provides convergence
            information.

    .. seealso:: :func:`scipy.sparse.linalg.cg`
    r   N
      )_make_systemmatvecshapecupy
empty_likelinalgnormfloatmaxr   dotcnrm2)Abx0tolmaxiterMcallbackatolxr   psolvenb_normritersrhozrho1pbetaqalpharesidinfos                           x/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupyx/scipy/sparse/linalg/_iterative.pycgr/      s   B aB**JAq!QXFXF	
Ab&Avvq!!1$$[a  F{{!t|U6]]"5;;eFmm 344	FF1IIAE
C
'//F1IIk!QA::AA:DD1HAF1IIfk!Q'''	M	M
HQKKKAD==% '//( D$d7N    c
                    t          | |||          \  } }}
}| j        }|j        }| j        d         }|dk    rt          j        |          dfS t          j                            |          }|dk    r|dfS ||t          |          z  }n-t          t          |          |t          |          z            }||dz  }|d}t          ||          }|	d}	|	dvr"t          d                    |	                    |d}	t          j        ||f| j        d	          }t          j        |d
z   |f| j        d	          }t          j        |d
z   f| j                  }t!          |          }d}	  ||
          }| ||          z
  }t#          j        |          }|	dk    r ||           n|	dk    r|dk    r |||z             ||k    s||k    rn||z  }||dddf<   ||d<   t'          |          D ]w} ||          } ||          } |||          \  |d|d
z   |f<   }t#          j        |||d
z   |f                    |d
z   |k     r|||d
z   |f         z  }||dd|d
z   f<   xt          j                            t          j        |          |          }t          j        |d                   }|
||z  z  }
||z  }[d}||k    r||k    s|}||fS )a  Uses Generalized Minimal RESidual iteration to solve ``Ax = b``.

    Args:
        A (ndarray, spmatrix or LinearOperator): The real or complex
            matrix of the linear system with shape ``(n, n)``. ``A`` must be
            :class:`cupy.ndarray`, :class:`cupyx.scipy.sparse.spmatrix` or
            :class:`cupyx.scipy.sparse.linalg.LinearOperator`.
        b (cupy.ndarray): Right hand side of the linear system with shape
            ``(n,)`` or ``(n, 1)``.
        x0 (cupy.ndarray): Starting guess for the solution.
        tol (float): Tolerance for convergence.
        restart (int): Number of iterations between restarts. Larger values
            increase iteration cost, but may be necessary for convergence.
        maxiter (int): Maximum number of iterations.
        M (ndarray, spmatrix or LinearOperator): Preconditioner for ``A``.
            The preconditioner should approximate the inverse of ``A``.
            ``M`` must be :class:`cupy.ndarray`,
            :class:`cupyx.scipy.sparse.spmatrix` or
            :class:`cupyx.scipy.sparse.linalg.LinearOperator`.
        callback (function): User-specified function to call on every restart.
            It is called as ``callback(arg)``, where ``arg`` is selected by
            ``callback_type``.
        callback_type (str): 'x' or 'pr_norm'. If 'x', the current solution
            vector is used as an argument of callback function. if 'pr_norm',
            relative (preconditioned) residual norm is used as an argument.
        atol (float): Tolerance for convergence.

    Returns:
        tuple:
            It returns ``x`` (cupy.ndarray) and ``info`` (int) where ``x`` is
            the converged solution and ``info`` provides convergence
            information.

    Reference:
        M. Wang, H. Klie, M. Parashar and H. Sudan, "Solving Sparse Linear
        Systems on NVIDIA Tesla GPUs", ICCS 2009 (2009).

    .. seealso:: :func:`scipy.sparse.linalg.gmres`
    r   Nr
      pr_norm)r   r3   zUnknown callback_type: {}F)dtypeorderr   r5   Tr   )out)r   r   r   r   r   r   r   r   r   min
ValueErrorformatemptyr5   zerosnumpy_make_compute_hur   r   rangelstsqasnumpyarray)r   r   r   r   restartr   r   r   r   callback_typer   r   r    r!   r"   VHe
compute_hur$   mxr#   r_normvjr&   uretyr-   s                                 r.   gmresrQ   \   sU   R aB**JAq!QXFXF	
AAvvq!!1$$[a  F{{!t|U6]]"5;;eFmm 344b&'1ooG!,,,4;;MJJKKK
Aw<qwc:::A
GAIw'qwcBBBAWQYL000A!!$$JEVAYYr

NQCHRLLLLi''EAIIHVf_%%%T>>Ug--J!!!Q$! w 	 	Aq		Aq		A&Jq!,,MAdqsdAgJKq1ay))))sW}}!A#q&	M!!!QqS&	 l  a!44Js1v	QU
;> D4t8Or0   c                    t          | |||          \  } }}}| j        }	|j        }
| j        d         }|dk    rt          j        |          dfS t          j                            |          }|dk    r|dfS ||t          |          z  }n-t          t          |          |t          |          z            }||dz  }| |	|          z
  }t          j	        ||          }|
                                }|}|
                                }d}	  |
|          } |	|          }t          j	        ||          }||z  }|||z  z
  } |
||z             }|||z  z  } |	|          }|||z  z  }t          j                            |          }|dz  }| ||           ||k    s||k    rn9t          j	        ||          }||z  }|}|||z  z   }||z  }||z  }||z  }||z  }d}||k    r||k     s|}||fS )a  Use Conjugate Gradient Squared iteration to solve ``Ax = b``.

    Args:
        A (ndarray, spmatrix or LinearOperator): The real or complex matrix of
            the linear system with shape ``(n, n)``.
        b (cupy.ndarray): Right hand side of the linear system with shape
            ``(n,)`` or ``(n, 1)``.
        x0 (cupy.ndarray): Starting guess for the solution.
        tol (float): Tolerance for convergence.
        maxiter (int): Maximum number of iterations.
        M (ndarray, spmatrix or LinearOperator): Preconditioner for ``A``.
            The preconditioner should approximate the inverse of ``A``.
            ``M`` must be :class:`cupy.ndarray`,
            :class:`cupyx.scipy.sparse.spmatrix` or
            :class:`cupyx.scipy.sparse.linalg.LinearOperator`.
        callback (function): User-specified function to call after each
            iteration. It is called as ``callback(xk)``, where ``xk`` is the
            current solution vector.
        atol (float): Tolerance for convergence.

    Returns:
        tuple:
            It returns ``x`` (cupy.ndarray) and ``info`` (int) where ``x`` is
            the converged solution and ``info`` provides convergence
            information.

    .. seealso:: :func:`scipy.sparse.linalg.cgs`
    r   N   Tr   )r   r   r   r   r   r   r   r   r   dotcopy)r   r   r   r   r   r   r   r   r   r   r    r!   r"   r0r%   r#   rN   r(   r$   rP   rL   sigmar+   r*   r&   AzrK   rho_newr)   r-   s                                 r.   cgsrZ      sJ   < aB**JAq!QXFXF	
AAvvq!!1$$[a  F{{!t|U6]]"5;;eFmm 344a%	
VVAYYB
(2r

C 			A
A
		AEF1IIF1IIQe	MF1q5MM	UQYVAYY	URZ !!!$$
HQKKKT>>Ug--(2q//}qL	T		Q	T		Q;> D$d7Nr0   c                 Z   t          |           }t          j        |           } |'t          j        | j        || j        | j                  } | j        d         | j        d         k    r't          d                    | j                            | j        j	        dvr't          d                    | j                            | j        d         }|j        |fk    s|j        |dfk    st          d          |                    | j                                                  }|t          j        |f| j        	          }nT|j        |fk    s|j        |dfk    st          d
          |                    | j                                                  }|!t          j        | j        | j                  }nkt          |          }t          j        |          }|'t          j        |j        ||j        |j                  }| j        |j        k    rt          d          | |||fS )a  Make a linear system Ax = b

    Args:
        A (cupy.ndarray or cupyx.scipy.sparse.spmatrix or
            cupyx.scipy.sparse.LinearOperator): sparse or dense matrix.
        M (cupy.ndarray or cupyx.scipy.sparse.spmatrix or
            cupyx.scipy.sparse.LinearOperator): preconditioner.
        x0 (cupy.ndarray): initial guess to iterative method.
        b (cupy.ndarray): right hand side.

    Returns:
        tuple:
            It returns (A, M, x, b).
            A (LinaerOperator): matrix of linear system
            M (LinearOperator): preconditioner
            x (cupy.ndarray): initial guess
            b (cupy.ndarray): right hand side.
    N)r   rmatvecr5   r   r   z"expected square matrix (shape: {})fdFDzunsupprted dtype (actual: {})zb has incompatible dimensionsr7   zx0 has incompatible dimensions)r   r5   z/matrix and preconditioner have different shapes)_make_fast_matvecr   aslinearoperatorLinearOperatorr   r\   r5   r:   r;   char	TypeErrorastyperavelr   r=   IdentityOperator)r   r   r   r   fast_matvecr!   r   s          r.   r   r   +  s   & $A&&K#A&&A%agk./iqwH H HwqzQWQZ=DDQWMMNNNw|6!!7>>qwGGHHH	
AGtOOqw1a&008999	!!A	zJt17+++QD  BHA$6$6=>>>IIag$$&&y'agQWEEE'**'**")!'+23)17L L LA7agNOOOaA:r0   c                   	
 ddl m ddlm t          j        |           r                    d          r}t          j                    j        t          j
        d| j                  	t          j
        d| j                  
t          j        | j                  j        t          j        | j        d         f| j                  }t          j        | j        d         f| j                  }j                            |           j                            |          }j                            |          }                    	j        j        j        |j        
j        j        |j        	  	        }t          j        |t          j                  ~~~~	
f
d}|S d S )Nr   )cusparsespmv      ?        r7   c                 .  
 t          j        |           }
j                            |           }
j                            |          }                    j        j        j        |j        j        j        |j        	j        j        
  
         |S )N)	r   r   DnVecDescriptorcreatespMVctypesdatadescptr)r   rP   desc_xdesc_y	_cusparsealgr+   r)   buff
cuda_dtyperh   desc_Ahandleop_as       r.   r   z!_make_fast_matvec.<locals>.matvect  s    ""A-44Q77F-44Q77FNNel/fk &+z3	O O O Hr0   )cupy_backends.cuda.libsrh   cupyxr   isspmatrix_csrcheck_availabilityr   get_cusparse_handle CUSPARSE_OPERATION_NON_TRANSPOSEr>   rC   r5   r   to_cuda_dtypeCUSPARSE_MV_ALG_DEFAULTr   r<   r   SpMatDescriptorrn   rm   spMV_bufferSizerp   rq   rr   int8)r   r   rP   rt   ru   	buff_sizer   rv   rw   r+   r)   rx   ry   rh   rz   r{   r|   s          @@@@@@@@@@r.   r^   r^   ^  s   ======1 ("="=f"E"E +--9C)){3(()!'22
/J
}AG444J
}AG444)0033)0033)0033--D%,+V[&+Kfk:s< <	 z)TY//vq&	 	 	 	 	 	 	 	 	 	 	 	 	 	 4r0   c                     t          j                     j        j        dk    rt          j        nV j        j        dk    rt          j        n9 j        j        dk    rt          j        n j        j        dk    rt          j         j	        d         t          j        d j                  t          j        d j                  t          j        d j                   fd	}|S )
Nfdr4   Dr   rj   rk   g      c                    t          j        |dz   fj                  } t          j        |dz   j        j        j        j        | j        j        d	j        j        |j        j        d            t          j        |dz   j        j        j        j        |j        j        dj        j        | j        j        d           || fS )Nr   r7   )	r   r<   r5   _cublasCUBLAS_OP_Crp   rq   rs   CUBLAS_OP_N)
rN   rM   hrF   gemvr{   moner!   onezeros
      r.   rI   z$_make_compute_hu.<locals>.compute_hu  s     J!vQW---VW(!QqS#*/16:
At{/Q	@ 	@ 	@VW(!QqS$+2BAFJ
Asz
A	? 	? 	?!tr0   )r   get_cublas_handler5   ra   r   sgemvdgemvcgemvzgemvr   r>   rC   )rF   rI   r   r{   r   r!   r   r   s   ` @@@@@@r.   r?   r?     s    %''Fw|s}	
		}	
		}	
		}	
A
+c17
#
#C;sAG$$D;tQW%%D           r0   )Nr   NNNN)Nr   NNNNNN)r>   r   r   
cupy._corer   	cupy.cudar   r}   r   cupyx.scipy.sparser   cupyx.scipy.sparse.linalgr   r/   rQ   rZ   r   r^   r?    r0   r.   <module>r      s%                       5 5 5 5 5 5 # # # # # # 0 0 0 0 0 0 @DM M M M` BF26l l l l^ AE] ] ] ]@0 0 0f     F    r0   