-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
cudalucas2.sh
208 lines (199 loc) · 7.81 KB
/
cudalucas2.sh
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
#!/bin/bash
# Teal Dulcet
# wget -qO - https://raw.github.com/tdulcet/Distributed-Computing-Scripts/master/cudalucas2.sh | bash -s --
# ./cudalucas2.sh <Computer number> [PrimeNet User ID] [Computer name] [Type of work] [Idle time to run (mins)]
# ./cudalucas2.sh <N> "$USER" "$HOSTNAME" 100 10
# ./cudalucas2.sh <N> ANONYMOUS
DIR="cudalucas"
if [[ $# -lt 1 || $# -gt 5 ]]; then
echo "Usage: $0 <Computer number> [PrimeNet User ID] [Computer name] [Type of work] [Idle time to run (mins)]" >&2
exit 1
fi
N=$1
USERID=${2:-$USER}
COMPUTER=${3:-$HOSTNAME}
TYPE=${4:-100}
TIME=${5:-10}
DEVICE=0
RE='^[0-9]+$'
if ! [[ $N =~ $RE ]]; then
echo "Usage: <Computer number> must be a number" >&2
exit 1
fi
RE='^10[0124]$'
if ! [[ $TYPE =~ $RE ]]; then
echo "Usage: [Type of work] must be a number" >&2
exit 1
fi
RE='^([0-9]*\.)?[0-9]+$'
if ! [[ $TIME =~ $RE ]]; then
echo "Usage: [Idle time to run] must be a number" >&2
exit 1
fi
echo -e "PrimeNet User ID:\t$USERID"
echo -e "Computer name:\t\t$COMPUTER"
echo -e "Type of work:\t\t$TYPE"
echo -e "Idle time to run:\t$TIME minutes\n"
if [[ -e idletime.sh ]]; then
bash -- idletime.sh
else
wget -qO - https://raw.github.com/tdulcet/Distributed-Computing-Scripts/master/idletime.sh | bash -s
fi
GPU=$(lspci | grep -i 'vga\|3d\|2d')
if ! echo "$GPU" | grep -iq 'nvidia'; then
echo -e "Please enter your password if prompted.\n"
sudo update-pciids
GPU=$(lspci | grep -i 'vga\|3d\|2d')
if ! echo "$GPU" | grep -iq 'nvidia'; then
echo "$GPU" | sed -n 's/^.*: //p'
echo "Error: This computer does not have an Nvidia GPU" >&2
exit 1
fi
fi
if ! command -v svn >/dev/null; then
echo -e "Installing Apache Subversion"
echo -e "Please enter your password if prompted.\n"
sudo apt-get update -y
sudo apt-get install -y subversion
fi
if ! command -v nvcc >/dev/null; then
echo -e "Installing the Nvidia CUDA Toolkit"
echo -e "Please enter your password if prompted.\n"
sudo apt-get update -y
sudo apt-get install -y nvidia-cuda-toolkit
fi
if [[ -n $CC ]] && ! command -v "$CC" >/dev/null; then
echo "Error: $CC is not installed." >&2
exit 1
fi
if ! command -v python3 >/dev/null; then
echo "Error: Python 3 is not installed." >&2
exit 1
fi
TIME=$(echo "$TIME" | awk '{ printf "%g", $1 * 60 }')
if [[ -d $DIR && -x "$DIR/CUDALucas" ]]; then
echo -e "CUDALucas is already downloaded\n"
cd "$DIR"
else
echo -e "Downloading CUDALucas\n"
svn checkout https://svn.code.sf.net/p/cudalucas/code/trunk "$DIR"
cd "$DIR"
echo -e "\nSetting up CUDALucas\n"
sed -i 's/\r//g' Makefile
sed -i 's/^OptLevel = 1/OptLevel = 3/' Makefile
CUDA=$(command -v nvcc | sed 's/\/bin\/nvcc$//')
sed -i "s/^CUDA = \/usr\/local\/cuda/CUDA = ${CUDA//\//\\/}/" Makefile
sed -i 's/--compiler-options=-Wall/-use_fast_math --compiler-options="-O$(OptLevel) -flto -Wall"/' Makefile
CC=$(command -v "${CC:-gcc}")
# sed -i "/^CUFLAGS / s/\$/ -ccbin ${CC//\//\\/}/" Makefile # -dlto
sed -i '/^CFLAGS / s/$/ -flto/' Makefile
# Adapted from: https://stackoverflow.com/a/37757606
cat <<EOF >/tmp/cudaComputeVersion.cu
#include <stdio.h>
int main()
{
const int device = $DEVICE;
cudaDeviceProp prop;
cudaError_t status = cudaGetDeviceProperties(&prop, device);
if (status != cudaSuccess) {
fprintf(stderr, "cudaGetDeviceProperties() for device %d failed: %s\n", device, cudaGetErrorString(status));
return 1;
}
const int v = prop.major * 10 + prop.minor;
printf("-gencode arch=compute_%d,code=sm_%d\n", v, v);
return 0;
}
EOF
trap 'rm /tmp/cudaComputeVersion{.cu,}' EXIT
nvcc /tmp/cudaComputeVersion.cu -O3 --compiler-options='-O3 -Wall' -o /tmp/cudaComputeVersion
if ! COMPUTE=$(/tmp/cudaComputeVersion); then
echo "$COMPUTE"
echo "Error: CUDA compute capability not found" >&2
exit 1
fi
sed -i "s/--generate-code arch=compute_35,code=sm_35/$COMPUTE/" Makefile
# Increase buffers to prevent buffer overflow
sed -i 's/file\[32\]/file[268]/g' CUDALucas.cu
sed -i 's/file_bak\[64\]/file_bak[455]/g' CUDALucas.cu
sed -i 's/chkpnt_sfn\[64\]/chkpnt_sfn[234]/g' CUDALucas.cu
sed -i 's/test\[64\]/test[203]/g' CUDALucas.cu
# Fix deprecation warnings
sed -i '/IniGet/ s/char \?\* \?name/const char* name/; s/char \?\* \?df/const char* df/' parse.{h,c}
make
make clean
fi
DIR=$PWD
if [[ -f "autoprimenet.py" ]]; then
echo -e "AutoPrimeNet is already downloaded\n"
else
echo -e "\nDownloading AutoPrimeNet\n"
if [[ -e ../autoprimenet.py ]]; then
cp -v ../autoprimenet.py .
else
wget -nv https://raw.github.com/tdulcet/AutoPrimeNet/main/autoprimenet.py
fi
chmod +x autoprimenet.py
python3 -OO -m py_compile autoprimenet.py
fi
echo -e "\nInstalling the Requests library\n"
# python3 -m ensurepip --default-pip || true
python3 -m pip install --upgrade pip || true
if ! python3 -m pip install requests; then
if command -v pip3 >/dev/null; then
pip3 install requests
else
echo -e "\nWarning: pip3 is not installed and the Requests library may also not be installed\n"
fi
fi
cp CUDALucas.ini "CUDALucas$N.ini"
sed -i "s/^WorkFile=worktodo.txt/WorkFile=worktodo$N.txt/" "CUDALucas$N.ini"
sed -i "s/^ResultsFile=results.txt/ResultsFile=results$N.txt/" "CUDALucas$N.ini"
sed -i "s/^DeviceNumber=0/DeviceNumber=$DEVICE/" "CUDALucas$N.ini"
echo -e "\nRegistering computer with PrimeNet\n"
ARGS=()
if command -v nvidia-smi >/dev/null && nvidia-smi >/dev/null; then
mapfile -t GPU < <(nvidia-smi --query-gpu=gpu_name --format=csv,noheader)
ARGS+=(--cpu-model="${GPU[DEVICE]}")
mapfile -t GPU_FREQ < <(nvidia-smi --query-gpu=clocks.max.gr --format=csv,noheader,nounits | grep -iv 'not supported')
if [[ -n $GPU_FREQ ]]; then
ARGS+=(--frequency="${GPU_FREQ[DEVICE]}")
fi
mapfile -t TOTAL_GPU_MEM < <(nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits | grep -iv 'not supported')
if [[ -n $TOTAL_GPU_MEM ]]; then
total=${TOTAL_GPU_MEM[DEVICE]}
ARGS+=(--memory="$total" --max-memory="$total")
fi
fi
python3 -OO autoprimenet.py -t 0 -T "$TYPE" -u "$USERID" -i "worktodo$N.txt" -r "results$N.txt" -L "primenet$N.log" -l "prime$N.ini" --cudalucas "cudalucas$N.out" -H "$COMPUTER" "${ARGS[@]}"
echo -e "\nStarting AutoPrimeNet\n"
nohup python3 -OO autoprimenet.py -l "prime$N.ini" >>"autoprimenet$N.out" &
sleep 1
echo -e "\nOptimizing CUDALucas for your computer and GPU\nThis may take a while…\n"
timeout -v 60 ./CUDALucas 2976221 || true
./CUDALucas -cufftbench 1024 12288 5
./CUDALucas -threadbench 1024 12288 5 0
echo -e "\nRunning self tests\nThis will take a while…\n"
./CUDALucas -r 1
# ./CUDALucas 6972593
echo -e "\nStarting CUDALucas\n"
nohup nice ./CUDALucas -i "CUDALucas$N.ini" >>"cudalucas$N.out" &
sleep 1
#crontab -l | { cat; echo "@reboot cd ${DIR@Q} && nohup nice ./CUDALucas -i 'CUDALucas$N.ini' >> 'cudalucas$N.out' &"; } | crontab -
#crontab -l | { cat; echo "@reboot cd ${DIR@Q} && nohup python3 -OO autoprimenet.py -l 'prime$N.ini' >> 'autoprimenet$N.out' &"; } | crontab -
cat <<EOF >CUDALucas.sh
#!/bin/bash
# Copyright © 2020 Teal Dulcet
# Start CUDALucas and AutoPrimeNet if the computer has not been used in the specified idle time and stop it when someone uses the computer
# ${DIR@Q}/CUDALucas.sh
NOW=\${EPOCHSECONDS:-\$(date +%s)}
if who -s | awk '{ print \$2 }' | (cd /dev && xargs -r stat -c '%U %X') | awk '{if ('"\$NOW"'-\$2<$TIME) { print \$1"\t"'"\$NOW"'-\$2; ++count }} END{if (count>0) { exit 1 }}' >/dev/null; then
pgrep -x CUDALucas >/dev/null || (cd ${DIR@Q} && exec nohup nice ./CUDALucas -i 'CUDALucas$N.ini' >>'cudalucas$N.out' &)
pgrep -f '^python3 -OO autoprimenet\.py' >/dev/null || (cd ${DIR@Q} && exec nohup python3 -OO autoprimenet.py -l 'prime$N.ini' >>'autoprimenet$N.out' &)
else
pgrep -x CUDALucas >/dev/null && killall CUDALucas
fi
EOF
chmod +x CUDALucas.sh
echo -e "\nRun this command for it to start if the computer has not been used in the specified idle time and stop it when someone uses the computer:\n"
echo "crontab -l | { cat; echo \"* * * * * ${DIR@Q}/CUDALucas.sh\"; } | crontab -"
echo -e "\nTo edit the crontab, run \"crontab -e\""