GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
CBLAS_wrapper_blas_level_1.c
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * MODULE: Grass numerical math interface
4  * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
5  * soerengebbert <at> googlemail <dot> com
6  *
7  * PURPOSE: grass blas implementation
8  * part of the gmath library
9  *
10  * COPYRIGHT: (C) 2010 by the GRASS Development Team
11  *
12  * This program is free software under the GNU General Public
13  * License (>=v2). Read the file COPYING that comes with GRASS
14  * for details.
15  *
16  *****************************************************************************/
17 
18 #include <math.h>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <grass/gmath.h>
23 
24 #if defined(HAVE_LIBBLAS)
25 #if defined(HAVE_CBLAS_ATLAS_H)
26 #include <cblas-atlas.h>
27 #else
28 #include <cblas.h>
29 #endif
30 #endif /* HAVE_LIBBLAS */
31 
32 /*!
33  * \brief Compute the dot product of vector x and y
34  * using the CBLAS routine cblas_ddot
35  *
36  * If grass was not compiled with CBLAS support
37  * it will call #G_math_f_x_dot_y, the OpenMP multi threaded
38  * grass implementatiom
39  *
40  * \param x (float *)
41  * \param y (float *)
42  * \param rows (int)
43  * \return (double)
44  *
45  * */
46 double G_math_ddot(double *x, double *y, int rows)
47 {
48 #if defined(HAVE_LIBBLAS)
49  return cblas_ddot(rows, x, 1, y, 1);
50 #else
51  double val;
52 
53  G_math_d_x_dot_y(x, y, &val, rows);
54  return val;
55 #endif
56 }
57 
58 /*!
59  * \brief Compute the dot product of vector x and y
60  * using the CBLAS routine cblas_sdsdot
61  *
62  * If grass was not compiled with CBLAS support
63  * it will call #G_math_f_x_dot_y, the OpenMP multi threaded
64  * grass implementatiom
65  *
66  * \param x (float *)
67  * \param y (float *)
68  * \param a (float)
69  * \param rows (int)
70  * \return (float)
71  *
72  * */
73 float G_math_sdsdot(float *x, float *y, float a, int rows)
74 {
75 #if defined(HAVE_LIBBLAS)
76  return cblas_sdsdot(rows, a, x, 1, y, 1);
77 #else
78  float val;
79 
80  G_math_f_x_dot_y(x, y, &val, rows);
81  return a + val;
82 #endif
83 }
84 
85 /*!
86  * \brief Compute the euclidean norm of vector x
87  * using the CBLAS routine cblas_dnrm2
88  *
89  * If grass was not compiled with CBLAS support
90  * it will call #G_math_d_euclid_norm, the OpenMP multi threaded
91  * grass implementatiom
92  *
93  * \param x (double *)
94  * \param rows (int)
95  * \return (double)
96  *
97  * */
98 double G_math_dnrm2(double *x, int rows)
99 {
100 #if defined(HAVE_LIBBLAS)
101  return cblas_dnrm2(rows, x, 1);
102 #else
103  double val;
104 
105  G_math_d_euclid_norm(x, &val, rows);
106  return val;
107 #endif
108 }
109 
110 /*!
111  * \brief Compute the absolute sum norm of vector x
112  * using the CBLAS routine cblas_dasum
113  *
114  * If grass was not compiled with CBLAS support
115  * it will call #G_math_d_asum_norm, the OpenMP multi threaded
116  * grass implementatiom
117  *
118  * \param x (double *)
119  * \param rows (int)
120  * \return (double)
121  *
122  * */
123 double G_math_dasum(double *x, int rows)
124 {
125 #if defined(HAVE_LIBBLAS)
126  return cblas_dasum(rows, x, 1);
127 #else
128  double val;
129 
130  G_math_d_asum_norm(x, &val, rows);
131  return val;
132 #endif
133 }
134 
135 /*!
136  * \brief Compute the maximum norm of vector x
137  * using the CBLAS routine cblas_idamax
138  *
139  * If grass was not compiled with CBLAS support
140  * it will call #G_math_d_max_norm, the OpenMP multi threaded
141  * grass implementatiom
142  *
143  * \param x (double *)
144  * \param rows (int)
145  * \return (double)
146  *
147  * */
148 double G_math_idamax(double *x, int rows)
149 {
150 #if defined(HAVE_LIBBLAS)
151  return cblas_idamax(rows, x, 1);
152 #else
153  double val;
154 
155  G_math_d_max_norm(x, &val, rows);
156  return val;
157 #endif
158 }
159 
160 /*!
161  * \brief Scale vector x with scalar a
162  * using the CBLAS routine cblas_dscal
163  *
164  * If grass was not compiled with CBLAS support
165  * it will call #G_math_d_ax_by, the OpenMP multi threaded
166  * grass implementatiom
167  *
168  * \param x (double *)
169  * \param a (double)
170  * \param rows (int)
171  * \return (void)
172  *
173  * */
174 void G_math_dscal(double *x, double a, int rows)
175 {
176 #if defined(HAVE_LIBBLAS)
177  cblas_dscal(rows, a, x, 1);
178 #else
179  G_math_d_ax_by(x, x, x, a, 0.0, rows);
180 #endif
181 
182  return;
183 }
184 
185 /*!
186  * \brief Copy vector x to vector y
187  *
188  * If grass was not compiled with CBLAS support
189  * it will call #G_math_d_copy
190  *
191  * \param x (double *)
192  * \param y (double *)
193  * \param rows (int)
194  * \return (void)
195  *
196  * */
197 void G_math_dcopy(double *x, double *y, int rows)
198 {
199 #if defined(HAVE_LIBBLAS)
200  cblas_dcopy(rows, x, 1, y, 1);
201 #else
202  G_math_d_copy(x, y, rows);
203 #endif
204 
205  return;
206 }
207 
208 /*!
209  * \brief Scale vector x with scalar a and add it to y
210  *
211  * \f[ {\bf z} = a{\bf x} + {\bf y} \f]
212  *
213  * If grass was not compiled with CBLAS support
214  * it will call #G_math_d_ax_by, the
215  * grass implementatiom
216 
217  *
218  * \param x (double *)
219  * \param y (double *)
220  * \param a (double)
221  * \param rows (int)
222  * \return (void)
223  *
224  * */
225 void G_math_daxpy(double *x, double *y, double a, int rows)
226 {
227 #if defined(HAVE_LIBBLAS)
228  cblas_daxpy(rows, a, x, 1, y, 1);
229 #else
230  G_math_d_ax_by(x, y, y, a, 1.0, rows);
231 #endif
232 
233  return;
234 }
235 
236 /****************************************************************** */
237 
238 /********* F L O A T / S I N G L E P R E C I S I O N ********* */
239 
240 /****************************************************************** */
241 
242 /*!
243  * \brief Compute the dot product of vector x and y
244  * using the CBLAS routine cblas_sdot
245  *
246  * If grass was not compiled with CBLAS support
247  * it will call #G_math_f_x_dot_y, the OpenMP multi threaded
248  * grass implementatiom
249  *
250  * \param x (float *)
251  * \param y (float *)
252  * \param rows (int)
253  * \return (float)
254  *
255  * */
256 float G_math_sdot(float *x, float *y, int rows)
257 {
258 #if defined(HAVE_LIBBLAS)
259  return cblas_sdot(rows, x, 1, y, 1);
260 #else
261  float val;
262 
263  G_math_f_x_dot_y(x, y, &val, rows);
264  return val;
265 #endif
266 }
267 
268 /*!
269  * \brief Compute the euclidean norm of vector x
270  * using the CBLAS routine cblas_dnrm2
271  *
272  * If grass was not compiled with CBLAS support
273  * it will call #G_math_f_euclid_norm, the OpenMP multi threaded
274  * grass implementatiom
275  *
276  * \param x (float *)
277  * \param rows (int)
278  * \return (float)
279  *
280  * */
281 float G_math_snrm2(float *x, int rows)
282 {
283 #if defined(HAVE_LIBBLAS)
284  return cblas_snrm2(rows, x, 1);
285 #else
286  float val;
287 
288  G_math_f_euclid_norm(x, &val, rows);
289  return val;
290 #endif
291 }
292 
293 /*!
294  * \brief Compute the absolute sum norm of vector x
295  * using the CBLAS routine cblas_dasum
296  *
297  * If grass was not compiled with CBLAS support
298  * it will call #G_math_f_asum_norm, the OpenMP multi threaded
299  * grass implementatiom
300  *
301  * \param x (float *)
302  * \param rows (int)
303  * \return (float)
304  *
305  * */
306 float G_math_sasum(float *x, int rows)
307 {
308 #if defined(HAVE_LIBBLAS)
309  return cblas_sasum(rows, x, 1);
310 #else
311  float val;
312 
313  G_math_f_asum_norm(x, &val, rows);
314  return val;
315 #endif
316 }
317 
318 /*!
319  * \brief Compute the maximum norm of vector x
320  * using the CBLAS routine cblas_idamax
321  *
322  * If grass was not compiled with CBLAS support
323  * it will call #G_math_f_max_norm, the OpenMP multi threaded
324  * grass implementatiom
325  *
326  * \param x (float *)
327  * \param rows (int)
328  * \return (float)
329  *
330  * */
331 float G_math_isamax(float *x, int rows)
332 {
333 #if defined(HAVE_LIBBLAS)
334  return cblas_isamax(rows, x, 1);
335 #else
336  float val;
337 
338  G_math_f_max_norm(x, &val, rows);
339  return val;
340 #endif
341 }
342 
343 /*!
344  * \brief Scale vector x with scalar a
345  * using the CBLAS routine cblas_dscal
346  *
347  * If grass was not compiled with CBLAS support
348  * it will call #G_math_f_ax_by, the OpenMP multi threaded
349  * grass implementatiom
350  *
351  * \param x (float *)
352  * \param a (float)
353  * \param rows (int)
354  * \return (float)
355  *
356  * */
357 void G_math_sscal(float *x, float a, int rows)
358 {
359 #if defined(HAVE_LIBBLAS)
360  cblas_sscal(rows, a, x, 1);
361 #else
362  G_math_f_ax_by(x, x, x, a, 0.0, rows);
363 #endif
364 
365  return;
366 }
367 
368 /*!
369  * \brief Copy vector x to vector y
370  *
371  * If grass was not compiled with CBLAS support
372  * it will call #G_math_f_copy, the
373  * grass implementatiom
374  *
375  * \param x (float *)
376  * \param y (float *)
377  * \param rows (int)
378  * \return (void)
379  *
380  * */
381 void G_math_scopy(float *x, float *y, int rows)
382 {
383 #if defined(HAVE_LIBBLAS)
384  cblas_scopy(rows, x, 1, y, 1);
385 #else
386  G_math_f_copy(x, y, rows);
387 #endif
388 
389  return;
390 }
391 
392 /*!
393  * \brief Scale vector x with scalar a and add it to y
394  *
395  * \f[ {\bf z} = a{\bf x} + {\bf y} \f]
396  *
397  * If grass was not compiled with CBLAS support
398  * it will call #G_math_f_ax_by, the
399  * grass implementatiom
400 
401  *
402  * \param x (float *)
403  * \param y (float *)
404  * \param a (float)
405  * \param rows (int)
406  * \return (void)
407  *
408  * */
409 void G_math_saxpy(float *x, float *y, float a, int rows)
410 {
411 #if defined(HAVE_LIBBLAS)
412  cblas_saxpy(rows, a, x, 1, y, 1);
413 #else
414  G_math_f_ax_by(x, y, y, a, 1.0, rows);
415 #endif
416 
417  return;
418 }
double G_math_idamax(double *x, int rows)
Compute the maximum norm of vector x using the CBLAS routine cblas_idamax.
float G_math_isamax(float *x, int rows)
Compute the maximum norm of vector x using the CBLAS routine cblas_idamax.
void G_math_sscal(float *x, float a, int rows)
Scale vector x with scalar a using the CBLAS routine cblas_dscal.
void G_math_saxpy(float *x, float *y, float a, int rows)
Scale vector x with scalar a and add it to y.
float G_math_snrm2(float *x, int rows)
Compute the euclidean norm of vector x using the CBLAS routine cblas_dnrm2.
void G_math_daxpy(double *x, double *y, double a, int rows)
Scale vector x with scalar a and add it to y.
float G_math_sasum(float *x, int rows)
Compute the absolute sum norm of vector x using the CBLAS routine cblas_dasum.
void G_math_dcopy(double *x, double *y, int rows)
Copy vector x to vector y.
double G_math_dasum(double *x, int rows)
Compute the absolute sum norm of vector x using the CBLAS routine cblas_dasum.
double G_math_dnrm2(double *x, int rows)
Compute the euclidean norm of vector x using the CBLAS routine cblas_dnrm2.
void G_math_dscal(double *x, double a, int rows)
Scale vector x with scalar a using the CBLAS routine cblas_dscal.
float G_math_sdot(float *x, float *y, int rows)
Compute the dot product of vector x and y using the CBLAS routine cblas_sdot.
double G_math_ddot(double *x, double *y, int rows)
Compute the dot product of vector x and y using the CBLAS routine cblas_ddot.
void G_math_scopy(float *x, float *y, int rows)
Copy vector x to vector y.
float G_math_sdsdot(float *x, float *y, float a, int rows)
Compute the dot product of vector x and y using the CBLAS routine cblas_sdsdot.
void G_math_d_ax_by(double *, double *, double *, double, double, int)
Scales vectors x and y with the scalars a and b and adds them.
Definition: blas_level_1.c:173
void G_math_f_ax_by(float *, float *, float *, float, float, int)
Scales vectors x and y with the scalars a and b and adds them.
Definition: blas_level_1.c:390
void G_math_f_max_norm(float *, float *, int)
Compute the maximum norm of vector x.
Definition: blas_level_1.c:358
void G_math_d_x_dot_y(double *, double *, double *, int)
Compute the dot product of vector x and y.
Definition: blas_level_1.c:47
void G_math_f_asum_norm(float *, float *, int)
Compute the asum norm of vector x.
Definition: blas_level_1.c:328
void G_math_f_copy(float *, float *, int)
Copy the vector x to y.
Definition: blas_level_1.c:453
void G_math_d_euclid_norm(double *, double *, int)
Compute the euclid norm of vector x.
Definition: blas_level_1.c:79
void G_math_f_x_dot_y(float *, float *, float *, int)
Compute the dot product of vector x and y.
Definition: blas_level_1.c:264
void G_math_d_asum_norm(double *, double *, int)
Compute the asum norm of vector x.
Definition: blas_level_1.c:111
void G_math_d_max_norm(double *, double *, int)
Compute the maximum norm of vector x.
Definition: blas_level_1.c:141
void G_math_d_copy(double *, double *, int)
Copy the vector x to y.
Definition: blas_level_1.c:237
void G_math_f_euclid_norm(float *, float *, int)
Compute the euclid norm of vector x.
Definition: blas_level_1.c:296
#define x