예제 #1
0
  @Test
  public void writeCopyReadBufferTest() {

    out.println(" - - - highLevelTest; copy buffer test - - - ");

    final int elements = NUM_ELEMENTS;

    final CLContext context = CLContext.create();

    // the CL.MEM_* flag is probably completely irrelevant in our case since we do not use a kernel
    // in this test
    final CLBuffer<ByteBuffer> clBufferA =
        context.createByteBuffer(elements * SIZEOF_INT, Mem.READ_ONLY);
    final CLBuffer<ByteBuffer> clBufferB =
        context.createByteBuffer(elements * SIZEOF_INT, Mem.READ_ONLY);

    // fill only first read buffer -> we will copy the payload to the second later.
    fillBuffer(clBufferA.buffer, 12345);

    final CLCommandQueue queue = context.getDevices()[0].createCommandQueue();

    // asynchronous write of data to GPU device, blocking read later to get the computed results
    // back.
    queue
        .putWriteBuffer(clBufferA, false) // write A
        .putCopyBuffer(clBufferA, clBufferB, clBufferA.buffer.capacity()) // copy A -> B
        .putReadBuffer(clBufferB, true) // read B
        .finish();

    context.release();

    out.println("validating computed results...");
    checkIfEqual(clBufferA.buffer, clBufferB.buffer, elements);
    out.println("results are valid");
  }
예제 #2
0
  @Test
  public void copyLimitedSlicedBuffersTest() {
    final int size = 4200 * SIZEOF_INT; // Arbitrary number that is a multiple of SIZEOF_INT;
    final int padding = 307; // Totally arbitrary number > 0
    final CLContext context = CLContext.create();
    final CLCommandQueue queue = context.getDevices()[0].createCommandQueue();

    // Make a buffer that is offset relative to the originally allocated position and has a limit
    // that is
    // not equal to the capacity to test whether all these attributes are correctly handled.
    ByteBuffer hostBuffer = ByteBuffer.allocateDirect(size + padding);
    hostBuffer.position(padding / 2); // Offset the original buffer
    hostBuffer = hostBuffer.slice(); // Slice it to have a new buffer that starts at the offset
    hostBuffer.limit(size);
    hostBuffer.order(ByteOrder.nativeOrder()); // Necessary for comparisons to work later on.
    fillBuffer(hostBuffer, 12345);

    final CLBuffer<ByteBuffer> bufferA = context.createBuffer(size).cloneWith(hostBuffer);
    final CLBuffer<ByteBuffer> bufferB = context.createByteBuffer(size);

    queue
        .putWriteBuffer(bufferA, false)
        .putCopyBuffer(bufferA, bufferB, bufferA.getNIOSize())
        .putReadBuffer(bufferB, true)
        .finish();

    hostBuffer.rewind();
    bufferB.buffer.rewind();
    checkIfEqual(hostBuffer, bufferB.buffer, size / SIZEOF_INT);
    context.release();
  }