ATLAS_wrapper_blas_level_1.c 8.4 KB

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