jcuda.driver.CUstream Java Examples

The following examples show how to use jcuda.driver.CUstream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GpuStream.java    From OSPREY3 with GNU General Public License v2.0 6 votes vote down vote up
public GpuStream(Context context) {
	this.context = context;
	
	stream = new CUstream();
	JCudaDriver.cuStreamCreate(stream, 0);
	
	byteBuffers = new BufferPool<ByteBuffer>(
		(Integer size) -> makeByteBuffer(size),
		(CUBuffer<ByteBuffer> buf, int size) -> makeOrExpandByteBuffer(buf, size)
	);
	intBuffers = new BufferPool<IntBuffer>(
		(Integer size) -> makeIntBuffer(size),
		(CUBuffer<IntBuffer> buf, int size) -> makeOrExpandIntBuffer(buf, size)
	);
	longBuffers = new BufferPool<LongBuffer>(
		(Integer size) -> makeLongBuffer(size),
		(CUBuffer<LongBuffer> buf, int size) -> makeOrExpandLongBuffer(buf, size)
	);
	doubleBuffers = new BufferPool<DoubleBuffer>(
		(Integer size) -> makeDoubleBuffer(size),
		(CUBuffer<DoubleBuffer> buf, int size) -> makeOrExpandDoubleBuffer(buf, size)
	);
}
 
Example #2
Source File: JCudaDriverHostFunction.java    From jcuda-samples with MIT License 5 votes vote down vote up
/**
 * Entry point
 * 
 * @param args Not used
 */
public static void main(String[] args)
{
    // Default initialization
    JCudaDriver.setExceptionsEnabled(true);
    cuInit(0);
    CUcontext context = new CUcontext();
    CUdevice device = new CUdevice();
    cuDeviceGet(device, 0);
    cuCtxCreate(context, 0, device);

    // Create a stream
    CUstream stream = new CUstream();
    cuStreamCreate(stream, 0);
    
    // Define a host function and launch it
    CUhostFn fn = new CUhostFn()
    {
        @Override
        public void call(Object userData)
        {
            System.out.println("Called with " + userData);
        }
    };
    cuLaunchHostFunc(stream, fn, "Example user object");
    
    // Wait for the stream to finish
    cuStreamSynchronize(stream);

    // Clean up
    cuCtxDestroy(context);
    
    System.out.println("Done");
}
 
Example #3
Source File: JCudaDriverStreamCallbacks.java    From jcuda-samples with MIT License 4 votes vote down vote up
/**
 * Create a Workload instance. This method is called by multiple host
 * threads, to create the individual workloads, and to send the 
 * commands for processing the workloads to CUDA
 * 
 * @param index The index of the workload 
 * @param executor The executor service 
 */
private static void createWorkloadOnHost(
    final int index, final ExecutorService executor)
{
    // Make sure that the CUDA context is current for the calling thread
    cuCtxSetCurrent(context);

    // Initialize the workload, and create the CUDA stream

    System.out.println(index + ": Initializing workload");
    final Workload workload = new Workload();
    workload.index = index;
    workload.stream = new CUstream();
    cuStreamCreate(workload.stream, 0);
    
    
    // Create the host data of the workload
    
    System.out.println(index + ": Create host data");
    workload.hostData = new Pointer();
    cuMemHostAlloc(workload.hostData, WORKLOAD_SIZE * Sizeof.INT, 0);
    ByteBuffer hostByteBuffer =
        workload.hostData.getByteBuffer(0, WORKLOAD_SIZE * Sizeof.INT);
    IntBuffer hostIntBuffer = 
        hostByteBuffer.order(ByteOrder.nativeOrder()).asIntBuffer();
    for (int i = 0; i < WORKLOAD_SIZE; i++)
    {
        hostIntBuffer.put(i, i);
    }
    workload.deviceData = new CUdeviceptr();
    cuMemAlloc(workload.deviceData, WORKLOAD_SIZE * Sizeof.INT);

    
    // Execute the CUDA commands:
    // - Copy the host data to the device
    // - Execute the kernel
    // - Copy the modified device data back to the host
    // All this is done asynchronously

    System.out.println(index + ": Execute CUDA commands");

    cuMemcpyHtoDAsync(workload.deviceData, workload.hostData,
        WORKLOAD_SIZE * Sizeof.INT, workload.stream);

    Pointer kernelParameters = Pointer.to(
        Pointer.to(new int[]{WORKLOAD_SIZE}),
        Pointer.to(workload.deviceData)
    );
    int blockSizeX = 256;
    int gridSizeX = (WORKLOAD_SIZE + blockSizeX - 1) / blockSizeX;
    cuLaunchKernel(function, gridSizeX,  1, 1, blockSizeX, 1, 1,
        0, workload.stream, kernelParameters, null);
    
    cuMemcpyDtoHAsync(workload.hostData, workload.deviceData,
        WORKLOAD_SIZE * Sizeof.INT, workload.stream);
    
    
    // Define the callback that will be called when all CUDA commands
    // on the stream have finished. This callback will forward the
    // workload to the "finishWorkloadOnHost" method.
    CUstreamCallback callback = new CUstreamCallback()
    {
        @Override
        public void call(
            CUstream hStream, int status, final Object userData)
        {
            System.out.println(index + ": Callback was called");
            Runnable runnable = new Runnable()
            {
                @Override
                public void run()
                {
                    finishWorkloadOnHost(userData);
                }
            };
            executor.submit(runnable);
        }
    };
    cuStreamAddCallback(workload.stream, callback, workload, 0);
}
 
Example #4
Source File: GpuStream.java    From OSPREY3 with GNU General Public License v2.0 4 votes vote down vote up
public CUstream getStream() {
	return stream;
}
 
Example #5
Source File: cudaStream_t.java    From jcuda with MIT License 2 votes vote down vote up
/**
 * Creates a cudaStream_t for the given {@link CUstream}. This
 * corresponds to casting a CUstream to a cudaStream_t.
 *
 * @param stream The other stream
 */
public cudaStream_t(CUstream stream)
{
    super(stream);
}