Пример #1
0
        // Use the CuFFT wrapper to perform a forward transform.
        static void DoForwardPlan(
            CudaAccelerator accelerator,
            CuFFT cufft,
            Complex[] input,
            out Complex[] output)
        {
            using var stream       = accelerator.CreateStream() as CudaStream;
            using var inputBuffer  = accelerator.Allocate1D(input);
            using var outputBuffer = accelerator.Allocate1D <Complex>(input.Length);

            CuFFTException.ThrowIfFailed(
                cufft.Plan1D(
                    out var plan,
                    input.Length,
                    CuFFTType.CUFFT_Z2Z,
                    batch: 1));
            using (plan)
            {
                plan.SetStream(stream);
                CuFFTException.ThrowIfFailed(
                    plan.ExecZ2Z(
                        inputBuffer.View.BaseView,
                        outputBuffer.View.BaseView,
                        CuFFTDirection.FORWARD));

                output = outputBuffer.GetAsArray1D(stream);
            }
            WorkaroundKnownIssue(accelerator, cufft.API);

            Console.WriteLine("Output Values:");
            for (var i = 0; i < output.Length; i++)
            {
                Console.WriteLine($"  [{i}] = {output[i]}");
            }
        }
Пример #2
0
        // Use the low-level CuFFT API to perform an inverse transform.
        static void DoInversePlan(
            CudaAccelerator accelerator,
            CuFFTAPI api,
            Complex[] input,
            out Complex[] output)
        {
            using var stream       = accelerator.CreateStream() as CudaStream;
            using var inputBuffer  = accelerator.Allocate1D(input);
            using var outputBuffer = accelerator.Allocate1D <Complex>(input.Length);

            CuFFTException.ThrowIfFailed(
                api.Plan1D(
                    out var plan,
                    input.Length,
                    CuFFTType.CUFFT_Z2Z,
                    batch: 1));
            try
            {
                CuFFTException.ThrowIfFailed(
                    api.SetStream(plan, stream));
                CuFFTException.ThrowIfFailed(
                    api.ExecZ2Z(
                        plan,
                        inputBuffer.View.BaseView,
                        outputBuffer.View.BaseView,
                        CuFFTDirection.INVERSE));

                output = outputBuffer.GetAsArray1D(stream);
            }
            finally
            {
                CuFFTException.ThrowIfFailed(
                    api.Destroy(plan));
            }
            WorkaroundKnownIssue(accelerator, api);

            // Scale the output to obtain the inverse.
            for (var i = 0; i < output.Length; i++)
            {
                output[i] /= output.Length;
            }

            Console.WriteLine("Inverse Values:");
            for (var i = 0; i < output.Length; i++)
            {
                Console.WriteLine($"  [{i}] = {output[i].Real}");
            }
        }