private static void TestDgemv() { Skip.IfNot(TestSettings.TestMkl, TestSettings.MessageWhenSkippingMKL); CBLAS_LAYOUT layout = CBLAS_LAYOUT.CblasColMajor; CBLAS_TRANSPOSE transA = CBLAS_TRANSPOSE.CblasNoTrans; double alpha = 1.0; double beta = 0.0; int incX = 1; int incY = 1; int m = RectangularFullRank10by5.NumRows; int n = RectangularFullRank10by5.NumCols; int ldA = m; double[] A = Conversions.Array2DToFullColMajor(RectangularFullRank10by5.Matrix); double[] X = RectangularFullRank10by5.Lhs5; double[] Y = new double[m]; LapackePInvokes.Dgemv(layout, transA, m, n, alpha, A, ldA, X, incX, beta, Y, incY); comparer.AssertEqual(RectangularFullRank10by5.Rhs10, Y); }
public static int Dtrsv(CBLAS_UPLO Uplo, CBLAS_TRANSPOSE TransA, CBLAS_DIAG Diag, PZMath_matrix A, PZMath_vector x) { int M = A.RowCount; int N = A.ColumnCount; if (M != N) { PZMath_errno.ERROR("PZMath_blas::Dtrsv(), matrix must be square", PZMath_errno.PZMath_ENOTSQR); } else if (N != x.Size) { PZMath_errno.ERROR("PZMath_blas::Dtrsv(), invalid length", PZMath_errno.PZMath_EBADLEN); } bool nonunit = (Diag == CBLAS_DIAG.CblasNonUnit); int ix, jx; int i, j; CBLAS_TRANSPOSE Trans = (TransA != CBLAS_TRANSPOSE.CblasConjTrans) ? TransA : CBLAS_TRANSPOSE.CblasTrans; CBLAS_ORDER order = CBLAS_ORDER.CblasRowMajor; if (N == 0) return PZMath_errno.PZMath_SUCCESS; /* form x := inv( A )*x */ if ((order == CBLAS_ORDER.CblasRowMajor && Trans == CBLAS_TRANSPOSE.CblasNoTrans && Uplo == CBLAS_UPLO.CblasUpper) || (order == CBLAS_ORDER.CblasColMajor && Trans == CBLAS_TRANSPOSE.CblasTrans && Uplo == CBLAS_UPLO.CblasLower)) { /* backsubstitution */ ix = N - 1; if (nonunit) x[ix] = x[ix] / A[N - 1, N - 1]; ix--; for (i = N - 1; i > 0 && (i-- > 0); ) { double tmp = x[ix]; jx = ix + 1; for (j = i + 1; j < N; j++) { double Aij = A[i, j]; tmp -= Aij * x[jx]; jx++; } if (nonunit) { x[ix] = tmp / A[i, i]; } else { x[ix] = tmp; } ix--; } } else if ((order == CBLAS_ORDER.CblasRowMajor && Trans == CBLAS_TRANSPOSE.CblasNoTrans && Uplo == CBLAS_UPLO.CblasLower) || (order == CBLAS_ORDER.CblasColMajor && Trans == CBLAS_TRANSPOSE.CblasTrans && Uplo == CBLAS_UPLO.CblasUpper)) { /* forward substitution */ ix = 0; if (nonunit) { x[ix] = x[ix] / A[0, 0]; } ix++; for (i = 1; i < N; i++) { double tmp = x[ix]; jx = 0; for (j = 0; j < i; j++) { double Aij = A[i, j]; tmp -= Aij * x[jx]; jx++; } if (nonunit) { x[ix] = tmp / A[i, i]; } else { x[ix] = tmp; } ix++; } } else if ((order == CBLAS_ORDER.CblasRowMajor && Trans == CBLAS_TRANSPOSE.CblasTrans && Uplo == CBLAS_UPLO.CblasUpper) || (order == CBLAS_ORDER.CblasColMajor && Trans == CBLAS_TRANSPOSE.CblasNoTrans && Uplo == CBLAS_UPLO.CblasLower)) { /* form x := inv( A' )*x */ /* forward substitution */ ix = 0; if (nonunit) { x[ix] = x[ix] / A[0, 0]; } ix++; for (i = 1; i < N; i++) { double tmp = x[ix]; jx = 0; for (j = 0; j < i; j++) { double Aji = A[j, i]; tmp -= Aji * x[jx]; jx++; } if (nonunit) { x[ix] = tmp / A[i, i]; } else { x[ix] = tmp; } ix++; } } else if ((order == CBLAS_ORDER.CblasRowMajor && Trans == CBLAS_TRANSPOSE.CblasTrans && Uplo == CBLAS_UPLO.CblasLower) || (order == CBLAS_ORDER.CblasColMajor && Trans == CBLAS_TRANSPOSE.CblasNoTrans && Uplo == CBLAS_UPLO.CblasUpper)) { /* backsubstitution */ ix = N - 1; if (nonunit) { x[ix] = x[ix] / A[(N - 1), (N - 1)]; } ix--; for (i = N - 1; i > 0 && (i-- > 0); ) { double tmp = x[ix]; jx = ix + 1; for (j = i + 1; j < N; j++) { double Aji = A[j, i]; tmp -= Aji * x[jx]; jx++; } if (nonunit) { x[ix] = tmp / A[i, i]; } else { x[ix] = tmp; } ix--; } } else { PZMath_errno.ERROR("PZMath_blas::Dtrsv(), unrecognized operation"); } return PZMath_errno.PZMath_SUCCESS; }
public static extern void Dgemv(CBLAS_LAYOUT layout, CBLAS_TRANSPOSE transA, int m, int n, double alpha, double[] A, int ldA, double[] X, int incX, double beta, double[] Y, int incY); // CBLAS, rather than LAPACKE
public static extern void dMatrixMatrixProduct(CBLAS_TRANSPOSE transa, CBLAS_TRANSPOSE transb, int m, int n, int k, double[] a, double[] b, double alpha, double beta, [In, Out] double[] c);
public static extern void dMatrixVectorProduct(CBLAS_TRANSPOSE trans, int m, int n, double[] a, double alpha, double[] x, double beta, [In, Out] double[] y);
/// <summary> /// generate a 1D matrix from a column vector /// </summary> /// <param name="v"></param> /// <param name="transV"></param> public PZMath_matrix(PZMath_vector v, CBLAS_TRANSPOSE transV) { if (transV == CBLAS_TRANSPOSE.CblasTrans) // column vector { row = v.Size; col = 1; } else // row vector { row = 1; col = v.Size; } offset = 0; stride = 1; orgrow = row; orgcol = col; length = orgrow * orgcol; data = new double[length]; for (int i = 0; i < v.Size; i++) data[i] = v[i]; }
static extern unsafe void cblas_sgemm(CBLAS_ORDER __Order, CBLAS_TRANSPOSE __TransA, CBLAS_TRANSPOSE __TransB, int __M, int __N, int __K, float __alpha, float *__A, int __lda, float *__B, int __ldb, float __beta, float *__C, int __ldc);
private static extern void cblas_dgemm(CBLAS_LAYOUT Layout, CBLAS_TRANSPOSE TransA, CBLAS_TRANSPOSE TransB, int m, int n, int k, double alpha, double* A, int lda, double* B, int ldb, double beta, double* C, int ldc);
private static extern void cblas_sgemm(CBLAS_LAYOUT Layout, CBLAS_TRANSPOSE TransA,CBLAS_TRANSPOSE TransB, int m, int n,int k, float alpha, float* A, int lda, float* B, int ldb, float beta, float* C, int ldc);