reg_gemm.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <math.h>
  19. #include "nr.h"
  20. #define REAL float
  21. #define SIZE 2000000
  22. #define ma 6
  23. typedef struct Coord
  24. {
  25. REAL x1;
  26. REAL x2;
  27. REAL x3;
  28. } coord, *pcoord;
  29. coord tabcoord[SIZE];
  30. REAL tabx[SIZE];
  31. REAL taby[SIZE];
  32. REAL sig[SIZE];
  33. REAL afunc[ma+1];
  34. int ia[ma+1];
  35. REAL a[ma+1];
  36. void funcs( REAL i, REAL afunc[ma+1], int ma2)
  37. {
  38. afunc[1]= 1;
  39. afunc[2]= tabcoord[(int)i].x1;
  40. afunc[3]= tabcoord[(int)i].x2;
  41. afunc[4]= tabcoord[(int)i].x1*tabcoord[(int)i].x2;
  42. afunc[5]= tabcoord[(int)i].x2*tabcoord[(int)i].x3;
  43. afunc[6]= tabcoord[(int)i].x1*tabcoord[(int)i].x2*tabcoord[(int)i].x3;
  44. //printf("%f %f %f \n",afunc[0],afunc[1],afunc[2]);
  45. }
  46. int main(int argc, char * argv[])
  47. {
  48. REAL total=0.0;
  49. REAL ecart=0.0;
  50. int len=0;
  51. char str2[1000];
  52. /* long double total=0.0; */
  53. /* long double ecart=0.0; */
  54. char *filename = argv[1];
  55. char perf_h[1000];
  56. char str[1000];
  57. int k,i;
  58. FILE * res;
  59. FILE *out;
  60. FILE *perf;
  61. // FILE *tmpperf;
  62. int ndat=atoi(argv[2]);
  63. REAL ** covar;
  64. REAL *chisq = (REAL *)malloc(sizeof(REAL));
  65. res = fopen(filename,"r");
  66. covar = (REAL**) malloc((ma+1) *sizeof(REAL*));
  67. for (i=0;i<ma+1;i++)
  68. covar[i]=(REAL*)malloc((ma+1) *sizeof(REAL));
  69. for (k=1;k<ndat+1;k++)
  70. {
  71. int x0, y0, x1, y1, x2, y2;
  72. REAL tmpfloat;
  73. fscanf(res,"%f\t%d\t%d\t%d\t%d\t%d\t%d\n", &tmpfloat, &x0, &y0, &x1, &y1, &x2, &y2);
  74. tabcoord[k].x1= x0 - y0;
  75. tabcoord[k].x2= y2;
  76. tabcoord[k].x3= y0;
  77. taby[k]=tmpfloat;
  78. sig[k]=1;
  79. tabx[k]=k;
  80. //fprintf(out,"%f %f %f\n",tabcoord[k].x1 ,tabcoord[k].x2 ,tabcoord[k].x3);
  81. //fprintf(out,"%f %f\n",tabx[k] , taby[k]);
  82. }
  83. for (k=1;k<ma+1;k++)
  84. ia[k]=1;
  85. lfit(tabx, taby, sig, ndat, a, ia, ma, covar, chisq, &funcs);
  86. for (k=1;k<ma+1;k++)
  87. {
  88. // printf("%.12lf\n", a[k]);
  89. //total+=a[k];
  90. }
  91. //calcul de l'ecart type
  92. for (k=1;k<ndat+1;k++)
  93. {
  94. double abs=0.0;
  95. abs += a[1];
  96. abs += tabcoord[k].x1*a[2]+tabcoord[k].x2*a[3];
  97. abs += tabcoord[k].x1*tabcoord[k].x2*a[4]+tabcoord[k].x2*tabcoord[k].x3*a[5];
  98. abs += tabcoord[k].x1*tabcoord[k].x2*tabcoord[k].x3*a[6];
  99. // fprintf(stderr,"k=%i ; calcul : %lf ; reel : %lf ; ", k, abs, taby[k]);
  100. abs = abs - taby[k];
  101. if (abs < 0)
  102. abs = - abs;
  103. // fprintf(stderr,"ecart : %lf\n ", abs);
  104. total += abs;
  105. //printf("%f %f %f\n",tabcoord[k].x1 ,tabcoord[k].x2 ,tabcoord[k].x3);
  106. }
  107. fprintf(stdout,"#define GEMM_A %e\n#define GEMM_B %e\n#define GEMM_C %e\n#define GEMM_D %e\n#define GEMM_E %e\n#define GEMM_F %e\n",a[6],a[4],a[5],a[2],a[3],a[1]);
  108. fprintf(stderr,"#define PERF_GEMM(i,j,k) (GEMM_A*(double)(i)*(double)(j)*(double)(k)+GEMM_B*(double)(i)*(double)(j)+GEMM_C*(double)(j)*(double)(k)+GEMM_D*(double)(i)+GEMM_E*(double)(j)+GEMM_F)\n");
  109. fprintf(stderr, "total %lf\n", total);
  110. ecart = total / ndat;
  111. fprintf(stderr, "ecart moyen %lf\n", ecart);
  112. return 0;
  113. }