|
| 1 | +#include <cuda_runtime.h> |
| 2 | + |
| 3 | +#include "fri/fri.cuh" |
| 4 | + |
| 5 | +#include "fields/field.cuh" |
| 6 | +#include "gpu-utils/error_handler.cuh" |
| 7 | +#include "gpu-utils/device_context.cuh" |
| 8 | + |
| 9 | +namespace fri { |
| 10 | + |
| 11 | + namespace { |
| 12 | + template <typename S, typename E> |
| 13 | + __device__ void ibutterfly(E& v0, E& v1, const S& itwid) |
| 14 | + { |
| 15 | + E tmp = v0; |
| 16 | + v0 = tmp + v1; |
| 17 | + v1 = (tmp - v1) * itwid; |
| 18 | + } |
| 19 | + |
| 20 | + template <typename S, typename E> |
| 21 | + __global__ void fold_line_kernel(E* eval, S* domain_xs, E alpha, E* folded_eval, uint64_t n) |
| 22 | + { |
| 23 | + unsigned idx = blockIdx.x * blockDim.x + threadIdx.x; |
| 24 | + if (idx % 2 == 0 && idx < n) { |
| 25 | + E f_x = eval[idx]; // even |
| 26 | + E f_x_neg = eval[idx + 1]; // odd |
| 27 | + S x_domain = domain_xs[idx / 2]; |
| 28 | + ibutterfly(f_x, f_x_neg, S::inverse(x_domain)); |
| 29 | + auto folded_eval_idx = idx / 2; |
| 30 | + folded_eval[folded_eval_idx] = f_x + alpha * f_x_neg; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + template <typename S, typename E> |
| 35 | + __global__ void fold_circle_into_line_kernel(E* eval, S* domain_ys, E alpha, E alpha_sq, E* folded_eval, uint64_t n) |
| 36 | + { |
| 37 | + unsigned idx = blockIdx.x * blockDim.x + threadIdx.x; |
| 38 | + if (idx % 2 == 0 && idx < n) { |
| 39 | + E f0_px = eval[idx]; |
| 40 | + E f1_px = eval[idx + 1]; |
| 41 | + ibutterfly(f0_px, f1_px, S::inverse(domain_ys[idx / 2])); |
| 42 | + E f_prime = f0_px + alpha * f1_px; |
| 43 | + auto folded_eval_idx = idx / 2; |
| 44 | + folded_eval[folded_eval_idx] = folded_eval[folded_eval_idx] * alpha_sq + f_prime; |
| 45 | + } |
| 46 | + } |
| 47 | + } // namespace |
| 48 | + |
| 49 | + template <typename S, typename E> |
| 50 | + cudaError_t fold_line(E* eval, S* domain_xs, E alpha, E* folded_eval, uint64_t n, FriConfig& cfg) |
| 51 | + { |
| 52 | + CHK_INIT_IF_RETURN(); |
| 53 | + |
| 54 | + cudaStream_t stream = cfg.ctx.stream; |
| 55 | + // Allocate and move line domain evals to device if necessary |
| 56 | + E* d_eval; |
| 57 | + if (!cfg.are_evals_on_device) { |
| 58 | + auto data_size = sizeof(E) * n; |
| 59 | + CHK_IF_RETURN(cudaMallocAsync(&d_eval, data_size, stream)); |
| 60 | + CHK_IF_RETURN(cudaMemcpyAsync(d_eval, eval, data_size, cudaMemcpyHostToDevice, stream)); |
| 61 | + } else { |
| 62 | + d_eval = eval; |
| 63 | + } |
| 64 | + |
| 65 | + // Allocate and move domain's elements to device if necessary |
| 66 | + S* d_domain_xs; |
| 67 | + if (!cfg.are_domain_elements_on_device) { |
| 68 | + auto data_size = sizeof(S) * n / 2; |
| 69 | + CHK_IF_RETURN(cudaMallocAsync(&d_domain_xs, data_size, stream)); |
| 70 | + CHK_IF_RETURN(cudaMemcpyAsync(d_domain_xs, domain_xs, data_size, cudaMemcpyHostToDevice, stream)); |
| 71 | + } else { |
| 72 | + d_domain_xs = domain_xs; |
| 73 | + } |
| 74 | + |
| 75 | + // Allocate folded_eval if pointer is not a device pointer |
| 76 | + E* d_folded_eval; |
| 77 | + if (!cfg.are_results_on_device) { |
| 78 | + CHK_IF_RETURN(cudaMallocAsync(&d_folded_eval, sizeof(E) * n / 2, stream)); |
| 79 | + } else { |
| 80 | + d_folded_eval = folded_eval; |
| 81 | + } |
| 82 | + |
| 83 | + uint64_t num_threads = 256; |
| 84 | + uint64_t num_blocks = (n / 2 + num_threads - 1) / num_threads; |
| 85 | + fold_line_kernel<<<num_blocks, num_threads, 0, stream>>>(d_eval, d_domain_xs, alpha, d_folded_eval, n); |
| 86 | + |
| 87 | + // Move folded_eval back to host if requested |
| 88 | + if (!cfg.are_results_on_device) { |
| 89 | + CHK_IF_RETURN(cudaMemcpyAsync(folded_eval, d_folded_eval, sizeof(E) * n / 2, cudaMemcpyDeviceToHost, stream)); |
| 90 | + CHK_IF_RETURN(cudaFreeAsync(d_folded_eval, stream)); |
| 91 | + } |
| 92 | + if (!cfg.are_domain_elements_on_device) { CHK_IF_RETURN(cudaFreeAsync(d_domain_xs, stream)); } |
| 93 | + if (!cfg.are_evals_on_device) { CHK_IF_RETURN(cudaFreeAsync(d_eval, stream)); } |
| 94 | + |
| 95 | + // Sync if stream is default stream |
| 96 | + if (stream == 0) CHK_IF_RETURN(cudaStreamSynchronize(stream)); |
| 97 | + |
| 98 | + return CHK_LAST(); |
| 99 | + } |
| 100 | + |
| 101 | + template <typename S, typename E> |
| 102 | + cudaError_t fold_circle_into_line(E* eval, S* domain_ys, E alpha, E* folded_eval, uint64_t n, FriConfig& cfg) |
| 103 | + { |
| 104 | + CHK_INIT_IF_RETURN(); |
| 105 | + |
| 106 | + cudaStream_t stream = cfg.ctx.stream; |
| 107 | + // Allocate and move circle domain evals to device if necessary |
| 108 | + E* d_eval; |
| 109 | + if (!cfg.are_evals_on_device) { |
| 110 | + auto data_size = sizeof(E) * n; |
| 111 | + CHK_IF_RETURN(cudaMallocAsync(&d_eval, data_size, stream)); |
| 112 | + CHK_IF_RETURN(cudaMemcpyAsync(d_eval, eval, data_size, cudaMemcpyHostToDevice, stream)); |
| 113 | + } else { |
| 114 | + d_eval = eval; |
| 115 | + } |
| 116 | + |
| 117 | + // Allocate and move domain's elements to device if necessary |
| 118 | + S* d_domain_ys; |
| 119 | + if (!cfg.are_domain_elements_on_device) { |
| 120 | + auto data_size = sizeof(S) * n / 2; |
| 121 | + CHK_IF_RETURN(cudaMallocAsync(&d_domain_ys, data_size, stream)); |
| 122 | + CHK_IF_RETURN(cudaMemcpyAsync(d_domain_ys, domain_ys, data_size, cudaMemcpyHostToDevice, stream)); |
| 123 | + } else { |
| 124 | + d_domain_ys = domain_ys; |
| 125 | + } |
| 126 | + |
| 127 | + // Allocate folded_evals if pointer is not a device pointer |
| 128 | + E* d_folded_eval; |
| 129 | + if (!cfg.are_results_on_device) { |
| 130 | + CHK_IF_RETURN(cudaMallocAsync(&d_folded_eval, sizeof(E) * n / 2, stream)); |
| 131 | + } else { |
| 132 | + d_folded_eval = folded_eval; |
| 133 | + } |
| 134 | + |
| 135 | + E alpha_sq = alpha * alpha; |
| 136 | + uint64_t num_threads = 256; |
| 137 | + uint64_t num_blocks = (n / 2 + num_threads - 1) / num_threads; |
| 138 | + fold_circle_into_line_kernel<<<num_blocks, num_threads, 0, stream>>>( |
| 139 | + d_eval, d_domain_ys, alpha, alpha_sq, d_folded_eval, n); |
| 140 | + |
| 141 | + // Move folded_evals back to host if requested |
| 142 | + if (!cfg.are_results_on_device) { |
| 143 | + CHK_IF_RETURN(cudaMemcpyAsync(folded_eval, d_folded_eval, sizeof(E) * n / 2, cudaMemcpyDeviceToHost, stream)); |
| 144 | + CHK_IF_RETURN(cudaFreeAsync(d_folded_eval, stream)); |
| 145 | + } |
| 146 | + if (!cfg.are_domain_elements_on_device) { CHK_IF_RETURN(cudaFreeAsync(d_domain_ys, stream)); } |
| 147 | + if (!cfg.are_evals_on_device) { CHK_IF_RETURN(cudaFreeAsync(d_eval, stream)); } |
| 148 | + |
| 149 | + // Sync if stream is default stream |
| 150 | + if (stream == 0) CHK_IF_RETURN(cudaStreamSynchronize(stream)); |
| 151 | + |
| 152 | + return CHK_LAST(); |
| 153 | + } |
| 154 | +} // namespace fri |
0 commit comments