imate
C++/CUDA Reference
lapack_api.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright 2021, Siavash Ameli <sameli@berkeley.edu>
3  * SPDX-License-Identifier: BSD-3-Clause
4  * SPDX-FileType: SOURCE
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the license found in the LICENSE.txt file in the root
8  * directory of this source tree.
9  */
10 
11 
12 // =======
13 // Headers
14 // =======
15 
16 #include "./lapack_api.h"
17 #include <cstddef> // NULL
18 
19 
20 // ============
21 // lapack xstev (float specialization)
22 // ============
23 
27 
28 template<>
29 void lapack_xstev<float>(char* jobz, int* n, float* d, float* e, float* z,
30  int* ldz, float* work, int* info)
31 {
32  // Calling float method
33  lapack_sstev(jobz, n, d, e, z, ldz, work, info);
34 }
35 
36 
37 // ============
38 // lapack xstev (double specialization)
39 // ============
40 
44 
45 template<>
46 void lapack_xstev<double>(char* jobz, int* n, double* d, double* e, double* z,
47  int* ldz, double* work, int* info)
48 {
49  // Calling double method
50  lapack_dstev(jobz, n, d, e, z, ldz, work, info);
51 }
52 
53 // ============
54 // lapack xstev (long double specialization)
55 // ============
56 
65 
66 template<>
67 void lapack_xstev<long double>(char* jobz, int* n, long double* d,
68  long double* e, long double* z, int* ldz,
69  long double* work, int* info)
70 {
71  // Mark unused variables to avoid compiler warnings (-Wno-unused-parameter)
72  (void) work;
73 
74  // Deep copy long double diagonal array to double
75  double *d_ = new double[(*n)];
76  for (int i=0; i < (*n); ++i)
77  {
78  d_[i] = static_cast<double>(d[i]);
79  }
80 
81  // Deep copy long double supdiagonal array to double
82  double *e_ = new double[(*n)-1];
83  for (int i=0; i < (*n)-1; ++i)
84  {
85  e_[i] = static_cast<double>(e[i]);
86  }
87 
88  // Declare eigenvectors and work arrays as double
89  double *z_ = new double[(*ldz)*(*n)];
90  double *work_ = new double[2*(*n)-2];
91 
92  // Calling double method
93  lapack_dstev(jobz, n, d_, e_, z_, ldz, work_, info);
94 
95  // Copy eigenvalues from double to long double
96  for (int i=0; i < (*n); ++i)
97  {
98  d[i] = static_cast<long double>(d_[i]);
99  }
100 
101  // Copy eigenvectors from double to long double
102  for (int i=0; i < (*ldz)*(*n); ++i)
103  {
104  z[i] = static_cast<long double>(z_[i]);
105  }
106 
107  // Deallocate memory
108  delete[] d_;
109  delete[] e_;
110  delete[] z_;
111  delete[] work_;
112 }
113 
114 
115 // =============
116 // lapack xbdsdc (float specialization)
117 // =============
118 
122 
123 template<>
124 void lapack_xbdsdc<float>(char* uplo, char* compq, int* n, float* d, float *e,
125  float* u, int* ldu, float* vt, int* ldvt, float* q,
126  int* iq, float* work, int* iwork, int* info)
127 {
128  lapack_sbdsdc(uplo, compq, n, d, e, u, ldu, vt, ldvt, q, iq, work, iwork,
129  info);
130 }
131 
132 
133 // =============
134 // lapack xbdsdc (double specialization)
135 // =============
136 
140 
141 template<>
142 void lapack_xbdsdc<double>(char* uplo, char* compq, int* n, double* d,
143  double *e, double* u, int* ldu, double* vt,
144  int* ldvt, double* q, int* iq, double* work,
145  int* iwork, int* info)
146 {
147  lapack_dbdsdc(uplo, compq, n, d, e, u, ldu, vt, ldvt, q, iq, work, iwork,
148  info);
149 }
150 
151 
152 // =============
153 // lapack xbdsdc (long double specialization)
154 // =============
155 
164 
165 template<>
166 void lapack_xbdsdc<long double>(char* uplo, char* compq, int* n,
167  long double* d, long double *e, long double* u,
168  int* ldu, long double* vt, int* ldvt,
169  long double* q, int* iq, long double* work,
170  int* iwork, int* info)
171 {
172  // Mark unused variables to avoid compiler warnings (-Wno-unused-parameter)
173  (void) q;
174  (void) work;
175 
176  // Deep copy long double diagonal array to double
177  double *d_ = new double[(*n)];
178  for (int i=0; i < (*n); ++i)
179  {
180  d_[i] = static_cast<double>(d[i]);
181  }
182 
183  // Deep copy long double supdiagonal array to double
184  double *e_ = new double[(*n)-1];
185  for (int i=0; i < (*n)-1; ++i)
186  {
187  e_[i] = static_cast<double>(e[i]);
188  }
189 
190  // Declare left and right eigenvectors arrays
191  double *u_ = new double[(*ldu)*(*n)];
192  double *vt_ = new double[(*ldvt)*(*n)];
193 
194  // Declare work variables
195  double* q_ = NULL;
196  double *work_ = new double[3*(*n)*(*n) + 4*(*n)];
197 
198  // Call lapack
199  lapack_dbdsdc(uplo, compq, n, d_, e_, u_, ldu, vt_, ldvt, q_, iq, work_,
200  iwork, info);
201 
202  // Copy back eigenvectors from double to long double
203  for (int i=0; i < (*n); ++i)
204  {
205  d[i] = static_cast<long double>(d_[i]);
206  }
207 
208  // Copy left and right eigenvectors fom double to long double
209  for (int i=0; i < (*ldu)*(*n); ++i)
210  {
211  u[i] = static_cast<long double>(u_[i]);
212  }
213 
214  for (int i=0; i < (*ldvt)*(*n); ++i)
215  {
216  vt[i] = static_cast<long double>(vt_[i]);
217  }
218 
219  // Deallocate memory
220  delete[] d_;
221  delete[] e_;
222  delete[] u_;
223  delete[] vt_;
224  delete[] work_;
225 }
void lapack_xstev< float >(char *jobz, int *n, float *d, float *e, float *z, int *ldz, float *work, int *info)
Overlodng wrapper for both lapack_sstev (a float function) and lapack_dstev (a double function)....
Definition: lapack_api.cpp:29
void lapack_xbdsdc< long double >(char *uplo, char *compq, int *n, long double *d, long double *e, long double *u, int *ldu, long double *vt, int *ldvt, long double *q, int *iq, long double *work, int *iwork, int *info)
Overlodng wrapper for both lapack_sbdsdc (a double function) and lapack_dbdsdc (a double function)....
Definition: lapack_api.cpp:166
void lapack_xbdsdc< double >(char *uplo, char *compq, int *n, double *d, double *e, double *u, int *ldu, double *vt, int *ldvt, double *q, int *iq, double *work, int *iwork, int *info)
Overlodng wrapper for both lapack_sbdsdc (a double function) and lapack_dbdsdc (a double function)....
Definition: lapack_api.cpp:142
void lapack_xbdsdc< float >(char *uplo, char *compq, int *n, float *d, float *e, float *u, int *ldu, float *vt, int *ldvt, float *q, int *iq, float *work, int *iwork, int *info)
Overlodng wrapper for both lapack_sbdsdc (a float function) and lapack_dbdsdc (a double function)....
Definition: lapack_api.cpp:124
void lapack_xstev< double >(char *jobz, int *n, double *d, double *e, double *z, int *ldz, double *work, int *info)
Overlodng wrapper for both lapack_sstev (a float function) and lapack_dstev (a double function)....
Definition: lapack_api.cpp:46
void lapack_xstev< long double >(char *jobz, int *n, long double *d, long double *e, long double *z, int *ldz, long double *work, int *info)
Overlodng wrapper for both lapack_sstev (a float function) and lapack_dstev (a double function)....
Definition: lapack_api.cpp:67
void lapack_sbdsdc(char *uplo, char *compq, int *n, float *d, float *e, float *u, int *ldu, float *vt, int *ldvt, float *q, int *iq, float *work, int *iwork, int *info)
void lapack_dstev(char *jobz, int *n, double *d, double *e, double *z, int *ldz, double *work, int *info)
void lapack_dbdsdc(char *uplo, char *compq, int *n, double *d, double *e, double *u, int *ldu, double *vt, int *ldvt, double *q, int *iq, double *work, int *iwork, int *info)