Example usage for org.apache.commons.pool2.impl GenericObjectPoolConfig clone

List of usage examples for org.apache.commons.pool2.impl GenericObjectPoolConfig clone

Introduction

In this page you can find the example usage for org.apache.commons.pool2.impl GenericObjectPoolConfig clone.

Prototype

@Override
    public GenericObjectPoolConfig clone() 

Source Link

Usage

From source file:org.nd4j.linalg.jcublas.context.ContextHolder.java

/**
 * Configure the given information/*from  w  ww  .  j av  a  2s .c o m*/
 * based on the device
 */
public void configure() {
    if (confCalled)
        return;

    JCublas2.setExceptionsEnabled(true);
    JCudaDriver.setExceptionsEnabled(true);
    JCuda.setExceptionsEnabled(true);

    if (deviceSetup.get())
        return;

    JCudaDriver.cuInit(0);
    int count[] = new int[1];
    cuDeviceGetCount(count);
    numDevices = count[0];
    log.debug("Found " + numDevices + " gpus");

    if (numDevices < 1)
        numDevices = 1;
    bannedDevices = new ArrayList<>();

    String props = System.getProperty(DEVICES_TO_BAN, "-1");
    String[] split = props.split(",");
    //Should only be used in multi device scenarios; otherwise always use one device
    if (split.length >= 1)
        for (String s : split) {
            Integer i = Integer.parseInt(s);
            if (i >= 0)
                bannedDevices.add(Integer.parseInt(s));

        }

    deviceSetup.set(true);
    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();

    for (int i = 0; i < numDevices; i++) {
        for (Thread thread : threadSet)
            getContext(i, thread.getName());
    }

    setContext();

    try {
        KernelFunctionLoader.getInstance().load();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    // Check if the device supports mapped host memory
    cudaDeviceProp deviceProperties = new cudaDeviceProp();
    JCuda.cudaGetDeviceProperties(deviceProperties, 0);
    if (deviceProperties.canMapHostMemory == 0) {
        System.err.println("This device can not map host memory");
        System.err.println(deviceProperties.toFormattedString());
        return;
    }

    //force certain ops to have a certain number of threads
    Properties threadProps = new Properties();
    try {
        InputStream is = ContextHolder.class.getResourceAsStream("/function_threads.properties");
        threadProps.load(is);
    } catch (IOException e) {
        e.printStackTrace();
    }

    for (String prop : threadProps.stringPropertyNames()) {
        threads.put(prop, Integer.parseInt(threadProps.getProperty(prop)));
    }

    try {
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        config.setJmxEnabled(true);
        config.setBlockWhenExhausted(false);
        config.setMaxIdle(Runtime.getRuntime().availableProcessors());
        config.setMaxTotal(Runtime.getRuntime().availableProcessors());
        config.setMinIdle(Runtime.getRuntime().availableProcessors());
        config.setJmxNameBase("handles");
        handlePool = new CublasHandlePool(new CublasHandlePooledItemFactory(), config);
        GenericObjectPoolConfig confClone = config.clone();
        confClone.setMaxTotal(Runtime.getRuntime().availableProcessors() * 10);
        confClone.setMaxIdle(Runtime.getRuntime().availableProcessors() * 10);
        confClone.setMinIdle(Runtime.getRuntime().availableProcessors() * 10);
        GenericObjectPoolConfig streamConf = confClone.clone();
        streamConf.setJmxNameBase("streams");
        streamPool = new StreamPool(new StreamItemFactory(), streamConf);
        GenericObjectPoolConfig oldStreamConf = streamConf.clone();
        oldStreamConf.setJmxNameBase("oldstream");
        oldStreamPool = new OldStreamPool(new OldStreamItemFactory(), oldStreamConf);
        setContext();
        //seed with multiple streams to encourage parallelism
        for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
            streamPool.addObject();
            oldStreamPool.addObject();
        }

        //force context initialization to occur
        JCuda.cudaFree(Pointer.to(new int[] { 0 }));

    } catch (Exception e) {
        log.warn("Unable to initialize cuda", e);
    }

    for (int i = 0; i < numDevices; i++) {
        ClassPathResource confFile = new ClassPathResource("devices/" + i,
                ContextHolder.class.getClassLoader());
        if (confFile.exists()) {
            Properties props2 = new Properties();
            try {
                props2.load(confFile.getInputStream());
                confs.put(i, new DeviceConfiguration(i, props2));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        } else
            confs.put(i, new DeviceConfiguration(i));

    }

    confCalled = true;
}