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_sgels.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 
27 #ifndef max
28 #define max(a, b) ((a) > (b) ? (a) : (b))
29 #endif
30 #ifndef min
31 #define min(a, b) ((a) < (b) ? (a) : (b))
32 #endif
33 
34 int check_solution(int, int, int, float*, int, float*, float*, int);
35 
36 int IONE=1;
37 int ISEED[4] = {0,0,0,1}; /* initial seed for slarnv() */
38 
39 int main ()
40 {
41 
42  int cores = 2;
43  int M = 15;
44  int N = 10;
45  int LDA = 15;
46  int NRHS = 5;
47  int LDB = 15;
48 
49  int info;
50  int info_solution;
51  int i,j;
52  int LDAxN = LDA*N;
53  int LDBxNRHS = LDB*NRHS;
54 
55  float *A1 = (float *)malloc(LDA*N*sizeof(float));
56  float *A2 = (float *)malloc(LDA*N*sizeof(float));
57  float *B1 = (float *)malloc(LDB*NRHS*sizeof(float));
58  float *B2 = (float *)malloc(LDB*NRHS*sizeof(float));
59  float *T;
60 
61  /* Check if unable to allocate memory */
62  if ((!A1)||(!A2)||(!B1)||(!B2)){
63  printf("Out of Memory \n ");
64  return EXIT_SUCCESS;
65  }
66 
67  /* Plasma Initialization */
68  PLASMA_Init(cores);
69  printf("-- PLASMA is initialized to run on %d cores. \n",cores);
70 
71  /* Allocate T */
73 
74  /* Initialize A1 and A2 */
75  LAPACKE_slarnv_work(IONE, ISEED, LDAxN, A1);
76  for (i = 0; i < M; i++)
77  for (j = 0; j < N; j++)
78  A2[LDA*j+i] = A1[LDA*j+i] ;
79 
80  /* Initialize B1 and B2 */
81  LAPACKE_slarnv_work(IONE, ISEED, LDBxNRHS, B1);
82  for (i = 0; i < M; i++)
83  for (j = 0; j < NRHS; j++)
84  B2[LDB*j+i] = B1[LDB*j+i] ;
85 
86  /* PLASMA SGELS */
87  info = PLASMA_sgels(PlasmaNoTrans, M, N, NRHS, A2, LDA, T, B2, LDB);
88 
89  /* Check the solution */
90  info_solution = check_solution(M, N, NRHS, A1, LDA, B1, B2, LDB);
91 
92  if ((info_solution != 0)|(info != 0))
93  printf("-- Error in SGELS example ! \n");
94  else
95  printf("-- Run of SGELS example successful ! \n");
96 
97  free(A1); free(A2); free(B1); free(B2); free(T);
98 
100 
101  return EXIT_SUCCESS;
102 }
103 
104 /*--------------------------------------------------------------
105  * Check the solution
106  */
107 
108 int check_solution(int M, int N, int NRHS, float *A1, int LDA, float *B1, float *B2, int LDB)
109 {
110  int info_solution;
111  float Rnorm, Anorm, Xnorm, Bnorm;
112  float alpha, beta;
113  float *work = (float *)malloc(max(M, N)* sizeof(float));
114  float eps;
115 
116  eps = LAPACKE_slamch_work('e');
117 
118  alpha = 1.0;
119  beta = -1.0;
120 
121  Anorm = LAPACKE_slange_work(LAPACK_COL_MAJOR, lapack_const(PlasmaInfNorm), M, N, A1, LDA, work);
122  Xnorm = LAPACKE_slange_work(LAPACK_COL_MAJOR, lapack_const(PlasmaInfNorm), M, NRHS, B2, LDB, work);
123  Bnorm = LAPACKE_slange_work(LAPACK_COL_MAJOR, lapack_const(PlasmaInfNorm), N, NRHS, B1, LDB, work);
124 
125  cblas_sgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, M, NRHS, N, (alpha), A1, LDA, B2, LDB, (beta), B1, LDB);
126 
127  if (M >= N) {
128  float *Residual = (float *)malloc(M*NRHS*sizeof(float));
129  memset((void*)Residual, 0, M*NRHS*sizeof(float));
130  cblas_sgemm(CblasColMajor, CblasTrans, CblasNoTrans, N, NRHS, M, (alpha), A1, LDA, B1, LDB, (beta), Residual, M);
131  Rnorm = LAPACKE_slange_work(LAPACK_COL_MAJOR, lapack_const(PlasmaInfNorm), M, NRHS, Residual, M, work);
132  free(Residual);
133  }
134  else {
135  float *Residual = (float *)malloc(N*NRHS*sizeof(float));
136  memset((void*)Residual, 0, N*NRHS*sizeof(float));
137  cblas_sgemm(CblasColMajor, CblasTrans, CblasNoTrans, N, NRHS, M, (alpha), A1, LDA, B1, LDB, (beta), Residual, N);
138  Rnorm = LAPACKE_slange_work(LAPACK_COL_MAJOR, lapack_const(PlasmaInfNorm), N, NRHS, Residual, N, work);
139  free(Residual);
140  }
141 
142  printf("============\n");
143  printf("Checking the Residual of the solution \n");
144  printf("-- ||Ax-B||_oo/((||A||_oo||x||_oo+||B||)_oo.N.eps) = %e \n",Rnorm/((Anorm*Xnorm+Bnorm)*N*eps));
145 
146  if (isnan(Rnorm / ((Anorm * Xnorm + Bnorm) * N * eps)) || (Rnorm / ((Anorm * Xnorm + Bnorm) * N * eps) > 10.0) ) {
147  printf("-- The solution is suspicious ! \n");
148  info_solution = 1;
149  }
150  else {
151  printf("-- The solution is CORRECT ! \n");
152  info_solution= 0 ;
153  }
154 
155  free(work);
156 
157  return info_solution;
158 }