/** List the OpenCL implementations that contain at least one GPU device. */
 public static CLPlatform[] listGPUPoweredPlatforms() {
   CLPlatform[] platforms = listPlatforms();
   List<CLPlatform> out = new ArrayList<CLPlatform>(platforms.length);
   for (CLPlatform platform : platforms) {
     if (platform.listGPUDevices(true).length > 0) out.add(platform);
   }
   return out.toArray(new CLPlatform[out.size()]);
 }
 /**
  * Creates an OpenCL context able to share entities with the current OpenGL context.
  *
  * @throws RuntimeException if JavaCL is unable to create an OpenGL-shared OpenCL context.
  */
 public static CLContext createContextFromCurrentGL() {
   RuntimeException first = null;
   for (CLPlatform platform : listPlatforms()) {
     try {
       CLContext ctx = platform.createContextFromCurrentGL();
       if (ctx != null) return ctx;
     } catch (RuntimeException ex) {
       if (first == null) first = ex;
     }
   }
   throw new RuntimeException(
       "Failed to create an OpenCL context based on the current OpenGL context", first);
 }
Example #3
0
/*    */ public final class CLSampler extends CLObjectChild<CLContext>
/*    */ {
  /* 41 */ private static final InfoUtil<CLSampler> util =
      CLPlatform.getInfoUtilInstance(CLSampler.class, "CL_SAMPLER_UTIL");
  /*    */
  /*    */ CLSampler(long pointer, CLContext context) {
    /* 44 */ super(pointer, context);
    /* 45 */ if (isValid()) /* 46 */ context.getCLSamplerRegistry().registerObject(this);
    /*    */ }
  /*    */
  /*    */ public int getInfoInt(int param_name) /*    */ {
    /* 59 */ return util.getInfoInt(this, param_name);
    /*    */ }
  /*    */
  /*    */ public long getInfoLong(int param_name) /*    */ {
    /* 71 */ return util.getInfoLong(this, param_name);
    /*    */ }
  /*    */
  /*    */ int release() /*    */ {
    /*    */ try
    /*    */ {
      /* 78 */ return super.release();
      /*    */ } finally {
      /* 80 */ if (!isValid())
        /* 81 */ ((CLContext) getParent()).getCLSamplerRegistry().unregisterObject(this);
      /*    */ }
    /*    */ }
  /*    */ }
Example #4
0
  @Test
  public void subBufferTest02FloatBuffer() {

    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));
      }
      // FIXME: See Bug 979: Offset/Alignment via offset calculation per element-count is faulty!
      final int floatsPerAlignment = iMaxAlignment / Buffers.SIZEOF_FLOAT;
      // device + direct buffer
      final CLBuffer<FloatBuffer> buffer =
          context.createFloatBuffer(floatsPerAlignment + subelements);
      assertFalse(buffer.isSubBuffer());
      assertNotNull(buffer.getSubBuffers());
      assertTrue(buffer.getSubBuffers().isEmpty());

      final CLSubBuffer<FloatBuffer> subBuffer =
          buffer.createSubBuffer(floatsPerAlignment, subelements);

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

      assertEquals(subBuffer.getCLCapacity(), subBuffer.getBuffer().capacity());

      subBuffer.release();
      assertEquals(0, buffer.getSubBuffers().size());

    } finally {
      context.release();
    }
  }
Example #5
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 #6
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 #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();
    }
  }
 /**
  * Returns the "best" OpenCL device based on the comparison of the provided prioritized device
  * feature.<br>
  * The returned device does not necessarily exhibit the features listed in preferredFeatures, but
  * it has the best ordered composition of them.<br>
  * For instance on a system with a GPU and a CPU device, <code>
  * JavaCL.getBestDevice(CPU, MaxComputeUnits)</code> will return the CPU device, but on another
  * system with two GPUs and no CPU device it will return the GPU that has the most compute units.
  */
 public static CLDevice getBestDevice(CLPlatform.DeviceFeature... preferredFeatures) {
   List<CLDevice> devices = new ArrayList<CLDevice>();
   for (CLPlatform platform : listPlatforms())
     devices.addAll(Arrays.asList(platform.listAllDevices(true)));
   return CLPlatform.getBestDevice(Arrays.asList(preferredFeatures), devices);
 }