PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/teca-env18/lib/python2.7/site-packages/scipy/linalg/_cython_signature_generator.py

https://github.com/dpritsos/WEGA
Python | 129 lines | 89 code | 13 blank | 27 comment | 12 complexity | 66d0bc830b040d55ce45a922a9a9715d MD5 | raw file
  1. """
  2. A script that uses f2py to generate the signature files used to make
  3. the Cython BLAS and LAPACK wrappers from the fortran source code for
  4. LAPACK and the reference BLAS.
  5. To generate the BLAS wrapper signatures call:
  6. python _cython_signature_generator.py blas <blas_directory> <out_file>
  7. To generate the LAPACK wrapper signatures call:
  8. python _cython_signature_generator.py lapack <lapack_src_directory> <out_file>
  9. """
  10. import glob
  11. from numpy.f2py import crackfortran
  12. sig_types = {'integer': 'int',
  13. 'complex': 'c',
  14. 'double precision': 'd',
  15. 'real': 's',
  16. 'complex*16': 'z',
  17. 'double complex': 'z',
  18. 'character': 'char',
  19. 'logical': 'bint'}
  20. def get_type(info, arg):
  21. argtype = sig_types[info['vars'][arg]['typespec']]
  22. if argtype == 'c' and info['vars'][arg].get('kindselector') is not None:
  23. argtype = 'z'
  24. return argtype
  25. def make_signature(filename):
  26. info = crackfortran.crackfortran(filename)[0]
  27. name = info['name']
  28. if info['block'] == 'subroutine':
  29. return_type = 'void'
  30. else:
  31. return_type = get_type(info, name)
  32. arglist = [' *'.join([get_type(info, arg), arg]) for arg in info['args']]
  33. args = ', '.join(arglist)
  34. # Eliminate strange variable naming that replaces rank with rank_bn.
  35. args = args.replace('rank_bn', 'rank')
  36. return '{0} {1}({2})\n'.format(return_type, name, args)
  37. def get_sig_name(line):
  38. return line.split('(')[0].split(' ')[-1]
  39. def sigs_from_dir(directory, outfile, manual_wrappers=None, exclusions=None):
  40. if directory[-1] in ['/', '\\']:
  41. directory = directory[:-1]
  42. files = glob.glob(directory + '/*.f*')
  43. if exclusions is None:
  44. exclusions = []
  45. if manual_wrappers is not None:
  46. exclusions += [get_sig_name(l) for l in manual_wrappers.split('\n')]
  47. signatures = []
  48. for filename in files:
  49. name = filename.split('\\')[-1][:-2]
  50. if name in exclusions:
  51. continue
  52. signatures.append(make_signature(filename))
  53. if manual_wrappers is not None:
  54. signatures += [l + '\n' for l in manual_wrappers.split('\n')]
  55. signatures.sort(key=get_sig_name)
  56. comment = ["# This file was generated by _cython_signature_generator.py.\n",
  57. "# Do not edit this file directly.\n\n"]
  58. with open(outfile, 'w') as f:
  59. f.writelines(comment)
  60. f.writelines(signatures)
  61. # The signature that is used for zcgesv in lapack 3.1.0 and 3.1.1 changed
  62. # in version 3.2.0. The version included in the clapack on OSX has the
  63. # more recent signature though.
  64. # slamch and dlamch are not in the lapack src directory, but,since they
  65. # already have Python wrappers, we'll wrap them as well.
  66. # The other manual signatures are used because the signature generating
  67. # functions don't work when function pointer arguments are used.
  68. lapack_manual_wrappers = '''void cgees(char *jobvs, char *sort, cselect1 *select, int *n, c *a, int *lda, int *sdim, c *w, c *vs, int *ldvs, c *work, int *lwork, s *rwork, bint *bwork, int *info)
  69. void cgeesx(char *jobvs, char *sort, cselect1 *select, char *sense, int *n, c *a, int *lda, int *sdim, c *w, c *vs, int *ldvs, s *rconde, s *rcondv, c *work, int *lwork, s *rwork, bint *bwork, int *info)
  70. void cgges(char *jobvsl, char *jobvsr, char *sort, cselect2 *selctg, int *n, c *a, int *lda, c *b, int *ldb, int *sdim, c *alpha, c *beta, c *vsl, int *ldvsl, c *vsr, int *ldvsr, c *work, int *lwork, s *rwork, bint *bwork, int *info)
  71. void cggesx(char *jobvsl, char *jobvsr, char *sort, cselect2 *selctg, char *sense, int *n, c *a, int *lda, c *b, int *ldb, int *sdim, c *alpha, c *beta, c *vsl, int *ldvsl, c *vsr, int *ldvsr, s *rconde, s *rcondv, c *work, int *lwork, s *rwork, int *iwork, int *liwork, bint *bwork, int *info)
  72. void dgees(char *jobvs, char *sort, dselect2 *select, int *n, d *a, int *lda, int *sdim, d *wr, d *wi, d *vs, int *ldvs, d *work, int *lwork, bint *bwork, int *info)
  73. void dgeesx(char *jobvs, char *sort, dselect2 *select, char *sense, int *n, d *a, int *lda, int *sdim, d *wr, d *wi, d *vs, int *ldvs, d *rconde, d *rcondv, d *work, int *lwork, int *iwork, int *liwork, bint *bwork, int *info)
  74. void dgges(char *jobvsl, char *jobvsr, char *sort, dselect3 *selctg, int *n, d *a, int *lda, d *b, int *ldb, int *sdim, d *alphar, d *alphai, d *beta, d *vsl, int *ldvsl, d *vsr, int *ldvsr, d *work, int *lwork, bint *bwork, int *info)
  75. void dggesx(char *jobvsl, char *jobvsr, char *sort, dselect3 *selctg, char *sense, int *n, d *a, int *lda, d *b, int *ldb, int *sdim, d *alphar, d *alphai, d *beta, d *vsl, int *ldvsl, d *vsr, int *ldvsr, d *rconde, d *rcondv, d *work, int *lwork, int *iwork, int *liwork, bint *bwork, int *info)
  76. d dlamch(char *cmach)
  77. void ilaver(int *vers_major, int *vers_minor, int *vers_patch)
  78. void sgees(char *jobvs, char *sort, sselect2 *select, int *n, s *a, int *lda, int *sdim, s *wr, s *wi, s *vs, int *ldvs, s *work, int *lwork, bint *bwork, int *info)
  79. void sgeesx(char *jobvs, char *sort, sselect2 *select, char *sense, int *n, s *a, int *lda, int *sdim, s *wr, s *wi, s *vs, int *ldvs, s *rconde, s *rcondv, s *work, int *lwork, int *iwork, int *liwork, bint *bwork, int *info)
  80. void sgges(char *jobvsl, char *jobvsr, char *sort, sselect3 *selctg, int *n, s *a, int *lda, s *b, int *ldb, int *sdim, s *alphar, s *alphai, s *beta, s *vsl, int *ldvsl, s *vsr, int *ldvsr, s *work, int *lwork, bint *bwork, int *info)
  81. void sggesx(char *jobvsl, char *jobvsr, char *sort, sselect3 *selctg, char *sense, int *n, s *a, int *lda, s *b, int *ldb, int *sdim, s *alphar, s *alphai, s *beta, s *vsl, int *ldvsl, s *vsr, int *ldvsr, s *rconde, s *rcondv, s *work, int *lwork, int *iwork, int *liwork, bint *bwork, int *info)
  82. s slamch(char *cmach)
  83. void zgees(char *jobvs, char *sort, zselect1 *select, int *n, z *a, int *lda, int *sdim, z *w, z *vs, int *ldvs, z *work, int *lwork, d *rwork, bint *bwork, int *info)
  84. void zgeesx(char *jobvs, char *sort, zselect1 *select, char *sense, int *n, z *a, int *lda, int *sdim, z *w, z *vs, int *ldvs, d *rconde, d *rcondv, z *work, int *lwork, d *rwork, bint *bwork, int *info)
  85. void zgges(char *jobvsl, char *jobvsr, char *sort, zselect2 *selctg, int *n, z *a, int *lda, z *b, int *ldb, int *sdim, z *alpha, z *beta, z *vsl, int *ldvsl, z *vsr, int *ldvsr, z *work, int *lwork, d *rwork, bint *bwork, int *info)
  86. void zggesx(char *jobvsl, char *jobvsr, char *sort, zselect2 *selctg, char *sense, int *n, z *a, int *lda, z *b, int *ldb, int *sdim, z *alpha, z *beta, z *vsl, int *ldvsl, z *vsr, int *ldvsr, d *rconde, d *rcondv, z *work, int *lwork, d *rwork, int *iwork, int *liwork, bint *bwork, int *info)'''
  87. if __name__ == '__main__':
  88. from sys import argv
  89. libname, src_dir, outfile = argv[1:]
  90. # Exclude scabs and sisnan since they aren't currently included
  91. # in the scipy-specific ABI wrappers.
  92. if libname.lower() == 'blas':
  93. sigs_from_dir(src_dir, outfile, exclusions=['scabs1', 'xerbla'])
  94. elif libname.lower() == 'lapack':
  95. # Exclude all routines that do not have consistent interfaces from
  96. # LAPACK 3.1.0 through 3.6.0.
  97. # Also exclude routines with string arguments to avoid
  98. # compatibility woes with different standards for string arguments.
  99. # Exclude sisnan and slaneg since they aren't currently included in
  100. # The ABI compatibility wrappers.
  101. exclusions = ['sisnan', 'csrot', 'zdrot', 'ilaenv', 'iparmq', 'lsamen',
  102. 'xerbla', 'zcgesv', 'dlaisnan', 'slaisnan', 'dlazq3',
  103. 'dlazq4', 'slazq3', 'slazq4', 'dlasq3', 'dlasq4',
  104. 'slasq3', 'slasq4', 'dlasq5', 'slasq5', 'slaneg',
  105. # Routines deprecated in LAPACK 3.6.0
  106. 'cgegs', 'cgegv', 'cgelsx', 'cgeqpf', 'cggsvd', 'cggsvp',
  107. 'clahrd', 'clatzm', 'ctzrqf', 'dgegs', 'dgegv', 'dgelsx',
  108. 'dgeqpf', 'dggsvd', 'dggsvp', 'dlahrd', 'dlatzm', 'dtzrqf',
  109. 'sgegs', 'sgegv', 'sgelsx', 'sgeqpf', 'sggsvd', 'sggsvp',
  110. 'slahrd', 'slatzm', 'stzrqf', 'zgegs', 'zgegv', 'zgelsx',
  111. 'zgeqpf', 'zggsvd', 'zggsvp', 'zlahrd', 'zlatzm', 'ztzrqf']
  112. sigs_from_dir(src_dir, outfile, manual_wrappers=lapack_manual_wrappers,
  113. exclusions=exclusions)