예제 #1
0
        /// <summary>
        /// Constructs a new Cuda backend.
        /// </summary>
        /// <param name="context">The context to use.</param>
        /// <param name="architecture">The target GPU architecture.</param>
        /// <param name="instructionSet">The target GPU instruction set.</param>
        public PTXBackend(
            Context context,
            PTXArchitecture architecture,
            PTXInstructionSet instructionSet)
            : base(
                context,
                BackendType.PTX,
                BackendFlags.None,
                new PTXArgumentMapper(context))
        {
            Architecture   = architecture;
            InstructionSet = instructionSet;

            InitializeKernelTransformers(
                Context.HasFlags(ContextFlags.EnableAssertions) ?
                IntrinsicSpecializerFlags.EnableAssertions :
                IntrinsicSpecializerFlags.None,
                builder =>
            {
                var transformerBuilder = Transformer.CreateBuilder(
                    TransformerConfiguration.Empty);
                transformerBuilder.AddBackendOptimizations(
                    new PTXAcceleratorSpecializer(),
                    context.OptimizationLevel);
                builder.Add(transformerBuilder.ToTransformer());
            });
        }
예제 #2
0
        /// <summary>
        /// Returns the PTX instruction set to use, based on the PTX architecture and
        /// installed CUDA drivers.
        /// </summary>
        /// <param name="architecture">The PTX architecture</param>
        /// <param name="installedDriverVersion">The CUDA driver version</param>
        /// <returns>The PTX instruction set</returns>
        public static PTXInstructionSet GetInstructionSet(
            PTXArchitecture architecture,
            CudaDriverVersion installedDriverVersion)
        {
            var architectureMinDriverVersion = CudaDriverVersionUtils
                                               .GetMinimumDriverVersion(architecture);
            var minDriverVersion = architectureMinDriverVersion;

            foreach (var instructionSet in PTXCodeGenerator.SupportedInstructionSets)
            {
                var instructionSetMinDriverVersion = CudaDriverVersionUtils.
                                                     GetMinimumDriverVersion(instructionSet);
                minDriverVersion =
                    architectureMinDriverVersion >= instructionSetMinDriverVersion
                    ? architectureMinDriverVersion
                    : instructionSetMinDriverVersion;
                if (installedDriverVersion >= minDriverVersion)
                {
                    return(instructionSet);
                }
            }

            throw new NotSupportedException(
                      string.Format(
                          RuntimeErrorMessages.NotSupportedDriverVersion,
                          installedDriverVersion,
                          minDriverVersion));
        }
예제 #3
0
 /// <summary>
 /// Resolves a PTX code generator for the given math-function configuration.
 /// </summary>
 /// <param name="minArchitecture">The target/minimum architecture.</param>
 /// <returns>The resolved intrinsic representation.</returns>
 private static PTXIntrinsic GetMathCodeGeneratorIntrinsic(
     PTXArchitecture minArchitecture) =>
 new PTXIntrinsic(
     PTXMathType,
     nameof(PTXMath.GenerateMathIntrinsic),
     IntrinsicImplementationMode.GenerateCode,
     minArchitecture);
예제 #4
0
 /// <summary>
 /// Constructs a new PTX intrinsic that can handle all architectures.
 /// </summary>
 /// <param name="handlerType">The associated target handler type.</param>
 /// <param name="mode">The code-generation mode.</param>
 /// <param name="architecture">The target architecture (if any).</param>
 public PTXIntrinsic(
     Type handlerType,
     IntrinsicImplementationMode mode,
     PTXArchitecture architecture)
     : this(handlerType, mode)
 {
     MinArchitecture = architecture;
 }
예제 #5
0
 /// <summary>
 /// Resolves the minimum CUDA driver version for the PTX architecture
 /// </summary>
 /// <param name="architecture">The PTX architecture</param>
 /// <returns>The minimum driver version</returns>
 public static CudaDriverVersion GetMinimumDriverVersion(PTXArchitecture architecture)
 {
     if (!ArchitectureLookup.TryGetValue(architecture, out var result))
     {
         throw new NotSupportedException(RuntimeErrorMessages.NotSupportedPTXArchitecture);
     }
     return(result);
 }
예제 #6
0
        /// <summary>
        /// Resolves the PTX architecture for the given major and minor versions.
        /// </summary>
        /// <param name="major">The major version.</param>
        /// <param name="minor">The minor version.</param>
        /// <returns>The resolved PTX version.</returns>
        public static PTXArchitecture GetArchitecture(int major, int minor)
        {
            var architecture = new PTXArchitecture(major, minor);

            return(architecture >= PTXArchitecture.SM_30
            ? architecture
            : throw new NotSupportedException(
                       RuntimeErrorMessages.NotSupportedPTXArchitecture));
        }
예제 #7
0
 /// <summary>
 /// Creates a new PTX intrinsic.
 /// </summary>
 /// <param name="name">The name of the intrinsic.</param>
 /// <param name="mode">The implementation mode.</param>
 /// <param name="minArchitecture">The minimum architecture.</param>
 /// <param name="maxArchitecture">The maximum architecture.</param>
 /// <returns>The created intrinsic.</returns>
 private static PTXIntrinsic CreateIntrinsic(
     string name,
     IntrinsicImplementationMode mode,
     PTXArchitecture?minArchitecture,
     PTXArchitecture maxArchitecture) =>
 new PTXIntrinsic(
     PTXIntrinsicsType,
     name,
     mode,
     minArchitecture,
     maxArchitecture);
예제 #8
0
 /// <summary>
 /// Constructs a new Cuda backend.
 /// </summary>
 /// <param name="context">The context to use.</param>
 /// <param name="architecture">The target gpu architecture.</param>
 /// <param name="platform">The target platform.</param>
 public PTXBackend(
     Context context,
     PTXArchitecture architecture,
     TargetPlatform platform)
     : base(
         context,
         new PTXABI(context.TypeContext, platform),
         new PTXArgumentMapper(context),
         CreateKernelTransformers)
 {
     Architecture = architecture;
 }
예제 #9
0
 /// <summary>
 /// Constructs a new PTX intrinsic.
 /// </summary>
 /// <param name="handlerType">The associated target handler type.</param>
 /// <param name="methodName">The target method name (or null).</param>
 /// <param name="mode">The code-generator mode.</param>
 /// <param name="architecture">The target architecture (if any).</param>
 public PTXIntrinsic(
     Type handlerType,
     string methodName,
     IntrinsicImplementationMode mode,
     PTXArchitecture architecture)
     : base(
         BackendType.PTX,
         handlerType,
         methodName,
         mode)
 {
     MinArchitecture = architecture;
 }
예제 #10
0
        /// <summary>
        /// Resolves the minimum CUDA driver version for the PTX architecture
        /// </summary>
        /// <param name="architecture">The PTX architecture</param>
        /// <returns>The minimum driver version</returns>
        public static CudaDriverVersion GetMinimumDriverVersion(
            PTXArchitecture architecture)
        {
            if (ArchitectureLookup.TryGetValue(architecture, out var result))
            {
                return(result);
            }

            // If the architecture is unknown, return the highest driver version that
            // we support. The user should already have a driver version higher than
            // this, because they are most likely using a brand new graphics card.
            return(ArchitectureLookup.OrderByDescending(x => x.Key).First().Value);
        }
예제 #11
0
 public GeneratorArgs(
     EntryPoint entryPoint,
     PTXDebugInfoGenerator debugInfoGenerator,
     StringBuilder stringBuilder,
     PTXArchitecture architecture,
     ABI abi,
     ContextFlags contextFlags)
 {
     EntryPoint         = entryPoint;
     DebugInfoGenerator = debugInfoGenerator;
     StringBuilder      = stringBuilder;
     Architecture       = architecture;
     ABI          = abi;
     ContextFlags = contextFlags;
 }
예제 #12
0
        /// <summary>
        /// Constructs a new Cuda backend.
        /// </summary>
        /// <param name="context">The context to use.</param>
        /// <param name="architecture">The target gpu architecture.</param>
        /// <param name="instructionSet">The target gpu instruction set.</param>
        /// <param name="platform">The target platform.</param>
        public PTXBackend(
            Context context,
            PTXArchitecture architecture,
            PTXInstructionSet instructionSet,
            TargetPlatform platform)
            : base(
                context,
                BackendType.PTX,
                BackendFlags.RequiresIntrinsicImplementations,
                new PTXABI(context.TypeContext, platform),
                _ => new PTXArgumentMapper(context))
        {
            Architecture   = architecture;
            InstructionSet = instructionSet;

            InitializeKernelTransformers(
                new IntrinsicSpecializerConfiguration(context.Flags),
                builder =>
            {
                // Append further backend specific transformations in release mode
                var transformerBuilder = Transformer.CreateBuilder(TransformerConfiguration.Empty);

                if (context.OptimizationLevel == OptimizationLevel.Release)
                {
                    var acceleratorConfiguration = new AcceleratorSpecializerConfiguration(ABI);
                    transformerBuilder.Add(
                        new AcceleratorSpecializer <AcceleratorSpecializerConfiguration>(
                            acceleratorConfiguration));
                }

                if (!context.HasFlags(ContextFlags.NoInlining))
                {
                    transformerBuilder.Add(new Inliner());
                }

                if (context.OptimizationLevel == OptimizationLevel.Release)
                {
                    transformerBuilder.Add(new SimplifyControlFlow());
                }

                var transformer = transformerBuilder.ToTransformer();
                if (transformer.Length > 0)
                {
                    builder.Add(transformer);
                }
            });
        }
예제 #13
0
        /// <summary>
        /// Constructs a new Cuda backend.
        /// </summary>
        /// <param name="context">The context to use.</param>
        /// <param name="capabilities">The supported capabilities.</param>
        /// <param name="architecture">The target GPU architecture.</param>
        /// <param name="instructionSet">The target GPU instruction set.</param>
        public PTXBackend(
            Context context,
            CudaCapabilityContext capabilities,
            PTXArchitecture architecture,
            PTXInstructionSet instructionSet)
            : base(
                context,
                capabilities,
                BackendType.PTX,
                BackendFlags.None,
                new PTXArgumentMapper(context))
        {
            Architecture   = architecture;
            InstructionSet = instructionSet;

            InitIntrinsicProvider();
            InitializeKernelTransformers(
                Context.HasFlags(ContextFlags.EnableAssertions) ?
                IntrinsicSpecializerFlags.EnableAssertions :
                IntrinsicSpecializerFlags.None,
                builder =>
            {
                var transformerBuilder = Transformer.CreateBuilder(
                    TransformerConfiguration.Empty);
                transformerBuilder.AddBackendOptimizations(
                    new PTXAcceleratorSpecializer(PointerType),
                    context.Flags,
                    context.OptimizationLevel);

                if (Context.HasFlags(ContextFlags.EnhancedPTXBackendFeatures))
                {
                    // Create an optimized PTX assembler block schedule
                    transformerBuilder.Add(new PTXBlockScheduling());
                    transformerBuilder.Add(new DeadCodeElimination());
                }

                builder.Add(transformerBuilder.ToTransformer());
            });
        }
예제 #14
0
        /// <summary>
        /// Constructs a new Cuda backend.
        /// </summary>
        /// <param name="context">The context to use.</param>
        /// <param name="architecture">The target GPU architecture.</param>
        /// <param name="instructionSet">The target GPU instruction set.</param>
        public PTXBackend(
            Context context,
            PTXArchitecture architecture,
            PTXInstructionSet instructionSet)
            : base(
                context,
                BackendType.PTX,
                BackendFlags.None,
                new PTXArgumentMapper(context))
        {
            Architecture   = architecture;
            InstructionSet = instructionSet;

            InitializeKernelTransformers(
                Context.HasFlags(ContextFlags.EnableAssertions) ?
                IntrinsicSpecializerFlags.EnableAssertions :
                IntrinsicSpecializerFlags.None,
                builder =>
            {
                var transformerBuilder = Transformer.CreateBuilder(
                    TransformerConfiguration.Empty);
                transformerBuilder.AddBackendOptimizations(
                    new PTXAcceleratorSpecializer(),
                    context.OptimizationLevel);

                // Append further backend specific transformations in release mode
                if (!context.HasFlags(ContextFlags.NoInlining))
                {
                    transformerBuilder.Add(new Inliner());
                }
                if (context.OptimizationLevel > OptimizationLevel.O0)
                {
                    transformerBuilder.Add(new SimplifyControlFlow());
                }

                builder.Add(transformerBuilder.ToTransformer());
            });
        }