-
Notifications
You must be signed in to change notification settings - Fork 9
/
nvidia-docker
executable file
·223 lines (184 loc) · 4.86 KB
/
nvidia-docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#! /bin/sh
# Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
DOCKER=${DOCKER:-"docker"}
NV_DEVICE="/dev/nvidia"
UVM_DEVICE="${NV_DEVICE}-uvm"
CTL_DEVICE="${NV_DEVICE}ctl"
CUDA_VERSION_LABEL="com.nvidia.cuda.version"
NV_BINS_VOLUME="/usr/local/bin"
NV_BINS="nvidia-cuda-mps-control \
nvidia-cuda-mps-server \
nvidia-debugdump \
nvidia-persistenced \
nvidia-smi"
NV_LIBS_VOLUME="/usr/local/nvidia"
NV_LIBS_CUDA="cuda \
nvcuvid \
nvidia-compiler \
nvidia-encode \
nvidia-ml"
NV_DOCKER_ARGS=""
__log()
{
local level="$1"
local msg="$2"
printf "[ NVIDIA ] =$level= $msg\n" >&2
}
__image_label()
{
local image="$1"
local label="$2"
echo $( $DOCKER inspect --format="{{index .Config.Labels \"$label\"}}" $image )
}
__nvsmi_query()
{
local section="$1"
local gpu_id="$2" # optional
local cmd="nvidia-smi -q"
if [ $# -eq 2 ]; then
cmd="$cmd -i $gpu_id"
fi
echo $( $cmd | grep "$section" | awk '{print $4}' )
}
__library_paths()
{
local lib="$1"
echo $( ldconfig -p | grep "lib${lib}.so" | awk '{print $4}' )
}
__library_arch()
{
local lib="$1"
echo $( file -L $lib | awk '{print $3}' | cut -d- -f1 )
}
__filter_duplicate_paths()
{
local paths="$1"
local sums="$( md5sum $paths | sed 's/[^/]*$/ &/' )"
local uniq="$( echo "$sums" | uniq -u -f2 | awk '{print $2$3}')"
local dupl="$( echo "$sums" | uniq --all-repeated=separate -f2 \
| uniq -w 32 | awk 'NF {print $2$3}')"
echo $uniq $dupl
}
check_prerequisites()
{
local cmds="nvidia-smi nvidia-modprobe"
for cmd in $cmds; do
command -v $cmd >/dev/null && continue
__log ERROR "Command not found: $cmd"
exit 1
done
}
parse_docker_args()
{
local args="$1"
local help="$2"
local shift=1
local skip=0
local non_bool_args="$( $DOCKER help $help | \
sed '/^\s*\(-[^=]\+\)=[^{true}{false}].*/!d;s//\1/;s/, /\n/' )"
for arg in $args; do
test $skip -eq 1 && skip=0 && continue
case $arg in
-*)
for nb_arg in $non_bool_args; do
test $arg = $nb_arg && skip=1 && break
done
shift=$((shift+$skip+1)) ;;
*)
return $shift ;;
esac
done
return 0
}
check_image_version()
{
local image="$1"
local driver_version="$( __nvsmi_query "Driver Version" )"
local cuda_image_version="$( __image_label $image $CUDA_VERSION_LABEL )"
if [ -z $cuda_image_version ]; then
__log INFO "Not a CUDA image, nothing to be done"
return 1
fi
__log INFO "Driver version: $driver_version"
__log INFO "CUDA image version: $cuda_image_version"
return 0
}
load_uvm()
{
if [ ! -e $UVM_DEVICE ]; then
nvidia-modprobe -u -c=0
fi
}
build_docker_args()
{
local args="--device=$CTL_DEVICE --device=$UVM_DEVICE"
for gpu in $( echo $GPU | tr -s ", " " " ); do
local minor="$( __nvsmi_query "Minor Number" $gpu )"
if [ -z $minor ]; then
__log WARN "Could not find GPU device: $gpu"
continue
fi
args="$args --device=${NV_DEVICE}$minor"
done
for lib in $NV_LIBS_CUDA; do
local paths="$( __library_paths $lib )"
if [ -z "$paths" ]; then
__log WARN "Could not find library: $lib"
continue
fi
for path in $( __filter_duplicate_paths "$paths" ); do
args="$args -v $path:$path"
case $( __library_arch "$path" ) in
32) args="$args -v $path:$NV_LIBS_VOLUME/lib/$(basename $path)" ;;
64) args="$args -v $path:$NV_LIBS_VOLUME/lib64/$(basename $path)" ;;
esac
done
done
for bin in $NV_BINS; do
local path="$( which $bin )"
if [ -z $path ]; then
__log WARN "Could not find binary: $bin"
continue
fi
args="$args -v $path:$NV_BINS_VOLUME/$bin"
done
NV_DOCKER_ARGS=$args
}
print_debug()
{
if [ ! -z $NVDEBUG ]; then
seq -s= 60 | tr -d '[:digit:]'
echo $NV_DOCKER_ARGS | sed 's/-[-v][^/]*//g' | tr ' ' '\n'
seq -s= 60 | tr -d '[:digit:]'
fi
}
setup()
{
local image="$1"
check_image_version $image
if [ $? -eq 0 ]; then
build_docker_args
load_uvm
print_debug >&2
fi
echo
}
check_prerequisites
parse_docker_args "$*"; ret=$?
DOCKER_ARGS=""
CMD="$( eval echo \${$ret} )"
i=0; while [ $i -lt $ret ]; do
DOCKER_ARGS="$DOCKER_ARGS $1"
i=$((i+1))
shift
done
case $CMD in
run|create)
parse_docker_args "$*" $CMD; ret=$?
if [ $ret -gt 0 ]; then
image="$( eval echo \${$ret} )"
setup $image
fi
;;
esac
$DOCKER $DOCKER_ARGS $NV_DOCKER_ARGS $@