示例#1
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();
  }
示例#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();
    }
  }