<commit_before>package org.nd4j.jita.allocator.context;

import jcuda.driver.CUcontext;
import jcuda.driver.CUdevice;
import jcuda.driver.CUresult;
import jcuda.driver.JCudaDriver;
import jcuda.jcublas.JCublas2;
import jcuda.jcublas.cublasHandle;
import jcuda.runtime.JCuda;
import jcuda.runtime.cudaStream_t;
import org.apache.commons.lang3.RandomUtils;
import org.nd4j.linalg.jcublas.context.CudaContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Semaphore;

import static jcuda.driver.JCudaDriver.cuCtxCreate;
import static jcuda.driver.JCudaDriver.cuDeviceGet;

/**
 * This is context pool implementation, addressing shared cublas allocations together with shared stream pools
 *
 * Each context given contains:
 * 1. Stream for custom kernel invocations.
 * 2. cuBLAS handle tied with separate stream.
 *
 * @author <EMAIL>
 */
public class ContextPool {
    // TODO: number of max threads should be device-dependant
    private static final int MAX_STREAMS_PER_DEVICE = 1;

    private volatile Map<Integer, CUcontext> cuPool = new ConcurrentHashMap<>();

    private volatile Map<Integer, cublasHandle> cublasPool = new ConcurrentHashMap<>();

    private volatile Map<Long, CudaContext> contextsPool = new ConcurrentHashMap<>();

    private volatile Map<Integer, Map<Integer, CudaContext>> contextsForDevices = new ConcurrentHashMap<>();

    private Semaphore lock = new Semaphore(1);<commit_msg>default num streams to 4<commit_after>package org.nd4j.jita.allocator.context;

import jcuda.driver.CUcontext;
import jcuda.driver.CUdevice;
import jcuda.driver.CUresult;
import jcuda.driver.JCudaDriver;
import jcuda.jcublas.JCublas2;
import jcuda.jcublas.cublasHandle;
import jcuda.runtime.JCuda;
import jcuda.runtime.cudaStream_t;
import org.apache.commons.lang3.RandomUtils;
import org.nd4j.linalg.jcublas.context.CudaContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Semaphore;

import static jcuda.driver.JCudaDriver.cuCtxCreate;
import static jcuda.driver.JCudaDriver.cuDeviceGet;

/**
 * This is context pool implementation, addressing shared cublas allocations together with shared stream pools
 *
 * Each context given contains:
 * 1. Stream for custom kernel invocations.
 * 2. cuBLAS handle tied with separate stream.
 *
 * @author <EMAIL>
 */
public class ContextPool {
    // TODO: number of max threads should be device-dependant
    private static final int MAX_STREAMS_PER_DEVICE = 4;

    private volatile Map<Integer, CUcontext> cuPool = new ConcurrentHashMap<>();

    private volatile Map<Integer, cublasHandle> cublasPool = new ConcurrentHashMap<>();

    private volatile Map<Long, CudaContext> contextsPool = new ConcurrentHashMap<>();

    private volatile Map<Integer, Map<Integer, CudaContext>> contextsForDevices = new ConcurrentHashMap<>();

    private Semaphore lock = new Semaphore(1);
{'fraction_non_alphanumeric': 0.09117743522466383, 'fraction_numerical': 0.006887504099704822, 'mean_word_length': 5.3146997929606625, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2776063', 'n_tokens_mistral': 962, 'n_tokens_neox': 938, 'n_words': 247}