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-2025.
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.TornadoBackend;
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.TornadoRuntimeProvider;
44  
45  /**
46   * Utility Routines to use the TornadoVM
47   */
48  public class FFXTornado {
49  
50    private FFXTornado() {
51      // Prevent instantiation.
52    }
53  
54    /**
55     * Get the default Tornado Device.
56     *
57     * @return The default TornadoDevice instance.
58     */
59    public static TornadoDevice getDevice() {
60      return TornadoRuntimeProvider.getTornadoRuntime().getDefaultDevice();
61    }
62  
63    /**
64     * Get the Tornado Device using specified driver and device index.
65     *
66     * @param driverIndex Driver index.
67     * @param deviceIndex Device index.
68     * @return The TornadoDevice instance.
69     */
70    public static TornadoDevice getDevice(int driverIndex, int deviceIndex) {
71      TornadoBackend tornadoBackend = TornadoRuntimeProvider.getTornadoRuntime().getBackend(driverIndex);
72      return tornadoBackend.getDevice(deviceIndex);
73    }
74  
75    /**
76     * Get the specified Tornado Device.
77     *
78     * @param deviceID The device ID.
79     * @return The TornadoDevice instance.
80     */
81    public static TornadoDevice getDevice(int deviceID) {
82      int n = 0;
83      int numBackends = TornadoRuntimeProvider.getTornadoRuntime().getNumBackends();
84      for (int backendIndex = 0; backendIndex < numBackends; backendIndex++) {
85        TornadoBackend tornadoBackend = TornadoRuntimeProvider.getTornadoRuntime().getBackend(backendIndex);
86        for (int deviceIndex = 0; deviceIndex < tornadoBackend.getNumDevices(); deviceIndex++) {
87          if (n == deviceID) {
88            TornadoRuntimeProvider.setProperty("devices", backendIndex + ":" + deviceIndex);
89            return getDevice(backendIndex, deviceIndex);
90          }
91          n++;
92        }
93      }
94      return null;
95    }
96  
97    /**
98     * Get all TornadoDevice instances.
99     *
100    * @return A List of TornadoDevice instances.
101    */
102   public static int getNumberOfDevices() {
103     int n = 0;
104     int numBackends = TornadoRuntimeProvider.getTornadoRuntime().getNumBackends();
105     for (int backendIndex = 0; backendIndex < numBackends; backendIndex++) {
106       TornadoBackend tornadoBackend = TornadoRuntimeProvider.getTornadoRuntime().getBackend(backendIndex);
107       int count = tornadoBackend.getNumDevices();
108       n += count;
109     }
110     return n;
111   }
112 
113   /**
114    * List details about the passed TornadoDevice instance.
115    *
116    * @param device The TornadoDevice instance.
117    */
118   public static void logDevice(TornadoDevice device) {
119     TornadoTargetDevice tornadoTargetDevice = device.getPhysicalDevice();
120     long[] workItemSize = tornadoTargetDevice.getDeviceMaxWorkItemSizes();
121     //        logger.info(format("\n Device Name:         %s",
122     // tornadoTargetDevice.getDeviceName()));
123     //        logger.info(format(" Compute Units:       %s",
124     // tornadoTargetDevice.getDeviceMaxComputeUnits()));
125     //        logger.info(format(" Max Work Item Sizes: [%d, %d, %d]", workItemSize[0],
126     // workItemSize[1], workItemSize[2]));
127     //        logger.info(format(" Clock Frequency:     %6d Ghz",
128     // tornadoTargetDevice.getDeviceMaxClockFrequency()));
129     //        logger.info(format(" Global Memory:       %6d MB",
130     // tornadoTargetDevice.getDeviceGlobalMemorySize() / 1024 / 1024));
131     //        logger.info(format(" Local Memory:        %6d KB",
132     // tornadoTargetDevice.getDeviceLocalMemorySize() / 1024));
133     System.out.printf("\n Device Name:         %s%n", tornadoTargetDevice.getDeviceName());
134     System.out.printf(" Backend:             %s%n", device.getTornadoVMBackend().name());
135     System.out.printf(" Compute Units:       %s%n", tornadoTargetDevice.getDeviceMaxComputeUnits());
136     System.out.printf(" Max Work Item Sizes: [%d, %d, %d]%n", workItemSize[0], workItemSize[1],
137         workItemSize[2]);
138     System.out.printf(" Clock Frequency:     %6d Ghz%n",
139         tornadoTargetDevice.getDeviceMaxClockFrequency());
140     System.out.printf(" Global Memory:       %6d MB%n",
141         tornadoTargetDevice.getDeviceGlobalMemorySize() / 1024 / 1024);
142     System.out.printf(" Local Memory:        %6d KB%n",
143         tornadoTargetDevice.getDeviceLocalMemorySize() / 1024);
144   }
145 }