test_blas3.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*****************************************************************************
  2. *
  3. * MODULE: Grass PDE Numerical Library
  4. * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2007
  5. * soerengebbert <at> gmx <dot> de
  6. *
  7. * PURPOSE: Unit tests for les creation
  8. *
  9. * COPYRIGHT: (C) 2007 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_3_double(void);
  24. static int test_blas_level_3_float(void);
  25. /* *************************************************************** */
  26. /* Perfrome the blas level 3 unit tests ************************** */
  27. /* *************************************************************** */
  28. int unit_test_blas_level_3(void)
  29. {
  30. int sum = 0;
  31. G_message(_("\n++ Running blas level 3 unit tests ++"));
  32. sum += test_blas_level_3_double();
  33. sum += test_blas_level_3_float();
  34. if (sum > 0)
  35. G_warning(_("\n-- blas level 3 unit tests failure --"));
  36. else
  37. G_message(_("\n-- blas level 3 unit tests finished successfully --"));
  38. return sum;
  39. }
  40. /* *************************************************************** */
  41. /* ************** D O U B L E ************************************ */
  42. /* *************************************************************** */
  43. int test_blas_level_3_double(void)
  44. {
  45. int sum = 0;
  46. int rows = TEST_NUM_ROWS;
  47. int cols = TEST_NUM_COLS;
  48. double **A, **B, **C, *x, *y, a = 0.0, b = 0.0, c = 0.0, d = 0.0;
  49. x = G_alloc_vector(cols);
  50. y = G_alloc_vector(rows);
  51. A = G_alloc_matrix(rows, cols);
  52. B = G_alloc_matrix(rows, cols);
  53. C = G_alloc_matrix(rows, cols);
  54. fill_d_vector_scalar(x, 1, cols);
  55. fill_d_vector_scalar(y, 0, rows);
  56. fill_d_vector_scalar(A[0], 1, rows*cols);
  57. fill_d_vector_scalar(B[0], 2, rows*cols);
  58. #pragma omp parallel default(shared)
  59. {
  60. G_math_d_aA_B(A, B, 1.0 , C, rows , cols );
  61. G_math_d_Ax(C, x, y, rows, cols);
  62. }
  63. G_math_d_asum_norm(y, &a, rows);
  64. if(a != 3*rows*cols) {
  65. G_message("Error in G_math_d_aA_B: %f != %f", a, (double)3*rows*cols);
  66. sum++;
  67. }
  68. #pragma omp parallel default(shared)
  69. {
  70. G_math_d_aA_B(A, B, -1.0 , C, rows , cols );
  71. G_math_d_Ax(C, x, y, rows, cols);
  72. }
  73. G_math_d_asum_norm(y, &b, rows);
  74. if(b != rows*cols) {
  75. G_message("Error in G_math_d_aA_B: %f != %f", b, (double)rows*cols);
  76. sum++;
  77. }
  78. #pragma omp parallel default(shared)
  79. {
  80. G_math_d_aA_B(A, B, 2.0 , C, rows , cols );
  81. G_math_d_Ax(C, x, y, rows, cols);
  82. }
  83. G_math_d_asum_norm(y, &c, rows);
  84. if(c != 4*rows*cols) {
  85. G_message("Error in G_math_d_aA_B: %f != %f", c, (double)4*rows*cols);
  86. sum++;
  87. }
  88. G_free_matrix(A);
  89. G_free_matrix(B);
  90. G_free_matrix(C);
  91. A = G_alloc_matrix(rows, cols);
  92. B = G_alloc_matrix(cols, rows);
  93. C = G_alloc_matrix(rows, rows);
  94. G_free_vector(x);
  95. G_free_vector(y);
  96. x = G_alloc_vector(rows);
  97. y = G_alloc_vector(rows);
  98. fill_d_vector_scalar(x, 1, rows);
  99. fill_d_vector_scalar(y, 0, rows);
  100. fill_d_vector_scalar(A[0], 1, rows*cols);
  101. fill_d_vector_scalar(B[0], 2, rows*cols);
  102. #pragma omp parallel default(shared)
  103. {
  104. G_math_d_AB(A, B, C, rows , cols , cols );
  105. G_math_d_Ax(C, x, y, rows, cols);
  106. }
  107. G_math_d_asum_norm(y, &d, rows);
  108. if(d != 2*rows*cols*cols) {
  109. G_message("Error in G_math_d_AB: %f != %f", d, (double)2*rows*cols*cols);
  110. sum++;
  111. }
  112. if(x)
  113. G_free_vector(x);
  114. if(y)
  115. G_free_vector(y);
  116. if(A)
  117. G_free_matrix(A);
  118. if(B)
  119. G_free_matrix(B);
  120. if(C)
  121. G_free_matrix(C);
  122. return sum;
  123. }
  124. /* *************************************************************** */
  125. /* ************** F L O A T ************************************** */
  126. /* *************************************************************** */
  127. int test_blas_level_3_float(void)
  128. {
  129. int sum = 0;
  130. int rows = TEST_NUM_ROWS;
  131. int cols = TEST_NUM_COLS;
  132. float **A, **B, **C, *x, *y, a = 0.0, b = 0.0, c = 0.0, d = 0.0;
  133. x = G_alloc_fvector(cols);
  134. y = G_alloc_fvector(rows);
  135. A = G_alloc_fmatrix(rows, cols);
  136. B = G_alloc_fmatrix(rows, cols);
  137. C = G_alloc_fmatrix(rows, cols);
  138. fill_f_vector_scalar(x, 1, cols);
  139. fill_f_vector_scalar(y, 0, rows);
  140. fill_f_vector_scalar(A[0], 1, rows*cols);
  141. fill_f_vector_scalar(B[0], 2, rows*cols);
  142. #pragma omp parallel default(shared)
  143. {
  144. G_math_f_aA_B(A, B, 1.0 , C, rows , cols );
  145. G_math_f_Ax(C, x, y, rows, cols);
  146. }
  147. G_math_f_asum_norm(y, &a, rows);
  148. if(a != 3*rows*cols) {
  149. G_message("Error in G_math_f_aA_B: %f != %f", a, (double)3*rows*cols);
  150. sum++;
  151. }
  152. #pragma omp parallel default(shared)
  153. {
  154. G_math_f_aA_B(A, B, -1.0 , C, rows , cols );
  155. G_math_f_Ax(C, x, y, rows, cols);
  156. }
  157. G_math_f_asum_norm(y, &b, rows);
  158. if(b != rows*cols) {
  159. G_message("Error in G_math_f_aA_B: %f != %f", b, (double)rows*cols);
  160. sum++;
  161. }
  162. #pragma omp parallel default(shared)
  163. {
  164. G_math_f_aA_B(A, B, 2.0 , C, rows , cols );
  165. G_math_f_Ax(C, x, y, rows, cols);
  166. }
  167. G_math_f_asum_norm(y, &c, rows);
  168. if(c != 4*rows*cols) {
  169. G_message("Error in G_math_f_aA_B: %f != %f", c, (double)4*rows*cols);
  170. sum++;
  171. }
  172. G_free_fmatrix(A);
  173. G_free_fmatrix(B);
  174. G_free_fmatrix(C);
  175. A = G_alloc_fmatrix(rows, cols);
  176. B = G_alloc_fmatrix(cols, rows);
  177. C = G_alloc_fmatrix(rows, rows);
  178. G_free_fvector(x);
  179. G_free_fvector(y);
  180. x = G_alloc_fvector(rows);
  181. y = G_alloc_fvector(rows);
  182. fill_f_vector_scalar(x, 1, rows);
  183. fill_f_vector_scalar(y, 0, rows);
  184. fill_f_vector_scalar(A[0], 1, rows*cols);
  185. fill_f_vector_scalar(B[0], 2, rows*cols);
  186. #pragma omp parallel default(shared)
  187. {
  188. G_math_f_AB(A, B, C, rows , cols , cols );
  189. G_math_f_Ax(C, x, y, rows, cols);
  190. }
  191. G_math_f_asum_norm(y, &d, rows);
  192. if(d != 2*rows*cols*cols) {
  193. G_message("Error in G_math_f_AB: %f != %f", d, (double)2*rows*cols*cols);
  194. sum++;
  195. }
  196. if(x)
  197. G_free_fvector(x);
  198. if(y)
  199. G_free_fvector(y);
  200. if(A)
  201. G_free_fmatrix(A);
  202. if(B)
  203. G_free_fmatrix(B);
  204. if(C)
  205. G_free_fmatrix(C);
  206. return sum;
  207. }