|
| 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