~ubuntu-branches/ubuntu/vivid/atlas/vivid

« back to all changes in this revision

Viewing changes to CONFIG/src/atlconf_misc.c

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot
  • Date: 2013-06-11 15:58:16 UTC
  • mfrom: (1.1.3 upstream)
  • mto: (2.2.21 experimental)
  • mto: This revision was merged to the branch mainline in revision 26.
  • Revision ID: package-import@ubuntu.com-20130611155816-b72z8f621tuhbzn0
Tags: upstream-3.10.1
Import upstream version 3.10.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *             Automatically Tuned Linear Algebra Software v3.8.4
 
2
 *             Automatically Tuned Linear Algebra Software v3.10.1
3
3
 *                    (C) Copyright 1998 R. Clint Whaley
4
4
 *
5
5
 * Redistribution and use in source and binary forms, with or without
28
28
 *
29
29
 */
30
30
#include "atlconf.h"
 
31
#include "atlconf_misc.h"
31
32
 
32
33
#include <time.h>
 
34
#include <ctype.h>
33
35
void GetDate(int *month, int *day, int *year, int *hour, int *min)
34
36
{
35
37
   time_t tv;
52
54
{
53
55
   char str[64];
54
56
   long iin;
55
 
   if (expstr) fprintf(stdout, "%sEnter %s [%d]: ", spc, expstr, Default);
 
57
   if (expstr) fprintf(stdout, "%sEnter %s [%ld]: ", spc, expstr, Default);
56
58
   if (fgets(str, 64, fpin) == NULL) return(Default);
57
59
   if (sscanf(str, " %ld ", &iin) != 1) return(Default);
58
60
   return(iin);
68
70
      if (i > Max)
69
71
      {
70
72
         keepOn = 1;
71
 
         fprintf(stderr, "\n%d larger than max value of %d.  Try again.\n\n",
 
73
         fprintf(stderr, "\n%ld larger than max value of %ld.  Try again.\n\n",
72
74
                 i, Max);
73
75
      }
74
76
      else if (i < Min)
75
77
      {
76
78
         keepOn = 1;
77
 
         fprintf(stderr, "\n%d smaller than min value of %d.  Try again.\n\n",
 
79
         fprintf(stderr, "\n%ld smaller than min value of %ld.  Try again.\n\n",
78
80
                 i, Min);
79
81
      }
80
82
      else keepOn = 0;
90
92
   do
91
93
   {
92
94
      i = GetIntRange(Default, Min, Max, spc, expstr);
93
 
      fprintf(stdout, "%s   You entered: %d\n", spc, i);
 
95
      fprintf(stdout, "%s   You entered: %ld\n", spc, i);
94
96
      j = GetIntRange(0, 0, 1, spc, "1 to reenter, 0 accepts");
95
97
   }
96
98
   while(j);
282
284
   return(iret);
283
285
}
284
286
 
 
287
char *ATL_fgetln(FILE *fpin)
 
288
/*
 
289
 * RETURNS: dynamically allocates string containing a line from fpin, regardless
 
290
 *          of how long that line is; RETURNS NULL if EOF
 
291
 */
 
292
{
 
293
   char *ln, *sp;
 
294
   int len=128, i;
 
295
   ln = malloc(len*sizeof(char));
 
296
   assert(ln);
 
297
   if (!fgets(ln, len, fpin))
 
298
      return(NULL);
 
299
   i = strlen(ln);
 
300
/*
 
301
 * Keep extending lnlen and reading in more chars until \n is found
 
302
 */
 
303
   while (ln[i-1] != '\n')
 
304
   {
 
305
      char *ln2;
 
306
      len <<= 1;
 
307
      ln2 = malloc(len*sizeof(char));
 
308
      assert(ln2);
 
309
      strcpy(ln2, ln);
 
310
      free(ln);
 
311
      ln = ln2;
 
312
      if (!fgets(ln+i, len-i, fpin))
 
313
         return(ln);
 
314
      i += strlen(ln+i);
 
315
   }
 
316
   return(ln);
 
317
}
 
318
 
285
319
int fNumLines(char *fnam)
286
320
{
287
321
   FILE *fp;
294
328
   return(i);
295
329
}
296
330
 
297
 
char *CmndResults(char *targ, char *cmnd)
298
 
{
299
 
   static char tnam[128];
300
 
   static int FirstTime=1;
301
 
   char ln[512];
302
 
 
303
 
   if (FirstTime)
304
 
   {
305
 
      FirstTime = 0;
306
 
      assert(tmpnam(tnam));
307
 
   }
308
 
   if (targ) sprintf(ln, "ssh %s \"%s\" > %s 2>&1 \n", targ, cmnd, tnam);
309
 
   else sprintf(ln, "%s > %s 2>&1\n", cmnd, tnam);
310
 
   if (!system(ln)) return(tnam);
311
 
   else return(NULL);
312
 
}
313
 
 
314
 
int CmndOneLine(char *targ, char *cmnd, char *ln)
315
 
/*
316
 
 * executes a system call with contents of cmnd, returns the output in ln;
317
 
 * Returns value returned by system call
318
 
 * if targ is set, we ssh to that machine
319
 
 */
320
 
{
321
 
   int i;
322
 
   FILE *fp;
323
 
   char *tnam;
324
 
 
325
 
   ln[0] = '\0';
326
 
   tnam = CmndResults(targ, cmnd);
327
 
   if (tnam)
328
 
   {
329
 
      fp = fopen(tnam, "r");
330
 
      assert(fp);
331
 
      if (!fgets(ln, 2048, fp)) ln[0] = '\0';
332
 
      fclose(fp);
333
 
      return(0);
334
 
   }
335
 
   else ln[0] = '\0';
336
 
   return(1);
337
 
}
338
331
 
339
332
int GetIntBeforeWord(char *word, char *ln)
340
333
/*
468
461
 */
469
462
{
470
463
   enum ARCHFAM fam=AFOther;
471
 
   char cmnd[256], res[256];
 
464
   char *cmnd, *res;
472
465
   char *uname;
 
466
   int i;
 
467
 
473
468
   uname = FindUname(targ);
 
469
   i = strlen(uname) + 4;
 
470
 
 
471
   cmnd = malloc(i*sizeof(char));
 
472
   assert(cmnd);
474
473
   sprintf(cmnd, "%s -m", uname);
475
 
   if ( !CmndOneLine(targ, cmnd, res) )
 
474
   res = atlsys_1L(targ, cmnd, 0, 0);
 
475
   if (res)
476
476
   {
477
477
      if (strstr(res, "ppc") || strstr(res, "Power Macintosh") ||
478
478
          strstr(res, "powerpc")) fam = AFPPC;
480
480
      else if (strstr(res, "alpha")) fam = AFALPHA;
481
481
      else if (strstr(res, "ia64")) fam = AFIA64;
482
482
      else if (strstr(res, "mips")) fam = AFMIPS;
 
483
      else if (strstr(res, "arm")) fam = AFARM;
 
484
      else if (strstr(res, "s390")) fam = AFS390;
483
485
      else if ( strstr(res, "i686") || strstr(res, "i586") ||
484
486
                strstr(res, "i486") || strstr(res, "i386") ||
485
487
                strstr(res, "x86") || strstr(res, "x86_64") ) fam = AFX86;
 
488
      free(res);
486
489
   }
487
490
/*
488
491
 * Try uname -p if uname -m didn't work
490
493
   if (fam == AFOther)
491
494
   {
492
495
      sprintf(cmnd, "%s -p", uname);
493
 
      if ( !CmndOneLine(targ, cmnd, res) )
 
496
      res = atlsys_1L(targ, cmnd, 0, 0);
 
497
      if (res)
494
498
      {
495
499
         if (strstr(res, "ppc") || strstr(res, "Power Macintosh") ||
496
500
             strstr(res, "powerpc")) fam = AFPPC;
501
505
                   strstr(res, "i486") || strstr(res, "i386") ||
502
506
                   strstr(res, "x86_64") ) fam = AFX86;
503
507
         else if (strstr(res, "mips")) fam = AFMIPS;
 
508
         else if (strstr(res, "arm")) fam = AFARM;
 
509
         else if (strstr(res, "s390")) fam = AFS390;
 
510
         free(res);
504
511
      }
505
512
   }
 
513
   free(cmnd);
506
514
   return(fam);
507
515
}
508
516
 
591
599
   for (lastslash=i=0; file[i]; i++)
592
600
      if (file[i] == '/')
593
601
         lastslash = i;
 
602
   i = lastslash;
594
603
   ch = file[i];
595
604
   file[i] = '\0';
596
605
   sp = NewStringCopy(file);
641
650
 * comp: 0: gcc;  1: egcs;  2: pgcc, 3: apple's gcc
642
651
 */
643
652
{
644
 
   char ln[512], ln2[512];
 
653
   char *cmnd, *res;
645
654
   int i, j;
646
655
 
647
656
   *comp = *major = *minor = *patch = -1;
648
 
   sprintf(ln, "%s --version", gcc);
649
 
   if (CmndOneLine(NULL, ln, ln2) == 0)
 
657
   i = strlen(gcc) + 12;
 
658
   cmnd = malloc(i * sizeof(char));
 
659
   assert(cmnd);
 
660
   sprintf(cmnd, "%s --version", gcc);
 
661
   res = atlsys_1L(NULL, cmnd, 0, 0);
 
662
   free(cmnd);
 
663
   if (res)
650
664
   {
651
 
      if (strstr(ln2, "Apple Computer"))
 
665
      if (strstr(res, "Apple Computer") || strstr(res, "Apple Inc"))
652
666
         *comp = 3;
653
667
/*
654
668
 *    Skip compiler name, which may have digits in it
655
669
 */
656
 
      for (i=0; ln2[i] && !isspace(ln2[i]); i++);
657
 
      *major = GetIntVers(ln2+i, &j); j += i;
 
670
      for (i=0; res[i] && !isspace(res[i]); i++);
 
671
      *major = GetIntVers(res+i, &j); j += i;
658
672
      if (*major != -1)
659
673
      {
660
 
         *minor = GetIntVers(ln2+j, &i); j += i;
 
674
         *minor = GetIntVers(res+j, &i); j += i;
661
675
         if (*minor != -1)
662
676
         {
663
 
            *patch = GetIntVers(ln2+j, &i); j += i;
664
 
            if (strstr(ln2, "egcs")) *comp = 1;
665
 
            else if (strstr(ln2, "pgcc")) *comp = 2;
666
 
            else *comp = 0;
 
677
            *patch = GetIntVers(res+j, &i); j += i;
 
678
            if (strstr(res, "egcs")) *comp = 1;
 
679
            else if (strstr(res, "pgcc")) *comp = 2;
 
680
            else if (*comp == -1) *comp = 0;
667
681
         }
668
682
      }
669
 
   }
 
683
      free(res);
 
684
   }
 
685
}
 
686
 
 
687
char *GetPathEnvVar(void)
 
688
/*
 
689
 * returns users path with inter-name spaces replaced by "\ "
 
690
 * and dirs separated by spaces
 
691
 */
 
692
{
 
693
   char *path, *pp, *p;
 
694
   int i, n;
 
695
 
 
696
   path = getenv("PATH");
 
697
   if (!path)
 
698
      return(NULL);
 
699
 
 
700
   n = strlen(path);
 
701
   p = pp = malloc((2*n+1)*sizeof(char));
 
702
   assert(pp);
 
703
   for (i=0; i < n; i++)
 
704
   {
 
705
      if (path[i] == ':')
 
706
         *p++ = ' ';
 
707
      else if (path[i] == ' ')
 
708
      {
 
709
         *p = '\\';
 
710
         p[1] = ' ';
 
711
         p += 2;
 
712
      }
 
713
      else
 
714
         *p++ = path[i];
 
715
   }
 
716
   *p = '\0';
 
717
   return(pp);
670
718
}
671
719
 
672
720
int CompIsGcc(char *comp)
673
721
/*
674
722
 * Tries to detect if compiler is gcc w/o scoping name of compiler
 
723
 * However, rejects compilers with c89 and c90 in name, since these
 
724
 * guys turn off inline assembly, which hurts the ATLAS install on performance.
675
725
 */
676
726
{
677
 
   char cmnd[1024], res[1024];
 
727
   char *cmnd, *res;
 
728
   char *cmpname;
 
729
   int i;
678
730
 
 
731
   cmpname = NameWithoutPath(comp);
 
732
   if (strstr(cmpname, "c89") || strstr(cmpname, "c90"))
 
733
   {
 
734
      free(cmpname);
 
735
      return(0);
 
736
   }
 
737
   free(cmpname);
 
738
   i = strlen(comp) + 16;
 
739
   cmnd = malloc(i*sizeof(char));
 
740
   assert(cmnd);
679
741
   sprintf(cmnd, "%s --version", comp);
680
 
   if (CmndOneLine(NULL, cmnd, res) == 0)
 
742
   res = atlsys_1L(NULL, cmnd, 0, 0);
 
743
   free(cmnd);
 
744
   if (res)
681
745
   {
682
746
/*
683
747
 *    The direct comps added because Ubuntu stopped printing (GCC) in the
684
748
 *    version line.  Don't want to search for just "gcc", since that will
685
749
 *    match with pgcc
686
750
 */
687
 
      if (strstr(res, "(GCC)") || strstr(res, " GCC ") ||
688
 
          strstr(res, "GNU Fortran") || strstr(res, "(GCC ") ||
689
 
          (res[0] == 'g' && res[1] == 'c' && res[2] == 'c' && res[3] == ' ')
 
751
      if (strstr(res, "(GCC)")  || strstr(res, " GCC ") ||
 
752
          strstr(res, "gcc-mp") || strstr(res, "(GCC ") ||
 
753
          strstr(res, "GNU Fortran") ||
 
754
          (res[0] == 'g' && res[1] == 'c' && res[2] == 'c' &&
 
755
           (res[3] == ' ' || res[3] == '-'))
690
756
         )
 
757
      {
 
758
         free(res);
691
759
         return(1);
692
 
   }
693
 
   return(0);
694
 
}
 
760
      }
 
761
      free(res);
 
762
   }
 
763
   return(0);
 
764
}
 
765
 
 
766
int CompIsMinGW(char *comp)
 
767
/*
 
768
 * Tries to detect if compiler is MinGW compiler
 
769
 */
 
770
{
 
771
   if (CompIsGcc(comp))
 
772
   {
 
773
      char *cmnd, *res;
 
774
      int i;
 
775
      i = strlen(comp) + 24;
 
776
      cmnd = malloc(sizeof(char)*i);
 
777
      assert(cmnd);
 
778
      sprintf(cmnd, "%s -v 2>&1 | fgrep mingw", comp);
 
779
      res = atlsys_1L(NULL, cmnd, 0, 0);
 
780
      free(cmnd);
 
781
      if (res)
 
782
      {
 
783
         if (strstr(res, "mingw"))
 
784
         {
 
785
            free(res);
 
786
            return(1);
 
787
         }
 
788
         free(res);
 
789
      }
 
790
   }
 
791
   return(0);
 
792
}
 
793
 
695
794
int CompIsAppleGcc(char *comp)
696
795
/*
697
796
 * Tries to detect if compiler is Apple's funked-up gcc
698
797
 */
699
798
{
700
 
   char cmnd[1024], res[1024];
 
799
   char *cmnd, *res;
701
800
 
702
801
   if (CompIsGcc(comp))
703
802
   {
704
 
      res[0] = '\0';
 
803
      int i;
 
804
      i = strlen(comp) + 24;
 
805
      cmnd = malloc(sizeof(char)*i);
 
806
      assert(cmnd);
705
807
      sprintf(cmnd, "%s -v 2>&1 | fgrep Apple", comp);
706
 
      CmndOneLine(NULL, cmnd, res);
707
 
      if (strstr(res, "Apple"))
708
 
         return(1);
 
808
      res = atlsys_1L(NULL, cmnd, 0, 0);
 
809
      free(cmnd);
 
810
      if (res)
 
811
      {
 
812
         if (strstr(res, "Apple"))
 
813
         {
 
814
            free(res);
 
815
            return(1);
 
816
         }
 
817
         free(res);
 
818
      }
709
819
   }
710
820
   return(0);
711
821
}
715
825
 * RETURNS: 1 if compiler is MIPSpro compiler, 0 otherwise
716
826
 */
717
827
{
718
 
   char cmnd[1024], res[1024];
 
828
   char *cmnd, *res;
 
829
   int i;
719
830
 
 
831
   i = strlen(comp) + 4;
 
832
   cmnd = malloc(sizeof(char)*i);
 
833
   assert(cmnd);
720
834
   sprintf(cmnd, "%s -v", comp);
721
 
   if (CmndOneLine(NULL, cmnd, res) == 0)
 
835
   res = atlsys_1L(NULL, cmnd, 0, 0);
 
836
   free(cmnd);
 
837
   if (res)
722
838
   {
723
839
      if (strstr(res, "MIPSpro Compiler"))
724
 
         return(1);
 
840
      {
 
841
         free(res);
 
842
         return(1);
 
843
      }
 
844
      free(res);
 
845
   }
 
846
   return(0);
 
847
}
 
848
 
 
849
int CompIsPathScale(char *comp)
 
850
{
 
851
   char *cmnd, *res;
 
852
   int i;
 
853
 
 
854
   i = strlen(comp) + 4;
 
855
   cmnd = malloc(sizeof(char)*i);
 
856
   assert(cmnd);
 
857
   sprintf(cmnd, "%s -v", comp);
 
858
   res = atlsys_1L(NULL, cmnd, 0, 0);
 
859
   free(cmnd);
 
860
   if (res)
 
861
   {
 
862
      if (strstr(res, "PathScale"))
 
863
      {
 
864
         free(res);
 
865
         return(1);
 
866
      }
 
867
      free(res);
725
868
   }
726
869
   return(0);
727
870
}
731
874
 * RETURNS: 1 if compiler is Sun WorkShop compiler, 0 otherwise
732
875
 */
733
876
{
734
 
   char cmnd[1024], res[1024];
 
877
   char *cmnd, *res;
 
878
   int i;
735
879
 
 
880
   i = strlen(comp) + 4;
 
881
   cmnd = malloc(sizeof(char)*i);
 
882
   assert(cmnd);
736
883
   sprintf(cmnd, "%s -V", comp);
737
 
   if (CmndOneLine(NULL, cmnd, res) == 0)
 
884
   res = atlsys_1L(NULL, cmnd, 0, 0);
 
885
   free(cmnd);
 
886
   if (res)
738
887
   {
739
888
      if (strstr(res, "Sun WorkShop"))
 
889
      {
 
890
         free(res);
740
891
         return(1);
 
892
      }
 
893
      free(res);
741
894
   }
742
895
   return(0);
743
896
}
747
900
 * RETURNS: 1 if compiler is an IBM XL compiler, 0 otherwise
748
901
 */
749
902
{
750
 
   char cmnd[1024], res[1024];
 
903
   char *cmnd, *res;
 
904
   int i;
751
905
 
 
906
   i = strlen(comp) + 11;
 
907
   cmnd = malloc(sizeof(char)*i);
 
908
   assert(cmnd);
752
909
   sprintf(cmnd, "%s -qversion", comp);
753
 
   if (CmndOneLine(NULL, cmnd, res) == 0)
 
910
   res = atlsys_1L(NULL, cmnd, 0, 0);
 
911
   free(cmnd);
 
912
   if (res)
754
913
   {
755
914
      if (strstr(res, "IBM XL"))
 
915
      {
 
916
         free(res);
756
917
         return(1);
 
918
      }
 
919
      free(res);
757
920
   }
758
921
   return(0);
759
922
}
760
923
 
761
 
char *NewStringCopy(char *old)
762
 
/*
763
 
 * RETURNS: newly allocates string containing copy of string old
764
 
 */
765
 
{
766
 
   char *new;
767
 
   new = malloc(sizeof(char)*(strlen(old)+1));
768
 
   strcpy(new, old);
769
 
   return(new);
770
 
}
771
 
char *NewAppendedString(char *old, char *app)
772
 
/*
773
 
 * RETURNS: string holding : old + " " + app
774
 
 * NOTE: frees old string after copy
775
 
 */
776
 
 
777
 
{
778
 
   char *new;
779
 
   if (!old)
780
 
   {
781
 
      new = malloc(sizeof(char)*(strlen(app)+1));
782
 
      assert(new);
783
 
      strcpy(new, app);
784
 
   }
785
 
   else
786
 
   {
787
 
      new = malloc(sizeof(char)*(strlen(old) + strlen(app)+2));
788
 
      assert(new);
789
 
      strcpy(new, old);
790
 
      strcat(new, " ");
791
 
      strcat(new, app);
792
 
      free(old);
793
 
   }
794
 
   return(new);
 
924
char **GetLinesFromFile
 
925
(
 
926
   FILE *fpin,    /* stream to read from */
 
927
   char **curlns  /* NULL-termed list of lines to prepend to file list ret */
 
928
)
 
929
/*
 
930
 * Read all lines from file fnam, suffix them to lines in curlns, delete
 
931
 * curlns, and return them as NULL-terminated array of strings
 
932
 */
 
933
{
 
934
   char **lns=NULL;
 
935
   int i, n, nc=0, N;
 
936
   char ln[2048];
 
937
 
 
938
   assert(fpin);
 
939
   for (n=0; fgets(ln, 2048, fpin); n++);  /* count number of lines in file */
 
940
   rewind(fpin);
 
941
   if (curlns)
 
942
      for (nc=0; curlns[nc]; nc++);
 
943
   N = n+nc;
 
944
   if (N < 1)
 
945
      return(NULL);
 
946
   lns = malloc((N+1)*sizeof(char*));
 
947
   assert(lns);
 
948
   if (nc)
 
949
   {
 
950
      for (i=0; i < nc; i++)
 
951
         lns[i] = curlns[i];
 
952
      free(curlns);
 
953
   }
 
954
   for (i=0; i < n; i++)
 
955
   {
 
956
      int k;
 
957
      char *sp;
 
958
      assert(fgets(ln, 2048, fpin));
 
959
      k = strlen(ln)+1;
 
960
      lns[i+nc] = sp = malloc(k*sizeof(char));
 
961
      assert(sp);
 
962
      strcpy(sp, ln);
 
963
/*
 
964
 *    Get rid of whitespace on end of line
 
965
 */
 
966
      for (k -= 2; isspace(sp[k]); k--) sp[k] = '\0';
 
967
   }
 
968
   lns[N] = NULL;
 
969
   return(lns);
 
970
}
 
971
 
 
972
void PrintAllStringsInList
 
973
(
 
974
   char *exp,
 
975
   char **strs  /* NULL terminated arrayof strings to be printed */
 
976
)
 
977
{
 
978
   int i;
 
979
   if (!strs)
 
980
   {
 
981
      printf("%s: NULL\n", exp);
 
982
      return;
 
983
   }
 
984
   printf("%s:\n", exp);
 
985
   for (i=0; strs[i]; i++)
 
986
      printf("   '%s'\n", strs[i]);
 
987
}
 
988
void KillAllStringsInList
 
989
(
 
990
   char **strs  /* NULL terminated arrayof strings to be freed */
 
991
)
 
992
{
 
993
   int i;
 
994
   if (!strs)
 
995
      return;
 
996
   for (i=0; strs[i]; i++)
 
997
      free(strs[i]);
 
998
   free(strs);
 
999
}
 
1000
 
 
1001
char *FreeListGetString
 
1002
(
 
1003
   char **strs, /* NULL-terminated array of strings to be freed */
 
1004
   int n        /* only string you want to retain from array (returned) */
 
1005
)
 
1006
{
 
1007
   char *ret;
 
1008
   int i;
 
1009
 
 
1010
   if (!strs)
 
1011
      return(NULL);
 
1012
   ret = strs[n];
 
1013
   for (i=0; strs[i]; i++)
 
1014
   {
 
1015
      if (i != n)
 
1016
         free(strs[i]);
 
1017
   }
 
1018
   free(strs);
 
1019
   return(ret);
 
1020
}
 
1021
 
 
1022
char **NewOneStringList
 
1023
(
 
1024
   char **strs, /* NULL-terminated array of strings to be freed */
 
1025
   int n        /* only string you want to retain from array (returned) */
 
1026
)
 
1027
{
 
1028
   char **nstrs;
 
1029
   nstrs = malloc(2*sizeof(char*));
 
1030
   assert(nstrs);
 
1031
   nstrs[0] = FreeListGetString(strs, n);
 
1032
   nstrs[1] = NULL;
 
1033
   return(nstrs);
795
1034
}