ATLAS_wrapper_blas_level_1.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. #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 a (double)
  153. * \param rows (int)
  154. * \return (void)
  155. *
  156. * */
  157. void G_math_dscal(double *x, double a, int rows)
  158. {
  159. #if defined(HAVE_ATLAS)
  160. cblas_dscal(rows, a, x, 1);
  161. #else
  162. G_math_d_ax_by(x, x, x, a, 0.0, rows);
  163. #endif
  164. return;
  165. }
  166. /*!
  167. * \brief Copy vector x to vector y
  168. *
  169. * If grass was not compiled with ATLAS support
  170. * it will call #G_math_d_copy
  171. *
  172. * \param x (double *)
  173. * \param y (double *)
  174. * \param rows (int)
  175. * \return (void)
  176. *
  177. * */
  178. void G_math_dcopy(double *x, double *y, int rows)
  179. {
  180. #if defined(HAVE_ATLAS)
  181. cblas_dcopy(rows, x, 1, y, 1);
  182. #else
  183. G_math_d_copy(x, y, rows);
  184. #endif
  185. return;
  186. }
  187. /*!
  188. * \brief Scale vector x with scalar a and add it to y
  189. *
  190. * \f[ {\bf z} = a{\bf x} + {\bf y} \f]
  191. *
  192. * If grass was not compiled with ATLAS support
  193. * it will call #G_math_d_ax_by, the
  194. * grass implementatiom
  195. *
  196. * \param x (double *)
  197. * \param y (double *)
  198. * \param a (double)
  199. * \param rows (int)
  200. * \return (void)
  201. *
  202. * */
  203. void G_math_daxpy(double *x, double *y, double a, int rows)
  204. {
  205. #if defined(HAVE_ATLAS)
  206. cblas_daxpy(rows, a, x, 1, y, 1);
  207. #else
  208. G_math_d_ax_by(x, y, y, a, 1.0, rows);
  209. #endif
  210. return;
  211. }
  212. /****************************************************************** */
  213. /********* F L O A T / S I N G L E P E P R E C I S I O N ******** */
  214. /****************************************************************** */
  215. /*!
  216. * \brief Compute the dot product of vector x and y
  217. * using the ATLAS routine cblas_sdot
  218. *
  219. * If grass was not compiled with ATLAS support
  220. * it will call #G_math_f_x_dot_y, the OpenMP multi threaded
  221. * grass implementatiom
  222. *
  223. * \param x (float *)
  224. * \param y (float *)
  225. * \param rows (int)
  226. * \return (float)
  227. *
  228. * */
  229. float G_math_sdot(float *x, float *y, int rows)
  230. {
  231. #if defined(HAVE_ATLAS)
  232. return cblas_sdot(rows, x, 1, y, 1);
  233. #else
  234. float val;
  235. G_math_f_x_dot_y(x, y, &val, rows);
  236. return val;
  237. #endif
  238. }
  239. /*!
  240. * \brief Compute the euclidean norm of vector x
  241. * using the ATLAS routine cblas_dnrm2
  242. *
  243. * If grass was not compiled with ATLAS support
  244. * it will call #G_math_f_euclid_norm, the OpenMP multi threaded
  245. * grass implementatiom
  246. *
  247. * \param x (float *)
  248. * \param rows (int)
  249. * \return (float)
  250. *
  251. * */
  252. float G_math_snrm2(float *x, int rows)
  253. {
  254. #if defined(HAVE_ATLAS)
  255. return cblas_snrm2(rows, x, 1);
  256. #else
  257. float val;
  258. G_math_f_euclid_norm(x, &val, rows);
  259. return val;
  260. #endif
  261. }
  262. /*!
  263. * \brief Compute the absolute sum norm of vector x
  264. * using the ATLAS routine cblas_dasum
  265. *
  266. * If grass was not compiled with ATLAS support
  267. * it will call #G_math_f_asum_norm, the OpenMP multi threaded
  268. * grass implementatiom
  269. *
  270. * \param x (float *)
  271. * \param rows (int)
  272. * \return (float)
  273. *
  274. * */
  275. float G_math_sasum(float *x, int rows)
  276. {
  277. #if defined(HAVE_ATLAS)
  278. return cblas_sasum(rows, x, 1);
  279. #else
  280. float val;
  281. G_math_f_asum_norm(x, &val, rows);
  282. return val;
  283. #endif
  284. }
  285. /*!
  286. * \brief Compute the maximum norm of vector x
  287. * using the ATLAS routine cblas_idamax
  288. *
  289. * If grass was not compiled with ATLAS support
  290. * it will call #G_math_f_max_norm, the OpenMP multi threaded
  291. * grass implementatiom
  292. *
  293. * \param x (float *)
  294. * \param rows (int)
  295. * \return (float)
  296. *
  297. * */
  298. float G_math_isamax(float *x, int rows)
  299. {
  300. #if defined(HAVE_ATLAS)
  301. return cblas_isamax(rows, x, 1);
  302. #else
  303. float val;
  304. G_math_f_max_norm(x, &val, rows);
  305. return val;
  306. #endif
  307. }
  308. /*!
  309. * \brief Scale vector x with scalar a
  310. * using the ATLAS routine cblas_dscal
  311. *
  312. * If grass was not compiled with ATLAS support
  313. * it will call #G_math_f_ax_by, the OpenMP multi threaded
  314. * grass implementatiom
  315. *
  316. * \param x (float *)
  317. * \param a (float)
  318. * \param rows (int)
  319. * \return (float)
  320. *
  321. * */
  322. void G_math_sscal(float *x, float a, int rows)
  323. {
  324. #if defined(HAVE_ATLAS)
  325. cblas_sscal(rows, a, x, 1);
  326. #else
  327. G_math_f_ax_by(x, x, x, a, 0.0, rows);
  328. #endif
  329. return;
  330. }
  331. /*!
  332. * \brief Copy vector x to vector y
  333. *
  334. * If grass was not compiled with ATLAS support
  335. * it will call #G_math_f_copy, the
  336. * grass implementatiom
  337. *
  338. * \param x (float *)
  339. * \param y (float *)
  340. * \param rows (int)
  341. * \return (void)
  342. *
  343. * */
  344. void G_math_scopy(float *x, float *y, int rows)
  345. {
  346. #if defined(HAVE_ATLAS)
  347. cblas_scopy(rows, x, 1, y, 1);
  348. #else
  349. G_math_f_copy(x, y, rows);
  350. #endif
  351. return;
  352. }
  353. /*!
  354. * \brief Scale vector x with scalar a and add it to y
  355. *
  356. * \f[ {\bf z} = a{\bf x} + {\bf y} \f]
  357. *
  358. * If grass was not compiled with ATLAS support
  359. * it will call #G_math_f_ax_by, the
  360. * grass implementatiom
  361. *
  362. * \param x (float *)
  363. * \param y (float *)
  364. * \param a (float)
  365. * \param rows (int)
  366. * \return (void)
  367. *
  368. * */
  369. void G_math_saxpy(float *x, float *y, float a, int rows)
  370. {
  371. #if defined(HAVE_ATLAS)
  372. cblas_saxpy(rows, a, x, 1, y, 1);
  373. #else
  374. G_math_f_ax_by(x, y, y, a, 1.0, rows);
  375. #endif
  376. return;
  377. }