Guest User

Untitled

a guest
Apr 23rd, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.40 KB | None | 0 0
  1. package com.cuda;
  2.  
  3. import com.googlecode.javacpp.FunctionPointer;
  4. import com.googlecode.javacpp.IntPointer;
  5. import com.googlecode.javacpp.Loader;
  6. import com.googlecode.javacpp.annotation.*;
  7. import com.skenzo.RequestWrapper;
  8. import com.skenzo.servlet.ServletTestEndPoint;
  9. import jcuda.Pointer;
  10. import jcuda.Sizeof;
  11. import jcuda.jcusparse.JCusparse;
  12. import jcuda.jcusparse.cusparseHandle;
  13. import jcuda.jcusparse.cusparseMatDescr;
  14. import jcuda.runtime.JCuda;
  15.  
  16. import java.util.*;
  17.  
  18. import static jcuda.jcusparse.JCusparse2.*;
  19. import static jcuda.jcusparse.cusparseIndexBase.CUSPARSE_INDEX_BASE_ZERO;
  20. import static jcuda.jcusparse.cusparseMatrixType.CUSPARSE_MATRIX_TYPE_GENERAL;
  21. import static jcuda.jcusparse.cusparseOperation.CUSPARSE_OPERATION_NON_TRANSPOSE;
  22. import static jcuda.runtime.JCuda.*;
  23. import static jcuda.runtime.cudaMemcpyKind.cudaMemcpyDeviceToHost;
  24. import static jcuda.runtime.cudaMemcpyKind.cudaMemcpyHostToDevice;
  25.  
  26. @Platform(include={"<thrust/host_vector.h>", "<thrust/device_vector.h>", "<thrust/generate.h>", "<thrust/sort.h>",
  27.         "<thrust/copy.h>", "<thrust/reduce.h>", "<thrust/functional.h>", "<algorithm>", "<cstdlib>"})
  28. @Namespace("thrust")
  29. public class CudaOps extends CudaCategory implements Runnable {
  30.     static {
  31.         Loader.load();
  32.     }
  33.     public static class IntGenerator extends FunctionPointer {
  34.         static { Loader.load(); }
  35.         protected IntGenerator() { allocate(); }
  36.         private native void allocate();
  37.         public native int call();
  38.     }
  39.     @Name("plus<int>")
  40.     public static class IntPlus extends Pointer {
  41.         static { Loader.load(); }
  42.         public IntPlus() { allocate(); }
  43.         private native void allocate();
  44.         public native @Name("operator()") int call(int x, int y);
  45.     }
  46.     @Name("host_vector<int>")
  47.     public static class IntHostVector extends Pointer {
  48.         static { Loader.load(); }
  49.         public IntHostVector() { allocate(0); }
  50.         public IntHostVector(long n) { allocate(n); }
  51.         public IntHostVector(IntDeviceVector v) { allocate(v); }
  52.         private native void allocate(long n);
  53.         private native void allocate(@ByRef IntDeviceVector v);
  54.  
  55.         public IntPointer begin() { return data(); }
  56.         public IntPointer end() { return data().position((int)size()); }
  57.  
  58.         public native IntPointer data();
  59.         public native long size();
  60.         public native void resize(long n);
  61.     }
  62.     @Name("device_ptr<int>")
  63.     public static class IntDevicePointer extends Pointer {
  64.         static { Loader.load(); }
  65.         public IntDevicePointer() { allocate(null); }
  66.         public IntDevicePointer(IntPointer ptr) { allocate(ptr); }
  67.         private native void allocate(IntPointer ptr);
  68.  
  69.         public native IntPointer get();
  70.     }
  71.     @Name("device_vector<int>")
  72.     public static class IntDeviceVector extends Pointer {
  73.         static { Loader.load(); }
  74.         public IntDeviceVector() { allocate(0); }
  75.         public IntDeviceVector(long n) { allocate(n); }
  76.         public IntDeviceVector(IntHostVector v) { allocate(v); }
  77.         private native void allocate(long n);
  78.         private native void allocate(@ByRef IntHostVector v);
  79.  
  80.         public IntDevicePointer begin() { return data(); }
  81.         public IntDevicePointer end() { return new IntDevicePointer(data().get().position((int)size())); }
  82.  
  83.         public native @ByVal IntDevicePointer data();
  84.         public native long size();
  85.         public native void resize(long n);
  86.     }
  87.     public static native @MemberGetter
  88.     @Namespace IntGenerator rand();
  89.     public static native void copy(@ByVal IntDevicePointer first, @ByVal IntDevicePointer last, IntPointer result);
  90.     public static native void generate(IntPointer first, IntPointer last, IntGenerator gen);
  91.     public static native void sort(@ByVal IntDevicePointer first, @ByVal IntDevicePointer last);
  92.     public static native int reduce(@ByVal IntDevicePointer first, @ByVal IntDevicePointer last, int init, @ByVal IntPlus binary_op);
  93.  
  94.  
  95.     private Map<UUID, OutputList> globalOutputMap = null;
  96.     private Pointer csrValAD = new Pointer();
  97.     private Pointer csrRowPtrAD = new Pointer();
  98.     private Pointer csrColIndAD = new Pointer();
  99.     private int currentDevice;
  100.     private float csrValBH[];
  101.     private int csrRowPtrBH[];
  102.     private int csrColIndBH[];
  103.     private Pointer csrValBD = new Pointer();
  104.     private Pointer csrRowPtrBD = new Pointer();
  105.     private Pointer csrColIndBD = new Pointer();
  106.     private float csrValCH[];
  107.     private int csrRowPtrCH[];
  108.     private int csrColIndCH[];
  109.     private Pointer csrValCD = new Pointer();
  110.     private Pointer csrRowPtrCD = new Pointer();
  111.     private Pointer csrColIndCD = new Pointer();
  112.     public static final int INPUT_BATCH = 99;
  113.  
  114.  
  115.     public CudaOps(int currentDevice, Map<UUID, OutputList> output) {
  116.         IntHostVector h_vec = new IntHostVector(32 << 20);
  117.         generate(h_vec.begin(), h_vec.end(), rand());
  118.  
  119.         // transfer data to the device
  120.         IntDeviceVector d_vec = new IntDeviceVector(h_vec);
  121.  
  122.         // sort data on the device (846M keys per second on GeForce GTX 480)
  123.         sort(d_vec.begin(), d_vec.end());
  124.  
  125.         // transfer data back to host
  126.         copy(d_vec.begin(), d_vec.end(), h_vec.begin());
  127.  
  128.         // compute sum on device
  129.         int x = reduce(d_vec.begin(), d_vec.end(), 0, new IntPlus());
  130.         System.out.println("sorting done");
  131.        
  132.     }
  133.  
  134.    
  135. }
Advertisement
Add Comment
Please, Sign In to add comment