Ejemplo n.º 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();
  }
Ejemplo n.º 2
0
  @Test
  public void createBufferTest() {

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

    final CLContext context = CLContext.create();
    try {
      final int size = 6;

      final CLBuffer<ByteBuffer> bb = context.createByteBuffer(size);
      final CLBuffer<ShortBuffer> sb = context.createShortBuffer(size);
      final CLBuffer<IntBuffer> ib = context.createIntBuffer(size);
      final CLBuffer<LongBuffer> lb = context.createLongBuffer(size);
      final CLBuffer<FloatBuffer> fb = context.createFloatBuffer(size);
      final CLBuffer<DoubleBuffer> db = context.createDoubleBuffer(size);

      final List<CLMemory<? extends Buffer>> buffers = context.getMemoryObjects();
      assertEquals(6, buffers.size());

      assertEquals(1, bb.getElementSize());
      assertEquals(2, sb.getElementSize());
      assertEquals(4, ib.getElementSize());
      assertEquals(8, lb.getElementSize());
      assertEquals(4, fb.getElementSize());
      assertEquals(8, db.getElementSize());

      final ByteBuffer anotherNIO = newDirectByteBuffer(2);

      for (final CLMemory<? extends Buffer> memory : buffers) {

        final CLBuffer<? extends Buffer> buffer = (CLBuffer<? extends Buffer>) memory;
        final Buffer nio = buffer.getBuffer();

        assertEquals(nio.capacity(), buffer.getCLCapacity());
        assertEquals(buffer.getNIOSize(), buffer.getCLSize());
        assertEquals(sizeOfBufferElem(nio), buffer.getElementSize());
        assertEquals(nio.capacity() * sizeOfBufferElem(nio), buffer.getCLSize());

        final CLBuffer<ByteBuffer> clone = buffer.cloneWith(anotherNIO);

        assertEquals(buffer.ID, clone.ID);
        assertTrue(clone.equals(buffer));
        assertTrue(buffer.equals(clone));

        assertEquals(buffer.getCLSize(), clone.getCLCapacity());
        assertEquals(buffer.getCLSize(), clone.getCLSize());
        assertEquals(anotherNIO.capacity(), clone.getNIOCapacity());
      }

    } finally {
      context.release();
    }
  }
Ejemplo n.º 3
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();
  }
Ejemplo n.º 4
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());
  }