dotsrch.c

来自「基于Blas CLapck的.用过的人知道是干啥的」· C语言 代码 · 共 1,067 行 · 第 1/2 页

C
1,067
字号
      for (fp=fbase; fp != fk && fp; fp = fp->next) pr = fp;      assert(fp && pr);      pr->next = fp->next;      fp->next = NULL;      KillAllFN(fp);   }   return(fbase);}FILENODE *TstFile(char pre, FILENODE *fbase, int *N){   FILENODE *fp, *fn;   char ln[2048];   int i, k=0;   for (fp=fbase; fp; fp = fn)   {      fn = fp->next;      i = sprintf(ln, "make %cdottest urout=%s opt=\"", pre, fp->rout);      if (CONJ)         i = sprintf(ln, "make %cdotctest urout=%s opt=\"", pre, fp->rout);      if (fp->incX == 0) i += sprintf(ln+i, " -X 4 1 -1 2 -3");      if (fp->incY == 0) i += sprintf(ln+i, " -Y 4 1 -1 3 -2");      i += sprintf(ln+i, "\" ");      if (fp->cc) i += sprintf(ln+i, "%cUCC=\"%s\" ", pre, fp->cc);      if (fp->ccflags) i += sprintf(ln+i, "%cUCCFLAGS=\"%s\" ",pre,fp->ccflags);      sprintf(ln+i, "\n");      fprintf(stderr, "TST: %s", ln);      #if 1         if (system(ln)) fbase = KillFN(fbase, fp);         else k++;      #else         k++;      #endif   }   *N = k;   return(fbase);}int NumOfAlpha(int alpha, int n, int *ia)/* * returns number of occurences of alpha in ia */{   int i, j;   for (i=j=0; i < n; i++) if (ia[i] == alpha) j++;   return(j);}char *GetNam(char pre, int alpha, int beta, int incX, int incY){   static char nam[128];   char sx, sy, ca, cb;   char *rout="dot";   if (CONJ) rout = "dotc";   if (alpha == 1) ca = '1';   else if (alpha == -1) ca = 'n';   else if (alpha == 0 && (pre=='c' || pre=='z')) ca = 'r';   else ca = 'X';   if (beta == 1) cb = '1';   else if (beta == -1) cb = 'n';   else if (beta == 0 && (pre=='c' || pre=='z')) cb = 'r';   else cb = 'X';   if (incX < 0) { sx = 'n'; incX = -incX; }   else sx = 'p';   if (incY < 0) { sy = 'n'; incY = -incY; }   else sy = 'p';   sprintf(nam, "ATL_%c%s_x%c%dy%c%da%cb%c",           pre, rout, sx, incX, sy, incY, ca, cb);   return(nam);}void MangleIncs(FILE *fpout, char *spc, char pre)/* * makes incs positive if possible, if one must be negative, make sure * it is not stride 1; if both are stride 1, or neither, * make incY positive over incX * NOTE: incX and incY will therefore never both be neg, *       incY will be negative only if incX = -1 */{   char *cspc = spc + 3;   assert(spc[0] == ' ' && spc[1] == ' ' && spc[2] == ' ');   fprintf(fpout, "/*\n");   fprintf(fpout, " *%s Manipulate incX and inxY such that:\n", cspc);   fprintf(fpout, " *%s -  Both are positive\n", cspc);   fprintf(fpout,   " *%s -  else if incX or incY has abs()=1, make it positive\n", cspc);   fprintf(fpout,   " *%s -  if both abs(inc) are 1, or if neither, make incY positive\n", cspc);   fprintf(fpout, " */\n");   fprintf(fpout,      "%sif (incX >= 0 && incY >= 0) goto L1;\n", spc);   fprintf(fpout,      "%selse if (incY < 0)\n", spc);   fprintf(fpout,      "%s{\n", spc);   fprintf(fpout,      "%s   if (incX < 0) /* make both positive */\n", spc);   fprintf(fpout,      "%s   {\n", spc);   fprintf(fpout,      "%s      incx = -incX;\n", spc);   fprintf(fpout,      "%s      incy = -incY;\n", spc);   fprintf(fpout,      "%s      X += ((N-1)SHIFT)*incX;\n", spc);   fprintf(fpout,      "%s      Y += ((N-1)SHIFT)*incY;\n", spc);   fprintf(fpout,      "%s   }\n", spc);   fprintf(fpout,      "%s   else if (incX != 1 || incY == -1)\n", spc);   fprintf(fpout,      "%s   {\n", spc);   fprintf(fpout,      "%s      incy = -incY;\n", spc);   fprintf(fpout,      "%s      Y += ((N-1)SHIFT)*incY;\n", spc);   fprintf(fpout,      "%s      incx = -incX;\n", spc);   fprintf(fpout,      "%s      X += ((N-1)SHIFT)*incX;\n", spc);   fprintf(fpout,      "%s   }\n", spc);   fprintf(fpout,      "%s}\n", spc);   fprintf(fpout,      "%selse if (incX == -1 && incY != 1)\n", spc);   fprintf(fpout,      "%s{\n", spc);   fprintf(fpout,      "%s   incx = 1;\n", spc);   fprintf(fpout,      "%s   X -= ((N-1)SHIFT);\n", spc);   fprintf(fpout,      "%s   incy = -incY;\n", spc);   fprintf(fpout,      "%s   Y += ((N-1)SHIFT)*incY;\n", spc);   fprintf(fpout,      "%s}\n", spc);   if (pre == 'c' || pre == 'z')      fprintf(fpout,         "%selse if (!incX || !incY) return;\n", spc);   else      fprintf(fpout,         "%selse if (!incX || !incY) return(ATL_rzero);\n", spc);   fprintf(fpout, "L1:\n");}void GenMakefile(char pre, int n, int *ix, int *iy, int *ia, int *ib,                 FILENODE **bp){   char *cc, *ccflags, *nam, *typ="";   char ln[32];   char *rout = "dot";   int i;   FILE *fpout;   if (CONJ) rout="dotc";   sprintf(ln, "GEN/Make_%c%s", pre, rout);   fpout = fopen(ln, "w");   assert(fpout);   if (pre == 's') typ="SREAL";   else if (pre == 'd') typ="DREAL";   else if (pre == 'c')   {      if (CONJ) typ="SCPLX -DConj_";      else typ="SCPLX";   }   else if (pre == 'z')   {      if (CONJ) typ="DCPLX -DConj_";      else typ="DCPLX";   }   fprintf(fpout, "include Make.inc\n\n");   fprintf(fpout, "obj = ATL_%c%s.o", pre, rout);   for (i=0; i < n; i++)      fprintf(fpout, " %s.o", GetNam(pre, ia[i], ib[i], ix[i], iy[i]));   fprintf(fpout, "\n\n");   fprintf(fpout, "all : lib\n%clib : lib\nlib : %c%s.grd\n\n", pre, pre, rout);   fprintf(fpout, "%c%s.grd : $(obj)\n", pre, rout);   fprintf(fpout, "\t$(ARCHIVER) $(ARFLAGS) $(ATLASlib) $(obj)\n");   fprintf(fpout, "\t$(RANLIB) $(ATLASlib)\n");   fprintf(fpout, "\ttouch %c%s.grd\n", pre, rout);   fprintf(fpout, "\n");   sprintf(ln, "ATL_%c%s", pre, rout);   fprintf(fpout, "%s.o : %s.c\n", ln, ln);   fprintf(fpout, "\t$(ICC) $(ICCFLAGS) -D%s -o $@ -c %s.c\n", typ, ln);   for (i=0; i < n; i++)   {      nam = GetNam(pre, ia[i], ib[i], ix[i], iy[i]);      cc = bp[i]->cc;      ccflags = bp[i]->ccflags;      if (!cc) cc = (pre == 's' || pre == 'c') ? "$(SKC)" : "$(DKC)";      if (!ccflags)         ccflags = (pre == 's' || pre == 'c') ? "$(SKCFLAGS)":"$(DKCFLAGS)";      fprintf(fpout, "%s.o : %s.c\n", nam, nam);      fprintf(fpout, "\t%s %s $(CDEFS) -D%s -o $@ -c %s.c\n",              cc, ccflags, typ, nam);   }   fclose(fpout);}void GenMainRout(char pre, int n, int *ix, int *iy, int *ia, int *ib,                 FILENODE **bp){   int i, j, NeedElse=0;   FILENODE *gp;   FILE *fpout;   char *els="else if", *ifs ="if";   char *spcs="                  ";   char *spc = spcs+18;   char ln[64];   char *dargs="const int, const TYPE*, const int, const TYPE*, const int";   char *args="N, X, incx, Y, incy";   char *cargs="N, X, incx, Y, incy, dotc";   if (pre == 'c' || pre == 'z') args = cargs;   sprintf(ln, "GEN/ATL_%cdot.c", pre);   if (CONJ) sprintf(ln, "GEN/ATL_%cdotc.c", pre);   fpout = fopen(ln, "w");   assert(fpout);   fprintf(fpout, "#include \"atlas_misc.h\"\n\n");   gp = FindGen(n, ix, iy, ia, ib, bp);   assert(gp);/* * prototype all routines */   for (i=0; i < n; i++)   {      if (pre == 'd' || pre == 's')         fprintf(fpout, "TYPE %s\n   (%s);\n",                 GetNam(pre, ia[i], ib[i], ix[i], iy[i]), dargs);      else         fprintf(fpout, "void %s\n   (%s, TYPE*);\n",                 GetNam(pre, ia[i], ib[i], ix[i], iy[i]), dargs);   }   if (pre == 'c' || pre == 'z')   {      if (CONJ)         fprintf(fpout,"\nvoid ATL_%cdotc_sub(const int N, const TYPE *X, const int incX,\n", pre);      else         fprintf(fpout,"\nvoid ATL_%cdotu_sub(const int N, const TYPE *X, const int incX,\n", pre);      fprintf(fpout,         "                   const TYPE *Y, const int incY, TYPE *dotc)\n{\n");   }   else   {      fprintf(fpout,      "\nTYPE ATL_%cdot(const int N, const TYPE *X, const int incX,\n", pre);      fprintf(fpout, "              const TYPE *Y, const int incY)\n{\n");   }   fprintf(fpout, "   int incx=incX, incy=incY;\n\n");   fprintf(fpout, "   if (N > 0)\n   {\n");   spc -= 6;   MangleIncs(fpout, spc, pre);/* * Handle general alpha & beta, specific incs */   fprintf(fpout, "\n");   for (i=0; i < n-1; i++)   {      if (ix[i] && iy[i])         fprintf(fpout, "%s%s (incx == %d && incy == %d)\n",                 spc, ifs, ix[i], iy[i]);      else if (iy[i]) fprintf(fpout, "%s%s (incy == %d)\n", spc, ifs, iy[i]);      else if (ix[i]) fprintf(fpout, "%s%s (incx == %d)\n", spc, ifs, ix[i]);      if (pre == 'd' || pre == 's')         fprintf(fpout, "%s   return(%s(%s));\n", spc,                 GetNam(pre, ia[i], ib[i], ix[i], iy[i]), args);      else         fprintf(fpout, "%s   %s(%s);\n", spc,                 GetNam(pre, ia[i], ib[i], ix[i], iy[i]), args);      ifs = els;      NeedElse = 1;   }   if (NeedElse)   {      fprintf(fpout, "%selse\n", spc);      spc -= 3;   }   if (pre == 's' || pre == 'd')      fprintf(fpout, "%sreturn(%s(%s));\n", spc,              GetNam(pre, AlphaX, AlphaX, 0, 0), args);   else   fprintf(fpout, "%s%s(%s);\n", spc,           GetNam(pre, AlphaX, AlphaX, 0, 0), args);   fprintf(fpout, "   }\n");   if (pre == 'd' || pre == 's') fprintf(fpout, "   else return(ATL_rzero);\n");   else fprintf(fpout, "   else dotc[0] = dotc[1] = ATL_rzero;\n");   fprintf(fpout, "}\n");   fclose(fpout);}void DumpFile(char *fnam, FILE *fpout){   FILE *fpin;   char ln[512];   sprintf(ln, "DOT/%s", fnam);   fpin = fopen(ln, "r");   assert(fpin);   while(fgets(ln, 512, fpin)) fputs(ln, fpout);   fclose(fpin);}void GenFiles(char pre, int n, int *ix, int *iy, int *ia, int *ib,              FILENODE **bp)/* * Generates all needed files */{   int i;   char *nam, *typ;   char ln[128];   FILE *fpout;   GenMainRout(pre, n, ix, iy, ia, ib, bp);   GenMakefile(pre, n, ix, iy, ia, ib, bp);   for (i=0; i < n; i++)   {      nam = GetNam(pre, ia[i], ib[i], ix[i], iy[i]);      sprintf(ln, "GEN/%s.c", nam);      fpout = fopen(ln, "w");      assert(fpout);      fprintf(fpout, "#define ATL_UDOT %s\n\n", nam);      DumpFile(bp[i]->rout, fpout);      fclose(fpout);   }}void CreateSumm(char pre, int n, int *ix, int *iy, int *ia, int *ib,                FILENODE **bp)/* * Creates summary file of form:<ncases><ID> <alpha> <beta> <incX> <incY> <rout> <auth> */{   int i;   char ln[64];   FILE *fpout;   sprintf(ln, "res/%cDOT_SUMM", pre);   if (CONJ) sprintf(ln, "res/%cDOTC_SUMM", pre);   fpout = fopen(ln, "w");   assert(fpout);   fprintf(fpout, "%d\n", n);   for (i=0; i < n; i++)   {      fprintf(fpout, "%3d %3d %3d %3d %3d %s \"%s\"\n",              bp[i]->ID, ia[i], ib[i], ix[i], iy[i], bp[i]->rout, bp[i]->auth);   }   fclose(fpout);}void AssBest(FILENODE *fbase, int n, int *id, FILENODE **bp)/* * Associates best file pointers with filenodes based on ID */{   int i, ID;   FILENODE *fp;   for (i=0; i < n; i++)   {      ID = id[i];      for (fp=fbase; fp; fp = fp->next) if (fp->ID == ID) break;      assert(fp);      bp[i] = fp;   }}FILENODE *GetSumm(char pre, int *N, int **IX, int **IY, int **IA,  int **IB,                  FILENODE ***BP){   FILENODE *fbase, **bp;   int i, n, *ix, *iy, *ia, *ib, *id;   char ln[512];   FILE *fpin;   *N = 0;   sprintf(ln, "res/%cDOT_SUMM", pre);   if (CONJ) sprintf(ln, "res/%cDOTC_SUMM", pre);   fpin = fopen(ln, "r");   if (fpin == NULL) return(NULL);   fbase = ReadFile(pre);   assert( fgets(ln, 512, fpin) != NULL );   sscanf(ln, " %d", &n);   assert(n > 0);   ix = malloc(5*n*sizeof(int));   assert(ix);   iy = ix + n;   ia = iy + n;   ib = ia + n;   id = ib + n;   bp = malloc(n*sizeof(FILENODE*));   assert(bp);   for (i=0; i < n; i++)   {      assert( fgets(ln, 512, fpin) != NULL );      assert(sscanf(ln, " %d %d %d %d %d", id+i, ia+i, ib+i, ix+i, iy+i) == 5);      bp[i] = NULL;   }   fclose(fpin);   AssBest(fbase, n, id, bp);   *N = n;   *IX = ix;   *IY = iy;   *IA = ia;   *IB = ib;   *BP = bp;   return(fbase);}void DoIt(char pre){   FILENODE *fbase, **bp;   int n;   int *ix, *iy, *ia, *ib;   fbase = ReadFile(pre);   fbase = TstFile(pre, fbase, &n);  /* get rid of nodes that don't work */   ix = malloc(4*n*sizeof(int));   assert(ix);   bp = malloc(n*sizeof(FILENODE*));   assert(bp);   iy = ix + n;   ia = iy + n;   ib = ia + n;   n = GetCombos(fbase, ix, iy, ia, ib, bp);   BruteTime(pre, n, fbase, ix, iy, ia, ib, bp);   n = KillSpecCases(n, ix, iy, ia, ib, bp);   fprintf(stdout, "%d:\n", __LINE__);   PrintTable(n, ix, iy, ia, ib, bp);   SortCases(n, ix, iy, ia, ib, bp);   fprintf(stdout, "%d:\n", __LINE__);   PrintTable(n, ix, iy, ia, ib, bp);   CreateSumm(pre, n, ix, iy, ia, ib, bp);   KillAllFN(fbase);   free(bp);   free(ix);}void GoToTown(char pre){   FILENODE *fptr, **bp;   int n, *ix, *iy, *ia, *ib;   fptr = GetSumm(pre, &n, &ix, &iy, &ia, &ib, &bp);   if (!fptr)   {      DoIt(pre);      fptr = GetSumm(pre, &n, &ix, &iy, &ia, &ib, &bp);      assert(fptr);   }   fprintf(stdout, "%d:\n", __LINE__);   PrintTable(n, ix, iy, ia, ib, bp);   GenFiles(pre, n, ix, iy, ia, ib, bp);   KillAllFN(fptr);   free(bp);   free(ix);}void PrintUsage(char *nam){   fprintf(stderr, "\nUSAGE: %s -p <pre>\n\n", nam);   exit(-1);}char GetFlags(int nargs, char **args){   char ch;   if (nargs < 2) return('d');   if (args[1][0] == '-')   {      if (nargs < 3) PrintUsage(args[0]);      ch = args[2][0];   }   else ch = args[1][0];   switch(ch)   {   case 's':   case 'd':   case 'c':   case 'z':       break;   case 'S':      ch = 's';      break;   case 'D':      ch = 'd';      break;   case 'C':   CONJ=1;      ch = 'c';      break;   case 'Z':   CONJ=1;      ch = 'z';      break;   default:      PrintUsage(args[0]);   }   return(ch);}main(int nargs, char **args){   char pre;   pre = GetFlags(nargs, args);   GoToTown(pre);   exit(0);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?