Example #1
0
        /// <summary>
        /// Initializes the associated kernel transformers.
        /// </summary>
        /// <typeparam name="TConfiguration">The configuration type.</typeparam>
        /// <param name="configuration">The specializer configuration.</param>
        /// <param name="createTransformers">The target handler.</param>
        protected void InitializeKernelTransformers <TConfiguration>(
            TConfiguration configuration,
            Action <ImmutableArray <Transformer> .Builder> createTransformers)
            where TConfiguration : IIntrinsicSpecializerConfiguration
        {
            InitializeKernelTransformers(builder =>
            {
                // Specialize intrinsic functions
                var resolver    = new IntrinsicResolver <TDelegate>(IntrinsicProvider);
                var specializer = new IntrinsicSpecializer <TConfiguration, TDelegate>(
                    configuration,
                    IntrinsicProvider);
                var lowerThreadIntrinsics = new LowerThreadIntrinsics();

                // Perform two general passes to specialize ILGPU-specific intrinsic
                // functions that are invoked by other specialized functions.
                // TODO: determine the number of passes automatically
                const int NumPasses = 2;
                for (int i = 0; i < NumPasses; ++i)
                {
                    builder.Add(Transformer.Create(
                                    TransformerConfiguration.Transformed,
                                    lowerThreadIntrinsics, resolver, specializer));
                }

                createTransformers(builder);
            });
        }
Example #2
0
        /// <summary>
        /// Creates the final kernel transformer for PTX kernels.
        /// </summary>
        /// <param name="context">The current context.</param>
        /// <param name="abi">The current ABI.</param>
        /// <param name="builder">The target builder.</param>
        private static void CreateKernelTransformers(
            Context context,
            ABI abi,
            ImmutableArray <Transformer> .Builder builder)
        {
            var specializerConfiguration = new SpecializerConfiguration(
                context.Flags,
                abi,
                context.PTXContextData);

            builder.Add(Transformer.Create(
                            TransformerConfiguration.Empty,
                            new IntrinsicSpecializer <SpecializerConfiguration>(
                                specializerConfiguration)));

            // Append further backend specific transformations in release mode
            var transformerBuilder = Transformer.CreateBuilder(TransformerConfiguration.Empty);

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

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

            var transformations = transformerBuilder.ToTransformer();

            if (transformations.Length > 0)
            {
                builder.Add(transformerBuilder.ToTransformer());
            }
        }
Example #3
0
        public async Task Composition()
        {
            var trans = Transformer.Create <int, int>(a => a * 2);
            var print = Consumer.Create <int>(Console.WriteLine);
            var pip   = PipeLine.Create(trans);
            var final = pip.Next(print);

            await final.Consume(25);
        }
Example #4
0
            public Resolver(Context context, IRContext irContext)
                : base(irContext)
            {
                MathImplementationResolver resolver;

                using (var phase = context.BeginCodeGeneration(irContext))
                {
                    using (var frontendPhase = phase.BeginFrontendCodeGeneration())
                    {
                        resolver = new MathImplementationResolver(
                            frontendPhase,
                            mathFunction => mathFunction.GetCustomAttribute <PTXMathIntrinsicAttribute>() == null,
                            typeof(XMath), typeof(Resolver));

                        // Declare debugging functions
                        var deviceAssertFunction = frontendPhase.DeclareMethod(
                            new MethodDeclaration(
                                DebugAssertFailedName,
                                irContext.VoidType,
                                MethodFlags.External));

                        using (var failedBuilder = deviceAssertFunction.CreateBuilder())
                        {
                            failedBuilder.AddParameter(irContext.StringType, "message");
                            failedBuilder.AddParameter(irContext.StringType, "file");
                            failedBuilder.AddParameter(
                                irContext.GetPrimitiveType(BasicValueType.Int32),
                                "line");
                            failedBuilder.AddParameter(irContext.StringType, "function");
                            failedBuilder.AddParameter(
                                irContext.GetPrimitiveType(BasicValueType.Int32),
                                "charSize");
                        }

                        debugAssertFunction = frontendPhase.DeclareMethod(
                            new MethodDeclaration(
                                WrapperDebugAssertFailedName,
                                irContext.VoidType,
                                MethodFlags.Inline));
                        using (var assertBuilder = debugAssertFunction.CreateBuilder())
                        {
                            var messageParam = assertBuilder.AddParameter(irContext.StringType, "message");
                            var entryBlock   = assertBuilder.CreateEntryBlock();

                            entryBlock.CreateCall(
                                deviceAssertFunction,
                                ImmutableArray.Create(
                                    messageParam,
                                    entryBlock.CreatePrimitiveValue("Kernel.cs"),
                                    entryBlock.CreatePrimitiveValue(0),
                                    entryBlock.CreatePrimitiveValue("Kernel"),
                                    entryBlock.CreatePrimitiveValue(1)));
                            entryBlock.CreateReturn();
                        }
                    }
                }

                // Perform an initial pass to resolve all PTX-specific intrinsic functions
                var transformer = Transformer.Create(
                    new TransformerConfiguration(MethodTransformationFlags.None, false),
                    new IntrinsicSpecializer <PTXIntrinsicConfiguration>(new PTXIntrinsicConfiguration(
                                                                             this)));

                irContext.Transform(transformer);
                resolver.ApplyTo(this);

                // Resolve all previously unresolved intrinsic functions

                /* This is required since the intrinsic implementation resolver does not perform
                 * time-consuming recursive dependency analyses. This is not an issue since
                 * these analyses and recursive passes are not required for default user kernels.
                 * Unfortunately, we need a second pass at this point to ensure that all intrinsic
                 * function declarations have been properly resolved. */
                irContext.Transform(transformer);

                // Optimize the whole module
                irContext.Optimize();
            }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
                                        JsonSerializer serializer)
        {
            reader.Read();
            if (reader.TokenType != JsonToken.PropertyName || (string)reader.Value != "type")
            {
                throw new ArgumentException("Expected property 'type' not found");
            }
            reader.Read();
            if (reader.TokenType != JsonToken.String && (string)reader.Value != "Topology")
            {
                throw new ArgumentException("Expected value 'Topology' not found");
            }

            IDictionary <string, TopoObject> dict = null;
            ITransform transform = null;

            double[][][] arcs = null;
            while (reader.Read())
            {
                if (reader.TokenType != JsonToken.PropertyName)
                {
                    throw new ArgumentException("Expected a property but found " + reader.TokenType);
                }

                string propertyName = (string)reader.Value;
                switch (propertyName)
                {
                case "objects":
                    reader.Read();     // start object
                    dict = serializer.Deserialize <IDictionary <string, TopoObject> >(reader);
                    break;

                case "transform":
                    reader.Read();     // start object
                    transform = serializer.Deserialize <ITransform>(reader);
                    break;

                case "arcs":
                    reader.Read();     // start array
                    arcs = serializer.Deserialize <double[][][]>(reader);
                    reader.Read();     // end array
                    break;

                default:
                    throw new ArgumentOutOfRangeException("unhandled property: " + propertyName);
                }
            }

            // nothing to do
            if (dict == null)
            {
                return(null);
            }

            if (arcs == null)
            {
                throw new ArgumentException("arcs should be not null");
            }
            transform = transform ?? new Transform(); // use a "null" transform as default
            ITransformer transformer = new Transformer(transform, arcs, _factory);

            IDictionary <string, FeatureCollection> result = new Dictionary <string, FeatureCollection>();

            foreach (string key in dict.Keys)
            {
                FeatureCollection coll = transformer.Create(dict[key]);
                result.Add(key, coll);
            }
            return(result);
        }