Exemplo n.º 1
0
        public TSCudaContext(int[] deviceIds)
        {
            //try
            //{
            //    this.deviceCount = CudaContext.GetDeviceCount();
            //}
            //catch
            //{
            //    // CudaContext.GetDeviceCount() throws if CUDA drivers are not installed
            //    this.deviceCount = 0;
            //}

            this.deviceIds = deviceIds;

            devices = new DeviceState[deviceIds.Length];
            for (int i = 0; i < deviceIds.Length; i++)
            {
                devices[i] = new DeviceState(deviceIds[i]);
            }


            //     if (deviceCount > 0)
            //   {
            p2pAccess = EnablePeerAccess(devices.Select(x => x.CudaContext).ToArray(), devices[0].CudaContext);
            //}
            //else
            //{
            //    p2pAccess = new bool[0, 0];
            //}

            this.diskCache = new RuntimeCompiler.KernelDiskCache(Path.Combine(Environment.CurrentDirectory, CacheDir));
            this.compiler  = new RuntimeCompiler.CudaCompiler(diskCache);

            OpRegistry.RegisterAssembly(Assembly.GetExecutingAssembly());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TSCudaContext"/> class.
        /// </summary>
        public TSCudaContext()
        {
            try
            {
                this.deviceCount = CudaContext.GetDeviceCount();
            }
            catch
            {
                // CudaContext.GetDeviceCount() throws if CUDA drivers are not installed
                this.deviceCount = 0;
            }

            this.devices = Enumerable.Repeat(0, deviceCount)
                           .Select(x => new DeviceState(x))
                           .ToArray();

            if (deviceCount > 0)
            {
                p2pAccess = EnablePeerAccess(devices.Select(x => x.CudaContext).ToArray(), devices[0].CudaContext);
            }
            else
            {
                p2pAccess = new bool[0, 0];
            }

            this.diskCache = new RuntimeCompiler.KernelDiskCache(Path.Combine(Environment.CurrentDirectory, CacheDir));
            this.compiler  = new RuntimeCompiler.CudaCompiler(diskCache);

            OpRegistry.RegisterAssembly(Assembly.GetExecutingAssembly());
        }
Exemplo n.º 3
0
        /// <summary>
        /// PTXs for configuration.
        /// </summary>
        /// <param name="compiler">The compiler.</param>
        /// <param name="config">The configuration.</param>
        /// <returns>System.Byte[].</returns>
        /// <exception cref="InvalidOperationException">
        /// All config arguments must be provided. Required: " + allRequired
        /// or
        /// Config provides some unnecessary arguments. Required: " + allRequired
        /// </exception>
        public byte[] PtxForConfig(CudaCompiler compiler, KernelConfig config)
        {
            byte[] cachedResult;
            if (ptxCache.TryGetValue(config, out cachedResult))
            {
                return(cachedResult);
            }

            if (!requiredConfigArgs.All(config.ContainsKey))
            {
                var allRequired = string.Join(", ", requiredConfigArgs);
                throw new InvalidOperationException("All config arguments must be provided. Required: " + allRequired);
            }

            // Checking this ensures that there is only one config argument that can evaluate to the same code,
            // which ensures that the ptx cacheing does not generate unnecessary combinations. Also, a mismatch
            // occurring here probably indicates a bug somewhere else.
            if (!config.Keys.All(requiredConfigArgs.Contains))
            {
                var allRequired = string.Join(", ", requiredConfigArgs);
                throw new InvalidOperationException("Config provides some unnecessary arguments. Required: " + allRequired);
            }

            //return new DeviceKernelCode(config.ApplyToTemplate(templateCode), requiredHeaders.ToArray());
            var finalCode = config.ApplyToTemplate(templateCode);

            var result = compiler.CompileToPtx(finalCode, requiredHeaders.ToArray());

            ptxCache.Add(config, result);
            return(result);
        }
Exemplo n.º 4
0
        public TSCudaContext(int[] deviceIds, float memoryUsageRatio = 0.9f, string[] compilerOptions = null)
        {
            this.deviceIds = deviceIds;

            devices = new DeviceState[deviceIds.Length];
            for (int i = 0; i < deviceIds.Length; i++)
            {
                devices[i] = new DeviceState(deviceIds[i], memoryUsageRatio);
            }
            p2pAccess = EnablePeerAccess(devices.Select(x => x.CudaContext).ToArray(), devices[0].CudaContext);

            diskCache = new RuntimeCompiler.KernelDiskCache(Path.Combine(Environment.CurrentDirectory, CacheDir));
            compiler  = new RuntimeCompiler.CudaCompiler(diskCache, compilerOptions);

            OpRegistry.RegisterAssembly(Assembly.GetExecutingAssembly());
        }
Exemplo n.º 5
0
 public byte[] PtxForConfig(CudaCompiler compiler, KernelConfig config)
 {
     if (ptxCache.TryGetValue(config, out byte[] cachedResult))