/// <summary> /// Executes the specified kernel with the given arguments. /// </summary> /// <typeparam name="TIndex">The index type.</typeparam> /// <param name="kernel">The kernel method.</param> /// <param name="dimension">The dimension.</param> /// <param name="arguments">The arguments.</param> public void Execute <TIndex>( MethodInfo kernel, TIndex dimension, params object[] arguments) where TIndex : struct, IIndex { using var stream = Accelerator.CreateStream(); // Compile kernel manually and load the compiled kernel into the accelerator var backend = Accelerator.Backend; Output.WriteLine($"Compiling '{kernel.Name}'"); var entryPoint = typeof(TIndex) == typeof(KernelConfig) ? EntryPointDescription.FromExplicitlyGroupedKernel(kernel) : EntryPointDescription.FromImplicitlyGroupedKernel(kernel); var compiled = backend.Compile(entryPoint, new KernelSpecialization()); // Load the compiled kernel Output.WriteLine($"Loading '{kernel.Name}'"); using var acceleratorKernel = Accelerator.LoadKernel(compiled); // Launch the kernel Output.WriteLine($"Launching '{kernel.Name}'"); acceleratorKernel.Launch(stream, dimension, arguments); stream.Synchronize(); }
/// <summary> /// Compiles and launches an explicitly grouped kernel. /// </summary> static void CompileAndLaunchKernel(Accelerator accelerator, int groupSize) { // Access the current backend for this device var backend = accelerator.Backend; // Resolve and compile method into a kernel var method = typeof(Program).GetMethod(nameof(GroupedKernel), BindingFlags.NonPublic | BindingFlags.Static); var entryPointDesc = EntryPointDescription.FromExplicitlyGroupedKernel(method); var compiledKernel = backend.Compile(entryPointDesc, default); // Info: If the current accelerator is a CudaAccelerator, we can cast the compiled kernel to a // PTXCompiledKernel in order to extract the PTX assembly code. // ------------------------------------------------------------------------------- // Load the explicitly grouped kernel // Note that the kernel has to be disposed manually. using (var kernel = accelerator.LoadKernel(compiledKernel)) { var launcher = kernel.CreateLauncherDelegate <Action <AcceleratorStream, KernelConfig, ArrayView <int>, int> >(); // ------------------------------------------------------------------------------- using (var buffer = accelerator.Allocate <int>(1024)) { // You can also use kernel.Launch; however, the generic launch method involves boxing. launcher( accelerator.DefaultStream, ((buffer.Length + groupSize - 1) / groupSize, // Compute the number of groups (round up) groupSize), // Use the given group size buffer.View, 42); accelerator.Synchronize(); // Resolve and verify data var data = buffer.GetAsArray(); for (int i = 0, e = data.Length; i < e; ++i) { if (data[i] != 42 + i) { Console.WriteLine($"Error at element location {i}: {data[i]} found"); } } } } }