Esempio n. 1
0
        /// <summary>
        /// Compiles and launches an explicltly-grouped kernel.
        /// </summary>
        static void CompileAndLaunchKernel(Accelerator accelerator, int groupSize)
        {
            // Create a backend for this device
            using (var backend = accelerator.CreateBackend())
            {
                // Create a new compile unit using the created backend
                using (var compileUnit = accelerator.Context.CreateCompileUnit(backend))
                {
                    // Resolve and compile method into a kernel
                    var method         = typeof(Program).GetMethod(nameof(GroupedKernel), BindingFlags.NonPublic | BindingFlags.Static);
                    var compiledKernel = backend.Compile(compileUnit, method);
                    // Info: use compiledKernel.GetBuffer() to retrieve the compiled kernel program data

                    // -------------------------------------------------------------------------------
                    // Load the explicitly grouped kernel
                    // Note that the kernel has to be disposed manually.
                    var kernel   = accelerator.LoadKernel(compiledKernel);
                    var launcher = kernel.CreateStreamLauncherDelegate <Action <GroupedIndex, ArrayView <int>, int> >();
                    // -------------------------------------------------------------------------------

                    using (var buffer = accelerator.Allocate <int>(1024))
                    {
                        // You can also use kernel.Launch; however, the generic launch method involves boxing.
                        launcher(
                            new GroupedIndex(
                                (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");
                            }
                        }
                    }
                    kernel.Dispose();
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Compiles and launches an auto-grouped implicitly-grouped kernel.
        /// </summary>
        static void CompileAndLaunchAutoGroupedKernel(Accelerator accelerator)
        {
            // Create a backend for this device
            using (var backend = accelerator.CreateBackend())
            {
                // Create a new compile unit using the created backend
                using (var compileUnit = accelerator.Context.CreateCompileUnit(backend))
                {
                    // Resolve and compile method into a kernel
                    var method         = typeof(Program).GetMethod(nameof(MyKernel), BindingFlags.NonPublic | BindingFlags.Static);
                    var compiledKernel = backend.Compile(compileUnit, method);
                    // Info: use compiledKernel.GetBuffer() to retrieve the compiled kernel program data

                    // -------------------------------------------------------------------------------
                    // Load the implicitly grouped kernel with an automatically determined group size.
                    // Note that the kernel has to be disposed manually.
                    var kernel   = accelerator.LoadAutoGroupedKernel(compiledKernel);
                    var launcher = kernel.CreateStreamLauncherDelegate <Action <Index, ArrayView <int>, int> >();
                    // -------------------------------------------------------------------------------

                    using (var buffer = accelerator.Allocate <int>(1024))
                    {
                        // Launch buffer.Length many threads and pass a view to buffer.
                        // You can also use kernel.Launch; however, the generic launch method involves boxing.
                        launcher(buffer.Length, buffer.View, 42);

                        // Wait for the kernel to finish...
                        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");
                            }
                        }
                    }

                    accelerator.Synchronize();

                    kernel.Dispose();
                }
            }
        }
Esempio n. 3
0
 public ParallelTranslator(Accelerator device)
 {
     _backend     = device.CreateBackend();
     _compileUnit = device.Context.CreateCompileUnit(_backend);
 }