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