utils.py 3.62 KB
Newer Older
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
1
import argparse
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
2
import ctypes
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
3
from datetime import date
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
4
import sys
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
5
6
7
8


def add_data_args(parser: argparse.ArgumentParser):
    parser.add_argument(
Gustaf Ahdritz's avatar
Fixes  
Gustaf Ahdritz committed
9
        '--uniref90_database_path', type=str, default=None,
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
10
11
    )
    parser.add_argument(
Gustaf Ahdritz's avatar
Fixes  
Gustaf Ahdritz committed
12
        '--mgnify_database_path', type=str, default=None,
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
13
14
    )
    parser.add_argument(
Gustaf Ahdritz's avatar
Fixes  
Gustaf Ahdritz committed
15
        '--pdb70_database_path', type=str, default=None,
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
16
    )
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
17
18
19
    parser.add_argument(
        '--pdb_seqres_database_path', type=str, default=None,
    )
20
21
22
    parser.add_argument(
        '--uniref30_database_path', type=str, default=None,
    )
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
23
    parser.add_argument(
Gustaf Ahdritz's avatar
Fixes  
Gustaf Ahdritz committed
24
        '--uniclust30_database_path', type=str, default=None,
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
25
    )
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
26
27
28
    parser.add_argument(
        '--uniprot_database_path', type=str, default=None,
    )
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
29
30
31
32
33
34
35
36
37
38
39
40
    parser.add_argument(
        '--bfd_database_path', type=str, default=None,
    )
    parser.add_argument(
        '--jackhmmer_binary_path', type=str, default='/usr/bin/jackhmmer'
    )
    parser.add_argument(
        '--hhblits_binary_path', type=str, default='/usr/bin/hhblits'
    )
    parser.add_argument(
        '--hhsearch_binary_path', type=str, default='/usr/bin/hhsearch'
    )
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
41
42
43
44
45
46
    parser.add_argument(
        '--hmmsearch_binary_path', type=str, default='/usr/bin/hmmsearch'
    )
    parser.add_argument(
        '--hmmbuild_binary_path', type=str, default='/usr/bin/hmmbuild'
    )
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
47
48
49
50
    parser.add_argument(
        '--kalign_binary_path', type=str, default='/usr/bin/kalign'
    )
    parser.add_argument(
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
51
        '--max_template_date', type=str,
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
52
53
54
55
56
        default=date.today().strftime("%Y-%m-%d"),
    )
    parser.add_argument(
        '--obsolete_pdbs_path', type=str, default=None
    )
57
58
59
    parser.add_argument(
        '--release_dates_path', type=str, default=None
    )
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
60
61
62
63
64
65
66
67


def get_nvidia_cc():
    """
    Returns a tuple containing the Compute Capability of the first GPU
    installed in the system (formatted as a tuple of strings) and an error
    message. When the former is provided, the latter is None, and vice versa.

Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
68
    Adapted from script by Jan Schlüte t
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
69
70
71
72
    https://gist.github.com/f0k/63a664160d016a491b2cbea15913d549
    """
    CUDA_SUCCESS = 0

73
74
75
76
    libnames = [
        'libcuda.so', 
        'libcuda.dylib', 
        'cuda.dll',
77
        '/usr/local/cuda/compat/libcuda.so', # For Docker
78
    ]
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
79
80
81
82
83
84
85
86
    for libname in libnames:
        try:
            cuda = ctypes.CDLL(libname)
        except OSError:
            continue
        else:
            break
    else:
87
        return None, "Could not load any of: " + ' '.join(libnames)
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
88
89
90
91
92
93
94
95
96
97
98

    nGpus = ctypes.c_int()
    cc_major = ctypes.c_int()
    cc_minor = ctypes.c_int()

    result = ctypes.c_int()
    device = ctypes.c_int()
    error_str = ctypes.c_char_p()

    result = cuda.cuInit(0)
    if result != CUDA_SUCCESS:
99
        cuda.cuGetErrorString(result, ctypes.byref(error_str))
100
101
102
103
        if error_str.value:
            return None, error_str.value.decode()
        else:
            return None, "Unknown error: cuInit returned %d" % result
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
104
105
    result = cuda.cuDeviceGetCount(ctypes.byref(nGpus))
    if result != CUDA_SUCCESS:
106
107
        cuda.cuGetErrorString(result, ctypes.byref(error_str))
        return None, error_str.value.decode()
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
108

109
    if nGpus.value < 1:
110
        return None, "No GPUs detected"
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
111
112
113

    result = cuda.cuDeviceGet(ctypes.byref(device), 0)
    if result != CUDA_SUCCESS:
114
115
        cuda.cuGetErrorString(result, ctypes.byref(error_str))
        return None, error_str.value.decode()
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
116
117
118
119
120
121
122
123

    if cuda.cuDeviceComputeCapability(ctypes.byref(cc_major), ctypes.byref(cc_minor), device) != CUDA_SUCCESS:
        return None, "Compute Capability not found"

    major = cc_major.value
    minor = cc_minor.value

    return (major, minor), None