View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2024.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.numerics.tornado;
39  
40  import uk.ac.manchester.tornado.api.TornadoDriver;
41  import uk.ac.manchester.tornado.api.TornadoTargetDevice;
42  import uk.ac.manchester.tornado.api.common.TornadoDevice;
43  import uk.ac.manchester.tornado.api.runtime.TornadoRuntime;
44  
45  import java.util.logging.Logger;
46  
47  /** Utility Routines to use the TornadoVM */
48  public class FFXTornado {
49  
50    private static final Logger logger = Logger.getLogger(FFXTornado.class.getName());
51  
52    /**
53     * Get the default Tornado Device.
54     *
55     * @return The default TornadoDevice instance.
56     */
57    public static TornadoDevice getDevice() {
58      return TornadoRuntime.getTornadoRuntime().getDefaultDevice();
59    }
60  
61    /**
62     * Get the Tornado Device using specified driver and device index.
63     *
64     * @param driverIndex Driver index.
65     * @param deviceIndex Device index.
66     * @return The TornadoDevice instance.
67     */
68    public static TornadoDevice getDevice(int driverIndex, int deviceIndex) {
69      TornadoDriver tornadoDriver = TornadoRuntime.getTornadoRuntime().getDriver(driverIndex);
70      return tornadoDriver.getDevice(deviceIndex);
71    }
72  
73    /**
74     * Get the specified Tornado Device.
75     *
76     * @param deviceID The device ID.
77     * @return The TornadoDevice instance.
78     */
79    public static TornadoDevice getDevice(int deviceID) {
80      int n = 0;
81      int numDrivers = TornadoRuntime.getTornadoRuntime().getNumDrivers();
82      for (int driverIndex = 0; driverIndex < numDrivers; driverIndex++) {
83        TornadoDriver driver = TornadoRuntime.getTornadoRuntime().getDriver(driverIndex);
84        for (int deviceIndex = 0; deviceIndex < driver.getDeviceCount(); deviceIndex++) {
85          if (n == deviceID) {
86            TornadoRuntime.setProperty("devices", driverIndex + ":" + deviceIndex);
87            return getDevice(driverIndex, deviceIndex);
88          }
89          n++;
90        }
91      }
92      return null;
93    }
94  
95    /**
96     * Get all TornadoDevice instances.
97     *
98     * @return A List of TornadoDevice instances.
99     */
100   public static int getNumberOfDevices() {
101     int n = 0;
102     int numDrivers = TornadoRuntime.getTornadoRuntime().getNumDrivers();
103     for (int driverIndex = 0; driverIndex < numDrivers; driverIndex++) {
104       TornadoDriver driver = TornadoRuntime.getTornadoRuntime().getDriver(driverIndex);
105       int count = driver.getDeviceCount();
106       n += count;
107     }
108     return n;
109   }
110 
111   /**
112    * List details about the passed TornadoDevice instance.
113    *
114    * @param device The TornadoDevice instance.
115    */
116   public static void logDevice(TornadoDevice device) {
117     TornadoTargetDevice tornadoTargetDevice = device.getPhysicalDevice();
118     long[] workItemSize = tornadoTargetDevice.getDeviceMaxWorkItemSizes();
119     //        logger.info(format("\n Device Name:         %s",
120     // tornadoTargetDevice.getDeviceName()));
121     //        logger.info(format(" Compute Units:       %s",
122     // tornadoTargetDevice.getDeviceMaxComputeUnits()));
123     //        logger.info(format(" Max Work Item Sizes: [%d, %d, %d]", workItemSize[0],
124     // workItemSize[1], workItemSize[2]));
125     //        logger.info(format(" Clock Frequency:     %6d Ghz",
126     // tornadoTargetDevice.getDeviceMaxClockFrequency()));
127     //        logger.info(format(" Global Memory:       %6d MB",
128     // tornadoTargetDevice.getDeviceGlobalMemorySize() / 1024 / 1024));
129     //        logger.info(format(" Local Memory:        %6d KB",
130     // tornadoTargetDevice.getDeviceLocalMemorySize() / 1024));
131     System.out.printf("\n Device Name:         %s%n", tornadoTargetDevice.getDeviceName());
132     System.out.printf(" Backend:             %s%n", device.getTornadoVMBackend().name());
133     System.out.printf(" Compute Units:       %s%n", tornadoTargetDevice.getDeviceMaxComputeUnits());
134     System.out.printf(" Max Work Item Sizes: [%d, %d, %d]%n", workItemSize[0], workItemSize[1],
135         workItemSize[2]);
136     System.out.printf(" Clock Frequency:     %6d Ghz%n",
137         tornadoTargetDevice.getDeviceMaxClockFrequency());
138     System.out.printf(" Global Memory:       %6d MB%n",
139         tornadoTargetDevice.getDeviceGlobalMemorySize() / 1024 / 1024);
140     System.out.printf(" Local Memory:        %6d KB%n",
141         tornadoTargetDevice.getDeviceLocalMemorySize() / 1024);
142   }
143 }