我正在用 Java 编写的程序中实现 GPU 计算。为此,我正在使用jcuda绑定。我需要一个快速的主机到设备的内存传输,有时是相对较大的数组。如果我想使用流,我必须使用固定内存。问题是如果我想分配大于 cca 600 Mbs RAM 的主机固定内存,我会得到“CUDA_ERROR_OUT_OF_MEMORY”异常。这是我用来测试可用固定内存大小的代码:
public static void main(String[] args) {
//Init GPU
JCudaDriver.setExceptionsEnabled(true);
// Initialize the device and create device context
cuInit(0);
CUdevice device = new CUdevice();
cuDeviceGet(device, 0);
CUcontext context = new CUcontext();
cuCtxCreate(context, 0, device);
Pointer p = new Pointer();
int Kb = 1024;
int Mb = 1024 * Kb;
int Gb = 1024 * Mb;
int sequenceSize = 172*Mb; // times 4 for float
float[] expecteds = new float[sequenceSize];
float[] actuals = new float[sequenceSize];
Arrays.fill(expecteds, 3.33f);
int i = 0;
try {
JCudaDriver.cuMemAllocHost(p, sequenceSize* Sizeof.FLOAT);
FloatBuffer fb = p.getByteBuffer(0, sequenceSize* Sizeof.FLOAT).
order(ByteOrder.nativeOrder()).
asFloatBuffer();
fb.position(0);
fb.put(expecteds);
fb.position(0);
fb.get(actuals);
JCudaDriver.cuMemFreeHost(p);
} catch (Exception e) {
e.printStackTrace();
JCudaDriver.cuMemFreeHost(p);
}
}
现在,我知道操作系统可以阻止我使用过多的固定内存,因为它是不可分页的。问题是我有 48Gb(45Gb 空闲)的物理内存,我需要一种强制操作系统给我更多的方法。有没有办法做到这一点(如果可能的话,优雅地)?
编辑:操作系统是 64 位 Windows 7 Professional SP1