2014-11-20 13:21:19 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2013-2014 Laurent CLOUET
|
|
|
|
|
* Author Laurent CLOUET <laurent.clouet@nopnop.net>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; version 2
|
|
|
|
|
* of the License.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.sheepit.client.hardware.gpu;
|
|
|
|
|
|
2018-05-03 13:35:57 +02:00
|
|
|
import java.util.HashMap;
|
2014-11-20 13:21:19 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import com.sheepit.client.os.OS;
|
|
|
|
|
import com.sun.jna.Native;
|
2016-07-20 16:29:17 +02:00
|
|
|
import com.sun.jna.ptr.IntByReference;
|
|
|
|
|
import com.sun.jna.ptr.LongByReference;
|
2014-11-20 13:21:19 +00:00
|
|
|
|
|
|
|
|
public class GPU {
|
|
|
|
|
public static List<GPUDevice> devices = null;
|
|
|
|
|
|
|
|
|
|
public static boolean generate() {
|
2016-10-12 01:40:59 +02:00
|
|
|
devices = new LinkedList<GPUDevice>();
|
|
|
|
|
|
2014-11-20 13:21:19 +00:00
|
|
|
OS os = OS.getOS();
|
|
|
|
|
String path = os.getCUDALib();
|
|
|
|
|
if (path == null) {
|
2015-02-06 21:04:27 +00:00
|
|
|
System.out.println("GPU::generate no CUDA lib path found");
|
2014-11-20 13:21:19 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
CUDA cudalib = null;
|
|
|
|
|
try {
|
|
|
|
|
cudalib = (CUDA) Native.loadLibrary(path, CUDA.class);
|
|
|
|
|
}
|
|
|
|
|
catch (java.lang.UnsatisfiedLinkError e) {
|
2015-02-06 21:04:27 +00:00
|
|
|
System.out.println("GPU::generate failed to load CUDA lib (path: " + path + ")");
|
2014-11-20 13:21:19 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
catch (java.lang.ExceptionInInitializerError e) {
|
2015-02-06 21:04:27 +00:00
|
|
|
System.out.println("GPU::generate ExceptionInInitializerError " + e);
|
2014-11-20 13:21:19 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
2015-02-06 21:04:27 +00:00
|
|
|
System.out.println("GPU::generate generic exception " + e);
|
2014-11-20 13:21:19 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int result = CUresult.CUDA_ERROR_UNKNOWN;
|
|
|
|
|
|
|
|
|
|
result = cudalib.cuInit(0);
|
|
|
|
|
if (result != CUresult.CUDA_SUCCESS) {
|
2015-02-06 21:04:27 +00:00
|
|
|
System.out.println("GPU::generate cuInit failed (ret: " + result + ")");
|
2015-06-25 00:42:46 +01:00
|
|
|
if (result == CUresult.CUDA_ERROR_UNKNOWN) {
|
|
|
|
|
System.out.println("If you are running Linux, this error is usually due to nvidia kernel module 'nvidia_uvm' not loaded.");
|
|
|
|
|
System.out.println("Relaunch the application as root or load the module.");
|
|
|
|
|
System.out.println("Most of time it does fix the issue.");
|
|
|
|
|
}
|
2014-11-20 13:21:19 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result == CUresult.CUDA_ERROR_NO_DEVICE) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-20 16:29:17 +02:00
|
|
|
IntByReference count = new IntByReference();
|
2014-11-20 13:21:19 +00:00
|
|
|
result = cudalib.cuDeviceGetCount(count);
|
|
|
|
|
|
|
|
|
|
if (result != CUresult.CUDA_SUCCESS) {
|
2015-02-06 21:04:27 +00:00
|
|
|
System.out.println("GPU::generate cuDeviceGetCount failed (ret: " + CUresult.stringFor(result) + ")");
|
2014-11-20 13:21:19 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-03 13:35:57 +02:00
|
|
|
HashMap<Integer, GPUDevice> devicesWithPciId = new HashMap<Integer, GPUDevice>(count.getValue());
|
2016-07-20 16:29:17 +02:00
|
|
|
for (int num = 0; num < count.getValue(); num++) {
|
2018-05-03 13:35:57 +02:00
|
|
|
IntByReference aDevice = new IntByReference();
|
|
|
|
|
|
|
|
|
|
result = cudalib.cuDeviceGet(aDevice, num);
|
|
|
|
|
if (result != CUresult.CUDA_SUCCESS) {
|
|
|
|
|
System.out.println("GPU::generate cuDeviceGet failed (ret: " + CUresult.stringFor(result) + ")");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IntByReference pciBusId = new IntByReference();
|
|
|
|
|
result = cudalib.cuDeviceGetAttribute(pciBusId, CUDeviceAttribute.CU_DEVICE_ATTRIBUTE_PCI_BUS_ID, aDevice.getValue());
|
|
|
|
|
if (result != CUresult.CUDA_SUCCESS) {
|
|
|
|
|
System.out.println("GPU::generate cuDeviceGetAttribute for CU_DEVICE_ATTRIBUTE_PCI_BUS_ID failed (ret: " + CUresult.stringFor(result) + ")");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-20 13:21:19 +00:00
|
|
|
byte name[] = new byte[256];
|
|
|
|
|
|
|
|
|
|
result = cudalib.cuDeviceGetName(name, 256, num);
|
|
|
|
|
if (result != CUresult.CUDA_SUCCESS) {
|
2015-02-06 21:04:27 +00:00
|
|
|
System.out.println("GPU::generate cuDeviceGetName failed (ret: " + CUresult.stringFor(result) + ")");
|
2014-11-20 13:21:19 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-20 16:29:17 +02:00
|
|
|
LongByReference ram = new LongByReference();
|
2017-02-28 12:55:12 +01:00
|
|
|
try {
|
|
|
|
|
result = cudalib.cuDeviceTotalMem_v2(ram, num);
|
|
|
|
|
}
|
|
|
|
|
catch (UnsatisfiedLinkError e) {
|
|
|
|
|
// fall back to old function
|
|
|
|
|
result = cudalib.cuDeviceTotalMem(ram, num);
|
|
|
|
|
}
|
2014-11-20 13:21:19 +00:00
|
|
|
|
|
|
|
|
if (result != CUresult.CUDA_SUCCESS) {
|
2015-02-06 21:04:27 +00:00
|
|
|
System.out.println("GPU::generate cuDeviceTotalMem failed (ret: " + CUresult.stringFor(result) + ")");
|
2014-11-20 13:21:19 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-03 13:35:57 +02:00
|
|
|
devicesWithPciId.put(pciBusId.getValue(), new GPUDevice(new String(name).trim(), ram.getValue(), "FAKE"));
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
2018-05-03 13:35:57 +02:00
|
|
|
|
|
|
|
|
// generate proper cuda id
|
|
|
|
|
// in theory a set to environment "CUDA_DEVICE_ORDER=PCI_BUS_ID" should be enough but it didn't work
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (HashMap.Entry<Integer, GPUDevice> entry : devicesWithPciId.entrySet()){
|
|
|
|
|
GPUDevice aDevice = entry.getValue();
|
|
|
|
|
aDevice.setCudaName("CUDA_" + Integer.toString(i));
|
|
|
|
|
devices.add(aDevice);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-20 13:21:19 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-24 17:49:27 +00:00
|
|
|
public static List<String> listModels() {
|
2014-11-20 13:21:19 +00:00
|
|
|
if (devices == null) {
|
|
|
|
|
generate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<String> devs = new LinkedList<String>();
|
|
|
|
|
for (GPUDevice dev : devices) {
|
|
|
|
|
devs.add(dev.getModel());
|
|
|
|
|
}
|
|
|
|
|
return devs;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-24 17:49:27 +00:00
|
|
|
public static List<GPUDevice> listDevices() {
|
|
|
|
|
if (devices == null) {
|
|
|
|
|
generate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return devices;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-20 13:21:19 +00:00
|
|
|
public static GPUDevice getGPUDevice(String device_model) {
|
|
|
|
|
if (device_model == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (devices == null) {
|
|
|
|
|
generate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (devices == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (GPUDevice dev : devices) {
|
|
|
|
|
if (device_model.equals(dev.getCudaName()) || device_model.equals(dev.getModel())) {
|
|
|
|
|
return dev;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|