Exemplo n.º 1
0
  /**
   * This method frees specific chunk of memory, described by AllocationPoint passed in
   *
   * @param point
   */
  @Override
  public void free(AllocationPoint point) {
    switch (point.getAllocationStatus()) {
      case HOST:
        {
          // cudaFreeHost call here
          // FIXME: it would be nice to get rid of typecasting here
          long reqMem = AllocationUtils.getRequiredMemory(point.getShape());

          //  log.info("Deallocating {} bytes on [HOST]", reqMem);

          NativeOps nativeOps = NativeOpsHolder.getInstance().getDeviceNativeOps();

          long result = nativeOps.freeHost(point.getPointers().getHostPointer().address());
          // JCuda.cudaFreeHost(new Pointer(point.getPointers().getHostPointer().address()));
          if (result == 0) throw new RuntimeException("Can't deallocate [HOST] memory...");
        }
        break;
      case DEVICE:
        {
          // cudaFree call
          // JCuda.cudaFree(new Pointer(point.getPointers().getDevicePointer().address()));

          long reqMem = AllocationUtils.getRequiredMemory(point.getShape());

          //       log.info("Deallocating {} bytes on [DEVICE]", reqMem);

          NativeOps nativeOps = NativeOpsHolder.getInstance().getDeviceNativeOps();

          long result = nativeOps.freeDevice(point.getPointers().getDevicePointer().address(), 0);
          if (result == 0) throw new RuntimeException("Can't deallocate [DEVICE] memory...");
        }
        break;
      default:
        throw new IllegalStateException(
            "Can't free memory on target [" + point.getAllocationStatus() + "]");
    }
  }