Skip to content

Commit f1f6d1b

Browse files
authored
Merge pull request #97 from ixfd64/helper-script
add Linux shell script for launching multiple instances
2 parents aca20d9 + 18f34f4 commit f1f6d1b

File tree

2 files changed

+146
-12
lines changed

2 files changed

+146
-12
lines changed

README.txt

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Table of contents
1212
3 Running mfaktc
1313
3.1 Linux
1414
3.2 Windows
15+
3.3 mfaktc and multi-GPU systems
1516
4 Getting work and reporting results
1617
5 Known issues
1718
5.1 Non-issues
@@ -185,6 +186,11 @@ to 71 bits.
185186
- build mfaktc using the above instructions or download a stable release
186187
- go to the mfaktc root folder and run "./mfaktc"
187188

189+
On remote systems, processes are killed when a session disconnects. To prevent
190+
this, use a terminal multiplexer such as tmux or GNU Screen to detach and
191+
reattach sessions. You can also use nohup as a simpler alternative, but be
192+
aware that nohup cannot reattach to a process.
193+
188194
###############
189195
# 3.2 Windows #
190196
###############
@@ -193,8 +199,27 @@ mfaktc works very similarly on Windows. You can just run "mfaktc-win-64" in
193199
Command Prompt (cmd.exe) to launch the executable, or simply double-click it in
194200
File Explorer.
195201

196-
However, you do need to prepend the executable name with ".\" in PowerShell or
197-
Windows Terminal.
202+
Please note: the executable name needs to be prepended with ".\" in PowerShell
203+
or Windows Terminal.
204+
205+
####################################
206+
# 3.3 mfaktc and multi-GPU systems #
207+
####################################
208+
209+
mfaktc currently does not support multiple threads, but the -d option can be
210+
used to start an instance on a specific device. Please note that device IDs
211+
are zero-indexed. For example, the option '-d 1' tells mfaktc to use the
212+
second GPU in a system with multiple GPUs. To avoid resource conflicts, make
213+
sure each instance has its own folder.
214+
215+
It is recommended to set SieveOnGPU=1 in mfaktc.ini as this enables mfaktc to
216+
make full use of a device. Otherwise, multiple instances are needed to fully
217+
saturate a GPU when sieving on the CPU.
218+
219+
For experienced users, the shell script start-mfaktc.sh can be used to easily
220+
launch multiple instances on Linux systems. It can be found in the "contribs"
221+
folder in the mfaktc repository on GitHub. Detailed information is available
222+
in the script file.
198223

199224

200225
########################################
@@ -350,16 +375,6 @@ before making changes. ;-)
350375
# 7 FAQ #
351376
#########
352377

353-
Q: Does mfaktc support multiple GPUs?
354-
A: Currently no, but you can use the -d option to start an instance on a
355-
specific device. Please also see the next question.
356-
357-
Q: Can I run multiple mfaktc instances on the same computer?
358-
A: Yes. In most cases, this is required to make full use of a GPU when sieving
359-
on the CPU. Otherwise, one instance should fully utilize a single GPU.
360-
361-
You will need a separate directory for each mfaktc instance.
362-
363378
Q: Are checkpoint files compatible between different mfaktc versions?
364379
A: Save files are compatible between different platforms and architectures. For
365380
example, the 32-bit Windows version can read a checkpoint from 64-bit Linux

contrib/start-mfaktc.sh

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/bin/bash
2+
3+
# This file is part of mfaktc.
4+
# Copyright (c) 2019-2025 Danny Chia
5+
# Copyright (c) 2009-2011 Oliver Weihe (o.weihe@t-online.de)
6+
#
7+
# mfaktc is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# mfaktc is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with mfaktc. If not, see <http://www.gnu.org/licenses/>.
19+
#
20+
# SPDX-License-Identifier: GPL-3.0-or-later
21+
22+
# start-mfaktc.sh
23+
#
24+
# Enables users to easily launch multiple mfaktc instances on multi-GPU
25+
# systems.
26+
#
27+
# Usage:
28+
# ./start-mfaktc.sh [device ID]
29+
#
30+
# Device ID provided:
31+
# Uses symbolic links to launch an mfaktc instance from a device-specific
32+
# folder. It is recommended to use AutoPrimeNet to distribute assignments to
33+
# each worker. Otherwise, you will need to manually add a worktodo.txt file
34+
# each time you start a new batch of assignments.
35+
#
36+
# Device-specific settings are supported. You can simply add an mfaktc.ini
37+
# file to the device-specific folder as the script will not attempt to
38+
# overwrite or delete a file that already exists.
39+
#
40+
# Device ID not provided:
41+
# Launches the mfaktc executable from the root folder. Default behavior.
42+
#
43+
# To prevent concurrency issues, the script enforces a limit of one mfaktc
44+
# instance per GPU. It is recommended to set SieveOnGPU=1 in mfaktc.ini so
45+
# that each instance can make full use of a device.
46+
47+
APP=mfaktc
48+
APP_SETTINGS=$APP.ini
49+
LOCK=$APP.lock
50+
51+
run_on_device() {
52+
# ensure instance has its own folder
53+
mkdir -p "device-$1"
54+
if ! cd "device-$1"
55+
then
56+
echo "error: could not enter directory 'device-$1' for device $1" >&2
57+
exit 1
58+
fi
59+
60+
# don't run if device is in use
61+
exec 200>"$LOCK"
62+
63+
if ! flock -n 200; then
64+
echo "error: lock file $LOCK exists, mfaktc may already be running on device $1" >&2
65+
exit 1
66+
fi
67+
68+
# create symbolic links
69+
if [[ -e $APP ]]; then
70+
echo "error: cannot create symbolic link '$APP' in directory 'device-$1' as a file" >&2
71+
echo " with that name already exists. Stopped to prevent potential data loss" >&2
72+
exit 1
73+
fi
74+
75+
# use device-specific configuration file if one exists
76+
if [[ ! -e $APP_SETTINGS ]]; then
77+
# otherwise, mfaktc.ini must be present in root folder
78+
if [[ ! -e ../$APP_SETTINGS ]]; then
79+
echo "error: $APP_SETTINGS not found in root folder" >&2
80+
exit 1
81+
else
82+
ln -s ../$APP_SETTINGS .
83+
fi
84+
fi
85+
86+
ln -s ../$APP . && app_created=1
87+
88+
cleanup() {
89+
[[ -n "$app_created" ]] && rm -f $APP
90+
91+
# don't delete mfaktc.ini unless it's a symbolic link
92+
[[ -L $APP_SETTINGS ]] && rm $APP_SETTINGS
93+
}
94+
trap 'cleanup' EXIT
95+
96+
# run mfaktc on specified device
97+
./$APP -d "$1"
98+
}
99+
100+
# mfaktc executable must be present
101+
if [[ ! -e $APP ]]; then
102+
echo "error: mfaktc executable not found in root folder" >&2
103+
exit 1
104+
fi
105+
106+
if [[ $# -eq 0 ]]; then
107+
# don't run if device is in use
108+
exec 200>"$LOCK"
109+
110+
if ! flock -n 200; then
111+
echo "error: lock file $LOCK exists, mfaktc may already be running" >&2
112+
exit 1
113+
fi
114+
115+
# run mfaktc on default device
116+
exec ./$APP
117+
else
118+
run_on_device "$1"
119+
fi

0 commit comments

Comments
 (0)