如果我理解正确的话,问题的重点是是否有这样的神奇功能
long address = pointer.someGetUnderlyingPointerValueFunction();
返回本机指针的地址。
简短的回答:不,没有这样的功能。
(旁注:很久以前已经请求过类似的功能,但我还没有添加它。主要是因为这样的功能对于指向 Java 数组或(非直接)字节缓冲区的指针没有意义。此外,手动在 32 位和 64 位机器上处理带有填充和对齐的结构,以及不同大小的指针,以及大端或小端的缓冲区是无穷无尽的头痛来源。但我明白了这一点,以及可能的应用案例,等等我很可能会添加类似getAddress()
函数的东西。也许只添加到CUdeviceptr
类中,这绝对是有意义的 - 至少比Pointer
类中更多。人们会使用这种方法来做奇怪的事情,他们会做一些会导致虚拟机严重崩溃的事情,但是 JCuda 本身是一个非常薄的抽象层,无论如何在这方面没有安全网......)
也就是说,您可以使用以下方法解决当前限制:
private static long getPointerAddress(CUdeviceptr p)
{
// WORKAROUND until a method like CUdeviceptr#getAddress exists
class PointerWithAddress extends Pointer
{
PointerWithAddress(Pointer other)
{
super(other);
}
long getAddress()
{
return getNativePointer() + getByteOffset();
}
}
return new PointerWithAddress(p).getAddress();
}
当然,这很丑陋,显然与制作getNativePointer()
和getByteOffset()
方法的意图相矛盾protected
。但它最终可能会被一些“官方”方法取代:
private static long getPointerAddress(CUdeviceptr p)
{
return p.getAddress();
}
直到现在,这可能是最接近您在 C 端可以做的解决方案。
这是我为测试而编写的一个示例。内核只是一个虚拟内核,它用“可识别”值填充结构(以查看它们是否最终出现在正确的位置),并且应该仅使用 1 个线程启动:
typedef struct __declspec(align(16)) {
float* pointer1;
float* pointer2;
float* pointer3;
} MyStruct;
extern "C"
__global__ void kernel(MyStruct *structs)
{
structs[0].pointer1[0] = 1.0f;
structs[0].pointer1[1] = 1.1f;
structs[0].pointer1[2] = 1.2f;
structs[0].pointer2[0] = 2.0f;
structs[0].pointer2[1] = 2.1f;
structs[0].pointer2[2] = 2.2f;
structs[0].pointer3[0] = 3.0f;
structs[0].pointer3[1] = 3.1f;
structs[0].pointer3[2] = 3.2f;
structs[1].pointer1[0] = 11.0f;
structs[1].pointer1[1] = 11.1f;
structs[1].pointer1[2] = 11.2f;
structs[1].pointer2[0] = 12.0f;
structs[1].pointer2[1] = 12.1f;
structs[1].pointer2[2] = 12.2f;
structs[1].pointer3[0] = 13.0f;
structs[1].pointer3[1] = 13.1f;
structs[1].pointer3[2] = 13.2f;
}
该内核在以下程序中启动(注意: PTX 文件的编译在此处即时完成,设置可能与您的应用程序不匹配。如有疑问,您可以手动编译 PTX 文件)。
每个结构的pointer1
,pointer2
和pointer3
指针都被初始化,以便它们指向设备缓冲区的连续元素A
,B
和C
,每个元素都有一个偏移量,允许识别内核写入的值。(请注意,我尝试处理在 32 位或 64 位机器上运行它的两种可能情况,这意味着不同的指针大小 - 尽管目前我只能测试 32 位版本)
import static jcuda.driver.JCudaDriver.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.util.Arrays;
import jcuda.Pointer;
import jcuda.Sizeof;
import jcuda.driver.CUcontext;
import jcuda.driver.CUdevice;
import jcuda.driver.CUdeviceptr;
import jcuda.driver.CUfunction;
import jcuda.driver.CUmodule;
import jcuda.driver.JCudaDriver;
public class JCudaPointersInStruct
{
public static void main(String args[]) throws IOException
{
JCudaDriver.setExceptionsEnabled(true);
String ptxFileName = preparePtxFile("JCudaPointersInStructKernel.cu");
cuInit(0);
CUdevice device = new CUdevice();
cuDeviceGet(device, 0);
CUcontext context = new CUcontext();
cuCtxCreate(context, 0, device);
CUmodule module = new CUmodule();
cuModuleLoad(module, ptxFileName);
CUfunction function = new CUfunction();
cuModuleGetFunction(function, module, "kernel");
int numElements = 9;
CUdeviceptr A = new CUdeviceptr();
cuMemAlloc(A, numElements * Sizeof.FLOAT);
cuMemsetD32(A, 0, numElements);
CUdeviceptr B = new CUdeviceptr();
cuMemAlloc(B, numElements * Sizeof.FLOAT);
cuMemsetD32(B, 0, numElements);
CUdeviceptr C = new CUdeviceptr();
cuMemAlloc(C, numElements * Sizeof.FLOAT);
cuMemsetD32(C, 0, numElements);
int numSteps = 2;
int sizeOfStruct = Sizeof.POINTER * 4;
ByteBuffer hostStructsBuffer =
ByteBuffer.allocate(numSteps * sizeOfStruct);
if (Sizeof.POINTER == 4)
{
IntBuffer b = hostStructsBuffer.order(
ByteOrder.nativeOrder()).asIntBuffer();
for(int x = 0; x<numSteps; x++)
{
CUdeviceptr pointer1 = A.withByteOffset(getStepOffsetA(x));
CUdeviceptr pointer2 = B.withByteOffset(getStepOffsetB(x));
CUdeviceptr pointer3 = C.withByteOffset(getStepOffsetC(x));
//System.out.println("Step "+x+" pointer1 is "+pointer1);
//System.out.println("Step "+x+" pointer2 is "+pointer2);
//System.out.println("Step "+x+" pointer3 is "+pointer3);
b.put((int)getPointerAddress(pointer1));
b.put((int)getPointerAddress(pointer2));
b.put((int)getPointerAddress(pointer3));
b.put(0);
}
}
else
{
LongBuffer b = hostStructsBuffer.order(
ByteOrder.nativeOrder()).asLongBuffer();
for(int x = 0; x<numSteps; x++)
{
CUdeviceptr pointer1 = A.withByteOffset(getStepOffsetA(x));
CUdeviceptr pointer2 = B.withByteOffset(getStepOffsetB(x));
CUdeviceptr pointer3 = C.withByteOffset(getStepOffsetC(x));
//System.out.println("Step "+x+" pointer1 is "+pointer1);
//System.out.println("Step "+x+" pointer2 is "+pointer2);
//System.out.println("Step "+x+" pointer3 is "+pointer3);
b.put(getPointerAddress(pointer1));
b.put(getPointerAddress(pointer2));
b.put(getPointerAddress(pointer3));
b.put(0);
}
}
CUdeviceptr structs = new CUdeviceptr();
cuMemAlloc(structs, numSteps * sizeOfStruct);
cuMemcpyHtoD(structs, Pointer.to(hostStructsBuffer),
numSteps * sizeOfStruct);
Pointer kernelParameters = Pointer.to(
Pointer.to(structs)
);
cuLaunchKernel(function,
1, 1, 1,
1, 1, 1,
0, null, kernelParameters, null);
cuCtxSynchronize();
float hostA[] = new float[numElements];
cuMemcpyDtoH(Pointer.to(hostA), A, numElements * Sizeof.FLOAT);
float hostB[] = new float[numElements];
cuMemcpyDtoH(Pointer.to(hostB), B, numElements * Sizeof.FLOAT);
float hostC[] = new float[numElements];
cuMemcpyDtoH(Pointer.to(hostC), C, numElements * Sizeof.FLOAT);
System.out.println("A "+Arrays.toString(hostA));
System.out.println("B "+Arrays.toString(hostB));
System.out.println("C "+Arrays.toString(hostC));
}
private static long getStepOffsetA(int x)
{
return x * Sizeof.FLOAT * 4 + 0 * Sizeof.FLOAT;
}
private static long getStepOffsetB(int x)
{
return x * Sizeof.FLOAT * 4 + 1 * Sizeof.FLOAT;
}
private static long getStepOffsetC(int x)
{
return x * Sizeof.FLOAT * 4 + 2 * Sizeof.FLOAT;
}
private static long getPointerAddress(CUdeviceptr p)
{
// WORKAROUND until a method like CUdeviceptr#getAddress exists
class PointerWithAddress extends Pointer
{
PointerWithAddress(Pointer other)
{
super(other);
}
long getAddress()
{
return getNativePointer() + getByteOffset();
}
}
return new PointerWithAddress(p).getAddress();
}
//-------------------------------------------------------------------------
// Ignore this - in practice, you'll compile the PTX manually
private static String preparePtxFile(String cuFileName) throws IOException
{
int endIndex = cuFileName.lastIndexOf('.');
if (endIndex == -1)
{
endIndex = cuFileName.length()-1;
}
String ptxFileName = cuFileName.substring(0, endIndex+1)+"ptx";
File cuFile = new File(cuFileName);
if (!cuFile.exists())
{
throw new IOException("Input file not found: "+cuFileName);
}
String modelString = "-m"+System.getProperty("sun.arch.data.model");
String command =
"nvcc " + modelString + " -ptx -arch sm_11 -lineinfo "+
cuFile.getPath()+" -o "+ptxFileName;
System.out.println("Executing\n"+command);
Process process = Runtime.getRuntime().exec(command);
String errorMessage =
new String(toByteArray(process.getErrorStream()));
String outputMessage =
new String(toByteArray(process.getInputStream()));
int exitValue = 0;
try
{
exitValue = process.waitFor();
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
throw new IOException(
"Interrupted while waiting for nvcc output", e);
}
if (exitValue != 0)
{
System.out.println("nvcc process exitValue "+exitValue);
System.out.println("errorMessage:\n"+errorMessage);
System.out.println("outputMessage:\n"+outputMessage);
throw new IOException(
"Could not create .ptx file: "+errorMessage);
}
System.out.println("Finished creating PTX file");
return ptxFileName;
}
private static byte[] toByteArray(InputStream inputStream)
throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte buffer[] = new byte[8192];
while (true)
{
int read = inputStream.read(buffer);
if (read == -1)
{
break;
}
baos.write(buffer, 0, read);
}
return baos.toByteArray();
}
}
结果是,正如预期/期望的那样:
A [1.0, 1.1, 1.2, 0.0, 11.0, 11.1, 11.2, 0.0, 0.0]
B [0.0, 2.0, 2.1, 2.2, 0.0, 12.0, 12.1, 12.2, 0.0]
C [0.0, 0.0, 3.0, 3.1, 3.2, 0.0, 13.0, 13.1, 13.2]