Skip to content

Instantly share code, notes, and snippets.

@papousek
Created April 3, 2011 10:46
Show Gist options
  • Save papousek/900355 to your computer and use it in GitHub Desktop.
Save papousek/900355 to your computer and use it in GitHub Desktop.
import jcuda.utils.KernelLauncher;
import jcuda.driver.CUdeviceptr;
import jcuda.runtime.JCuda;
public class Main {
public static void main(String[] args) {
float[] a = new float[] {1, 2, 3, 4]
float[] b = new float[] {1, 2, 3, 4]
CUdeviceptr devA = new CUdeviceptr();
CUdeviceptr devB = new CUdeviceptr();
CUdeviceptr devR = new CUdeviceptr();
JCuda.cudaMalloc(devA, a.length * Sizeof.FLOAT);
JCuda.cudaMalloc(devB, b.length * Sizeof.FLOAT);
JCuda.cudaMalloc(devR, b.length * Sizeof.FLOAT);
JCuda.cudaMemcpy(devA, Pointer.to(a), a.length * Sizeof.FLOAT, daMemcpyKind.cudaMemcpyHostToDevice);
JCuda.cudaMemcpy(devB, Pointer.to(b), b.length * Sizeof.FLOAT, daMemcpyKind.cudaMemcpyHostToDevice);
KernelLauncher launcher = KernelLauncher.create(add_vec_kernel.cu, "add_vec_kernel");
launcher.setBlockSize(4, 1, 1);
launcher.setBlockSize(1, 1, 1);
launcher.call(devA, devB, 4, devR);
float[] result = new float[4];
JCuda.cudaMemcpy(Pointer.to(result), devR, 4 * Sizeof.FLOAT, cudaMemcpyKind.cudaMemcpyDeviceToHost);
for (int i=0; i < result.length; i++) {
System.out.println(result[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment