Example usage for java.nio ByteOrder nativeOrder

List of usage examples for java.nio ByteOrder nativeOrder

Introduction

In this page you can find the example usage for java.nio ByteOrder nativeOrder.

Prototype

public static ByteOrder nativeOrder() 

Source Link

Document

Returns the current platform byte order.

Usage

From source file:com.alvermont.terraj.fracplanet.geom.VertexBufferArray.java

/**
 * Creates a new instance of VertexBufferArray
 *
 * @param capacity The initial capacity of this buffer
 *///w w w  . j a  v a2 s.c  o m
public VertexBufferArray(int capacity) {
    this.buffer = ByteBuffer.allocateDirect(capacity * ELEMENTSIZE).order(ByteOrder.nativeOrder());

    sliceAndDice(capacity);
}

From source file:org.apache.tez.runtime.library.common.sort.impl.dflt.DefaultSorter.java

public DefaultSorter(OutputContext outputContext, Configuration conf, int numOutputs,
        long initialMemoryAvailable) throws IOException {
    super(outputContext, conf, numOutputs, initialMemoryAvailable);
    deflater = TezCommonUtils.newBestCompressionDeflater();
    // sanity checks
    final float spillper = this.conf.getFloat(TezRuntimeConfiguration.TEZ_RUNTIME_SORT_SPILL_PERCENT,
            TezRuntimeConfiguration.TEZ_RUNTIME_SORT_SPILL_PERCENT_DEFAULT);
    final int sortmb = computeSortBufferSize((int) availableMemoryMb, outputContext.getDestinationVertexName());

    Preconditions.checkArgument(spillper <= (float) 1.0 && spillper > (float) 0.0,
            TezRuntimeConfiguration.TEZ_RUNTIME_SORT_SPILL_PERCENT
                    + " should be greater than 0 and less than or equal to 1");

    indexCacheMemoryLimit = this.conf.getInt(TezRuntimeConfiguration.TEZ_RUNTIME_INDEX_CACHE_MEMORY_LIMIT_BYTES,
            TezRuntimeConfiguration.TEZ_RUNTIME_INDEX_CACHE_MEMORY_LIMIT_BYTES_DEFAULT);

    boolean confPipelinedShuffle = this.conf.getBoolean(
            TezRuntimeConfiguration.TEZ_RUNTIME_PIPELINED_SHUFFLE_ENABLED,
            TezRuntimeConfiguration.TEZ_RUNTIME_PIPELINED_SHUFFLE_ENABLED_DEFAULT);

    if (confPipelinedShuffle) {
        LOG.warn(outputContext.getDestinationVertexName() + ": "
                + TezRuntimeConfiguration.TEZ_RUNTIME_PIPELINED_SHUFFLE_ENABLED + " does not work "
                + "with DefaultSorter. It is supported only with PipelinedSorter.");
    }//w  w  w .  ja v a2  s .  c  o  m
    auxiliaryService = conf.get(TezConfiguration.TEZ_AM_SHUFFLE_AUXILIARY_SERVICE_ID,
            TezConfiguration.TEZ_AM_SHUFFLE_AUXILIARY_SERVICE_ID_DEFAULT);

    // buffers and accounting
    int maxMemUsage = sortmb << 20;
    maxMemUsage -= maxMemUsage % METASIZE;
    kvbuffer = new byte[maxMemUsage];
    bufvoid = kvbuffer.length;
    kvmeta = ByteBuffer.wrap(kvbuffer).order(ByteOrder.nativeOrder()).asIntBuffer();
    setEquator(0);
    bufstart = bufend = bufindex = equator;
    kvstart = kvend = kvindex;

    maxRec = kvmeta.capacity() / NMETA;
    softLimit = (int) (kvbuffer.length * spillper);
    bufferRemaining = softLimit;
    if (LOG.isInfoEnabled()) {
        LOG.info(outputContext.getDestinationVertexName() + ": "
                + TezRuntimeConfiguration.TEZ_RUNTIME_IO_SORT_MB + "=" + sortmb + ", soft limit=" + softLimit
                + ", bufstart=" + bufstart + ", bufvoid=" + bufvoid + ", kvstart=" + kvstart + ", legnth="
                + maxRec + ", finalMergeEnabled=" + isFinalMergeEnabled());
    }

    // k/v serialization
    valSerializer.open(bb);
    keySerializer.open(bb);

    spillInProgress = false;
    minSpillsForCombine = this.conf.getInt(TezRuntimeConfiguration.TEZ_RUNTIME_COMBINE_MIN_SPILLS, 3);
    spillThread.setDaemon(true);
    spillThread.setName(
            "SpillThread {" + TezUtilsInternal.cleanVertexName(outputContext.getDestinationVertexName() + "}"));
    spillLock.lock();
    try {
        spillThread.start();
        while (!spillThreadRunning) {
            spillDone.await();
        }
    } catch (InterruptedException e) {
        //interrupt spill thread
        spillThread.interrupt();
        Thread.currentThread().interrupt();
        throw new IOException("Spill thread failed to initialize", e);
    } finally {
        spillLock.unlock();
    }
    if (sortSpillException != null) {
        throw new IOException("Spill thread failed to initialize", sortSpillException);
    }
}

From source file:cpcc.ros.services.RosImageConverterTest.java

@Test
public void shouldConvertRGBA8Images() throws IOException {
    int height = 240;
    int width = 320;
    int step = 1280;
    String encoding = "rgba8";
    String imageName = "data/test-image-rgba8.rgba8";
    String convertedImageName = "data/test-image-rgba8.png";

    InputStream stream = RosImageConverterTest.class.getResourceAsStream(imageName);
    byte[] imageData = IOUtils.toByteArray(stream);

    stream = RosImageConverterTest.class.getResourceAsStream(convertedImageName);
    byte[] convertedImageData = IOUtils.toByteArray(stream);

    when(buffer.array()).thenReturn(imageData);

    when(message.getEncoding()).thenReturn(encoding);
    when(message.getHeight()).thenReturn(height);
    when(message.getWidth()).thenReturn(width);
    when(message.getStep()).thenReturn(step);
    when(message.getData()).thenReturn(ChannelBuffers.copiedBuffer(ByteOrder.nativeOrder(), imageData));

    BufferedImage result = conv.messageToBufferedImage(message);
    assertThat(result).isNotNull();//from   www  . ja v a2  s .co  m
    assertThat(result.getHeight()).isEqualTo(height);
    assertThat(result.getWidth()).isEqualTo(width);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ImageIO.write(result, "PNG", bos);

    assertThat(bos.toByteArray()).isEqualTo(convertedImageData);
}

From source file:com.google.vrtoolkit.cardboard.samples.treasurehunt.MainActivity.java

/**
 * Creates the buffers we use to store information about the 3D world.
 *
 * <p>OpenGL doesn't use Java arrays, but rather needs data in a format it can understand.
 * Hence we use ByteBuffers.//from  w  w w  .j  a  va 2 s  .  co m
 *
 * @param config The EGL configuration used when creating the surface.
 */
@Override
public void onSurfaceCreated(EGLConfig config) {
    Log.i(TAG, "onSurfaceCreated");
    GLES20.glClearColor(0.1f, 0.1f, 0.1f, 0.5f); // Dark background so text shows up well.

    ByteBuffer bbVertices = ByteBuffer.allocateDirect(WorldLayoutData.CUBE_COORDS.length * 4);
    bbVertices.order(ByteOrder.nativeOrder());
    cubeVertices = bbVertices.asFloatBuffer();
    cubeVertices.put(WorldLayoutData.CUBE_COORDS);
    cubeVertices.position(0);

    ByteBuffer bbColors = ByteBuffer.allocateDirect(WorldLayoutData.CUBE_COLORS.length * 4);
    bbColors.order(ByteOrder.nativeOrder());
    cubeColors = bbColors.asFloatBuffer();
    cubeColors.put(WorldLayoutData.CUBE_COLORS);
    cubeColors.position(0);

    ByteBuffer bbFoundColors = ByteBuffer.allocateDirect(WorldLayoutData.CUBE_FOUND_COLORS.length * 4);
    bbFoundColors.order(ByteOrder.nativeOrder());
    cubeFoundColors = bbFoundColors.asFloatBuffer();
    cubeFoundColors.put(WorldLayoutData.CUBE_FOUND_COLORS);
    cubeFoundColors.position(0);

    ByteBuffer bbNormals = ByteBuffer.allocateDirect(WorldLayoutData.CUBE_NORMALS.length * 4);
    bbNormals.order(ByteOrder.nativeOrder());
    cubeNormals = bbNormals.asFloatBuffer();
    cubeNormals.put(WorldLayoutData.CUBE_NORMALS);
    cubeNormals.position(0);

    ByteBuffer mcbbVertices = ByteBuffer.allocateDirect(WorldLayoutData.MINI_CUBE_COORDS.length * 4);
    mcbbVertices.order(ByteOrder.nativeOrder());
    miniCubeVertices = mcbbVertices.asFloatBuffer();
    miniCubeVertices.put(WorldLayoutData.MINI_CUBE_COORDS);
    miniCubeVertices.position(0);

    ByteBuffer mcbbColors = ByteBuffer.allocateDirect(WorldLayoutData.MINI_CUBE_COLORS.length * 4);
    mcbbColors.order(ByteOrder.nativeOrder());
    miniCubeColors = mcbbColors.asFloatBuffer();
    miniCubeColors.put(WorldLayoutData.MINI_CUBE_COLORS);
    miniCubeColors.position(0);

    ByteBuffer mcbbNormals = ByteBuffer.allocateDirect(WorldLayoutData.CUBE_NORMALS.length * 4);
    mcbbNormals.order(ByteOrder.nativeOrder());
    miniCubeNormals = mcbbNormals.asFloatBuffer();
    miniCubeNormals.put(WorldLayoutData.CUBE_NORMALS);
    miniCubeNormals.position(0);

    // make a floor
    ByteBuffer bbFloorVertices = ByteBuffer.allocateDirect(WorldLayoutData.FLOOR_COORDS.length * 4);
    bbFloorVertices.order(ByteOrder.nativeOrder());
    floorVertices = bbFloorVertices.asFloatBuffer();
    floorVertices.put(WorldLayoutData.FLOOR_COORDS);
    floorVertices.position(0);

    ByteBuffer bbFloorNormals = ByteBuffer.allocateDirect(WorldLayoutData.FLOOR_NORMALS.length * 4);
    bbFloorNormals.order(ByteOrder.nativeOrder());
    floorNormals = bbFloorNormals.asFloatBuffer();
    floorNormals.put(WorldLayoutData.FLOOR_NORMALS);
    floorNormals.position(0);

    ByteBuffer bbFloorColors = ByteBuffer.allocateDirect(WorldLayoutData.FLOOR_COLORS.length * 4);
    bbFloorColors.order(ByteOrder.nativeOrder());
    floorColors = bbFloorColors.asFloatBuffer();
    floorColors.put(WorldLayoutData.FLOOR_COLORS);
    floorColors.position(0);

    int vertexShader = loadGLShader(GLES20.GL_VERTEX_SHADER, R.raw.light_vertex);
    int gridShader = loadGLShader(GLES20.GL_FRAGMENT_SHADER, R.raw.grid_fragment);
    int passthroughShader = loadGLShader(GLES20.GL_FRAGMENT_SHADER, R.raw.passthrough_fragment);

    cubeProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(cubeProgram, vertexShader);
    GLES20.glAttachShader(cubeProgram, passthroughShader);
    GLES20.glLinkProgram(cubeProgram);
    GLES20.glUseProgram(cubeProgram);

    checkGLError("Cube program");

    cubePositionParam = GLES20.glGetAttribLocation(cubeProgram, "a_Position");
    cubeNormalParam = GLES20.glGetAttribLocation(cubeProgram, "a_Normal");
    cubeColorParam = GLES20.glGetAttribLocation(cubeProgram, "a_Color");

    cubeModelParam = GLES20.glGetUniformLocation(cubeProgram, "u_Model");
    cubeModelViewParam = GLES20.glGetUniformLocation(cubeProgram, "u_MVMatrix");
    cubeModelViewProjectionParam = GLES20.glGetUniformLocation(cubeProgram, "u_MVP");
    cubeLightPosParam = GLES20.glGetUniformLocation(cubeProgram, "u_LightPos");

    GLES20.glEnableVertexAttribArray(cubePositionParam);
    GLES20.glEnableVertexAttribArray(cubeNormalParam);
    GLES20.glEnableVertexAttribArray(cubeColorParam);

    checkGLError("Cube program params");

    //Minicube
    miniCubeProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(miniCubeProgram, vertexShader);
    GLES20.glAttachShader(miniCubeProgram, passthroughShader);
    GLES20.glLinkProgram(miniCubeProgram);
    GLES20.glUseProgram(miniCubeProgram);

    checkGLError("Cube program");

    miniCubePositionParam = GLES20.glGetAttribLocation(miniCubeProgram, "a_Position");
    miniCubeNormalParam = GLES20.glGetAttribLocation(miniCubeProgram, "a_Normal");
    miniCubeColorParam = GLES20.glGetAttribLocation(miniCubeProgram, "a_Color");

    miniCubeModelParam = GLES20.glGetUniformLocation(miniCubeProgram, "u_Model");
    miniCubeModelViewParam = GLES20.glGetUniformLocation(miniCubeProgram, "u_MVMatrix");
    miniCubeModelViewProjectionParam = GLES20.glGetUniformLocation(miniCubeProgram, "u_MVP");
    miniCubeLightPosParam = GLES20.glGetUniformLocation(miniCubeProgram, "u_LightPos");

    GLES20.glEnableVertexAttribArray(miniCubePositionParam);
    GLES20.glEnableVertexAttribArray(miniCubeNormalParam);
    GLES20.glEnableVertexAttribArray(miniCubeColorParam);

    checkGLError("Cube program params");

    floorProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(floorProgram, vertexShader);
    GLES20.glAttachShader(floorProgram, gridShader);
    GLES20.glLinkProgram(floorProgram);
    GLES20.glUseProgram(floorProgram);

    checkGLError("Floor program");

    floorModelParam = GLES20.glGetUniformLocation(floorProgram, "u_Model");
    floorModelViewParam = GLES20.glGetUniformLocation(floorProgram, "u_MVMatrix");
    floorModelViewProjectionParam = GLES20.glGetUniformLocation(floorProgram, "u_MVP");
    floorLightPosParam = GLES20.glGetUniformLocation(floorProgram, "u_LightPos");

    floorPositionParam = GLES20.glGetAttribLocation(floorProgram, "a_Position");
    floorNormalParam = GLES20.glGetAttribLocation(floorProgram, "a_Normal");
    floorColorParam = GLES20.glGetAttribLocation(floorProgram, "a_Color");

    GLES20.glEnableVertexAttribArray(floorPositionParam);
    GLES20.glEnableVertexAttribArray(floorNormalParam);
    GLES20.glEnableVertexAttribArray(floorColorParam);

    checkGLError("Floor program params");

    Matrix.setIdentityM(modelFloor, 0);
    Matrix.translateM(modelFloor, 0, 0, -floorDepth, 0); // Floor appears below user.

    // Avoid any delays during start-up due to decoding of sound files.
    new Thread(new Runnable() {
        public void run() {
            // Start spatial audio playback of SOUND_FILE at the model postion. The returned
            //soundId handle is stored and allows for repositioning the sound object whenever
            // the cube position changes.
            cardboardAudioEngine.preloadSoundFile(SOUND_FILE);
            soundId = cardboardAudioEngine.createSoundObject(SOUND_FILE);
            cardboardAudioEngine.setSoundObjectPosition(soundId, modelPosition[0], modelPosition[1],
                    modelPosition[2]);
            cardboardAudioEngine.playSound(soundId, true /* looped playback */);
        }
    }).start();

    updateModelPosition();

    checkGLError("onSurfaceCreated");
}

From source file:org.apache.sysml.runtime.matrix.data.SinglePrecisionCudaSupportFunctions.java

@Override
public void deviceToHost(GPUContext gCtx, Pointer src, double[] dest, String instName, boolean isEviction) {
    long t0 = DMLScript.STATISTICS ? System.nanoTime() : 0;
    // We invoke transfer matrix from device to host in two cases:
    // 1. During eviction of unlocked matrices
    // 2. During acquireHostRead
    // //from www. j a  v  a  2 s. c om
    // If the single-precision support is enabled, then float-to-double conversion is required as CP expects the data to be in double format. 
    // This conversion can be done on host or on device. We typically prefer to do this conversion on device due to GPU's high-memory bandwidth. 
    // However, the conversion requires an additional space to be allocated for the conversion, which can lead to infinite recursion 
    // during eviction: `evict -> devictToHost -> float2double -> allocate -> ensureFreeSpace -> evict`. 
    // To avoid this recursion, it is necessary to perform this conversion in host.
    if (PERFORM_CONVERSION_ON_DEVICE && !isEviction) {
        Pointer deviceDoubleData = gCtx.allocate(((long) dest.length) * Sizeof.DOUBLE);
        LibMatrixCUDA.float2double(gCtx, src, deviceDoubleData, dest.length);
        cudaMemcpy(Pointer.to(dest), deviceDoubleData, ((long) dest.length) * Sizeof.DOUBLE,
                cudaMemcpyDeviceToHost);
        gCtx.cudaFreeHelper(instName, deviceDoubleData, DMLScript.EAGER_CUDA_FREE);
    } else {
        LOG.debug("Potential OOM: Allocated additional space on host in deviceToHost");
        FloatBuffer floatData = ByteBuffer.allocateDirect(Sizeof.FLOAT * dest.length)
                .order(ByteOrder.nativeOrder()).asFloatBuffer();
        cudaMemcpy(Pointer.to(floatData), src, ((long) dest.length) * Sizeof.FLOAT, cudaMemcpyDeviceToHost);
        LibMatrixNative.fromFloatBuffer(floatData, dest);
    }
    if (DMLScript.STATISTICS) {
        long totalTime = System.nanoTime() - t0;
        GPUStatistics.cudaFloat2DoubleTime.add(totalTime);
        GPUStatistics.cudaFloat2DoubleCount.add(1);
        if (DMLScript.FINEGRAINED_STATISTICS && instName != null)
            GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_DEVICE_TO_HOST, totalTime);
    }
}

From source file:org.basdroid.common.NetworkUtils.java

public static String getMacAddressFromNetworkInterface(final Context context) {

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();

    // Convert little-endian to big-endianif needed
    if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
        ipAddress = Integer.reverseBytes(ipAddress);
    }/*from  ww w .  j ava2  s  .c o  m*/

    byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray();

    String result;
    try {
        InetAddress addr = InetAddress.getByAddress(bytes);
        NetworkInterface netInterface = NetworkInterface.getByInetAddress(addr);
        Log.d(TAG, "Wifi netInterface.getName() = " + netInterface.getName());

        byte[] mac = netInterface.getHardwareAddress();
        if (mac == null || mac.length == 0)
            return "";
        StringBuilder buf = new StringBuilder();
        for (int idx = 0; idx < mac.length; idx++) {
            buf.append(String.format("%02X:", mac[idx]));
        }
        if (buf.length() > 0)
            buf.deleteCharAt(buf.length() - 1);
        return buf.toString();
    } catch (UnknownHostException ex) {
        Log.e(TAG, "getMacAddressFromNetworkInterface() Unknown host.", ex);
        result = null;
    } catch (SocketException ex) {
        Log.e(TAG, "getMacAddressFromNetworkInterface() Socket exception.", ex);
        result = null;
    } catch (Exception ex) {
        Log.e(TAG, "getMacAddressFromNetworkInterface() Exception.", ex);
        result = null;
    }

    return result;
}

From source file:xbird.storage.io.RemoteVarSegments.java

private byte[] recvResponse(final ByteChannel channel, final ByteBuffer rcvBuf) throws IOException {
    ByteBuffer tmpBuf = ByteBuffer.allocate(4);
    NIOUtils.readFully(channel, tmpBuf, 4);
    tmpBuf.flip();/*from w ww.ja  va  2s.c  o  m*/
    int datalen = tmpBuf.getInt();

    final ByteBuffer buf = truncateBuffer(rcvBuf, datalen);
    NIOUtils.readFully(channel, buf, datalen);
    buf.flip();
    if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
        buf.order(ByteOrder.BIG_ENDIAN);
    }
    final byte[] b = new byte[datalen];
    buf.get(b);
    if (buf != rcvBuf) {
        _rbufPool.returnObject(buf);
    }
    return b;
}

From source file:ffx.numerics.fft.Complex3DCuda.java

/**
 * {@inheritDoc}//w w  w. j a  v a 2s. c o m
 */
@Override
public void run() {
    JCudaDriver.setExceptionsEnabled(true);
    JCufft.setExceptionsEnabled(true);
    JCudaDriver.setLogLevel(LogLevel.LOG_INFO);
    JCufft.setLogLevel(LogLevel.LOG_INFO);
    JCufft.initialize();

    // Initialize the driver and create a context for the first device.
    cuInit(0);
    CUcontext pctx = new CUcontext();
    CUdevice dev = new CUdevice();
    CUdevprop prop = new CUdevprop();
    cuDeviceGetProperties(prop, dev);
    logger.log(Level.INFO, "   CUDA {0}", prop.toFormattedString());
    cuDeviceGet(dev, 0);

    // Create a context that allows the GPU to map pinned host memory.
    if (usePinnedMemory) {
        cuCtxCreate(pctx, CUctx_flags.CU_CTX_MAP_HOST, dev);
    } else {
        // Create a context that does not allows the GPU to map pinned host memory.
        cuCtxCreate(pctx, 0, dev);
    }

    // Load the CUBIN file and obtain the "recipSummation" function.
    try {
        String bit = System.getProperty("sun.arch.data.model").trim();
        URL source = getClass().getClassLoader()
                .getResource("ffx/numerics/fft/recipSummation-" + bit + ".cubin");
        File cubinFile = File.createTempFile("recipSummation", "cubin");
        FileUtils.copyURLToFile(source, cubinFile);
        module = new CUmodule();
        cuModuleLoad(module, cubinFile.getCanonicalPath());
        function = new CUfunction();
        cuModuleGetFunction(function, module, "recipSummation");
    } catch (Exception e) {
        String message = " Error loading the reciprocal summation kernel";
        logger.log(Level.SEVERE, message, e);
    }

    pinnedMemory = new Pointer();

    if (usePinnedMemory) {
        // Allocate pinned memory mapped into the GPU address space.
        cuMemHostAlloc(pinnedMemory, len * 2 * Sizeof.DOUBLE, CU_MEMHOSTALLOC_DEVICEMAP);
    } else {
        // Allocate memory
        cuMemHostAlloc(pinnedMemory, len * 2 * Sizeof.DOUBLE, 0);
    }

    ByteBuffer byteBuffer = pinnedMemory.getByteBuffer(0, len * 2 * Sizeof.DOUBLE);
    byteBuffer.order(ByteOrder.nativeOrder());
    pinnedMemoryBuffer = byteBuffer.asDoubleBuffer();

    // Allocate a work array on the device.
    dataDevice = new CUdeviceptr();
    cuMemAlloc(dataDevice, len * 2 * Sizeof.DOUBLE);

    // Allocate memory on the device for the reciprocal space array.
    recipDevice = new CUdeviceptr();
    cuMemAlloc(recipDevice, len * Sizeof.DOUBLE);

    // Create and execute a JCufft plan for the data
    plan = new cufftHandle();

    cufftPlan3d(plan, nZ, nY, nX, cufftType.CUFFT_Z2Z);
    //cufftSetCompatibilityMode(plan, cufftCompatibility.CUFFT_COMPATIBILITY_FFTW_ALL);

    dataGPUPtr = Pointer.to(dataDevice);
    recipGPUPtr = Pointer.to(recipDevice);

    int threads = prop.maxThreadsPerBlock;
    int nBlocks = len / threads + (len % threads == 0 ? 0 : 1);
    int gridSize = (int) Math.floor(Math.sqrt(nBlocks)) + 1;

    logger.info(format("   CUDA thread initialized: %d threads per block", threads));
    logger.info(format("   Grid Size:                     (%3d,%3d,%3d)", gridSize, gridSize, 1));

    assert (gridSize * gridSize * threads >= len);

    synchronized (this) {
        while (!free) {
            if (mode != null) {
                switch (mode) {

                case RECIP:
                    cuMemcpyHtoD(recipDevice, recipCPUPtr, len * Sizeof.DOUBLE);
                    break;

                case FFT:
                    // Zero Copy
                    if (usePinnedMemory) {
                        cufftExecZ2Z(plan, pinnedMemory, pinnedMemory, CUFFT_FORWARD);
                    } else {
                        cuMemcpyHtoD(dataDevice, pinnedMemory, 2 * len * Sizeof.DOUBLE);
                        cufftExecZ2Z(plan, dataDevice, dataDevice, CUFFT_FORWARD);
                        cuMemcpyDtoH(pinnedMemory, dataDevice, 2 * len * Sizeof.DOUBLE);
                    }
                    break;

                case CONVOLUTION:

                    if (usePinnedMemory) {
                        // Zero Copy
                        cufftExecZ2Z(plan, pinnedMemory, dataDevice, CUFFT_FORWARD);
                    } else {
                        // Copy data to device and run forward FFT.
                        cuMemcpyHtoD(dataDevice, pinnedMemory, 2 * len * Sizeof.DOUBLE);
                        cufftExecZ2Z(plan, dataDevice, dataDevice, CUFFT_FORWARD);
                    }

                    // Set up the execution parameters for the kernel
                    cuFuncSetBlockShape(function, threads, 1, 1);
                    int offset = 0;
                    offset = align(offset, Sizeof.POINTER);
                    cuParamSetv(function, offset, dataGPUPtr, Sizeof.POINTER);
                    offset += Sizeof.POINTER;
                    offset = align(offset, Sizeof.POINTER);
                    cuParamSetv(function, offset, recipGPUPtr, Sizeof.POINTER);
                    offset += Sizeof.POINTER;
                    offset = align(offset, Sizeof.INT);
                    cuParamSeti(function, offset, len);
                    offset += Sizeof.INT;
                    cuParamSetSize(function, offset);
                    // Call the kernel function.
                    cuLaunchGrid(function, gridSize, gridSize);
                    if (usePinnedMemory) {
                        // Zero Copy
                        cufftExecZ2Z(plan, dataDevice, pinnedMemory, CUFFT_INVERSE);
                    } else {
                        // Perform inverse FFT and copy memory back to the CPU.
                        cufftExecZ2Z(plan, dataDevice, dataDevice, CUFFT_INVERSE);
                        cuMemcpyDtoH(pinnedMemory, dataDevice, 2 * len * Sizeof.DOUBLE);
                    }
                    break;

                case IFFT:
                    // Zero Copy
                    if (usePinnedMemory) {
                        cufftExecZ2Z(plan, pinnedMemory, pinnedMemory, CUFFT_INVERSE);
                    } else {
                        cuMemcpyHtoD(dataDevice, pinnedMemory, 2 * len * Sizeof.DOUBLE);
                        cufftExecZ2Z(plan, dataDevice, dataDevice, CUFFT_INVERSE);
                        cuMemcpyDtoH(pinnedMemory, dataDevice, 2 * len * Sizeof.DOUBLE);
                    }
                    break;
                }

                // Block for the context's tasks to complete.
                cuCtxSynchronize();

                // Reset the mode to null and notify the calling thread.
                mode = null;
                notify();
            }
            // The CUDA thread will wait until it's notified again.
            try {
                wait();
            } catch (InterruptedException e) {
                logger.severe(e.toString());
            }
        }
        cufftDestroy(plan);
        cuMemFree(dataDevice);
        cuMemFree(recipDevice);
        cuMemFreeHost(pinnedMemory);
        dead = true;
        notify();
    }
    logger.info(" CUDA Thread Done!");
}

From source file:ffx.xray.MTZFilter.java

/**
 * {@inheritDoc}/*from  w  ww  . j  a  v  a 2s.com*/
 */
@Override
public boolean readFile(File mtzFile, ReflectionList reflectionlist, DiffractionRefinementData refinementdata,
        CompositeConfiguration properties) {
    int nread, nignore, nres, nfriedel, ncut;
    ByteOrder b = ByteOrder.nativeOrder();
    FileInputStream fis;
    DataInputStream dis;
    boolean transpose = false;

    StringBuilder sb = new StringBuilder();
    //sb.append(String.format("\n Opening %s\n", mtzFile.getName()));
    try {
        fis = new FileInputStream(mtzFile);
        dis = new DataInputStream(fis);

        byte headeroffset[] = new byte[4];
        byte bytes[] = new byte[80];
        int offset = 0;

        // eat "MTZ" title
        dis.read(bytes, offset, 4);
        String mtzstr = new String(bytes);

        // header offset
        dis.read(headeroffset, offset, 4);

        // machine stamp
        dis.read(bytes, offset, 4);
        ByteBuffer bb = ByteBuffer.wrap(bytes);
        int stamp = bb.order(ByteOrder.BIG_ENDIAN).getInt();
        String stampstr = Integer.toHexString(stamp);
        switch (stampstr.charAt(0)) {
        case '1':
        case '3':
            if (b.equals(ByteOrder.LITTLE_ENDIAN)) {
                b = ByteOrder.BIG_ENDIAN;
            }
            break;
        case '4':
            if (b.equals(ByteOrder.BIG_ENDIAN)) {
                b = ByteOrder.LITTLE_ENDIAN;
            }
            break;
        }

        bb = ByteBuffer.wrap(headeroffset);
        int headeroffseti = bb.order(b).getInt();

        // skip to header and parse
        dis.skipBytes((headeroffseti - 4) * 4);

        for (Boolean parsing = true; parsing; dis.read(bytes, offset, 80)) {
            mtzstr = new String(bytes);
            parsing = parseHeader(mtzstr);
        }

        // column identifiers
        foString = sigfoString = rfreeString = null;
        if (properties != null) {
            foString = properties.getString("fostring", null);
            sigfoString = properties.getString("sigfostring", null);
            rfreeString = properties.getString("rfreestring", null);
        }
        h = k = l = fo = sigfo = rfree = -1;
        fplus = sigfplus = fminus = sigfminus = rfreeplus = rfreeminus = -1;
        boolean print = true;
        parseColumns(print);

        if (h < 0 || k < 0 || l < 0) {
            String message = "Fatal error in MTZ file - no H K L indexes?\n";
            logger.log(Level.SEVERE, message);
            return false;
        }

        // reopen to start at beginning
        fis = new FileInputStream(mtzFile);
        dis = new DataInputStream(fis);

        // skip initial header
        dis.skipBytes(80);

        // check if HKLs need to be transposed or not
        float data[] = new float[nColumns];
        HKL mate = new HKL();
        int nposignore = 0;
        int ntransignore = 0;
        int nzero = 0;
        int none = 0;
        for (int i = 0; i < nReflections; i++) {
            for (int j = 0; j < nColumns; j++) {
                dis.read(bytes, offset, 4);
                bb = ByteBuffer.wrap(bytes);
                data[j] = bb.order(b).getFloat();
            }
            int ih = (int) data[h];
            int ik = (int) data[k];
            int il = (int) data[l];
            boolean friedel = reflectionlist.findSymHKL(ih, ik, il, mate, false);
            HKL hklpos = reflectionlist.getHKL(mate);
            if (hklpos == null) {
                nposignore++;
            }

            friedel = reflectionlist.findSymHKL(ih, ik, il, mate, true);
            HKL hkltrans = reflectionlist.getHKL(mate);
            if (hkltrans == null) {
                ntransignore++;
            }
            if (rfree > 0) {
                if (((int) data[rfree]) == 0) {
                    nzero++;
                } else if (((int) data[rfree]) == 1) {
                    none++;
                }
            }
            if (rfreeplus > 0) {
                if (((int) data[rfreeplus]) == 0) {
                    nzero++;
                } else if (((int) data[rfreeplus]) == 1) {
                    none++;
                }
            }
            if (rfreeminus > 0) {
                if (((int) data[rfreeminus]) == 0) {
                    nzero++;
                } else if (((int) data[rfreeminus]) == 1) {
                    none++;
                }
            }
        }
        if (nposignore > ntransignore) {
            transpose = true;
        }

        if (none > (nzero * 2) && refinementdata.rfreeflag < 0) {
            refinementdata.setFreeRFlag(0);
            sb.append(String.format(" Setting R free flag to %d based on MTZ file data.\n",
                    refinementdata.rfreeflag));
        } else if (nzero > (none * 2) && refinementdata.rfreeflag < 0) {
            refinementdata.setFreeRFlag(1);
            sb.append(String.format(" Setting R free flag to %d based on MTZ file data.\n",
                    refinementdata.rfreeflag));
        } else if (refinementdata.rfreeflag < 0) {
            refinementdata.setFreeRFlag(0);
            sb.append(String.format(" Setting R free flag to MTZ default: %d\n", refinementdata.rfreeflag));
        }

        // reopen to start at beginning
        fis = new FileInputStream(mtzFile);
        dis = new DataInputStream(fis);

        // skip initial header
        dis.skipBytes(80);

        // read in data
        double anofsigf[][] = new double[refinementdata.n][4];
        for (int i = 0; i < refinementdata.n; i++) {
            anofsigf[i][0] = anofsigf[i][1] = anofsigf[i][2] = anofsigf[i][3] = Double.NaN;
        }
        nread = nignore = nres = nfriedel = ncut = 0;
        for (int i = 0; i < nReflections; i++) {
            for (int j = 0; j < nColumns; j++) {
                dis.read(bytes, offset, 4);
                bb = ByteBuffer.wrap(bytes);
                data[j] = bb.order(b).getFloat();
            }
            int ih = (int) data[h];
            int ik = (int) data[k];
            int il = (int) data[l];
            boolean friedel = reflectionlist.findSymHKL(ih, ik, il, mate, transpose);
            HKL hkl = reflectionlist.getHKL(mate);

            if (hkl != null) {
                if (fo > 0 && sigfo > 0) {
                    if (refinementdata.fsigfcutoff > 0.0) {
                        if ((data[fo] / data[sigfo]) < refinementdata.fsigfcutoff) {
                            ncut++;
                            continue;
                        }
                    }
                    if (friedel) {
                        anofsigf[hkl.index()][2] = data[fo];
                        anofsigf[hkl.index()][3] = data[sigfo];
                        nfriedel++;
                    } else {
                        anofsigf[hkl.index()][0] = data[fo];
                        anofsigf[hkl.index()][1] = data[sigfo];
                    }
                } else {
                    if (fplus > 0 && sigfplus > 0) {
                        if (refinementdata.fsigfcutoff > 0.0) {
                            if ((data[fplus] / data[sigfplus]) < refinementdata.fsigfcutoff) {
                                ncut++;
                                continue;
                            }
                        }
                        anofsigf[hkl.index()][0] = data[fplus];
                        anofsigf[hkl.index()][1] = data[sigfplus];
                    }
                    if (fminus > 0 && sigfminus > 0) {
                        if (refinementdata.fsigfcutoff > 0.0) {
                            if ((data[fminus] / data[sigfminus]) < refinementdata.fsigfcutoff) {
                                ncut++;
                                continue;
                            }
                        }
                        anofsigf[hkl.index()][2] = data[fminus];
                        anofsigf[hkl.index()][3] = data[sigfminus];
                    }
                }
                if (rfree > 0) {
                    refinementdata.setFreeR(hkl.index(), (int) data[rfree]);
                } else {
                    if (rfreeplus > 0 && rfreeminus > 0) {
                        // not sure what the correct thing to do here is?
                        refinementdata.setFreeR(hkl.index(), (int) data[rfreeplus]);
                    } else if (rfreeplus > 0) {
                        refinementdata.setFreeR(hkl.index(), (int) data[rfreeplus]);
                    } else if (rfreeminus > 0) {
                        refinementdata.setFreeR(hkl.index(), (int) data[rfreeminus]);
                    }
                }
                nread++;
            } else {
                HKL tmp = new HKL(ih, ik, il);
                if (!reflectionlist.resolution
                        .inInverseResSqRange(Crystal.invressq(reflectionlist.crystal, tmp))) {
                    nres++;
                } else {
                    nignore++;
                }
            }
        }

        // set up fsigf from F+ and F-
        refinementdata.generate_fsigf_from_anofsigf(anofsigf);

        sb.append(String.format(" MTZ file type (machine stamp): %s\n", stampstr));
        sb.append(String.format(" HKL data is %s\n", transpose ? "transposed" : "not transposed"));
        sb.append(String.format(" HKL read in:                             %d\n", nread));
        sb.append(String.format(" HKL read as friedel mates:               %d\n", nfriedel));
        sb.append(String.format(" HKL NOT read in (too high resolution):   %d\n", nres));
        sb.append(String.format(" HKL NOT read in (not in internal list?): %d\n", nignore));
        sb.append(String.format(" HKL NOT read in (F/sigF cutoff):         %d\n", ncut));
        sb.append(
                String.format(" HKL in internal list:                    %d\n", reflectionlist.hkllist.size()));
        if (logger.isLoggable(Level.INFO)) {
            logger.info(sb.toString());
        }

        if (rfree < 0 && rfreeplus < 0 && rfreeminus < 0) {
            refinementdata.generateRFree();
        }
    } catch (EOFException eof) {
        System.out.println("EOF reached ");
        return false;
    } catch (IOException ioe) {
        System.out.println("IO Exception: " + ioe.getMessage());
        return false;
    }

    return true;
}

From source file:org.apache.sysml.runtime.matrix.data.SinglePrecisionCudaSupportFunctions.java

@Override
public void hostToDevice(GPUContext gCtx, double[] src, Pointer dest, String instName) {
    LOG.debug("Potential OOM: Allocated additional space in hostToDevice");
    // TODO: Perform conversion on GPU using double2float and float2double kernels
    long t0 = DMLScript.STATISTICS ? System.nanoTime() : 0;
    if (PERFORM_CONVERSION_ON_DEVICE) {
        Pointer deviceDoubleData = gCtx.allocate(((long) src.length) * Sizeof.DOUBLE);
        cudaMemcpy(deviceDoubleData, Pointer.to(src), ((long) src.length) * Sizeof.DOUBLE,
                cudaMemcpyHostToDevice);
        LibMatrixCUDA.double2float(gCtx, deviceDoubleData, dest, src.length);
        gCtx.cudaFreeHelper(instName, deviceDoubleData, DMLScript.EAGER_CUDA_FREE);
    } else {//w ww  .ja  v a2 s. c o  m
        FloatBuffer floatData = ByteBuffer.allocateDirect(Sizeof.FLOAT * src.length)
                .order(ByteOrder.nativeOrder()).asFloatBuffer();
        IntStream.range(0, src.length).parallel().forEach(i -> floatData.put(i, (float) src[i]));
        cudaMemcpy(dest, Pointer.to(floatData), ((long) src.length) * Sizeof.FLOAT, cudaMemcpyHostToDevice);
    }

    if (DMLScript.STATISTICS) {
        long totalTime = System.nanoTime() - t0;
        GPUStatistics.cudaDouble2FloatTime.add(totalTime);
        GPUStatistics.cudaDouble2FloatCount.add(1);
        if (DMLScript.FINEGRAINED_STATISTICS && instName != null)
            GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_HOST_TO_DEVICE, totalTime);
    }
}