r1search.c

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

C
572
字号
              char *cc, char *ccflags){   double low = .5, high = 1.0;   double mflow, mfhigh;   int ilow, ihigh;   if (ATL_NoBlock(flag)) flag -= 32;  /* always actually block these times */   do   {      ilow = (low  * 100.0);      ihigh = (high * 100.0);      mflow  = r1case(pre, r1nam, flag, mu, nu, cas, cc, ccflags, ilow);      mfhigh = r1case(pre, r1nam, flag, mu, nu, cas, cc, ccflags, ihigh);      fprintf(stdout, "      %d%% %.2fMFLOP  ---  %d%% %.2fMFLOP\n",              ilow, mflow, ihigh, mfhigh);      if (mflow < mfhigh) low += 0.5*(high-low);      else high -= 0.5 * (high-low);   }   while (ihigh-ilow);   fprintf(stdout, "\n\nBEST %% of L1 cache: %.2f\n", low*100.0);   return(ilow);}int ConfirmBlock(char pre, char *r1nam, int flag, int mu, int nu, int cas,                 char *cc, char *ccflags, int l1mul){   int bflag;   double mfblock, mfnoblock;   if ( ATL_NoBlock(flag) )   {      bflag = flag - 32;      mfblock   = r1case(pre, r1nam, bflag, mu, nu, cas, cc, ccflags, l1mul);      mfnoblock = r1case(pre, r1nam,  flag, mu, nu, cas, cc, ccflags, l1mul);      fprintf(stdout, "\nWith blocking=%lf, without=%lf\n\n",              mfblock, mfnoblock);      if (mfblock >= mfnoblock) return(bflag);   }   return(flag);}void iGetIDCase(char pre, int ID, char *r1nam, char *auth,                char *cc, char *ccflags, int *flag, int *mu, int *nu){   char ln[512];   FILE *fp;   int i, id, n;   sprintf(ln, "%ccases.dsc", pre);   fp = fopen(ln, "r");   assert(fp);   assert(fgets(ln, 128, fp));   assert(sscanf(ln, " %d", &n) == 1);   for (i=0; i < n; i++)   {      assert(fgets(ln, 512, fp));      assert(sscanf(ln, " %d %d %d %d %s \"%[^\"]",                    &id, flag, mu, nu, r1nam, auth) == 6);      if (LineIsCont(ln))      {         assert(fgets(cc, 256, fp));         assert(fgets(ccflags, 512, fp));         NoEndLineWhiteSpace(cc);         NoEndLineWhiteSpace(ccflags);      }      else cc[0] = ccflags[0] = '\0';      if (id == ID) break;   }   fclose(fp);}void GetCases(char pre, int *ncases, int **IDs, char ***r1nams, char ***auths,              char ***CC, char ***CCFLAGS, int **flags, int **mus, int **nus){   int n, i, j;   char fnam[64], ln[512];   char **r1nam, **auth, **cc, **ccflags;   int *flag, *mu, *nu, *id;   FILE *fp;   sprintf(fnam, "%ccases.dsc", pre);   fp = fopen(fnam, "r");   assert(fp);   assert(fgets(ln, 128, fp));   assert(sscanf(ln, " %d", &n) == 1);   assert(n < 100 && n > 0);   r1nam   = malloc(n * sizeof(char*));   auth    = malloc(n * sizeof(char*));   cc      = malloc(n * sizeof(char*));   ccflags = malloc(n * sizeof(char*));   assert(r1nam && auth && cc && ccflags);   for (i=0; i < n; i++)   {      assert(r1nam[i] = malloc(64*sizeof(char)));      assert(auth[i] = malloc(64*sizeof(char)));   }   mu = malloc(n * sizeof(int));   nu = malloc(n * sizeof(int));   id = malloc(n * sizeof(int));   flag = malloc(n * sizeof(int));   assert(mu && nu && flag && id);   for (i=0; i < n; i++)   {      assert(fgets(ln, 512, fp));      assert(sscanf(ln, " %d %d %d %d %s \"%[^\"]",                    id+i, flag+i, mu+i, nu+i, r1nam[i], auth[i]) == 6);      assert(mu[i] >= 0 && nu[i] >= 0 && r1nam[i][0] != '\0');      if (LineIsCont(ln))      {         fgets(ln, 512, fp);         NoEndLineWhiteSpace(ln);         if (ln[0] == '+' && (ln[1] == '\0' || ln[1] == ' ')) /* adding flags */            cc[i] = NULL;         else         {            j = strlen(ln);            cc[i] = malloc((j+1)*sizeof(char));            assert(cc[i]);            strcpy(cc[i], ln);         }         fgets(ln, 256, fp);         NoEndLineWhiteSpace(ln);         j = strlen(ln);         ccflags[i] = malloc((j+1)*sizeof(char));         assert(ccflags[i]);         strcpy(ccflags[i], ln);      }      else cc[i] = ccflags[i] = NULL;   }   fclose(fp);   for (i=0; i < n; i++) /* fix cc & ccflags */   {      if (cc[i] == NULL)      {         cc[i] = malloc(2*sizeof(char));         assert(cc[i]);         cc[i][0] = cc[i][1] = '\0';      }      if (ccflags[i] == NULL)      {         ccflags[i] = malloc(2*sizeof(char));         assert(ccflags[i]);         ccflags[i][0] = ccflags[i][1] = '\0';      }   }   *IDs = id;   *ncases = n;   *r1nams = r1nam;   *auths = auth;   *CC = cc;   *CCFLAGS = ccflags;   *flags = flag;   *mus = mu;   *nus = nu;}void RunCases(char pre){   char **r1nams, **auths, **cc, **ccflags;   int ncases, *ids, *flags, *mus, *nus;   int i, imax=0;   int l1mul, mf, mfmax=0.0;   GetCases(pre, &ncases, &ids, &r1nams, &auths, &cc, &ccflags, &flags,            &mus, &nus);   for (i=0; i < ncases; i++)   {      mf = r1case(pre, r1nams[i], flags[i], mus[i], nus[i], ids[i],                  cc[i], ccflags[i], 75);      fprintf(stdout, "%s : %.2f\n", r1nams[i], mf);      if (mf > mfmax)      {         if (r1tstcase(pre, r1nams[i], cc[i], ccflags[i]) == 0)         {    /* make it pass tester */            mfmax = mf;            imax = i+1;         }      }   }   assert(imax);   imax--;   l1mul = FindL1Mul(pre, imax+1, r1nams[imax], flags[imax], mus[imax],                     nus[imax], cc[imax], ccflags[imax]);   flags[imax] = ConfirmBlock(pre, r1nams[imax], flags[imax], mus[imax],                              nus[imax], ids[imax], cc[imax],                              ccflags[imax], l1mul);   fprintf(stdout, "\nBEST: %s, case %d, mu=%d, nu=%d; at %.2f MFLOPS\n\n",           r1nams[imax], imax+1, mus[imax], nus[imax], mfmax);   CreateSum(pre, l1mul, ids[imax], r1nams[imax], auths[imax], flags[imax],             mus[imax], nus[imax], mfmax);   for (i=0; i < ncases; i++)   {      free(r1nams[i]);      free(auths[i]);      free(cc[i]);      free(ccflags[i]);   }   free(cc);   free(ccflags);   free(r1nams);   free(auths);   free(flags);   free(mus);   free(nus);}void GoToTown(pre){   char r1nam[128], auth[128], ln[128], cc[256], ccflags[512];   int flag, mu, nu, id, l1mul;   double mf;   FILE *fp;   sprintf(ln, "res/%cR1RES", pre);   if (!FileExists(ln))   {      RunCases(pre);      fp = fopen(ln, "r");      assert(fp);   }   else /* if default does not pass tester, rerun cases */   {      fp = fopen(ln, "r");      ReadSum(pre, &l1mul, &id, r1nam, auth, &flag, &mu, &nu, &mf);      iGetIDCase(pre, id, r1nam, auth, cc, ccflags, &flag, &mu, &nu);      if (r1tstcase(pre, r1nam, cc, ccflags) != 0)      {         fclose(fp);         assert(remove(ln) == 0);         RunCases(pre);         fp = fopen(ln, "r");         assert(fp);      }   }   fclose(fp);   ReadSum(pre, &l1mul, &id, r1nam, auth, &flag, &mu, &nu, &mf);   iGetIDCase(pre, id, r1nam, auth, cc, ccflags, &flag, &mu, &nu);   fprintf(stdout, "\nBEST: %s, mu=%d, nu=%d; at %.2f MFLOPS\n\n",           r1nam, mu, nu, mf);   r1install(pre, id, r1nam, auth, l1mul, flag, cc, ccflags, mu, nu);}void PrintUsage(char *fnam){   fprintf(stderr, "USAGE: %s [-p <d/s/z/c>]\n", fnam);   exit(-1);}void GetFlags(int nargs, char **args, char *pre){   char ctmp;   int i;   *pre = 'd';   for (i=1; i < nargs; i++)   {      if (args[i][0] != '-') PrintUsage(&args[0][0]);      switch(args[i][1])      {      case 'p':         ctmp = args[++i][0];         ctmp = tolower(ctmp);         if (ctmp == 's' || ctmp == 'd' || ctmp == 'c' || ctmp == 'z')            *pre = ctmp;         else PrintUsage(&args[0][0]);         break;      default:         fprintf(stderr, "Unknown flag : %s\n", args[i]);         PrintUsage(&args[0][0]);      }   }}main(int nargs, char **args){   char pre;   GetFlags(nargs, args, &pre);   GoToTown(pre);   exit(0);}

⌨️ 快捷键说明

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