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