You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_dsum.c 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*****************************************************************************
  2. Copyright (c) 2023, The OpenBLAS Project
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. 3. Neither the name of the OpenBLAS project nor the names of
  14. its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written
  16. permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  26. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. **********************************************************************************/
  28. #include "utest/openblas_utest.h"
  29. #include <cblas.h>
  30. #define ELEMENTS 50
  31. #define INCREMENT 2
  32. #ifdef BUILD_DOUBLE
  33. /**
  34. * Fortran API specific test
  35. * Test dsum by comparing it against pre-calculated values
  36. */
  37. CTEST(dsum, bad_args_N_0){
  38. blasint i;
  39. blasint N = 0, inc = 1;
  40. double x[ELEMENTS];
  41. for (i = 0; i < ELEMENTS * inc; i ++) {
  42. x[i] = 1000 - i;
  43. }
  44. double sum = BLASFUNC(dsum)(&N, x, &inc);
  45. ASSERT_DBL_NEAR_TOL(0.0, sum, DOUBLE_EPS);
  46. }
  47. /**
  48. * Fortran API specific test
  49. * Test dsum by comparing it against pre-calculated values
  50. */
  51. CTEST(dsum, step_zero){
  52. blasint i;
  53. blasint N = ELEMENTS, inc = 0;
  54. double x[ELEMENTS];
  55. x[0]=0.;
  56. for (i = 0; i < N * inc; i ++) {
  57. x[i] = i + 1000;
  58. }
  59. x[8] = 0.0;
  60. double sum = BLASFUNC(dsum)(&N, x, &inc);
  61. ASSERT_DBL_NEAR_TOL(0.0, sum, DOUBLE_EPS);
  62. }
  63. /**
  64. * Fortran API specific test
  65. * Test dsum by comparing it against pre-calculated values
  66. */
  67. CTEST(dsum, step_1_N_1){
  68. blasint N = 1, inc = 1;
  69. double x[] = {1.1};
  70. double sum = BLASFUNC(dsum)(&N, x, &inc);
  71. ASSERT_DBL_NEAR_TOL(1.1, sum, DOUBLE_EPS);
  72. }
  73. /**
  74. * Fortran API specific test
  75. * Test dsum by comparing it against pre-calculated values
  76. */
  77. CTEST(dsum, step_2_N_1){
  78. blasint N = 1, inc = 2;
  79. double x[] = {1.1, 0.0};
  80. double sum = BLASFUNC(dsum)(&N, x, &inc);
  81. ASSERT_DBL_NEAR_TOL(1.1, sum, DOUBLE_EPS);
  82. }
  83. /**
  84. * Fortran API specific test
  85. * Test dsum by comparing it against pre-calculated values
  86. */
  87. CTEST(dsum, step_1_N_2){
  88. blasint N = 2, inc = 1;
  89. double x[] = {1.1, -1.0};
  90. double sum = BLASFUNC(dsum)(&N, x, &inc);
  91. ASSERT_DBL_NEAR_TOL(0.1, sum, DOUBLE_EPS);
  92. }
  93. /**
  94. * Fortran API specific test
  95. * Test dsum by comparing it against pre-calculated values
  96. */
  97. CTEST(dsum, step_2_N_2){
  98. blasint N = 2, inc = 2;
  99. double x[] = {1.1, -1.5, 1.0, 1.0};
  100. double sum = BLASFUNC(dsum)(&N, x, &inc);
  101. ASSERT_DBL_NEAR_TOL(2.1, sum, DOUBLE_EPS);
  102. }
  103. /**
  104. * Fortran API specific test
  105. * Test dsum by comparing it against pre-calculated values
  106. */
  107. CTEST(dsum, step_1_N_3){
  108. blasint N = 3, inc = 1;
  109. double x[] = {1.1, 1.0, 2.2};
  110. double sum = BLASFUNC(dsum)(&N, x, &inc);
  111. ASSERT_DBL_NEAR_TOL(4.3, sum, DOUBLE_EPS);
  112. }
  113. /**
  114. * Fortran API specific test
  115. * Test dsum by comparing it against pre-calculated values
  116. */
  117. CTEST(dsum, step_2_N_3){
  118. blasint N = 3, inc = 2;
  119. double x[] = {1.1, 0.0, -1.0, -3.0, 2.2, 3.0};
  120. double sum = BLASFUNC(dsum)(&N, x, &inc);
  121. ASSERT_DBL_NEAR_TOL(2.3, sum, DOUBLE_EPS);
  122. }
  123. /**
  124. * Fortran API specific test
  125. * Test dsum by comparing it against pre-calculated values
  126. */
  127. CTEST(dsum, step_1_N_4){
  128. blasint N = 4, inc = 1;
  129. double x[] = {1.1, 1.0, -2.2, 3.3};
  130. double sum = BLASFUNC(dsum)(&N, x, &inc);
  131. ASSERT_DBL_NEAR_TOL(3.2, sum, DOUBLE_EPS);
  132. }
  133. /**
  134. * Fortran API specific test
  135. * Test dsum by comparing it against pre-calculated values
  136. */
  137. CTEST(dsum, step_2_N_4){
  138. blasint N = 4, inc = 2;
  139. double x[] = {1.1, 0.0, 1.0, 2.0, 2.2, 2.7, -3.3, -5.9};
  140. double sum = BLASFUNC(dsum)(&N, x, &inc);
  141. ASSERT_DBL_NEAR_TOL(1.0, sum, DOUBLE_EPS);
  142. }
  143. /**
  144. * Fortran API specific test
  145. * Test dsum by comparing it against pre-calculated values
  146. */
  147. CTEST(dsum, step_1_N_5){
  148. blasint N = 5, inc = 1;
  149. double x[] = {0.0, 1.0, 2.2, 3.3, 0.0};
  150. double sum = BLASFUNC(dsum)(&N, x, &inc);
  151. ASSERT_DBL_NEAR_TOL(6.5, sum, DOUBLE_EPS);
  152. }
  153. /**
  154. * Fortran API specific test
  155. * Test dsum by comparing it against pre-calculated values
  156. */
  157. CTEST(dsum, step_2_N_5){
  158. blasint N = 5, inc = 2;
  159. double x[] = {0.0, 3.0, 1.0, -2.2, 2.2, -1.7, 3.3, 14.5, 0.0, -9.0};
  160. double sum = BLASFUNC(dsum)(&N, x, &inc);
  161. ASSERT_DBL_NEAR_TOL(6.5, sum, DOUBLE_EPS);
  162. }
  163. /**
  164. * Fortran API specific test
  165. * Test dsum by comparing it against pre-calculated values
  166. */
  167. CTEST(dsum, step_1_N_50){
  168. blasint i;
  169. blasint N = ELEMENTS, inc = 1;
  170. double x[ELEMENTS];
  171. for (i = 0; i < N * inc; i ++) {
  172. x[i] = (i & 1) ? -1.0 : 1.0;
  173. }
  174. double sum = BLASFUNC(dsum)(&N, x, &inc);
  175. ASSERT_DBL_NEAR_TOL(0.0, sum, DOUBLE_EPS);
  176. }
  177. /**
  178. * Fortran API specific test
  179. * Test dsum by comparing it against pre-calculated values
  180. */
  181. CTEST(dsum, step_2_N_50){
  182. blasint i;
  183. blasint N = ELEMENTS, inc = INCREMENT;
  184. double x[ELEMENTS * INCREMENT];
  185. for (i = 0; i < N * inc; i ++) {
  186. x[i] = (i & 1) ? -1.0 : 1.0;
  187. }
  188. double sum = BLASFUNC(dsum)(&N, x, &inc);
  189. ASSERT_DBL_NEAR_TOL(50.0, sum, DOUBLE_EPS);
  190. }
  191. #ifndef NO_CBLAS
  192. /**
  193. * C API specific test
  194. * Test dsum by comparing it against pre-calculated values
  195. */
  196. CTEST(dsum, c_api_bad_args_N_0){
  197. blasint i;
  198. blasint N = 0, inc = 1;
  199. double x[ELEMENTS];
  200. for (i = 0; i < ELEMENTS * inc; i ++) {
  201. x[i] = 1000 - i;
  202. }
  203. double sum = cblas_dsum(N, x, inc);
  204. ASSERT_DBL_NEAR_TOL(0.0, sum, DOUBLE_EPS);
  205. }
  206. /**
  207. * C API specific test
  208. * Test dsum by comparing it against pre-calculated values
  209. */
  210. CTEST(dsum, c_api_step_zero){
  211. blasint i;
  212. blasint N = ELEMENTS, inc = 0;
  213. double x[ELEMENTS];
  214. x[0]=0.;
  215. for (i = 0; i < N * inc; i ++) {
  216. x[i] = i + 1000;
  217. }
  218. x[8] = 0.0;
  219. double sum = cblas_dsum(N, x, inc);
  220. ASSERT_DBL_NEAR_TOL(0.0, sum, DOUBLE_EPS);
  221. }
  222. /**
  223. * C API specific test
  224. * Test dsum by comparing it against pre-calculated values
  225. */
  226. CTEST(dsum, c_api_step_1_N_1){
  227. blasint N = 1, inc = 1;
  228. double x[] = {1.1};
  229. double sum = cblas_dsum(N, x, inc);
  230. ASSERT_DBL_NEAR_TOL(1.1, sum, DOUBLE_EPS);
  231. }
  232. /**
  233. * C API specific test
  234. * Test dsum by comparing it against pre-calculated values
  235. */
  236. CTEST(dsum, c_api_step_2_N_1){
  237. blasint N = 1, inc = 2;
  238. double x[] = {1.1, 0.0};
  239. double sum = cblas_dsum(N, x, inc);
  240. ASSERT_DBL_NEAR_TOL(1.1, sum, DOUBLE_EPS);
  241. }
  242. /**
  243. * C API specific test
  244. * Test dsum by comparing it against pre-calculated values
  245. */
  246. CTEST(dsum, c_api_step_1_N_2){
  247. blasint N = 2, inc = 1;
  248. double x[] = {1.1, -1.0};
  249. double sum = cblas_dsum(N, x, inc);
  250. ASSERT_DBL_NEAR_TOL(0.1, sum, DOUBLE_EPS);
  251. }
  252. /**
  253. * C API specific test
  254. * Test dsum by comparing it against pre-calculated values
  255. */
  256. CTEST(dsum, c_api_step_2_N_2){
  257. blasint N = 2, inc = 2;
  258. double x[] = {1.1, -1.5, 1.0, 1.0};
  259. double sum = cblas_dsum(N, x, inc);
  260. ASSERT_DBL_NEAR_TOL(2.1, sum, DOUBLE_EPS);
  261. }
  262. /**
  263. * C API specific test
  264. * Test dsum by comparing it against pre-calculated values
  265. */
  266. CTEST(dsum, c_api_step_1_N_3){
  267. blasint N = 3, inc = 1;
  268. double x[] = {1.1, 1.0, 2.2};
  269. double sum = cblas_dsum(N, x, inc);
  270. ASSERT_DBL_NEAR_TOL(4.3, sum, DOUBLE_EPS);
  271. }
  272. /**
  273. * C API specific test
  274. * Test dsum by comparing it against pre-calculated values
  275. */
  276. CTEST(dsum, c_api_step_2_N_3){
  277. blasint N = 3, inc = 2;
  278. double x[] = {1.1, 0.0, -1.0, -3.0, 2.2, 3.0};
  279. double sum = cblas_dsum(N, x, inc);
  280. ASSERT_DBL_NEAR_TOL(2.3, sum, DOUBLE_EPS);
  281. }
  282. /**
  283. * C API specific test
  284. * Test dsum by comparing it against pre-calculated values
  285. */
  286. CTEST(dsum, c_api_step_1_N_4){
  287. blasint N = 4, inc = 1;
  288. double x[] = {1.1, 1.0, -2.2, 3.3};
  289. double sum = cblas_dsum(N, x, inc);
  290. ASSERT_DBL_NEAR_TOL(3.2, sum, DOUBLE_EPS);
  291. }
  292. /**
  293. * C API specific test
  294. * Test dsum by comparing it against pre-calculated values
  295. */
  296. CTEST(dsum, c_api_step_2_N_4){
  297. blasint N = 4, inc = 2;
  298. double x[] = {1.1, 0.0, 1.0, 2.0, 2.2, 2.7, -3.3, -5.9};
  299. double sum = cblas_dsum(N, x, inc);
  300. ASSERT_DBL_NEAR_TOL(1.0, sum, DOUBLE_EPS);
  301. }
  302. /**
  303. * C API specific test
  304. * Test dsum by comparing it against pre-calculated values
  305. */
  306. CTEST(dsum, c_api_step_1_N_5){
  307. blasint N = 5, inc = 1;
  308. double x[] = {0.0, 1.0, 2.2, 3.3, 0.0};
  309. double sum = cblas_dsum(N, x, inc);
  310. ASSERT_DBL_NEAR_TOL(6.5, sum, DOUBLE_EPS);
  311. }
  312. /**
  313. * C API specific test
  314. * Test dsum by comparing it against pre-calculated values
  315. */
  316. CTEST(dsum, c_api_step_2_N_5){
  317. blasint N = 5, inc = 2;
  318. double x[] = {0.0, 3.0, 1.0, -2.2, 2.2, -1.7, 3.3, 14.5, 0.0, -9.0};
  319. double sum = cblas_dsum(N, x, inc);
  320. ASSERT_DBL_NEAR_TOL(6.5, sum, DOUBLE_EPS);
  321. }
  322. /**
  323. * C API specific test
  324. * Test dsum by comparing it against pre-calculated values
  325. */
  326. CTEST(dsum, c_api_step_1_N_50){
  327. blasint i;
  328. blasint N = ELEMENTS, inc = 1;
  329. double x[ELEMENTS];
  330. for (i = 0; i < N * inc; i ++) {
  331. x[i] = (i & 1) ? -1.0 : 1.0;
  332. }
  333. double sum = cblas_dsum(N, x, inc);
  334. ASSERT_DBL_NEAR_TOL(0.0, sum, DOUBLE_EPS);
  335. }
  336. /**
  337. * C API specific test
  338. * Test dsum by comparing it against pre-calculated values
  339. */
  340. CTEST(dsum, c_api_step_2_N_50){
  341. blasint i;
  342. blasint N = ELEMENTS, inc = INCREMENT;
  343. double x[ELEMENTS * INCREMENT];
  344. for (i = 0; i < N * inc; i ++) {
  345. x[i] = (i & 1) ? -1.0 : 1.0;
  346. }
  347. double sum = cblas_dsum(N, x, inc);
  348. ASSERT_DBL_NEAR_TOL(50.0, sum, DOUBLE_EPS);
  349. }
  350. #endif
  351. #endif