|
| 1 | +package net.haesleinhuepf.clijx.parallel; |
| 2 | + |
| 3 | +import ij.IJ; |
| 4 | +import net.haesleinhuepf.clij.CLIJ; |
| 5 | +import org.scijava.ItemVisibility; |
| 6 | +import org.scijava.command.Command; |
| 7 | +import org.scijava.command.DynamicCommand; |
| 8 | +import org.scijava.plugin.Parameter; |
| 9 | +import org.scijava.plugin.Plugin; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | + |
| 13 | +/** |
| 14 | + * Allow to specify how the default static {@link CLIJxPool} is created |
| 15 | + * One can exclude some devices from the pool, or fine tune some devices by setting |
| 16 | + * how many contexts should be created for each available CL device |
| 17 | + * |
| 18 | + * TODO: there's an issue if there are multiple identical devices, say 2x 4080 cards. I need to test a configuration like that. Maybe with BIOP Desktop. |
| 19 | + */ |
| 20 | + |
| 21 | +@Plugin(type = Command.class, headless = true, |
| 22 | + menuPath = "Plugins>Specify CLIJxPool", |
| 23 | + initializer = "init", // Allows to user to know its configuration |
| 24 | + description = "Allow to specify how GPU resources are split (only applies to tasks using CLIJxPool.getInstance()) ") |
| 25 | +public class CLIJPoolSpecifyCommand extends DynamicCommand { |
| 26 | + |
| 27 | + @Parameter(visibility = ItemVisibility.MESSAGE, persist = false) |
| 28 | + String info_for_user; |
| 29 | + |
| 30 | + @Parameter(label = "Devices To Exclude (comma separated)") |
| 31 | + String devices_to_exclude_csv = ""; |
| 32 | + |
| 33 | + @Parameter(label = "Split Devices (device name then number, comma separated)", description = "For instance '3080, 8, Intel, 4'") |
| 34 | + String devices_split_csv = ""; |
| 35 | + |
| 36 | + @Override |
| 37 | + public void run() { |
| 38 | + try { |
| 39 | + CLIJ.getAvailableDeviceNames(); |
| 40 | + } catch (Exception e) { |
| 41 | + System.out.println("Could not get CL devices information - skipping command."); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + if (CLIJxPool.isIntanceSet()) { |
| 46 | + IJ.log("A CLIJxPool instance is already set, closing it..."); |
| 47 | + CLIJxPool.getInstance().shutdown(); |
| 48 | + IJ.log("Previous pool closed."); |
| 49 | + } |
| 50 | + |
| 51 | + if (!devices_split_csv.trim().isEmpty()) { |
| 52 | + CLIJxPool.clearDevicesSplit(); |
| 53 | + String[] devices_to_split = devices_split_csv.trim().split(","); |
| 54 | + if (devices_to_split.length % 2 == 1) { |
| 55 | + System.err.println("You have an issue with the device split parameters, the number or arguments is odd."); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + // Validate first |
| 60 | + for (int iDevice = 0; iDevice<devices_to_split.length; iDevice+=2) { |
| 61 | + try { |
| 62 | + int nContexts = Integer.parseInt(devices_to_split[iDevice + 1]); |
| 63 | + if (nContexts <= 0 ) { |
| 64 | + System.err.println("Invalid contexts number, a strictly positive number is expected."); |
| 65 | + return; |
| 66 | + } |
| 67 | + } catch (Exception e) { |
| 68 | + System.err.println("Error while parsing argument "+devices_to_split[iDevice+1]+" - it should be an integer."); |
| 69 | + return; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Do |
| 74 | + for (int iDevice = 0; iDevice<devices_to_split.length; iDevice+=2) { |
| 75 | + String deviceName = devices_to_split[iDevice].trim(); |
| 76 | + int nContexts = Integer.parseInt(devices_to_split[iDevice+1]); |
| 77 | + CLIJxPool.setNumberOfInstancePerDevice(deviceName, nContexts); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (!devices_to_exclude_csv.trim().isEmpty()) { |
| 82 | + CLIJxPool.clearExcludedDevices(); |
| 83 | + String[] devices_to_exclude = devices_to_exclude_csv.trim().split(","); |
| 84 | + for (String device_to_exclude: devices_to_exclude) { |
| 85 | + CLIJxPool.excludeDevice(device_to_exclude); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + IJ.log("- Creating CLIJ Pool:"); |
| 90 | + CLIJxPool.getInstance(); |
| 91 | + |
| 92 | + IJ.log("- CLIJ Pool specifications:"); |
| 93 | + IJ.log(CLIJxPool.getInstance().getDetails()); |
| 94 | + } |
| 95 | + |
| 96 | + protected void init() { // DO NOT DELETE! This is actually used by the initializer |
| 97 | + ArrayList<String> allDevices = null; |
| 98 | + try { |
| 99 | + allDevices = CLIJ.getAvailableDeviceNames(); |
| 100 | + } catch (Exception e) { |
| 101 | + info_for_user = "Could not get CL devices information - this command will not work."; |
| 102 | + return; |
| 103 | + } |
| 104 | + info_for_user = "<html>"; |
| 105 | + info_for_user += "<h2>Specify pool of CLIJ instances</h2><br>"; |
| 106 | + info_for_user += "Available devices:<br>"; |
| 107 | + for (String device: allDevices) { |
| 108 | + info_for_user += " - "+device+" <br>"; |
| 109 | + } |
| 110 | + info_for_user+="</html>"; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | + |
0 commit comments