test_blas1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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: Unit tests for les creation
  8. *
  9. * COPYRIGHT: (C) 2000 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <grass/gis.h>
  17. #include <grass/glocale.h>
  18. #include <grass/gmath.h>
  19. #include <math.h>
  20. #include "test_gmath_lib.h"
  21. #define EPSILON 0.000001
  22. /* prototypes */
  23. static int test_blas_level_1_double(void);
  24. static int test_blas_level_1_float(void);
  25. static int test_blas_level_1_int(void);
  26. /* *************************************************************** */
  27. /* Perfrome the blas level 1 unit tests ************************** */
  28. /* *************************************************************** */
  29. int unit_test_blas_level_1(void)
  30. {
  31. int sum = 0;
  32. G_message(_("\n++ Running blas level 1 unit tests ++"));
  33. sum += test_blas_level_1_double();
  34. sum += test_blas_level_1_float();
  35. sum += test_blas_level_1_int();
  36. if (sum > 0)
  37. G_warning(_("\n-- blas level 1 unit tests failure --"));
  38. else
  39. G_message(_("\n-- blas level 1 unit tests finished successfully --"));
  40. return sum;
  41. }
  42. /* *************************************************************** */
  43. /* ************** D O U B L E ************************************ */
  44. /* *************************************************************** */
  45. int test_blas_level_1_double(void)
  46. {
  47. int sum = 0;
  48. int rows = 10000;
  49. double *x, *y, *z, a = 0.0, b = 0.0, c = 0.0, d = 0.0, e = 0.0;
  50. x = G_alloc_vector(rows);
  51. y = G_alloc_vector(rows);
  52. z = G_alloc_vector(rows);
  53. fill_d_vector_scalar(x, 1, rows);
  54. fill_d_vector_scalar(y, 2, rows);
  55. /*test the grass implementation*/
  56. G_math_d_x_dot_y(x, y, &a, rows);
  57. G_math_d_asum_norm(x, &b, rows);
  58. G_math_d_euclid_norm(x, &c, rows);
  59. if(a != 2.0*rows) {
  60. G_message("Error in G_math_d_x_dot_y %f != %f", 2.0*rows, a);
  61. sum++;
  62. }
  63. if(b != rows) {
  64. G_message("Error in G_math_d_asum_norm");
  65. sum++;
  66. }
  67. if(c != sqrt(rows)) {
  68. G_message("Error in G_math_d_euclid_norm");
  69. sum++;
  70. }
  71. /*test the ATLAS implementation*/
  72. a = G_math_dnrm2(x, rows);
  73. b = G_math_dasum(x, rows);
  74. c = G_math_ddot(x, y, rows);
  75. if(a != sqrt(rows)) {
  76. G_message("Error in G_math_dnrm2 %f != %f", sqrt(rows), a);
  77. sum++;
  78. }
  79. if(b != rows) {
  80. G_message("Error in G_math_dasum %f != %f", 2.0*rows, b);
  81. sum++;
  82. }
  83. if(c != 2.0*rows) {
  84. G_message("Error in G_math_ddot %f != %f", 2.0*rows, c);
  85. sum++;
  86. }
  87. fill_d_vector_range_1(x, 1.0, rows);
  88. fill_d_vector_range_2(y, 1.0, rows);
  89. /*grass function*/
  90. G_math_d_max_norm(x, &a, rows);
  91. /*atlas function*/
  92. b = G_math_idamax(x, rows);
  93. if(a != 1.0*(rows - 1)) {
  94. G_message("Error in G_math_d_max_norm: %f != %f", (double)1.0*(rows - 1), a);
  95. sum++;
  96. }
  97. if(b != 1.0*(rows - 1)) {
  98. G_message("Error in G_math_idamax: %f != %f", (double)1.0*(rows - 1), b);
  99. sum++;
  100. }
  101. #pragma omp parallel default(shared)
  102. {
  103. G_math_d_ax_by(x, y, z, 1.0, 1.0, rows);
  104. }
  105. G_math_d_asum_norm(z, &a, rows);
  106. #pragma omp parallel default(shared)
  107. {
  108. G_math_d_ax_by(x, y, z, 1.0, -1.0, rows);
  109. }
  110. G_math_d_asum_norm(z, &b, rows);
  111. #pragma omp parallel default(shared)
  112. {
  113. G_math_d_ax_by(x, y, z, 2.0, 1.0, rows);
  114. }
  115. G_math_d_asum_norm(z, &c, rows);
  116. if(a != 1.0*(rows - 1)* rows) {
  117. G_message("Error in G_math_d_ax_by: %f != %f", (double)1.0*(rows - 1)* rows, a);
  118. sum++;
  119. }
  120. if(b != 5.0*(rows)*(rows/10)) {
  121. G_message("Error in G_math_d_ax_by: %f != %f", (double)5.0*(rows)*(rows/10), b);
  122. sum++;
  123. }
  124. if(c != 149985000) {
  125. G_message("Error in G_math_d_ax_by: 149985000 != %f", c);
  126. sum++;
  127. }
  128. #pragma omp parallel default(shared)
  129. {
  130. /*scale x with 1*/
  131. G_math_d_ax_by(x, z, z, 1.0, 0.0, rows);
  132. }
  133. G_math_d_asum_norm(x, &a, rows);
  134. G_math_d_asum_norm(z, &b, rows);
  135. /*scale x with -1*/
  136. #pragma omp parallel default(shared)
  137. {
  138. G_math_d_ax_by(x, z, z, -1.0, 0.0, rows);
  139. }
  140. G_math_d_asum_norm(z, &c, rows);
  141. /*ATLAS implementation*/
  142. G_math_dscal(x, 1.0, rows);
  143. G_math_d_asum_norm(x, &d, rows);
  144. /*ATLAS implementation*/
  145. fill_d_vector_range_1(x, 1.0, rows);
  146. fill_d_vector_scalar(z, 0.0, rows);
  147. G_math_daxpy(x, z, 1.0, rows);
  148. G_math_d_asum_norm(z, &e, rows);
  149. if(a != 49995000 || a != b || b != c) {
  150. G_message("Error in G_math_d_ax: 49995000 != %f", a);
  151. sum++;
  152. }
  153. if(49995000 != d) {
  154. G_message("Error in G_math_dscal: 49995000 != %f", d);
  155. sum++;
  156. }
  157. if(49995000 != e) {
  158. G_message("Error in G_math_daxpy: 49995000 != %f", e);
  159. sum++;
  160. }
  161. fill_d_vector_scalar(z, 0, rows);
  162. G_math_d_copy(x, z, rows);
  163. G_math_d_asum_norm(x, &a, rows);
  164. G_math_dcopy(x, z, rows);
  165. G_math_d_asum_norm(x, &b, rows);
  166. if(a != 49995000) {
  167. G_message("Error in G_math_d_copy: 49995000 != %f", a);
  168. sum++;
  169. }
  170. if(b != 49995000) {
  171. G_message("Error in G_math_dcopy: 49995000 != %f", a);
  172. sum++;
  173. }
  174. G_free_vector(x);
  175. G_free_vector(y);
  176. G_free_vector(z);
  177. return sum;
  178. }
  179. /* *************************************************************** */
  180. /* ************** F L O A T ************************************** */
  181. /* *************************************************************** */
  182. int test_blas_level_1_float(void)
  183. {
  184. int sum = 0;
  185. int rows = 1000;
  186. float *x, *y, *z, a = 0.0, b = 0.0, c = 0.0, d = 0.0, e = 0.0;
  187. x = G_alloc_fvector(rows);
  188. y = G_alloc_fvector(rows);
  189. z = G_alloc_fvector(rows);
  190. fill_f_vector_scalar(x, 1, rows);
  191. fill_f_vector_scalar(y, 2, rows);
  192. /*test the grass implementation*/
  193. G_math_f_x_dot_y(x, y, &a, rows);
  194. G_math_f_asum_norm(x, &b, rows);
  195. G_math_f_euclid_norm(x, &c, rows);
  196. if(a != 2.0*rows) {
  197. G_message("Error in G_math_f_x_dot_y %f != %f", 2.0*rows, a);
  198. sum++;
  199. }
  200. if(b != rows) {
  201. G_message("Error in G_math_f_asum_norm");
  202. sum++;
  203. }
  204. if(fabs(c - (float)sqrt(rows)) > EPSILON) {
  205. G_message("Error in G_math_f_euclid_norm");
  206. sum++;
  207. }
  208. /*test the ATLAS implementation*/
  209. a = G_math_snrm2(x, rows);
  210. b = G_math_sasum(x, rows);
  211. c = G_math_sdot(x, y, rows);
  212. if(fabs(a - sqrt(rows)) > EPSILON) {
  213. G_message("Error in G_math_snrm2 %f != %f", sqrt(rows), a);
  214. sum++;
  215. }
  216. if(b != rows) {
  217. G_message("Error in G_math_sasum %f != %f", 2.0*rows, b);
  218. sum++;
  219. }
  220. if(c != 2.0*rows) {
  221. G_message("Error in G_math_sdot %f != %f", 2.0*rows, c);
  222. sum++;
  223. }
  224. fill_f_vector_range_1(x, 1.0, rows);
  225. fill_f_vector_range_2(y, 1.0, rows);
  226. /*grass function*/
  227. G_math_f_max_norm(x, &a, rows);
  228. /*atlas function*/
  229. b = G_math_isamax(x, rows);
  230. if(a != 1.0*(rows - 1)) {
  231. G_message("Error in G_math_f_max_norm: %f != %f", (float)1.0*(rows - 1), a);
  232. sum++;
  233. }
  234. if(b != 1.0*(rows - 1)) {
  235. G_message("Error in G_math_isamax: %f != %f", (float)1.0*(rows - 1), b);
  236. sum++;
  237. }
  238. #pragma omp parallel default(shared)
  239. {
  240. G_math_f_ax_by(x, y, z, 1.0, 1.0, rows);
  241. }
  242. G_math_f_asum_norm(z, &a, rows);
  243. #pragma omp parallel default(shared)
  244. {
  245. G_math_f_ax_by(x, y, z, 1.0, -1.0, rows);
  246. }
  247. G_math_f_asum_norm(z, &b, rows);
  248. #pragma omp parallel default(shared)
  249. {
  250. G_math_f_ax_by(x, y, z, 2.0, 1.0, rows);
  251. }
  252. G_math_f_asum_norm(z, &c, rows);
  253. if(fabs(a - 1.0*(rows - 1)* rows) > EPSILON) {
  254. G_message("Error in G_math_f_ax_by 1: %f != %f", (float)1.0*(rows - 1)* rows, a);
  255. sum++;
  256. }
  257. if(fabs(b - 5.0*(rows)*(rows/10)) > EPSILON) {
  258. G_message("Error in G_math_f_ax_by 2: %f != %f", (float)5.0*(rows)*(rows/10), b);
  259. sum++;
  260. }
  261. if(fabs(c - 1498500) > EPSILON) {
  262. G_message("Error in G_math_f_ax_by 3: 14998500 != %f", c);
  263. sum++;
  264. }
  265. #pragma omp parallel default(shared)
  266. {
  267. /*scale x with 1*/
  268. G_math_f_ax_by(x, z, z, 1.0, 0.0, rows);
  269. }
  270. G_math_f_asum_norm(x, &a, rows);
  271. G_math_f_asum_norm(z, &b, rows);
  272. /*scale x with -1*/
  273. #pragma omp parallel default(shared)
  274. {
  275. G_math_f_ax_by(x, z, z, -1.0, 0.0, rows);
  276. }
  277. G_math_f_asum_norm(z, &c, rows);
  278. /*ATLAS implementation*/
  279. G_math_sscal(x, 1.0, rows);
  280. G_math_f_asum_norm(x, &d, rows);
  281. /*ATLAS implementation*/
  282. fill_f_vector_range_1(x, 1.0, rows);
  283. fill_f_vector_scalar(z, 0.0, rows);
  284. G_math_saxpy(x, z, 1.0, rows);
  285. G_math_f_asum_norm(z, &e, rows);
  286. if(fabs(a - 499500) > EPSILON) {
  287. G_message("Error in G_math_f_ax_by 4: 4999500 != %f", a);
  288. sum++;
  289. }
  290. if(fabs(b - 499500) > EPSILON) {
  291. G_message("Error in G_math_f_ax_by 4: 4999500 != %f", b);
  292. sum++;
  293. }
  294. if(fabs(c - 499500) > EPSILON) {
  295. G_message("Error in G_math_f_ax_by 4: 4999500 != %f", c);
  296. sum++;
  297. }
  298. if(fabs(d - 499500) > EPSILON) {
  299. G_message("Error in G_math_sscal: 4999500 != %f", d);
  300. sum++;
  301. }
  302. if(fabs(e - 499500) > EPSILON) {
  303. G_message("Error in G_math_saxpy: 4999500 != %f", e);
  304. sum++;
  305. }
  306. fill_f_vector_range_1(x, 1.0, rows);
  307. fill_f_vector_scalar(z, 0, rows);
  308. G_math_f_copy(x, z, rows);
  309. G_math_f_asum_norm(x, &a, rows);
  310. G_math_scopy(x, z, rows);
  311. G_math_f_asum_norm(x, &b, rows);
  312. if(fabs(a - 499500) > EPSILON) {
  313. G_message("Error in G_math_f_copy: 4999500 != %f", a);
  314. sum++;
  315. }
  316. if(fabs(b - 499500) > EPSILON) {
  317. G_message("Error in G_math_scopy: 4999500 != %f", b);
  318. sum++;
  319. }
  320. G_free_fvector(x);
  321. G_free_fvector(y);
  322. G_free_fvector(z);
  323. return sum;
  324. }
  325. /* *************************************************************** */
  326. /* ************** I N T E G E R ********************************** */
  327. /* *************************************************************** */
  328. int test_blas_level_1_int(void)
  329. {
  330. int sum = 0;
  331. int rows = 10000;
  332. int *x, *y, *z, max;
  333. double a, b, c;
  334. x = G_alloc_ivector(rows);
  335. y = G_alloc_ivector(rows);
  336. z = G_alloc_ivector(rows);
  337. fill_i_vector_scalar(x, 1, rows);
  338. fill_i_vector_scalar(y, 2, rows);
  339. G_math_i_x_dot_y(x, y, &a, rows);
  340. G_math_i_asum_norm(x, &b, rows);
  341. G_math_i_euclid_norm(x, &c, rows);
  342. if(a != 2*rows) {
  343. G_message("Error in G_math_i_x_dot_y");
  344. sum++;
  345. }
  346. if(b != rows) {
  347. G_message("Error in G_math_i_asum_norm");
  348. sum++;
  349. }
  350. if(c != sqrt((double)rows)) {
  351. G_message("Error in G_math_i_euclid_norm");
  352. sum++;
  353. }
  354. fill_i_vector_range_1(x, 1, rows);
  355. fill_i_vector_range_2(y, 1, rows);
  356. G_math_i_max_norm(x, &max, rows);
  357. if(max != (rows - 1)) {
  358. G_message("Error in G_math_i_max_norm: %i != %i", (rows - 1), max);
  359. sum++;
  360. }
  361. #pragma omp parallel default(shared)
  362. {
  363. G_math_i_ax_by(x, y, z, 1, 1, rows);
  364. }
  365. G_math_i_asum_norm(z, &a, rows);
  366. #pragma omp parallel default(shared)
  367. {
  368. G_math_i_ax_by(x, y, z, 1, -1, rows);
  369. }
  370. G_math_i_asum_norm(z, &b, rows);
  371. #pragma omp parallel default(shared)
  372. {
  373. G_math_i_ax_by(x, y, z, 2, 1, rows);
  374. }
  375. G_math_i_asum_norm(z, &c, rows);
  376. if(a != 1.0*(rows - 1)* rows) {
  377. G_message("Error in G_math_i_ax_by: %f != %f", 1.0*(rows - 1)* rows, a);
  378. sum++;
  379. }
  380. if(b != 5.0*(rows)*(rows/10)) {
  381. G_message("Error in G_math_i_ax_by: %f != %f", 5.0*(rows)*(rows/10), b);
  382. sum++;
  383. }
  384. if(c != 149985000) {
  385. G_message("Error in G_math_i_ax_by: 149985000 != %f", c);
  386. sum++;
  387. }
  388. #pragma omp parallel default(shared)
  389. {
  390. /*scale x with 1*/
  391. G_math_i_ax_by(x, z, z, 1, 0, rows);
  392. }
  393. G_math_i_asum_norm(x, &a, rows);
  394. G_math_i_asum_norm(z, &b, rows);
  395. /*scale a with -1*/
  396. #pragma omp parallel default(shared)
  397. {
  398. G_math_i_ax_by(x, z, z, -1, 0, rows);
  399. }
  400. G_math_i_asum_norm(z, &c, rows);
  401. if(a != 49995000 || a != b || b != c) {
  402. G_message("Error in G_math_i_ax_by: 49995000 != %f", a);
  403. sum++;
  404. }
  405. return sum;
  406. }