PLASMA  2.4.5
PLASMA - Parallel Linear Algebra for Scalable Multi-core Architectures
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
example_zgesv.c
Go to the documentation of this file.
1 
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <math.h>
21 
22 #include <plasma.h>
23 #include <cblas.h>
24 #include <lapacke.h>
25 #include <core_blas.h>
26 
28 
29 int IONE=1;
30 int ISEED[4] = {0,0,0,1}; /* initial seed for zlarnv() */
31 
32 int main ()
33 {
34 
35  int cores = 2;
36  int N = 10;
37  int LDA = 10;
38  int NRHS = 5;
39  int LDB = 10;
40  int info;
41  int info_solution;
42  int i,j;
43  int LDAxN = LDA*N;
44  int LDBxNRHS = LDB*NRHS;
45 
46  PLASMA_Complex64_t *A1 = (PLASMA_Complex64_t *)malloc(LDA*N*(sizeof*A1));
47  PLASMA_Complex64_t *A2 = (PLASMA_Complex64_t *)malloc(LDA*N*(sizeof*A2));
48  PLASMA_Complex64_t *B1 = (PLASMA_Complex64_t *)malloc(LDB*NRHS*(sizeof*B1));
49  PLASMA_Complex64_t *B2 = (PLASMA_Complex64_t *)malloc(LDB*NRHS*(sizeof*B2));
51  int *IPIV;
52 
53  /* Check if unable to allocate memory */
54  if ((!A1)||(!A2)||(!B1)||(!B2)){
55  printf("Out of Memory \n ");
56  return EXIT_SUCCESS;
57  }
58 
59  /*Plasma Initialize*/
60  PLASMA_Init(cores);
61  printf("-- PLASMA is initialized to run on %d cores. \n",cores);
62 
63  /* Initialize A1 and A2 Matrix */
64  LAPACKE_zlarnv_work(IONE, ISEED, LDAxN, A1);
65  for ( i = 0; i < N; i++)
66  for ( j = 0; j < N; j++)
67  A2[LDA*j+i] = A1[LDA*j+i];
68 
69  /* Initialize B1 and B2 */
70  LAPACKE_zlarnv_work(IONE, ISEED, LDBxNRHS, B1);
71  for ( i = 0; i < N; i++)
72  for ( j = 0; j < NRHS; j++)
73  B2[LDB*j+i] = B1[LDB*j+i];
74 
75  /* PLASMA ZGESV */
76  info = PLASMA_Alloc_Workspace_zgesv_incpiv(N, &L, &IPIV);
77  info = PLASMA_zgesv_incpiv(N, NRHS, A2, LDA, L, IPIV, B2, LDB);
78 
79  /* Check the factorization and the solution */
80  info_solution = check_solution(N, NRHS, A1, LDA, B1, B2, LDB);
81 
82  if ((info_solution != 0)|(info != 0))
83  printf("-- Error in ZGESV example ! \n");
84  else
85  printf("-- Run of ZGESV example successful ! \n");
86 
87  free(A1); free(A2); free(B1); free(B2); free(IPIV); free(L);
88 
90 
91  return EXIT_SUCCESS;
92 }
93 
94 /*------------------------------------------------------------------------
95  * Check the accuracy of the solution of the linear system
96  */
97 
98 int check_solution(int N, int NRHS, PLASMA_Complex64_t *A1, int LDA, PLASMA_Complex64_t *B1, PLASMA_Complex64_t *B2, int LDB)
99 {
100  int info_solution;
101  double Rnorm, Anorm, Xnorm, Bnorm;
102  PLASMA_Complex64_t alpha, beta;
103  double *work = (double *)malloc(N*sizeof(double));
104  double eps;
105 
106  eps = LAPACKE_dlamch_work('e');
107 
108  alpha = 1.0;
109  beta = -1.0;
110 
111  Xnorm = LAPACKE_zlange_work(LAPACK_COL_MAJOR, lapack_const(PlasmaInfNorm), N, NRHS, B2, LDB, work);
112  Anorm = LAPACKE_zlange_work(LAPACK_COL_MAJOR, lapack_const(PlasmaInfNorm), N, N, A1, LDA, work);
113  Bnorm = LAPACKE_zlange_work(LAPACK_COL_MAJOR, lapack_const(PlasmaInfNorm), N, NRHS, B1, LDB, work);
114 
115  cblas_zgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, N, NRHS, N, CBLAS_SADDR(alpha), A1, LDA, B2, LDB, CBLAS_SADDR(beta), B1, LDB);
116  Rnorm = LAPACKE_zlange_work(LAPACK_COL_MAJOR, lapack_const(PlasmaInfNorm), N, NRHS, B1, LDB, work);
117 
118  printf("============\n");
119  printf("Checking the Residual of the solution \n");
120  printf("-- ||Ax-B||_oo/((||A||_oo||x||_oo+||B||_oo).N.eps) = %e \n",Rnorm/((Anorm*Xnorm+Bnorm)*N*eps));
121 
122  if ( isnan(Rnorm/((Anorm*Xnorm+Bnorm)*N*eps)) || (Rnorm/((Anorm*Xnorm+Bnorm)*N*eps) > 10.0) ){
123  printf("-- The solution is suspicious ! \n");
124  info_solution = 1;
125  }
126  else{
127  printf("-- The solution is CORRECT ! \n");
128  info_solution = 0;
129  }
130 
131  free(work);
132 
133  return info_solution;
134 }