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.

linpack.c 8.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 GETRF
  46. #undef GETRS
  47. #ifndef COMPLEX
  48. #ifdef XDOUBLE
  49. #define GETRF BLASFUNC(qgetrf)
  50. #define GETRS BLASFUNC(qgetrs)
  51. #elif defined(DOUBLE)
  52. #define GETRF BLASFUNC(dgetrf)
  53. #define GETRS BLASFUNC(dgetrs)
  54. #else
  55. #define GETRF BLASFUNC(sgetrf)
  56. #define GETRS BLASFUNC(sgetrs)
  57. #endif
  58. #else
  59. #ifdef XDOUBLE
  60. #define GETRF BLASFUNC(xgetrf)
  61. #define GETRS BLASFUNC(xgetrs)
  62. #elif defined(DOUBLE)
  63. #define GETRF BLASFUNC(zgetrf)
  64. #define GETRS BLASFUNC(zgetrs)
  65. #else
  66. #define GETRF BLASFUNC(cgetrf)
  67. #define GETRS BLASFUNC(cgetrs)
  68. #endif
  69. #endif
  70. #if defined(__WIN32__) || defined(__WIN64__)
  71. #ifndef DELTA_EPOCH_IN_MICROSECS
  72. #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
  73. #endif
  74. int gettimeofday(struct timeval *tv, void *tz){
  75. FILETIME ft;
  76. unsigned __int64 tmpres = 0;
  77. static int tzflag;
  78. if (NULL != tv)
  79. {
  80. GetSystemTimeAsFileTime(&ft);
  81. tmpres |= ft.dwHighDateTime;
  82. tmpres <<= 32;
  83. tmpres |= ft.dwLowDateTime;
  84. /*converting file time to unix epoch*/
  85. tmpres /= 10; /*convert into microseconds*/
  86. tmpres -= DELTA_EPOCH_IN_MICROSECS;
  87. tv->tv_sec = (long)(tmpres / 1000000UL);
  88. tv->tv_usec = (long)(tmpres % 1000000UL);
  89. }
  90. return 0;
  91. }
  92. #endif
  93. #if !defined(__WIN32__) && !defined(__WIN64__) && !defined(__CYGWIN32__) && 0
  94. static void *huge_malloc(BLASLONG size){
  95. int shmid;
  96. void *address;
  97. #ifndef SHM_HUGETLB
  98. #define SHM_HUGETLB 04000
  99. #endif
  100. if ((shmid =shmget(IPC_PRIVATE,
  101. (size + HUGE_PAGESIZE) & ~(HUGE_PAGESIZE - 1),
  102. SHM_HUGETLB | IPC_CREAT |0600)) < 0) {
  103. printf( "Memory allocation failed(shmget).\n");
  104. exit(1);
  105. }
  106. address = shmat(shmid, NULL, SHM_RND);
  107. if ((BLASLONG)address == -1){
  108. printf( "Memory allocation failed(shmat).\n");
  109. exit(1);
  110. }
  111. shmctl(shmid, IPC_RMID, 0);
  112. return address;
  113. }
  114. #define malloc huge_malloc
  115. #endif
  116. int main(int argc, char *argv[]){
  117. FLOAT *a, *b;
  118. blasint *ipiv;
  119. blasint m, i, j, info;
  120. blasint unit = 1;
  121. int from = 1;
  122. int to = 200;
  123. int step = 1;
  124. FLOAT maxerr;
  125. struct timeval start, stop;
  126. double time1, time2;
  127. argc--;argv++;
  128. if (argc > 0) { from = atol(*argv); argc--; argv++;}
  129. if (argc > 0) { to = MAX(atol(*argv), from); argc--; argv++;}
  130. if (argc > 0) { step = atol(*argv); argc--; argv++;}
  131. fprintf(stderr, "From : %3d To : %3d Step = %3d\n", from, to, step);
  132. if (( a = (FLOAT *)malloc(sizeof(FLOAT) * to * to * COMPSIZE)) == NULL){
  133. fprintf(stderr,"Out of Memory!!\n");exit(1);
  134. }
  135. if (( b = (FLOAT *)malloc(sizeof(FLOAT) * to * COMPSIZE)) == NULL){
  136. fprintf(stderr,"Out of Memory!!\n");exit(1);
  137. }
  138. if (( ipiv = (blasint *)malloc(sizeof(blasint) * to * COMPSIZE)) == NULL){
  139. fprintf(stderr,"Out of Memory!!\n");exit(1);
  140. }
  141. #ifdef linux
  142. srandom(getpid());
  143. #endif
  144. fprintf(stderr, " SIZE Residual Decompose Solve Total\n");
  145. for(m = from; m <= to; m += step){
  146. fprintf(stderr, " %6d : ", (int)m);
  147. for(j = 0; j < m; j++){
  148. for(i = 0; i < m * COMPSIZE; i++){
  149. a[i + j * m * COMPSIZE] = ((FLOAT) rand() / (FLOAT) RAND_MAX) - 0.5;
  150. }
  151. }
  152. for (i = 0; i < m * COMPSIZE; ++i) b[i] = 0.;
  153. for (j = 0; j < m; ++j) {
  154. for (i = 0; i < m * COMPSIZE; ++i) {
  155. b[i] += a[i + j * m * COMPSIZE];
  156. }
  157. }
  158. gettimeofday( &start, (struct timezone *)0);
  159. GETRF (&m, &m, a, &m, ipiv, &info);
  160. gettimeofday( &stop, (struct timezone *)0);
  161. if (info) {
  162. fprintf(stderr, "Matrix is not singular .. %d\n", info);
  163. exit(1);
  164. }
  165. time1 = (double)(stop.tv_sec - start.tv_sec) + (double)((stop.tv_usec - start.tv_usec)) * 1.e-6;
  166. gettimeofday( &start, (struct timezone *)0);
  167. GETRS("N", &m, &unit, a, &m, ipiv, b, &m, &info);
  168. gettimeofday( &stop, (struct timezone *)0);
  169. if (info) {
  170. fprintf(stderr, "Matrix is not singular .. %d\n", info);
  171. exit(1);
  172. }
  173. time2 = (double)(stop.tv_sec - start.tv_sec) + (double)((stop.tv_usec - start.tv_usec)) * 1.e-6;
  174. maxerr = 0.;
  175. for(i = 0; i < m; i++){
  176. #ifndef XDOUBLE
  177. if (maxerr < fabs(b[i * COMPSIZE] - 1.0)) maxerr = fabs(b[i * COMPSIZE] - 1.0);
  178. #ifdef COMPLEX
  179. if (maxerr < fabs(b[i * COMPSIZE] + 1)) maxerr = fabs(b[i * COMPSIZE + 1]);
  180. #endif
  181. #else
  182. if (maxerr < fabsl(b[i * COMPSIZE] - 1.0L)) maxerr = fabsl(b[i * COMPSIZE] - 1.0L);
  183. #ifdef COMPLEX
  184. if (maxerr < fabsl(b[i * COMPSIZE] + 1)) maxerr = fabsl(b[i * COMPSIZE + 1]);
  185. #endif
  186. #endif
  187. }
  188. #ifdef XDOUBLE
  189. fprintf(stderr," %Le ", maxerr);
  190. #else
  191. fprintf(stderr," %e ", maxerr);
  192. #endif
  193. fprintf(stderr,
  194. " %10.2f MFlops %10.2f MFlops %10.2f MFlops\n",
  195. COMPSIZE * COMPSIZE * 2. / 3. * (double)m * (double)m * (double)m / time1 * 1.e-6,
  196. COMPSIZE * COMPSIZE * 2. * (double)m * (double)m / time2 * 1.e-6,
  197. COMPSIZE * COMPSIZE * (2. / 3. * (double)m * (double)m * (double)m + 2. * (double)m * (double)m) / (time1 + time2) * 1.e-6);
  198. #if 0
  199. if (
  200. #ifdef DOUBLE
  201. maxerr > 1.e-8
  202. #else
  203. maxerr > 1.e-1
  204. #endif
  205. ) {
  206. fprintf(stderr, "Error is too large.\n");
  207. exit(1);
  208. }
  209. #endif
  210. }
  211. return 0;
  212. }
  213. // void main(int argc, char *argv[]) __attribute__((weak, alias("MAIN__")));