Example usage for java.nio FloatBuffer put

List of usage examples for java.nio FloatBuffer put

Introduction

In this page you can find the example usage for java.nio FloatBuffer put.

Prototype

public abstract FloatBuffer put(int index, float f);

Source Link

Document

Writes a float to the specified index of this buffer; the position is not changed.

Usage

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 {//from  w  ww.  j  a  v a  2s .  co 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);
    }
}

From source file:GeometryByReferenceNIOBuffer.java

public void updateData(Geometry geometry) {
    int i;/*  ww  w  .  j a v a 2 s  . co m*/
    float val;
    float val1;
    if (updateIndex == 1) { // geometry
        // Translate the geometry by a small amount in x
        vertexCount++;
        if ((vertexCount & 1) == 1)
            val = 0.2f;
        else
            val = -0.2f;

        FloatBuffer indexedCoord = (FloatBuffer) indexedFloatBufferCoord.getBuffer();
        indexedCoord.rewind();
        FloatBuffer coord = (FloatBuffer) floatBufferCoord.getBuffer();
        coord.rewind();

        if (vertexIndex == 0) {
            // Do Indexed geometry
            for (i = 0; i < indexedCoord.limit(); i += 3) {
                val1 = indexedCoord.get(i);
                indexedCoord.put(i, val1 + val);
            }
            // Do non-indexed float geometry
            for (i = 0; i < coord.limit(); i += 3) {
                val1 = coord.get(i);
                coord.put(i, val1 + val);
            }
        }
    } else if (updateIndex == 2) { // colors
        colorCount++;
        if ((colorCount & 1) == 1)
            val = 0.4f;
        else
            val = -0.4f;

        FloatBuffer indexedColors = (FloatBuffer) indexedFloatBufferColor.getBuffer();
        indexedColors.rewind();
        FloatBuffer colors = (FloatBuffer) floatBufferColor.getBuffer();
        colors.rewind();

        if (colorIndex == 0) {
            // Do Indexed geometry
            for (i = 0; i < indexedColors.limit(); i += 3) {
                indexedColors.put(i, indexedColors.get(i) + val);
            }
            // Do non-indexed float geometry
            for (i = 0; i < colors.limit(); i += 3) {
                colors.put(i, colors.get(i) + val);
            }
        }

    }

}

From source file:org.deegree.tools.rendering.manager.buildings.PrototypeManager.java

/**
 * @param rqm//from ww  w  .j a v a  2 s  .c  om
 */
private RenderableQualityModel createScaledQualityModel(RenderableQualityModel rqm) {
    float minX = Float.MAX_VALUE;
    float minY = Float.MAX_VALUE;
    float minZ = Float.MAX_VALUE;
    float maxX = Float.MIN_VALUE;
    float maxY = Float.MIN_VALUE;
    float maxZ = Float.MIN_VALUE;
    ArrayList<RenderableQualityModelPart> qualityModelParts = rqm.getQualityModelParts();
    for (RenderableQualityModelPart rqmp : qualityModelParts) {
        if (rqmp != null) {
            RenderableGeometry geom = (RenderableGeometry) rqmp;
            FloatBuffer fb = geom.getCoordBuffer();
            fb.rewind();
            int position = fb.position();
            while (position < fb.capacity()) {
                float x = fb.get();
                float y = fb.get();
                float z = fb.get();

                minX = Math.min(minX, x);
                minY = Math.min(minY, y);
                minZ = Math.min(minZ, z);

                maxX = Math.max(maxX, x);
                maxY = Math.max(maxY, y);
                maxZ = Math.max(maxZ, z);
                position = fb.position();
            }
        }
    }

    double scaleX = 1d / (maxX - minX);
    double scaleY = 1d / (maxY - minY);
    double scaleZ = 1d / (maxZ - minZ);

    for (RenderableQualityModelPart rqmp : qualityModelParts) {
        if (rqmp != null) {
            RenderableGeometry geom = (RenderableGeometry) rqmp;
            FloatBuffer fb = geom.getCoordBuffer();
            fb.rewind();
            int i = 0;
            while ((i + 3) <= fb.capacity()) {
                float x = fb.get(i);
                x -= minX;
                x *= scaleX;
                fb.put(i, x);

                float y = fb.get(i + 1);
                y -= minY;
                y *= scaleY;
                fb.put(i + 1, y);

                float z = fb.get(i + 2);
                z -= minZ;
                z *= scaleZ;
                fb.put(i + 2, z);
                i += 3;
            }
        }
    }
    return rqm;

}