Migrate from ant to gradle

This commit is contained in:
Laurent Clouet
2019-08-10 22:09:32 +02:00
parent 9f1f509bb6
commit 3230807a7d
75 changed files with 317 additions and 80 deletions

View File

@@ -0,0 +1,93 @@
/*
* Copyright (C) 2010-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.cpu;
public class CPU {
private String name;
private String model;
private String family;
private String arch; // 32 or 64 bits
public CPU() {
this.name = null;
this.model = null;
this.family = null;
this.generateArch();
}
public String name() {
return this.name;
}
public String model() {
return this.model;
}
public String family() {
return this.family;
}
public String arch() {
return this.arch;
}
public int cores() {
return Runtime.getRuntime().availableProcessors();
}
public void setName(String name_) {
this.name = name_;
}
public void setModel(String model_) {
this.model = model_;
}
public void setFamily(String family_) {
this.family = family_;
}
public void setArch(String arch_) {
this.arch = arch_;
}
public void generateArch() {
String arch = System.getProperty("os.arch").toLowerCase();
switch (arch) {
case "i386":
case "i686":
case "x86":
this.arch = "32bit";
break;
case "amd64":
case "x86_64":
this.arch = "64bit";
break;
default:
this.arch = null;
break;
}
}
public boolean haveData() {
return this.name != null && this.model != null && this.family != null && this.arch != null;
}
}

View File

@@ -0,0 +1,97 @@
/*
* 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;
import java.util.LinkedList;
import java.util.List;
import com.sheepit.client.Configuration;
import com.sheepit.client.hardware.gpu.nvidia.Nvidia;
import com.sheepit.client.hardware.gpu.opencl.OpenCL;
import com.sheepit.client.os.OS;
import com.sheepit.client.os.Windows;
public class GPU {
public static List<GPUDevice> devices = null;
public static boolean generate() {
devices = new LinkedList<GPUDevice>();
List<GPUDevice> gpus = new Nvidia().getGpus();
if (gpus != null) {
devices.addAll(gpus);
}
OS os = OS.getOS();
if (os instanceof Windows) { // opencl detection will crash on Mac (and sometimes on Linux)
gpus = new OpenCL().getGpus();
if (gpus != null) {
devices.addAll(gpus);
}
}
return true;
}
public static List<String> listModels() {
if (devices == null) {
generate();
}
List<String> devs = new LinkedList<String>();
for (GPUDevice dev : devices) {
devs.add(dev.getModel());
}
return devs;
}
public static List<GPUDevice> listDevices(Configuration config) {
if (devices == null) {
if (config.isDetectGPUs()) {
generate();
}
else {
devices = new LinkedList<GPUDevice>();
}
}
return devices;
}
public static GPUDevice getGPUDevice(String deviceId) {
if (deviceId == null) {
return null;
}
if (devices == null) {
generate();
}
if (devices == null) {
return null;
}
for (GPUDevice dev : devices) {
if (deviceId.equals(dev.getId()) || deviceId.equals(dev.getOldId())) {
return dev;
}
}
return null;
}
}

View File

@@ -0,0 +1,89 @@
/*
* 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;
public class GPUDevice {
private String type;
private String model;
private long memory; // in B
private String id;
private String oldId; // for backward compatibility
public GPUDevice(String type, String model, long ram, String id) {
this.type = type;
this.model = model;
this.memory = ram;
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public long getMemory() {
return memory;
}
public void setMemory(long memory) {
this.memory = memory;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getOldId() {
return oldId;
}
public void setOldId(String id) {
this.oldId = id;
}
@Override
public String toString() {
return "GPUDevice [type=" + type + ", model='" + model + "', memory=" + memory + ", id=" + id + "]";
}
public int getRecommandedTileSize() {
// GPU
// if the vram is lower than 1G reduce the size of tile to avoid black output
return (getMemory() > 1073741824L) ? 256 : 128;
}
}

View File

@@ -0,0 +1,7 @@
package com.sheepit.client.hardware.gpu;
import java.util.List;
public interface GPULister {
public abstract List<GPUDevice> getGpus();
}

View File

@@ -0,0 +1,43 @@
/*
* 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.nvidia;
import com.sun.jna.Library;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.LongByReference;
public interface CUDA extends Library {
public int cuInit(int flags);
/*
* @return: CUDA_SUCCESS, CUDA_ERROR_DEINITIALIZED, CUDA_ERROR_NOT_INITIALIZED, CUDA_ERROR_INVALID_CONTEXT, CUDA_ERROR_INVALID_VALUE
*/
public int cuDeviceGetCount(IntByReference count);
public int cuDeviceGetName(byte[] name, int len, int dev);
public int cuDeviceGet (IntByReference device, int ordinal);
public int cuDeviceGetAttribute (IntByReference pi, int attrib, int dev );
public int cuDeviceTotalMem_v2(LongByReference bytes, int dev);
public int cuDeviceTotalMem(LongByReference bytes, int dev);
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright (C) 2018 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.nvidia;
/**
* CUDA Device properties. Taken directly from the online manual:
* https://docs.nvidia.com/cuda/cuda-driver-api
*/
public class CUDeviceAttribute {
/**
* PCI bus ID of the device
*/
public static final int CU_DEVICE_ATTRIBUTE_PCI_BUS_ID = 33;
/**
* PCI device ID of the device
*/
public static final int CU_DEVICE_ATTRIBUTE_PCI_DEVICE_ID = 34;
/**
* PCI domain ID of the device
*/
public static final int CU_DEVICE_ATTRIBUTE_PCI_DOMAIN_ID = 50;
}

View File

@@ -0,0 +1,513 @@
/*
* JCuda - Java bindings for NVIDIA CUDA driver and runtime API
*
* Copyright (c) 2009-2012 Marco Hutter - http://www.jcuda.org
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
//package jcuda.driver;
package com.sheepit.client.hardware.gpu.nvidia;
/**
* Error codes.<br />
* <br />
* Most comments are taken from the CUDA reference manual.
*/
public class CUresult {
/**
* The API call returned with no errors. In the case of query calls, this
* can also mean that the operation being queried is complete (see
* ::cuEventQuery() and ::cuStreamQuery()).
*/
public static final int CUDA_SUCCESS = 0;
/**
* This indicates that one or more of the parameters passed to the API call
* is not within an acceptable range of values.
*/
public static final int CUDA_ERROR_INVALID_VALUE = 1;
/**
* The API call failed because it was unable to allocate enough memory to
* perform the requested operation.
*/
public static final int CUDA_ERROR_OUT_OF_MEMORY = 2;
/**
* This indicates that the CUDA driver has not been initialized with
* ::cuInit() or that initialization has failed.
*/
public static final int CUDA_ERROR_NOT_INITIALIZED = 3;
/**
* This indicates that the CUDA driver is in the process of shutting down.
*/
public static final int CUDA_ERROR_DEINITIALIZED = 4;
/**
* This indicates profiling APIs are called while application is running
* in visual profiler mode.
*/
public static final int CUDA_ERROR_PROFILER_DISABLED = 5;
/**
* This indicates profiling has not been initialized for this context.
* Call cuProfilerInitialize() to resolve this.
*
* @deprecated This error return is deprecated as of CUDA 5.0.
* It is no longer an error to attempt to enable/disable the
* profiling via ::cuProfilerStart or ::cuProfilerStop without
* initialization.
*/
public static final int CUDA_ERROR_PROFILER_NOT_INITIALIZED = 6;
/**
* This indicates profiler has already been started and probably
* cuProfilerStart() is incorrectly called.
*
* @deprecated This error return is deprecated as of CUDA 5.0.
* It is no longer an error to call cuProfilerStart() when
* profiling is already enabled.
*/
public static final int CUDA_ERROR_PROFILER_ALREADY_STARTED = 7;
/**
* This indicates profiler has already been stopped and probably
* cuProfilerStop() is incorrectly called.
*
* @deprecated This error return is deprecated as of CUDA 5.0.
* It is no longer an error to call cuProfilerStop() when
* profiling is already disabled.
*/
public static final int CUDA_ERROR_PROFILER_ALREADY_STOPPED = 8;
/**
* This indicates that no CUDA-capable devices were detected by the installed
* CUDA driver.
*/
public static final int CUDA_ERROR_NO_DEVICE = 100;
/**
* This indicates that the device ordinal supplied by the user does not
* correspond to a valid CUDA device.
*/
public static final int CUDA_ERROR_INVALID_DEVICE = 101;
/**
* This indicates that the device kernel image is invalid. This can also
* indicate an invalid CUDA module.
*/
public static final int CUDA_ERROR_INVALID_IMAGE = 200;
/**
* This most frequently indicates that there is no context bound to the
* current thread. This can also be returned if the context passed to an
* API call is not a valid handle (such as a context that has had
* ::cuCtxDestroy() invoked on it). This can also be returned if a user
* mixes different API versions (i.e. 3010 context with 3020 API calls).
* See ::cuCtxGetApiVersion() for more details.
*/
public static final int CUDA_ERROR_INVALID_CONTEXT = 201;
/**
* This indicated that the context being supplied as a parameter to the
* API call was already the active context.
* \deprecated
* This error return is deprecated as of CUDA 3.2. It is no longer an
* error to attempt to push the active context via ::cuCtxPushCurrent().
*/
public static final int CUDA_ERROR_CONTEXT_ALREADY_CURRENT = 202;
/**
* This indicates that a map or register operation has failed.
*/
public static final int CUDA_ERROR_MAP_FAILED = 205;
/**
* This indicates that an unmap or unregister operation has failed.
*/
public static final int CUDA_ERROR_UNMAP_FAILED = 206;
/**
* This indicates that the specified array is currently mapped and thus
* cannot be destroyed.
*/
public static final int CUDA_ERROR_ARRAY_IS_MAPPED = 207;
/**
* This indicates that the resource is already mapped.
*/
public static final int CUDA_ERROR_ALREADY_MAPPED = 208;
/**
* This indicates that there is no kernel image available that is suitable
* for the device. This can occur when a user specifies code generation
* options for a particular CUDA source file that do not include the
* corresponding device configuration.
*/
public static final int CUDA_ERROR_NO_BINARY_FOR_GPU = 209;
/**
* This indicates that a resource has already been acquired.
*/
public static final int CUDA_ERROR_ALREADY_ACQUIRED = 210;
/**
* This indicates that a resource is not mapped.
*/
public static final int CUDA_ERROR_NOT_MAPPED = 211;
/**
* This indicates that a mapped resource is not available for access as an
* array.
*/
public static final int CUDA_ERROR_NOT_MAPPED_AS_ARRAY = 212;
/**
* This indicates that a mapped resource is not available for access as a
* pointer.
*/
public static final int CUDA_ERROR_NOT_MAPPED_AS_POINTER = 213;
/**
* This indicates that an uncorrectable ECC error was detected during
* execution.
*/
public static final int CUDA_ERROR_ECC_UNCORRECTABLE = 214;
/**
* This indicates that the ::CUlimit passed to the API call is not
* supported by the active device.
*/
public static final int CUDA_ERROR_UNSUPPORTED_LIMIT = 215;
/**
* This indicates that the ::CUcontext passed to the API call can
* only be bound to a single CPU thread at a time but is already
* bound to a CPU thread.
*/
public static final int CUDA_ERROR_CONTEXT_ALREADY_IN_USE = 216;
/**
* This indicates that peer access is not supported across the given
* devices.
*/
public static final int CUDA_ERROR_PEER_ACCESS_UNSUPPORTED = 217;
/**
* This indicates that the device kernel source is invalid.
*/
public static final int CUDA_ERROR_INVALID_SOURCE = 300;
/**
* This indicates that the file specified was not found.
*/
public static final int CUDA_ERROR_FILE_NOT_FOUND = 301;
/**
* This indicates that a link to a shared object failed to resolve.
*/
public static final int CUDA_ERROR_SHARED_OBJECT_SYMBOL_NOT_FOUND = 302;
/**
* This indicates that initialization of a shared object failed.
*/
public static final int CUDA_ERROR_SHARED_OBJECT_INIT_FAILED = 303;
/**
* This indicates that an OS call failed.
*/
public static final int CUDA_ERROR_OPERATING_SYSTEM = 304;
/**
* This indicates that a resource handle passed to the API call was not
* valid. Resource handles are opaque types like ::CUstream and ::CUevent.
*/
public static final int CUDA_ERROR_INVALID_HANDLE = 400;
/**
* This indicates that a named symbol was not found. Examples of symbols
* are global/constant variable names, texture names, and surface names.
*/
public static final int CUDA_ERROR_NOT_FOUND = 500;
/**
* This indicates that asynchronous operations issued previously have not
* completed yet. This result is not actually an error, but must be indicated
* differently than ::CUDA_SUCCESS (which indicates completion). Calls that
* may return this value include ::cuEventQuery() and ::cuStreamQuery().
*/
public static final int CUDA_ERROR_NOT_READY = 600;
/**
* An exception occurred on the device while executing a kernel. Common
* causes include dereferencing an invalid device pointer and accessing
* out of bounds shared memory. The context cannot be used, so it must
* be destroyed (and a new one should be created). All existing device
* memory allocations from this context are invalid and must be
* reconstructed if the program is to continue using CUDA.
*/
public static final int CUDA_ERROR_LAUNCH_FAILED = 700;
/**
* This indicates that a launch did not occur because it did not have
* appropriate resources. This error usually indicates that the user has
* attempted to pass too many arguments to the device kernel, or the
* kernel launch specifies too many threads for the kernel's register
* count. Passing arguments of the wrong size (i.e. a 64-bit pointer
* when a 32-bit int is expected) is equivalent to passing too many
* arguments and can also result in this error.
*/
public static final int CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES = 701;
/**
* This indicates that the device kernel took too long to execute. This can
* only occur if timeouts are enabled - see the device attribute
* ::CU_DEVICE_ATTRIBUTE_KERNEL_EXEC_TIMEOUT for more information. The
* context cannot be used (and must be destroyed similar to
* ::CUDA_ERROR_LAUNCH_FAILED). All existing device memory allocations from
* this context are invalid and must be reconstructed if the program is to
* continue using CUDA.
*/
public static final int CUDA_ERROR_LAUNCH_TIMEOUT = 702;
/**
* This error indicates a kernel launch that uses an incompatible texturing
* mode.
*/
public static final int CUDA_ERROR_LAUNCH_INCOMPATIBLE_TEXTURING = 703;
/**
* This error indicates that a call to ::cuCtxEnablePeerAccess() is
* trying to re-enable peer access to a context which has already
* had peer access to it enabled.
*/
public static final int CUDA_ERROR_PEER_ACCESS_ALREADY_ENABLED = 704;
/**
* This error indicates that a call to ::cuMemPeerRegister is trying to
* register memory from a context which has not had peer access
* enabled yet via ::cuCtxEnablePeerAccess(), or that
* ::cuCtxDisablePeerAccess() is trying to disable peer access
* which has not been enabled yet.
*/
public static final int CUDA_ERROR_PEER_ACCESS_NOT_ENABLED = 705;
/**
* This error indicates that a call to ::cuMemPeerRegister is trying to
* register already-registered memory.
*
* @deprecated This value has been added in CUDA 4.0 RC,
* and removed in CUDA 4.0 RC2
*/
public static final int CUDA_ERROR_PEER_MEMORY_ALREADY_REGISTERED = 706;
/**
* This error indicates that a call to ::cuMemPeerUnregister is trying to
* unregister memory that has not been registered.
*
* @deprecated This value has been added in CUDA 4.0 RC,
* and removed in CUDA 4.0 RC2
*/
public static final int CUDA_ERROR_PEER_MEMORY_NOT_REGISTERED = 707;
/**
* This error indicates that ::cuCtxCreate was called with the flag
* ::CU_CTX_PRIMARY on a device which already has initialized its
* primary context.
*/
public static final int CUDA_ERROR_PRIMARY_CONTEXT_ACTIVE = 708;
/**
* This error indicates that the context current to the calling thread
* has been destroyed using ::cuCtxDestroy, or is a primary context which
* has not yet been initialized.
*/
public static final int CUDA_ERROR_CONTEXT_IS_DESTROYED = 709;
/**
* A device-side assert triggered during kernel execution. The context
* cannot be used anymore, and must be destroyed. All existing device
* memory allocations from this context are invalid and must be
* reconstructed if the program is to continue using CUDA.
*/
public static final int CUDA_ERROR_ASSERT = 710;
/**
* This error indicates that the hardware resources required to enable
* peer access have been exhausted for one or more of the devices
* passed to ::cuCtxEnablePeerAccess().
*/
public static final int CUDA_ERROR_TOO_MANY_PEERS = 711;
/**
* This error indicates that the memory range passed to ::cuMemHostRegister()
* has already been registered.
*/
public static final int CUDA_ERROR_HOST_MEMORY_ALREADY_REGISTERED = 712;
/**
* This error indicates that the pointer passed to ::cuMemHostUnregister()
* does not correspond to any currently registered memory region.
*/
public static final int CUDA_ERROR_HOST_MEMORY_NOT_REGISTERED = 713;
/**
* This error indicates that the attempted operation is not permitted.
*/
public static final int CUDA_ERROR_NOT_PERMITTED = 800;
/**
* This error indicates that the attempted operation is not supported
* on the current system or device.
*/
public static final int CUDA_ERROR_NOT_SUPPORTED = 801;
/**
* This indicates that an unknown internal error has occurred.
*/
public static final int CUDA_ERROR_UNKNOWN = 999;
/**
* Returns the String identifying the given CUresult
*
* @param result The CUresult value
* @return The String identifying the given CUresult
*/
public static String stringFor(int result) {
switch (result) {
case CUDA_SUCCESS:
return "CUDA_SUCCESS";
case CUDA_ERROR_INVALID_VALUE:
return "CUDA_ERROR_INVALID_VALUE";
case CUDA_ERROR_OUT_OF_MEMORY:
return "CUDA_ERROR_OUT_OF_MEMORY";
case CUDA_ERROR_NOT_INITIALIZED:
return "CUDA_ERROR_NOT_INITIALIZED";
case CUDA_ERROR_DEINITIALIZED:
return "CUDA_ERROR_DEINITIALIZED";
case CUDA_ERROR_PROFILER_DISABLED:
return "CUDA_ERROR_PROFILER_DISABLED";
case CUDA_ERROR_PROFILER_NOT_INITIALIZED:
return "CUDA_ERROR_PROFILER_NOT_INITIALIZED";
case CUDA_ERROR_PROFILER_ALREADY_STARTED:
return "CUDA_ERROR_PROFILER_ALREADY_STARTED";
case CUDA_ERROR_PROFILER_ALREADY_STOPPED:
return "CUDA_ERROR_PROFILER_ALREADY_STOPPED";
case CUDA_ERROR_NO_DEVICE:
return "CUDA_ERROR_NO_DEVICE";
case CUDA_ERROR_INVALID_DEVICE:
return "CUDA_ERROR_INVALID_DEVICE";
case CUDA_ERROR_INVALID_IMAGE:
return "CUDA_ERROR_INVALID_IMAGE";
case CUDA_ERROR_INVALID_CONTEXT:
return "CUDA_ERROR_INVALID_CONTEXT";
case CUDA_ERROR_CONTEXT_ALREADY_CURRENT:
return "CUDA_ERROR_CONTEXT_ALREADY_CURRENT";
case CUDA_ERROR_MAP_FAILED:
return "CUDA_ERROR_MAP_FAILED";
case CUDA_ERROR_UNMAP_FAILED:
return "CUDA_ERROR_UNMAP_FAILED";
case CUDA_ERROR_ARRAY_IS_MAPPED:
return "CUDA_ERROR_ARRAY_IS_MAPPED";
case CUDA_ERROR_ALREADY_MAPPED:
return "CUDA_ERROR_ALREADY_MAPPED";
case CUDA_ERROR_NO_BINARY_FOR_GPU:
return "CUDA_ERROR_NO_BINARY_FOR_GPU";
case CUDA_ERROR_ALREADY_ACQUIRED:
return "CUDA_ERROR_ALREADY_ACQUIRED";
case CUDA_ERROR_NOT_MAPPED:
return "CUDA_ERROR_NOT_MAPPED";
case CUDA_ERROR_NOT_MAPPED_AS_ARRAY:
return "CUDA_ERROR_NOT_MAPPED_AS_ARRAY";
case CUDA_ERROR_NOT_MAPPED_AS_POINTER:
return "CUDA_ERROR_NOT_MAPPED_AS_POINTER";
case CUDA_ERROR_ECC_UNCORRECTABLE:
return "CUDA_ERROR_ECC_UNCORRECTABLE";
case CUDA_ERROR_UNSUPPORTED_LIMIT:
return "CUDA_ERROR_UNSUPPORTED_LIMIT";
case CUDA_ERROR_CONTEXT_ALREADY_IN_USE:
return "CUDA_ERROR_CONTEXT_ALREADY_IN_USE";
case CUDA_ERROR_PEER_ACCESS_UNSUPPORTED:
return "CUDA_ERROR_PEER_ACCESS_UNSUPPORTED";
case CUDA_ERROR_INVALID_SOURCE:
return "CUDA_ERROR_INVALID_SOURCE";
case CUDA_ERROR_FILE_NOT_FOUND:
return "CUDA_ERROR_FILE_NOT_FOUND";
case CUDA_ERROR_SHARED_OBJECT_SYMBOL_NOT_FOUND:
return "CUDA_ERROR_SHARED_OBJECT_SYMBOL_NOT_FOUND";
case CUDA_ERROR_SHARED_OBJECT_INIT_FAILED:
return "CUDA_ERROR_SHARED_OBJECT_INIT_FAILED";
case CUDA_ERROR_OPERATING_SYSTEM:
return "CUDA_ERROR_OPERATING_SYSTEM";
case CUDA_ERROR_INVALID_HANDLE:
return "CUDA_ERROR_INVALID_HANDLE";
case CUDA_ERROR_NOT_FOUND:
return "CUDA_ERROR_NOT_FOUND";
case CUDA_ERROR_NOT_READY:
return "CUDA_ERROR_NOT_READY";
case CUDA_ERROR_LAUNCH_FAILED:
return "CUDA_ERROR_LAUNCH_FAILED";
case CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES:
return "CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES";
case CUDA_ERROR_LAUNCH_TIMEOUT:
return "CUDA_ERROR_LAUNCH_TIMEOUT";
case CUDA_ERROR_LAUNCH_INCOMPATIBLE_TEXTURING:
return "CUDA_ERROR_LAUNCH_INCOMPATIBLE_TEXTURING";
case CUDA_ERROR_PEER_ACCESS_ALREADY_ENABLED:
return "CUDA_ERROR_PEER_ACCESS_ALREADY_ENABLED";
case CUDA_ERROR_PEER_ACCESS_NOT_ENABLED:
return "CUDA_ERROR_PEER_ACCESS_NOT_ENABLED";
case CUDA_ERROR_PEER_MEMORY_ALREADY_REGISTERED:
return "CUDA_ERROR_PEER_MEMORY_ALREADY_REGISTERED";
case CUDA_ERROR_PEER_MEMORY_NOT_REGISTERED:
return "CUDA_ERROR_PEER_MEMORY_NOT_REGISTERED";
case CUDA_ERROR_PRIMARY_CONTEXT_ACTIVE:
return "CUDA_ERROR_PRIMARY_CONTEXT_ACTIVE";
case CUDA_ERROR_CONTEXT_IS_DESTROYED:
return "CUDA_ERROR_CONTEXT_IS_DESTROYED";
case CUDA_ERROR_ASSERT:
return "CUDA_ERROR_ASSERT";
case CUDA_ERROR_TOO_MANY_PEERS:
return "CUDA_ERROR_TOO_MANY_PEERS";
case CUDA_ERROR_HOST_MEMORY_ALREADY_REGISTERED:
return "CUDA_ERROR_HOST_MEMORY_ALREADY_REGISTERED";
case CUDA_ERROR_HOST_MEMORY_NOT_REGISTERED:
return "CUDA_ERROR_HOST_MEMORY_NOT_REGISTERED";
case CUDA_ERROR_NOT_PERMITTED:
return "CUDA_ERROR_NOT_PERMITTED";
case CUDA_ERROR_NOT_SUPPORTED:
return "CUDA_ERROR_NOT_SUPPORTED";
case CUDA_ERROR_UNKNOWN:
return "CUDA_ERROR_UNKNOWN";
}
return "INVALID CUresult: " + result;
}
/**
* Private constructor to prevent instantiation.
*/
private CUresult() {
}
}

View File

@@ -0,0 +1,141 @@
package com.sheepit.client.hardware.gpu.nvidia;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.sheepit.client.hardware.gpu.nvidia.CUDeviceAttribute;
import com.sheepit.client.hardware.gpu.GPUDevice;
import com.sheepit.client.hardware.gpu.GPULister;
import com.sheepit.client.os.OS;
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.LongByReference;
public class Nvidia implements GPULister {
public static String TYPE = "CUDA";
@Override
public List<GPUDevice> getGpus() {
OS os = OS.getOS();
String path = os.getCUDALib();
if (path == null) {
return null;
}
CUDA cudalib = null;
try {
cudalib = (CUDA) Native.loadLibrary(path, CUDA.class);
}
catch (java.lang.UnsatisfiedLinkError e) {
return null;
}
catch (java.lang.ExceptionInInitializerError e) {
System.out.println("Nvidia::getGpus ExceptionInInitializerError " + e);
return null;
}
catch (Exception e) {
System.out.println("Nvidia::getGpus generic exception " + e);
return null;
}
int result = CUresult.CUDA_ERROR_UNKNOWN;
result = cudalib.cuInit(0);
if (result != CUresult.CUDA_SUCCESS) {
System.out.println("Nvidia::getGpus cuInit failed (ret: " + result + ")");
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.");
}
return null;
}
if (result == CUresult.CUDA_ERROR_NO_DEVICE) {
return null;
}
IntByReference count = new IntByReference();
result = cudalib.cuDeviceGetCount(count);
if (result != CUresult.CUDA_SUCCESS) {
System.out.println("Nvidia::getGpus cuDeviceGetCount failed (ret: " + CUresult.stringFor(result) + ")");
return null;
}
List<GPUDevice> devices = new LinkedList<GPUDevice>();
HashMap<String, GPUDevice> devicesWithPciId = new HashMap<String, GPUDevice>(count.getValue());
for (int num = 0; num < count.getValue(); num++) {
IntByReference aDevice = new IntByReference();
result = cudalib.cuDeviceGet(aDevice, num);
if (result != CUresult.CUDA_SUCCESS) {
System.out.println("Nvidia::getGpus cuDeviceGet failed (ret: " + CUresult.stringFor(result) + ")");
continue;
}
IntByReference pciDomainId = new IntByReference();
IntByReference pciBusId = new IntByReference();
IntByReference pciDeviceId = new IntByReference();
result = cudalib.cuDeviceGetAttribute(pciDomainId, CUDeviceAttribute.CU_DEVICE_ATTRIBUTE_PCI_DOMAIN_ID, aDevice.getValue());
if (result != CUresult.CUDA_SUCCESS) {
System.out.println("Nvidia::getGpus cuDeviceGetAttribute for CU_DEVICE_ATTRIBUTE_PCI_DOMAIN_ID failed (ret: " + CUresult.stringFor(result) + ")");
continue;
}
result = cudalib.cuDeviceGetAttribute(pciBusId, CUDeviceAttribute.CU_DEVICE_ATTRIBUTE_PCI_BUS_ID, aDevice.getValue());
if (result != CUresult.CUDA_SUCCESS) {
System.out.println("Nvidia::getGpus cuDeviceGetAttribute for CU_DEVICE_ATTRIBUTE_PCI_BUS_ID failed (ret: " + CUresult.stringFor(result) + ")");
continue;
}
result = cudalib.cuDeviceGetAttribute(pciDeviceId, CUDeviceAttribute.CU_DEVICE_ATTRIBUTE_PCI_DEVICE_ID, aDevice.getValue());
if (result != CUresult.CUDA_SUCCESS) {
System.out.println("Nvidia::getGpus cuDeviceGetAttribute for CU_DEVICE_ATTRIBUTE_PCI_DEVICE_ID failed (ret: " + CUresult.stringFor(result) + ")");
continue;
}
byte name[] = new byte[256];
result = cudalib.cuDeviceGetName(name, 256, num);
if (result != CUresult.CUDA_SUCCESS) {
System.out.println("Nvidia::getGpus cuDeviceGetName failed (ret: " + CUresult.stringFor(result) + ")");
continue;
}
LongByReference ram = new LongByReference();
try {
result = cudalib.cuDeviceTotalMem_v2(ram, num);
}
catch (UnsatisfiedLinkError e) {
// fall back to old function
result = cudalib.cuDeviceTotalMem(ram, num);
}
if (result != CUresult.CUDA_SUCCESS) {
System.out.println("Nvidia::getGpus cuDeviceTotalMem failed (ret: " + CUresult.stringFor(result) + ")");
return null;
}
String blenderId = String.format("CUDA_%s_%04x:%02x:%02x",
new String(name).trim(),
pciDomainId.getValue(),
pciBusId.getValue(),
pciDeviceId.getValue());
devicesWithPciId.put(Integer.toString(pciBusId.getValue()), new GPUDevice(TYPE, new String(name).trim(), ram.getValue(), blenderId));
}
// for backward compatibility generate a CUDA_N 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 (Map.Entry<String, GPUDevice> entry : devicesWithPciId.entrySet()){
GPUDevice aDevice = entry.getValue();
aDevice.setOldId(TYPE + "_" + Integer.toString(i));
devices.add(aDevice);
i++;
}
return devices;
}
}

View File

@@ -0,0 +1,172 @@
/*
* 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.opencl;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
import com.sheepit.client.hardware.gpu.GPUDevice;
import com.sheepit.client.hardware.gpu.GPULister;
import com.sheepit.client.hardware.gpu.opencl.OpenCLLib.CLDeviceId;
import com.sheepit.client.hardware.gpu.opencl.OpenCLLib.CLPlatformId;
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;
public class OpenCL implements GPULister {
public static String TYPE = "OPENCL";
@Override
public List<GPUDevice> getGpus() {
OpenCLLib lib = null;
String path = "OpenCL";
try {
lib = (OpenCLLib) Native.loadLibrary(path, OpenCLLib.class);
}
catch (java.lang.UnsatisfiedLinkError e) {
System.out.println("OpenCL::getGpus failed(A) to load OpenCL lib (path: " + path + ")");
return null;
}
catch (java.lang.ExceptionInInitializerError e) {
System.out.println("OpenCL::getGpus failed(B) ExceptionInInitializerError " + e);
return null;
}
catch (Exception e) {
System.out.println("OpenCL::getGpus failed(C) generic exception " + e);
return null;
}
int status = -1;
// get the number of platform
IntByReference number_platforms = new IntByReference();
status = lib.clGetPlatformIDs(0, null, number_platforms);
if (status != OpenCLLib.CL_SUCCESS) {
System.out.println("OpenCL::getGpus failed(D) status: " + status);
return null;
}
// now we can create the platforms
final OpenCLLib.CLPlatformId.ByReference e6ref = new OpenCLLib.CLPlatformId.ByReference();
OpenCLLib.CLPlatformId.ByReference[] plateforms = (OpenCLLib.CLPlatformId.ByReference[]) e6ref.toArray(number_platforms.getValue());
status = lib.clGetPlatformIDs(number_platforms.getValue(), plateforms, null);
if (status != OpenCLLib.CL_SUCCESS) {
System.out.println("OpenCL::getGpus failed(E) status: " + status);
return null;
}
List<GPUDevice> available_devices = new ArrayList<GPUDevice>(1);
// Devices are numbered consecutively across platforms.
int id = 0;
for (int i = 0; i < number_platforms.getValue(); i++) {
// get number of devices in platform
IntByReference device_count = new IntByReference();
status = lib.clGetDeviceIDs(plateforms[i], OpenCLLib.CL_DEVICE_TYPE_GPU, 0, null, device_count);
if (status == OpenCLLib.CL_DEVICE_NOT_FOUND) {
System.out.println("OpenCL::getGpus no device found on plateforms[" + i + "]");
continue;
}
if (status != OpenCLLib.CL_SUCCESS) {
System.out.println("OpenCL::getGpus failed(F) status: " + status);
return null;
}
final OpenCLLib.CLDeviceId.ByReference e6ref4 = new OpenCLLib.CLDeviceId.ByReference();
OpenCLLib.CLDeviceId.ByReference[] devices = (OpenCLLib.CLDeviceId.ByReference[]) e6ref4.toArray(device_count.getValue());
status = lib.clGetDeviceIDs(plateforms[i], OpenCLLib.CL_DEVICE_TYPE_GPU, device_count.getValue(), devices, null);
if (status != OpenCLLib.CL_SUCCESS) {
System.out.println("OpenCL::getGpus failed(G) status: " + status);
return null;
}
for (int j = 0; j < device_count.getValue(); j++) {
String name = getInfodeviceString(lib, devices[j], OpenCLLib.CL_DEVICE_BOARD_NAME_AMD);
long vram = getInfodeviceLong(lib, devices[j], OpenCLLib.CL_DEVICE_GLOBAL_MEM_SIZE);
if (name != null && vram > 0) {
GPUDevice gpu = new GPUDevice(TYPE, new String(name).trim(), vram, getBlenderId(lib, devices[j]));
gpu.setOldId(TYPE + "_" + id);
available_devices.add(gpu);
}
id++;
}
}
return available_devices;
}
private static String getInfodeviceString(OpenCLLib lib, CLDeviceId.ByReference device, int type) {
byte name[] = new byte[256];
int status = lib.clGetDeviceInfo(device, type, 256, name, null);
if (status != OpenCLLib.CL_SUCCESS) {
System.out.println("OpenCL::getInfodeviceString failed(H) status: " + status + " type: " + type);
return null;
}
return new String(name).trim();
}
private static long getInfodeviceLong(OpenCLLib lib, CLDeviceId.ByReference device, int type) {
byte name[] = new byte[256];
int status = lib.clGetDeviceInfo(device, type, 256, name, null);
if (status != OpenCLLib.CL_SUCCESS) {
System.out.println("OpenCL::getInfodeviceLong failed(I) status: " + status + " type: " + type);
return -1;
}
ByteBuffer wrapped = ByteBuffer.wrap(name);
wrapped.order(ByteOrder.LITTLE_ENDIAN);
return wrapped.getLong();
}
private static String getInfoPlatform(OpenCLLib lib, CLPlatformId.ByReference platform, int type) {
byte name[] = new byte[256];
int status = lib.clGetPlatformInfo(platform, type, 256, name, null);
if (status != OpenCLLib.CL_SUCCESS) {
System.out.println("GPU::getInfoPlatform failed(J) status: " + status + " type: " + type);
return null;
}
return new String(name).trim();
}
private static String getBlenderId(OpenCLLib lib, CLDeviceId.ByReference device) {
byte topology[] = new byte[24];
int status = lib.clGetDeviceInfo(device, 0x4037, 24, topology, null);
if (status != OpenCLLib.CL_SUCCESS) {
System.out.println("OpenCL::getBlenderId failed(I) status: " + status);
return "";
}
return String.format("%02x:%02x.%01x", topology[21], topology[22], topology[23]);
}
}

View File

@@ -0,0 +1,80 @@
/*
* 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.opencl;
import java.util.Arrays;
import java.util.List;
import com.sun.jna.Library;
import com.sun.jna.Structure;
import com.sun.jna.ptr.IntByReference;
public interface OpenCLLib extends Library {
// status
public static final int CL_SUCCESS = 0;
public static final int CL_DEVICE_NOT_FOUND = -1;
public static final int CL_PLATFORM_VENDOR = 0x0903;
// cl_device_type
public static final int CL_DEVICE_TYPE_DEFAULT = (1 << 0);
public static final int CL_DEVICE_TYPE_CPU = (1 << 1);
public static final int CL_DEVICE_TYPE_GPU = (1 << 2);
public static final int CL_DEVICE_TYPE_ACCELERATOR = (1 << 3);
public static final int CL_DEVICE_TYPE_CUSTOM = (1 << 4);
public static final int CL_DEVICE_TYPE_ALL = 0xFFFFFFFF;
// cl_device_info
public static final int CL_DEVICE_NAME = 0x102B;
public static final int CL_DEVICE_GLOBAL_MEM_SIZE = 0x101F;
public static final int CL_DEVICE_BOARD_NAME_AMD = 0x4038;
public int clGetPlatformIDs(int num_entries, CLPlatformId.ByReference[] platforms, IntByReference num_platforms);
public int clGetPlatformInfo(CLPlatformId.ByReference platform, int param_name, long param_value_size, byte[] destination, long size_ret[]);
public int clGetDeviceIDs(CLPlatformId.ByReference platform, int param_name, int num_entries, CLDeviceId.ByReference[] devices, IntByReference device_count);
public int clGetDeviceInfo(CLDeviceId.ByReference device, int param_name, long param_value_size, byte[] destination, long size_ret[]);
public static class CLPlatformId extends Structure {
public static class ByReference extends CLPlatformId implements Structure.ByReference {
}
public int id;
@Override
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "id" });
}
}
public static class CLDeviceId extends Structure {
public static class ByReference extends CLDeviceId implements Structure.ByReference {
}
public int id;
@Override
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "id" });
}
}
}