Ejemplo n.º 1
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());
        }
Ejemplo n.º 2
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);
        }