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.

cholesky.c 8.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*********************************************************************/
  2. /* Copyright 2009, 2010 The University of Texas at Austin. */
  3. /* All rights reserved. */
  4. /* */
  5. /* Redistribution and use in source and binary forms, with or */
  6. /* without modification, are permitted provided that the following */
  7. /* conditions are met: */
  8. /* */
  9. /* 1. Redistributions of source code must retain the above */
  10. /* copyright notice, this list of conditions and the following */
  11. /* disclaimer. */
  12. /* */
  13. /* 2. Redistributions in binary form must reproduce the above */
  14. /* copyright notice, this list of conditions and the following */
  15. /* disclaimer in the documentation and/or other materials */
  16. /* provided with the distribution. */
  17. /* */
  18. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  19. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  20. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  21. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  22. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  23. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  24. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  25. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  26. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  27. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  28. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  29. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  30. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  31. /* POSSIBILITY OF SUCH DAMAGE. */
  32. /* */
  33. /* The views and conclusions contained in the software and */
  34. /* documentation are those of the authors and should not be */
  35. /* interpreted as representing official policies, either expressed */
  36. /* or implied, of The University of Texas at Austin. */
  37. /*********************************************************************/
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #ifdef __CYGWIN32__
  41. #include <sys/time.h>
  42. #endif
  43. #include "common.h"
  44. double fabs(double);
  45. #undef POTRF
  46. #ifndef COMPLEX
  47. #ifdef XDOUBLE
  48. #define POTRF BLASFUNC(qpotrf)
  49. #define SYRK BLASFUNC(qsyrk)
  50. #elif defined(DOUBLE)
  51. #define POTRF BLASFUNC(dpotrf)
  52. #define SYRK BLASFUNC(dsyrk)
  53. #else
  54. #define POTRF BLASFUNC(spotrf)
  55. #define SYRK BLASFUNC(ssyrk)
  56. #endif
  57. #else
  58. #ifdef XDOUBLE
  59. #define POTRF BLASFUNC(xpotrf)
  60. #define SYRK BLASFUNC(xherk)
  61. #elif defined(DOUBLE)
  62. #define POTRF BLASFUNC(zpotrf)
  63. #define SYRK BLASFUNC(zherk)
  64. #else
  65. #define POTRF BLASFUNC(cpotrf)
  66. #define SYRK BLASFUNC(cherk)
  67. #endif
  68. #endif
  69. #if defined(__WIN32__) || defined(__WIN64__)
  70. int gettimeofday(struct timeval *tv, void *tz){
  71. FILETIME ft;
  72. unsigned __int64 tmpres = 0;
  73. static int tzflag;
  74. if (NULL != tv)
  75. {
  76. GetSystemTimeAsFileTime(&ft);
  77. tmpres |= ft.dwHighDateTime;
  78. tmpres <<= 32;
  79. tmpres |= ft.dwLowDateTime;
  80. /*converting file time to unix epoch*/
  81. tmpres /= 10; /*convert into microseconds*/
  82. tmpres -= DELTA_EPOCH_IN_MICROSECS;
  83. tv->tv_sec = (long)(tmpres / 1000000UL);
  84. tv->tv_usec = (long)(tmpres % 1000000UL);
  85. }
  86. return 0;
  87. }
  88. #endif
  89. static __inline double getmflops(int ratio, int m, double secs){
  90. double mm = (double)m;
  91. double mulflops, addflops;
  92. if (secs==0.) return 0.;
  93. mulflops = mm * (1./3. + mm * (1./2. + mm * 1./6.));
  94. addflops = 1./6. * mm * (mm * mm - 1);
  95. if (ratio == 1) {
  96. return (mulflops + addflops) / secs * 1.e-6;
  97. } else {
  98. return (2. * mulflops + 6. * addflops) / secs * 1.e-6;
  99. }
  100. }
  101. int MAIN__(int argc, char *argv[]){
  102. #ifndef COMPLEX
  103. char *trans[] = {"T", "N"};
  104. #else
  105. char *trans[] = {"C", "N"};
  106. #endif
  107. char *uplo[] = {"U", "L"};
  108. FLOAT alpha[] = {1.0, 0.0};
  109. FLOAT beta [] = {0.0, 0.0};
  110. FLOAT *a, *b;
  111. blasint m, i, j, info, uplos;
  112. int from = 1;
  113. int to = 200;
  114. int step = 1;
  115. FLOAT maxerr;
  116. struct timeval start, stop;
  117. double time1;
  118. argc--;argv++;
  119. if (argc > 0) { from = atol(*argv); argc--; argv++;}
  120. if (argc > 0) { to = MAX(atol(*argv), from); argc--; argv++;}
  121. if (argc > 0) { step = atol(*argv); argc--; argv++;}
  122. fprintf(stderr, "From : %3d To : %3d Step = %3d\n", from, to, step);
  123. if (( a = (FLOAT *)malloc(sizeof(FLOAT) * to * to * COMPSIZE)) == NULL){
  124. fprintf(stderr,"Out of Memory!!\n");exit(1);
  125. }
  126. if (( b = (FLOAT *)malloc(sizeof(FLOAT) * to * to * COMPSIZE)) == NULL){
  127. fprintf(stderr,"Out of Memory!!\n");exit(1);
  128. }
  129. for(m = from; m <= to; m += step){
  130. fprintf(stderr, "M = %6d : ", (int)m);
  131. for (uplos = 0; uplos < 2; uplos ++) {
  132. #ifndef COMPLEX
  133. if (uplos & 1) {
  134. for (j = 0; j < m; j++) {
  135. for(i = 0; i < j; i++) a[i + j * m] = 0.;
  136. a[j + j * m] = ((double) rand() / (double) RAND_MAX) + 8.;
  137. for(i = j + 1; i < m; i++) a[i + j * m] = ((double) rand() / (double) RAND_MAX) - 0.5;
  138. }
  139. } else {
  140. for (j = 0; j < m; j++) {
  141. for(i = 0; i < j; i++) a[i + j * m] = ((double) rand() / (double) RAND_MAX) - 0.5;
  142. a[j + j * m] = ((double) rand() / (double) RAND_MAX) + 8.;
  143. for(i = j + 1; i < m; i++) a[i + j * m] = 0.;
  144. }
  145. }
  146. #else
  147. if (uplos & 1) {
  148. for (j = 0; j < m; j++) {
  149. for(i = 0; i < j; i++) {
  150. a[(i + j * m) * 2 + 0] = 0.;
  151. a[(i + j * m) * 2 + 1] = 0.;
  152. }
  153. a[(j + j * m) * 2 + 0] = ((double) rand() / (double) RAND_MAX) + 8.;
  154. a[(j + j * m) * 2 + 1] = 0.;
  155. for(i = j + 1; i < m; i++) {
  156. a[(i + j * m) * 2 + 0] = ((double) rand() / (double) RAND_MAX) - 0.5;
  157. a[(i + j * m) * 2 + 1] = ((double) rand() / (double) RAND_MAX) - 0.5;
  158. }
  159. }
  160. } else {
  161. for (j = 0; j < m; j++) {
  162. for(i = 0; i < j; i++) {
  163. a[(i + j * m) * 2 + 0] = ((double) rand() / (double) RAND_MAX) - 0.5;
  164. a[(i + j * m) * 2 + 1] = ((double) rand() / (double) RAND_MAX) - 0.5;
  165. }
  166. a[(j + j * m) * 2 + 0] = ((double) rand() / (double) RAND_MAX) + 8.;
  167. a[(j + j * m) * 2 + 1] = 0.;
  168. for(i = j + 1; i < m; i++) {
  169. a[(i + j * m) * 2 + 0] = 0.;
  170. a[(i + j * m) * 2 + 1] = 0.;
  171. }
  172. }
  173. }
  174. #endif
  175. SYRK(uplo[uplos], trans[uplos], &m, &m, alpha, a, &m, beta, b, &m);
  176. gettimeofday( &start, (struct timezone *)0);
  177. POTRF(uplo[uplos], &m, b, &m, &info);
  178. gettimeofday( &stop, (struct timezone *)0);
  179. if (info != 0) {
  180. fprintf(stderr, "Info = %d\n", info);
  181. exit(1);
  182. }
  183. time1 = (double)(stop.tv_sec - start.tv_sec) + (double)((stop.tv_usec - start.tv_usec)) * 1.e-6;
  184. maxerr = 0.;
  185. if (!(uplos & 1)) {
  186. for (j = 0; j < m; j++) {
  187. for(i = 0; i <= j; i++) {
  188. #ifndef COMPLEX
  189. if (maxerr < fabs(a[i + j * m] - b[i + j * m])) maxerr = fabs(a[i + j * m] - b[i + j * m]);
  190. #else
  191. if (maxerr < fabs(a[(i + j * m) * 2 + 0] - b[(i + j * m) * 2 + 0])) maxerr = fabs(a[(i + j * m) * 2 + 0] - b[(i + j * m) * 2 + 0]);
  192. if (maxerr < fabs(a[(i + j * m) * 2 + 1] - b[(i + j * m) * 2 + 1])) maxerr = fabs(a[(i + j * m) * 2 + 1] - b[(i + j * m) * 2 + 1]);
  193. #endif
  194. }
  195. }
  196. } else {
  197. for (j = 0; j < m; j++) {
  198. for(i = j; i < m; i++) {
  199. #ifndef COMPLEX
  200. if (maxerr < fabs(a[i + j * m] - b[i + j * m])) maxerr = fabs(a[i + j * m] - b[i + j * m]);
  201. #else
  202. if (maxerr < fabs(a[(i + j * m) * 2 + 0] - b[(i + j * m) * 2 + 0])) maxerr = fabs(a[(i + j * m) * 2 + 0] - b[(i + j * m) * 2 + 0]);
  203. if (maxerr < fabs(a[(i + j * m) * 2 + 1] - b[(i + j * m) * 2 + 1])) maxerr = fabs(a[(i + j * m) * 2 + 1] - b[(i + j * m) * 2 + 1]);
  204. #endif
  205. }
  206. }
  207. }
  208. fprintf(stderr,
  209. #ifdef XDOUBLE
  210. " %Le %10.3f MFlops", maxerr,
  211. #else
  212. " %e %10.3f MFlops", maxerr,
  213. #endif
  214. getmflops(COMPSIZE * COMPSIZE, m, time1));
  215. if (maxerr > 1.e-3) {
  216. fprintf(stderr, "Hmm, probably it has bug.\n");
  217. exit(1);
  218. }
  219. }
  220. fprintf(stderr, "\n");
  221. }
  222. return 0;
  223. }
  224. void main(int argc, char *argv[]) __attribute__((weak, alias("MAIN__")));