Skip to content

Feature request: add support of runtime compiled parts of kernel  #415

@mikhail-khalizev

Description

@mikhail-khalizev

Faced the following problem. There is a common kernel logic. And there are additional methods for transformation numbers that are generated dynamically before starting the kernel (as in the example below, which fails).

I propose to add support for executing Action and Func through a mechanism like SpecializedValue.

using ILGPU;
using ILGPU.Algorithms;
using ILGPU.Runtime;
using System;
using System.Linq;
using System.Linq.Expressions;

namespace SimpleKernel
{
    public class Program
    {
        private static Func<int, int> _transform;

        static unsafe void MyKernel(ArrayView<int> dataView)
        {
            if (Grid.GlobalIndex.X == 0)
                dataView[0] = _transform(10);
        }

        static unsafe void Main()
        {
            var arg = Expression.Parameter(typeof(int), "arg");

            // Runtime generated transformation of number.
            _transform = Expression.Lambda<Func<int, int>>(
                Expression.Add(
                    arg,
                    Expression.Constant(1)),
                arg).Compile();


            using var context = new Context();
            
            foreach (var acceleratorId in Accelerator.Accelerators)
            {
                using var accelerator = Accelerator.Create(context, acceleratorId);

                Console.WriteLine($"Performing operations on {accelerator}");
                        
                var kernel = accelerator.LoadStreamKernel<ArrayView<int>>(MyKernel);
                var config = accelerator.ComputeGridStrideLoopExtent(1);

                using (var buffer = accelerator.Allocate<int>(1))
                {
                    buffer.MemSetToZero();

                    kernel(config, buffer);
                    accelerator.Synchronize();
                    var data = buffer.GetAsArray()[0];
                            
                    Console.WriteLine($"Data: {data}");
                }

                Console.WriteLine();
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    difficulty:advancedA task that requires advanced knowledgedifficulty:intermediateA task with intermediate difficultyfeatureA new feature (or feature request)

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions