test_blas2.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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.0000001
  22. /* prototypes */
  23. static int test_blas_level_2_double(void);
  24. static int test_blas_level_2_float(void);
  25. /* *************************************************************** */
  26. /* Perfrome the blas level 2 unit tests ************************** */
  27. /* *************************************************************** */
  28. int unit_test_blas_level_2(void)
  29. {
  30. int sum = 0;
  31. G_message(_("\n++ Running blas level 2 unit tests ++"));
  32. sum += test_blas_level_2_double();
  33. sum += test_blas_level_2_float();
  34. if (sum > 0)
  35. G_warning(_("\n-- blas level 2 unit tests failure --"));
  36. else
  37. G_message(_("\n-- blas level 2 unit tests finished successfully --"));
  38. return sum;
  39. }
  40. /*
  41. extern double *G_math_Ax_sparse(G_math_spvector **, double *, double *, int );
  42. extern double *G_math_d_Ax(double **, double *, double *, int , int );
  43. extern float *G_math_f_Ax(float **, float *, float *, int , int );
  44. extern double **G_math_d_x_dyad_y(double *, double *, double **, int, int );
  45. extern float **G_math_f_x_dyad_y(float *, float *, float **, int, int );
  46. extern double *G_math_d_aAx_by(double **, double *, double *, double , double , double *, int , int );
  47. extern float *G_math_f_aAx_by(float **, float *, float *, float , float , float *, int , int );
  48. */
  49. /* *************************************************************** */
  50. /* ************** D O U B L E ************************************ */
  51. /* *************************************************************** */
  52. int test_blas_level_2_double(void)
  53. {
  54. int sum = 0;
  55. int rows = TEST_NUM_ROWS;
  56. double **A, **B, **C, *x, *y, *z, a = 0.0, b = 0.0, c = 0.0, d = 0.0, e = 0.0, f = 0.0, g = 0.0, h = 0.0, i = 0.0, j =0.0;
  57. G_math_les *les;
  58. les = create_normal_unsymmetric_les(rows);
  59. G_math_les *sples;
  60. sples = create_sparse_symmetric_les(rows);
  61. G_math_les *bles;
  62. bles = create_symmetric_band_les(rows);
  63. x = G_alloc_vector(rows);
  64. y = G_alloc_vector(rows);
  65. z = G_alloc_vector(rows);
  66. A = G_alloc_matrix(rows, rows);
  67. B = G_alloc_matrix(rows, rows);
  68. C = G_alloc_matrix(rows, rows);
  69. fill_d_vector_scalar(x, 1, rows);
  70. fill_d_vector_scalar(y, 0, rows);
  71. #pragma omp parallel default(shared)
  72. {
  73. G_message("Testing G_math_Ax_sparse");
  74. G_math_Ax_sparse(sples->Asp, x, sples->b, rows);
  75. G_math_print_les(sples);
  76. G_math_d_asum_norm(sples->b, &a, rows);
  77. G_message("Testing G_math_Ax_band");
  78. G_math_Ax_sband(bles->A, x, bles->b, rows, rows);
  79. G_math_print_les(bles);
  80. G_math_d_asum_norm(bles->b, &j, rows);
  81. G_message("Testing G_math_d_Ax");
  82. G_math_d_Ax(les->A, x, z, rows, rows);
  83. G_math_d_asum_norm(z, &b, rows);
  84. G_message("Testing G_math_d_aAx_by");
  85. G_math_d_aAx_by(les->A, x, y, 1.0, 1.0, z, rows, rows);
  86. G_math_d_asum_norm(z, &c, rows);
  87. G_message("Testing G_math_d_aAx_by");
  88. G_math_d_aAx_by(les->A, x, y, -1.0, 1.0, z, rows, rows);
  89. G_math_d_asum_norm(z, &d, rows);
  90. G_message("Testing G_math_d_aAx_by");
  91. G_math_d_aAx_by(les->A, x, y, 1.0, 0.0, z, rows, rows);
  92. G_math_d_asum_norm(z, &e, rows);
  93. G_message("Testing G_math_d_aAx_by");
  94. G_math_d_aAx_by(les->A, x, y, -1.0, -1.0, z, rows, rows);
  95. G_math_d_asum_norm(z, &f, rows);
  96. G_message("Testing G_math_d_x_dyad_y");
  97. G_math_d_x_dyad_y(x, x, A, rows, rows);
  98. G_math_d_Ax(A, x, z, rows, rows);
  99. G_math_d_asum_norm(z, &g, rows);
  100. G_message("Testing G_math_d_x_dyad_y");
  101. G_math_d_x_dyad_y(x, x, C, rows, rows);
  102. G_math_d_Ax(A, x, z, rows, rows);
  103. G_math_d_asum_norm(z, &h, rows);
  104. }
  105. G_math_d_asum_norm(les->b, &i, rows);
  106. if(a - i > EPSILON) {
  107. G_message("Error in G_math_Ax_sparse: %f != %f", i, a);
  108. sum++;
  109. }
  110. if(j - i > EPSILON) {
  111. G_message("Error in G_math_Ax_sband: %f != %f", i, j);
  112. sum++;
  113. }
  114. if(b - i > EPSILON) {
  115. G_message("Error in G_math_d_Ax: %f != %f", i, b);
  116. sum++;
  117. }
  118. if(c - i > EPSILON) {
  119. G_message("Error in G_math_aAx_by: %f != %f", i, c);
  120. sum++;
  121. }
  122. if(d - i > EPSILON) {
  123. G_message("Error in G_math_aAx_by: %f != %f", i, d);
  124. sum++;
  125. }
  126. if(e - i > EPSILON) {
  127. G_message("Error in G_math_aAx_by: %f != %f", i, e);
  128. sum++;
  129. }
  130. if(f - i > EPSILON) {
  131. G_message("Error in G_math_aAx_by: %f != %f", i, f);
  132. sum++;
  133. }
  134. if(g - (double)rows*rows > EPSILON) {
  135. G_message("Error in G_math_d_x_dyad_y: %f != %f", (double)rows*rows, g);
  136. sum++;
  137. }
  138. if(h - (double)rows*rows > EPSILON) {
  139. G_message("Error in G_math_d_x_dyad_y: %f != %f", (double)rows*rows, h);
  140. sum++;
  141. }
  142. if(x)
  143. G_free_vector(x);
  144. if(y)
  145. G_free_vector(y);
  146. if(z)
  147. G_free_vector(z);
  148. G_math_free_les(les);
  149. G_math_free_les(bles);
  150. G_math_free_les(sples);
  151. if(A)
  152. G_free_matrix(A);
  153. if(B)
  154. G_free_matrix(B);
  155. if(C)
  156. G_free_matrix(C);
  157. return sum;
  158. }
  159. /* *************************************************************** */
  160. /* ************** F L O A T ************************************** */
  161. /* *************************************************************** */
  162. int test_blas_level_2_float(void)
  163. {
  164. int sum = 0;
  165. int rows =TEST_NUM_ROWS;
  166. float **A, **B, **C, *x, *y, *z, b = 0.0, c = 0.0, d = 0.0, e = 0.0, f = 0.0, g = 0.0, h = 0.0, i = 0.0;
  167. G_math_f_les *les;
  168. les = create_normal_unsymmetric_f_les(rows);
  169. x = G_alloc_fvector(rows);
  170. y = G_alloc_fvector(rows);
  171. z = G_alloc_fvector(rows);
  172. A = G_alloc_fmatrix(rows, rows);
  173. B = G_alloc_fmatrix(rows, rows);
  174. C = G_alloc_fmatrix(rows, rows);
  175. fill_f_vector_scalar(x, 1, rows);
  176. fill_f_vector_scalar(y, 0, rows);
  177. #pragma omp parallel default(shared)
  178. {
  179. G_message("Testing G_math_f_Ax");
  180. G_math_f_Ax(les->A, x, z, rows, rows);
  181. G_math_f_asum_norm(z, &b, rows);
  182. G_message("Testing G_math_f_aAx_by");
  183. G_math_f_aAx_by(les->A, x, y, 1.0, 1.0, z, rows, rows);
  184. G_math_f_asum_norm(z, &c, rows);
  185. G_message("Testing G_math_f_aAx_by");
  186. G_math_f_aAx_by(les->A, x, y, -1.0, 1.0, z, rows, rows);
  187. G_math_f_asum_norm(z, &d, rows);
  188. G_message("Testing G_math_f_aAx_by");
  189. G_math_f_aAx_by(les->A, x, y, 1.0, 0.0, z, rows, rows);
  190. G_math_f_asum_norm(z, &e, rows);
  191. G_message("Testing G_math_f_aAx_by");
  192. G_math_f_aAx_by(les->A, x, y, -1.0, -1.0, z, rows, rows);
  193. G_math_f_asum_norm(z, &f, rows);
  194. G_message("Testing G_math_f_x_dyad_y");
  195. G_math_f_x_dyad_y(x, x, A, rows, rows);
  196. G_math_f_Ax(A, x, z, rows, rows);
  197. G_math_f_asum_norm(z, &g, rows);
  198. G_message("Testing G_math_f_x_dyad_y");
  199. G_math_f_x_dyad_y(x, x, C, rows, rows);
  200. G_math_f_Ax(A, x, z, rows, rows);
  201. G_math_f_asum_norm(z, &h, rows);
  202. }
  203. G_math_f_asum_norm(les->b, &i, rows);
  204. if(b - i > EPSILON) {
  205. G_message("Error in G_math_f_Ax: %f != %f", i, b);
  206. sum++;
  207. }
  208. if(c - i > EPSILON) {
  209. G_message("Error in G_math_f_aAx_by: %f != %f", i, c);
  210. sum++;
  211. }
  212. if(d - i > EPSILON) {
  213. G_message("Error in G_math_f_aAx_by: %f != %f", i, d);
  214. sum++;
  215. }
  216. if(e - i > EPSILON) {
  217. G_message("Error in G_math_f_aAx_by: %f != %f", i, e);
  218. sum++;
  219. }
  220. if(f - i > EPSILON) {
  221. G_message("Error in G_math_f_aAx_by: %f != %f", i, f);
  222. sum++;
  223. }
  224. if(g - (float)rows*rows > EPSILON) {
  225. G_message("Error in G_math_f_x_dyad_y: %f != %f", (float)rows*rows, g);
  226. sum++;
  227. }
  228. if(h - (float)rows*rows > EPSILON) {
  229. G_message("Error in G_math_f_x_dyad_y: %f != %f", (float)rows*rows, h);
  230. sum++;
  231. }
  232. if(x)
  233. G_free_fvector(x);
  234. if(y)
  235. G_free_fvector(y);
  236. if(z)
  237. G_free_fvector(z);
  238. G_math_free_f_les(les);
  239. if(A)
  240. G_free_fmatrix(A);
  241. if(B)
  242. G_free_fmatrix(B);
  243. if(C)
  244. G_free_fmatrix(C);
  245. return sum;
  246. }