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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 "bench.h"
  39. double fabs(double);
  40. #undef POTRF
  41. #ifndef COMPLEX
  42. #ifdef XDOUBLE
  43. #define POTRF BLASFUNC(qpotrf)
  44. #define SYRK BLASFUNC(qsyrk)
  45. #elif defined(DOUBLE)
  46. #define POTRF BLASFUNC(dpotrf)
  47. #define SYRK BLASFUNC(dsyrk)
  48. #else
  49. #define POTRF BLASFUNC(spotrf)
  50. #define SYRK BLASFUNC(ssyrk)
  51. #endif
  52. #else
  53. #ifdef XDOUBLE
  54. #define POTRF BLASFUNC(xpotrf)
  55. #define SYRK BLASFUNC(xherk)
  56. #elif defined(DOUBLE)
  57. #define POTRF BLASFUNC(zpotrf)
  58. #define SYRK BLASFUNC(zherk)
  59. #else
  60. #define POTRF BLASFUNC(cpotrf)
  61. #define SYRK BLASFUNC(cherk)
  62. #endif
  63. #endif
  64. static __inline double getmflops(int ratio, int m, double secs){
  65. double mm = (double)m;
  66. double mulflops, addflops;
  67. if (secs==0.) return 0.;
  68. mulflops = mm * (1./3. + mm * (1./2. + mm * 1./6.));
  69. addflops = 1./6. * mm * (mm * mm - 1);
  70. if (ratio == 1) {
  71. return (mulflops + addflops) / secs * 1.e-6;
  72. } else {
  73. return (2. * mulflops + 6. * addflops) / secs * 1.e-6;
  74. }
  75. }
  76. int main(int argc, char *argv[]){
  77. #ifndef COMPLEX
  78. char *trans[] = {"T", "N"};
  79. #else
  80. char *trans[] = {"C", "N"};
  81. #endif
  82. char *uplo[] = {"U", "L"};
  83. FLOAT alpha[] = {1.0, 0.0};
  84. FLOAT beta [] = {0.0, 0.0};
  85. FLOAT *a, *b;
  86. blasint m, i, j, info, uplos;
  87. int from = 1;
  88. int to = 200;
  89. int step = 1;
  90. FLOAT maxerr;
  91. double time1;
  92. argc--;argv++;
  93. if (argc > 0) { from = atol(*argv); argc--; argv++;}
  94. if (argc > 0) { to = MAX(atol(*argv), from); argc--; argv++;}
  95. if (argc > 0) { step = atol(*argv); argc--; argv++;}
  96. fprintf(stderr, "From : %3d To : %3d Step = %3d\n", from, to, step);
  97. if (( a = (FLOAT *)malloc(sizeof(FLOAT) * to * to * COMPSIZE)) == NULL){
  98. fprintf(stderr,"Out of Memory!!\n");exit(1);
  99. }
  100. if (( b = (FLOAT *)malloc(sizeof(FLOAT) * to * to * COMPSIZE)) == NULL){
  101. fprintf(stderr,"Out of Memory!!\n");exit(1);
  102. }
  103. for(m = from; m <= to; m += step){
  104. fprintf(stderr, "M = %6d : ", (int)m);
  105. for (uplos = 0; uplos < 2; uplos ++) {
  106. #ifndef COMPLEX
  107. if (uplos & 1) {
  108. for (j = 0; j < m; j++) {
  109. for(i = 0; i < j; i++) a[(long)i + (long)j * (long)m] = 0.;
  110. a[(long)j + (long)j * (long)m] = ((double) rand() / (double) RAND_MAX) + 8.;
  111. for(i = j + 1; i < m; i++) a[(long)i + (long)j * (long)m] = ((double) rand() / (double) RAND_MAX) - 0.5;
  112. }
  113. } else {
  114. for (j = 0; j < m; j++) {
  115. for(i = 0; i < j; i++) a[(long)i + (long)j * (long)m] = ((double) rand() / (double) RAND_MAX) - 0.5;
  116. a[(long)j + (long)j * (long)m] = ((double) rand() / (double) RAND_MAX) + 8.;
  117. for(i = j + 1; i < m; i++) a[(long)i + (long)j * (long)m] = 0.;
  118. }
  119. }
  120. #else
  121. if (uplos & 1) {
  122. for (j = 0; j < m; j++) {
  123. for(i = 0; i < j; i++) {
  124. a[((long)i + (long)j * (long)m) * 2 + 0] = 0.;
  125. a[((long)i + (long)j * (long)m) * 2 + 1] = 0.;
  126. }
  127. a[((long)j + (long)j * (long)m) * 2 + 0] = ((double) rand() / (double) RAND_MAX) + 8.;
  128. a[((long)j + (long)j * (long)m) * 2 + 1] = 0.;
  129. for(i = j + 1; i < m; i++) {
  130. a[((long)i + (long)j * (long)m) * 2 + 0] = ((double) rand() / (double) RAND_MAX) - 0.5;
  131. a[((long)i + (long)j * (long)m) * 2 + 1] = ((double) rand() / (double) RAND_MAX) - 0.5;
  132. }
  133. }
  134. } else {
  135. for (j = 0; j < m; j++) {
  136. for(i = 0; i < j; i++) {
  137. a[((long)i + (long)j * (long)m) * 2 + 0] = ((double) rand() / (double) RAND_MAX) - 0.5;
  138. a[((long)i + (long)j * (long)m) * 2 + 1] = ((double) rand() / (double) RAND_MAX) - 0.5;
  139. }
  140. a[((long)j + (long)j * (long)m) * 2 + 0] = ((double) rand() / (double) RAND_MAX) + 8.;
  141. a[((long)j + (long)j * (long)m) * 2 + 1] = 0.;
  142. for(i = j + 1; i < m; i++) {
  143. a[((long)i + (long)j * (long)m) * 2 + 0] = 0.;
  144. a[((long)i + (long)j * (long)m) * 2 + 1] = 0.;
  145. }
  146. }
  147. }
  148. #endif
  149. SYRK(uplo[uplos], trans[uplos], &m, &m, alpha, a, &m, beta, b, &m);
  150. begin();
  151. POTRF(uplo[uplos], &m, b, &m, &info);
  152. end();
  153. if (info != 0) {
  154. fprintf(stderr, "Info = %d\n", info);
  155. exit(1);
  156. }
  157. time1 = getsec();
  158. if (!(uplos & 1)) {
  159. for (j = 0; j < m; j++) {
  160. for(i = 0; i <= j; i++) {
  161. #ifndef COMPLEX
  162. if (maxerr < fabs(a[(long)i + (long)j * (long)m] - b[(long)i + (long)j * (long)m]))
  163. maxerr = fabs(a[(long)i + (long)j * (long)m] - b[(long)i + (long)j * (long)m]);
  164. #else
  165. if (maxerr < fabs(a[((long)i + (long)j * (long)m) * 2 + 0] - b[((long)i + (long)j * (long)m) * 2 + 0]))
  166. maxerr = fabs(a[((long)i + (long)j * (long)m) * 2 + 0] - b[((long)i + (long)j * (long)m) * 2 + 0]);
  167. if (maxerr < fabs(a[((long)i + (long)j * (long)m) * 2 + 1] - b[((long)i + (long)j * (long)m) * 2 + 1]))
  168. maxerr = fabs(a[((long)i + (long)j * (long)m) * 2 + 1] - b[((long)i + (long)j * (long)m) * 2 + 1]);
  169. #endif
  170. }
  171. }
  172. } else {
  173. for (j = 0; j < m; j++) {
  174. for(i = j; i < m; i++) {
  175. #ifndef COMPLEX
  176. if (maxerr < fabs(a[(long)i + (long)j * (long)m] - b[(long)i + (long)j * (long)m]))
  177. maxerr = fabs(a[(long)i + (long)j * (long)m] - b[(long)i + (long)j * (long)m]);
  178. #else
  179. if (maxerr < fabs(a[((long)i + (long)j * (long)m) * 2 + 0] - b[((long)i + (long)j * (long)m) * 2 + 0]))
  180. maxerr = fabs(a[((long)i + (long)j * (long)m) * 2 + 0] - b[((long)i + (long)j * (long)m) * 2 + 0]);
  181. if (maxerr < fabs(a[((long)i + (long)j * (long)m) * 2 + 1] - b[((long)i + (long)j * (long)m) * 2 + 1]))
  182. maxerr = fabs(a[((long)i + (long)j * (long)m) * 2 + 1] - b[((long)i + (long)j * (long)m) * 2 + 1]);
  183. #endif
  184. }
  185. }
  186. }
  187. fprintf(stderr,
  188. #ifdef XDOUBLE
  189. " %Le %10.3f MFlops", maxerr,
  190. #else
  191. " %e %10.3f MFlops", maxerr,
  192. #endif
  193. getmflops(COMPSIZE * COMPSIZE, m, time1));
  194. if (maxerr > 1.e-3) {
  195. fprintf(stderr, "Hmm, probably it has bug.\n");
  196. exit(1);
  197. }
  198. }
  199. fprintf(stderr, "\n");
  200. }
  201. return 0;
  202. }
  203. // void main(int argc, char *argv[]) __attribute__((weak, alias("MAIN__")));