コード例 #1
0
ファイル: CLBufferTest.java プロジェクト: WadeWalker/jocl
  @Test
  public void mapBufferTest() {

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

    final int elements = NUM_ELEMENTS;
    final int sizeInBytes = elements * SIZEOF_INT;

    CLContext context;
    CLBuffer<?> clBufferA;
    CLBuffer<?> clBufferB;

    // We will have to allocate mappable NIO memory on non CPU contexts
    // since we can't map e.g GPU memory.
    if (CLPlatform.getDefault().listCLDevices(CLDevice.Type.CPU).length > 0) {

      context = CLContext.create(CLDevice.Type.CPU);

      clBufferA = context.createBuffer(sizeInBytes, Mem.READ_WRITE);
      clBufferB = context.createBuffer(sizeInBytes, Mem.READ_WRITE);
    } else {

      context = CLContext.create();

      clBufferA = context.createByteBuffer(sizeInBytes, Mem.READ_WRITE, Mem.USE_BUFFER);
      clBufferB = context.createByteBuffer(sizeInBytes, Mem.READ_WRITE, Mem.USE_BUFFER);
    }

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

    // fill only first buffer -> we will copy the payload to the second later.
    final ByteBuffer mappedBufferA = queue.putMapBuffer(clBufferA, Map.WRITE, true);
    assertEquals(sizeInBytes, mappedBufferA.capacity());

    fillBuffer(mappedBufferA, 12345); // write to A

    queue
        .putUnmapMemory(clBufferA, mappedBufferA) // unmap A
        .putCopyBuffer(clBufferA, clBufferB); // copy A -> B

    // map B for read operations
    final ByteBuffer mappedBufferB = queue.putMapBuffer(clBufferB, Map.READ, true);
    assertEquals(sizeInBytes, mappedBufferB.capacity());

    out.println("validating computed results...");
    checkIfEqual(mappedBufferA, mappedBufferB, elements); // A == B ?
    out.println("results are valid");

    queue.putUnmapMemory(clBufferB, mappedBufferB); // unmap B

    context.release();
  }