Java Code Examples for jcuda.jcublas.JCublas2#setExceptionsEnabled()

The following examples show how to use jcuda.jcublas.JCublas2#setExceptionsEnabled() . 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: JCudnnMnist.java    From jcuda-samples with MIT License 5 votes vote down vote up
public static void main(String args[])
{
    JCuda.setExceptionsEnabled(true);
    JCudnn.setExceptionsEnabled(true);
    JCublas2.setExceptionsEnabled(true);

    int version = (int) cudnnGetVersion();
    System.out.printf("cudnnGetVersion() : %d , " + 
        "CUDNN_VERSION from cudnn.h : %d\n",
        version, CUDNN_VERSION);

    System.out.println("Creating network and layers...");
    Network mnist = new Network();
    
    System.out.println("Classifying...");
    int i1 = mnist.classifyExample(dataDirectory + first_image);
    int i2 = mnist.classifyExample(dataDirectory + second_image);

    mnist.setConvolutionAlgorithm(CUDNN_CONVOLUTION_FWD_ALGO_FFT);
    int i3 = mnist.classifyExample(dataDirectory + third_image);
    
    System.out.println(
        "\nResult of classification: " + i1 + " " + i2 + " " + i3);
    if (i1 != 1 || i2 != 3 || i3 != 5)
    {
        System.out.println("\nTest failed!\n");
    }
    else
    {
        System.out.println("\nTest passed!\n");
    }
    mnist.destroy();
}
 
Example 2
Source File: JCublas2SgemmExSample.java    From jcuda-samples with MIT License 4 votes vote down vote up
public static void main(String args[])
{
    JCublas2.setExceptionsEnabled(true);
    testSgemm(2000);
}
 
Example 3
Source File: JCublas2SgemmBatched.java    From jcuda-samples with MIT License 4 votes vote down vote up
public static void main(String[] args)
{
    JCublas2.setExceptionsEnabled(true);
    JCuda.setExceptionsEnabled(true);
    testSgemmBatched(10, 100);
}
 
Example 4
Source File: JCublas2Sample.java    From jcuda-samples with MIT License 4 votes vote down vote up
public static void main(String args[])
{
    JCublas2.setExceptionsEnabled(true);
    testSgemm(1000);
}
 
Example 5
Source File: JCublas2PointerModes.java    From jcuda-samples with MIT License 4 votes vote down vote up
/**
 * Entry point of this sample
 * 
 * @param args Not used
 */
public static void main(String[] args)
{
    // Enable exceptions and omit subsequent error checks
    JCublas2.setExceptionsEnabled(true);
    JCuda.setExceptionsEnabled(true);

    // Create the input data: A vector containing the
    // value 1.0 exactly n times.
    int n = 1000000;
    float hostData[] = new float[n];
    Arrays.fill(hostData,  1.0f);

    // Allocate device memory, and copy the input data to the device
    Pointer deviceData = new Pointer();
    cudaMalloc(deviceData, n * Sizeof.FLOAT);
    cudaMemcpy(deviceData, Pointer.to(hostData), n * Sizeof.FLOAT,
        cudaMemcpyHostToDevice);

    // Create a CUBLAS handle
    cublasHandle handle = new cublasHandle();
    cublasCreate(handle);


    // Execute the 'dot' function in HOST pointer mode:
    // The result will be written to a pointer that
    // points to host memory.

    // Set the pointer mode to HOST
    cublasSetPointerMode(handle, CUBLAS_POINTER_MODE_HOST);

    // Prepare the pointer for the result in HOST memory
    float hostResult[] = { -1.0f };
    Pointer hostResultPointer = Pointer.to(hostResult);

    // Execute the 'dot' function
    long beforeHostCall = System.nanoTime();
    cublasSdot(handle, n, deviceData, 1, deviceData, 1, hostResultPointer);
    long afterHostCall = System.nanoTime();

    // Print the result and timing information
    double hostDuration = (afterHostCall - beforeHostCall) / 1e6;
    System.out.println("Host call duration: " + hostDuration + " ms");
    System.out.println("Result: " + hostResult[0]);


    // Execute the 'dot' function in DEVICE pointer mode:
    // The result will be written to a pointer that
    // points to device memory.

    // Set the pointer mode to DEVICE
    cublasSetPointerMode(handle, CUBLAS_POINTER_MODE_DEVICE);

    // Prepare the pointer for the result in DEVICE memory
    Pointer deviceResultPointer = new Pointer();
    cudaMalloc(deviceResultPointer, Sizeof.FLOAT);

    // Execute the 'dot' function
    long beforeDeviceCall = System.nanoTime();
    cublasSdot(handle, n, deviceData, 1, deviceData, 1,
        deviceResultPointer);
    long afterDeviceCall = System.nanoTime();

    // Synchronize in order to wait for the result to
    // be available (note that this is done implicitly
    // when cudaMemcpy is called)
    cudaDeviceSynchronize();
    long afterDeviceSync = System.nanoTime();

    // Copy the result from the device to the host
    float deviceResult[] = { -1.0f };
    cudaMemcpy(Pointer.to(deviceResult), deviceResultPointer, 
        Sizeof.FLOAT, cudaMemcpyDeviceToHost);

    // Print the result and timing information
    double deviceCallDuration = (afterDeviceCall - beforeDeviceCall) / 1e6;
    double deviceFullDuration = (afterDeviceSync - beforeDeviceCall) / 1e6;
    System.out .println(
        "Device call duration: " + deviceCallDuration + " ms");
    System.out.println(
        "Device full duration: " + deviceFullDuration + " ms");
    System.out.println("Result: " + deviceResult[0]);

    // Clean up
    cudaFree(deviceData);
    cublasDestroy(handle);
}
 
Example 6
Source File: JCudaRuntimeMappedMemory.java    From jcuda-samples with MIT License 4 votes vote down vote up
/**
 * Entry point of this sample
 * 
 * @param args Not used
 */
public static void main(String args[])
{
    // Enable exceptions to quickly be informed about errors in this test
    JCuda.setExceptionsEnabled(true);
    JCublas2.setExceptionsEnabled(true);

    // Check if the device supports mapped host memory
    cudaDeviceProp deviceProperties = new cudaDeviceProp();
    cudaGetDeviceProperties(deviceProperties, 0);
    if (deviceProperties.canMapHostMemory == 0)
    {
        System.err.println("This device can not map host memory");
        System.err.println(deviceProperties.toFormattedString());
        return;
    }

    // Set the flag indicating that mapped memory will be used
    cudaSetDeviceFlags(cudaDeviceMapHost);

    // Allocate mappable host memory
    int n = 5;
    Pointer hostPointer = new Pointer();
    cudaHostAlloc(hostPointer, n * Sizeof.FLOAT, cudaHostAllocMapped);

    // Create a device pointer mapping the host memory
    Pointer devicePointer = new Pointer();
    cudaHostGetDevicePointer(devicePointer, hostPointer, 0);

    // Obtain a ByteBuffer for accessing the data in the host
    // pointer. Modifications in this ByteBuffer will be
    // visible in the device memory.
    ByteBuffer byteBuffer = hostPointer.getByteBuffer(0, n * Sizeof.FLOAT);

    // Set the byte order of the ByteBuffer
    byteBuffer.order(ByteOrder.nativeOrder());

    // For convenience, view the ByteBuffer as a FloatBuffer
    // and fill it with some sample data
    FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
    System.out.print("Input : ");
    for (int i = 0; i < n; i++)
    {
        floatBuffer.put(i, (float) i);
        System.out.print(floatBuffer.get(i) + ", ");
    }
    System.out.println();

    // Apply a CUBLAS routine to the device pointer. This will
    // modify the host data, which was mapped to the device.
    cublasHandle handle = new cublasHandle();
    cublasCreate(handle);
    Pointer two = Pointer.to(new float[] { 2.0f });
    cublasSscal(handle, n, two, devicePointer, 1);
    cublasDestroy(handle);
    cudaDeviceSynchronize();

    // Print the contents of the host memory after the
    // modification via the mapped pointer.
    System.out.print("Output: ");
    for (int i = 0; i < n; i++)
    {
        System.out.print(floatBuffer.get(i) + ", ");
    }
    System.out.println();

    // Clean up
    cudaFreeHost(hostPointer);
}