Example #1
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();
  }
Example #2
0
  @Test
  public void subBufferTest01ByteBuffer() {

    out.println(" - - - subBufferTest - - - ");

    @SuppressWarnings("unchecked")
    final CLPlatform platform = CLPlatform.getDefault(version(CL_1_1));
    if (platform == null) {
      out.println("aborting subBufferTest");
      return;
    }

    final CLContext context = CLContext.create(platform);
    try {
      final int subelements = 5;
      final long lMaxAlignment = context.getMaxMemBaseAddrAlign();
      final int iMaxAlignment = Bitstream.uint32LongToInt(lMaxAlignment);
      System.err.println(
          "XXX: maxAlignment "
              + lMaxAlignment
              + ", 0x"
              + Long.toHexString(lMaxAlignment)
              + ", (int)"
              + iMaxAlignment
              + ", (int)0x"
              + Integer.toHexString(iMaxAlignment));
      if (-1 == iMaxAlignment) {
        throw new RuntimeException(
            "Cannot handle MaxMemBaseAddrAlign > MAX_INT, has 0x"
                + Long.toHexString(lMaxAlignment));
      }
      // device only
      final CLBuffer<?> buffer = context.createBuffer(iMaxAlignment + subelements);

      assertFalse(buffer.isSubBuffer());
      assertNotNull(buffer.getSubBuffers());
      assertTrue(buffer.getSubBuffers().isEmpty());

      final CLSubBuffer<?> subBuffer = buffer.createSubBuffer(iMaxAlignment, subelements);

      assertTrue(subBuffer.isSubBuffer());
      assertEquals(subelements, subBuffer.getCLSize());
      assertEquals(iMaxAlignment, subBuffer.getOffset());
      assertEquals(iMaxAlignment, subBuffer.getCLOffset());
      assertEquals(buffer, subBuffer.getParent());
      assertEquals(1, buffer.getSubBuffers().size());

      subBuffer.release();
      assertEquals(0, buffer.getSubBuffers().size());
    } finally {
      context.release();
    }
  }
Example #3
0
  @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();
  }
Example #4
0
  @Test
  public void bufferWithHostPointerTest() {

    out.println(" - - - highLevelTest; host pointer test - - - ");

    final int elements = NUM_ELEMENTS;

    final CLContext context = CLContext.create();

    final ByteBuffer buffer = Buffers.newDirectByteBuffer(elements * SIZEOF_INT);
    // fill only first read buffer -> we will copy the payload to the second later.
    fillBuffer(buffer, 12345);

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

    final Mem[] bufferConfig = new Mem[] {Mem.COPY_BUFFER, Mem.USE_BUFFER};

    for (int i = 0; i < bufferConfig.length; i++) {

      out.println("testing with " + bufferConfig[i] + " config");

      final CLBuffer<ByteBuffer> clBufferA =
          context.createBuffer(buffer, Mem.READ_ONLY, bufferConfig[i]);
      final CLBuffer<ByteBuffer> clBufferB =
          context.createByteBuffer(elements * SIZEOF_INT, Mem.READ_ONLY);

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

      assertEquals(2, context.getMemoryObjects().size());
      clBufferA.release();
      assertEquals(1, context.getMemoryObjects().size());
      clBufferB.release();
      assertEquals(0, context.getMemoryObjects().size());

      // uploading worked when a==b.
      out.println("validating computed results...");
      checkIfEqual(clBufferA.buffer, clBufferB.buffer, elements);
      out.println("results are valid");
    }

    context.release();
  }
Example #5
0
  @Test
  public void cloneWithLimitedBufferTest() {
    final int elements = NUM_ELEMENTS;
    final int padding = 312; // Arbitrary number
    final CLContext context = CLContext.create();

    final IntBuffer hostBuffer =
        ByteBuffer.allocateDirect((elements + padding) * SIZEOF_INT).asIntBuffer();
    hostBuffer.limit(elements);

    final CLBuffer<?> deviceBuffer =
        context.createBuffer(elements * SIZEOF_INT).cloneWith(hostBuffer);
    assertEquals(elements, deviceBuffer.getCLCapacity());
    assertEquals(elements * SIZEOF_INT, deviceBuffer.getNIOSize());
    assertEquals(elements, deviceBuffer.getNIOCapacity());

    context.release();
  }
Example #6
0
  @Test
  public void createBufferFromLimitedBuffer() {
    final int elements = NUM_ELEMENTS;
    final int padding =
        19 * SIZEOF_INT * 2; // Totally arbitrary number > 0 divisible by 2*SIZEOF_INT
    final CLContext context = CLContext.create();

    // 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 byteBuffer = ByteBuffer.allocateDirect(elements * SIZEOF_INT + padding);
    byteBuffer.position(padding / 2); // Offset the original buffer
    IntBuffer intBuffer =
        byteBuffer
            .slice()
            .order(ByteOrder.nativeOrder())
            .asIntBuffer(); // Slice it to have a new buffer that starts at the offset
    intBuffer.limit(elements);

    final CLBuffer<IntBuffer> deviceBuffer = context.createBuffer(intBuffer);
    assertEquals(elements, deviceBuffer.getCLCapacity());
    assertEquals(elements * SIZEOF_INT, deviceBuffer.getNIOSize());
    assertEquals(elements, deviceBuffer.getNIOCapacity());
  }
Example #7
0
  @Test
  public void destructorCallbackTest() throws InterruptedException {

    out.println(" - - - destructorCallbackTest - - - ");

    @SuppressWarnings("unchecked")
    final CLPlatform platform = CLPlatform.getDefault(version(CL_1_1));
    if (platform == null) {
      out.println("aborting destructorCallbackTest");
      return;
    }

    final CLContext context = CLContext.create(platform);

    try {

      final CLBuffer<?> buffer = context.createBuffer(32);
      final CountDownLatch countdown = new CountDownLatch(1);

      buffer.registerDestructorCallback(
          new CLMemObjectListener() {
            public void memoryDeallocated(final CLMemory<?> mem) {
              out.println("buffer released");
              assertEquals(mem, buffer);
              countdown.countDown();
            }
          });
      buffer.release();

      countdown.await(2, TimeUnit.SECONDS);
      assertEquals(countdown.getCount(), 0);

    } finally {
      context.release();
    }
  }