-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
183 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <cuda.h> | ||
#include <stdexcept> | ||
|
||
#include "icicle/errors.h" | ||
#include "icicle/vec_ops.h" | ||
#include "gpu-utils/error_handler.h" | ||
|
||
namespace montgomery { | ||
#define MAX_THREADS_PER_BLOCK 256 | ||
|
||
template <typename E, bool is_into> | ||
__global__ void MontgomeryKernel(const E* input, int n, E* output) | ||
{ | ||
int tid = blockIdx.x * blockDim.x + threadIdx.x; | ||
if (tid < n) { output[tid] = is_into ? E::to_montgomery(input[tid]) : E::from_montgomery(input[tid]); } | ||
} | ||
|
||
template <typename E, bool is_into> | ||
cudaError_t ConvertMontgomery(const E* input, size_t n, const VecOpsConfig& config, E* output) | ||
{ | ||
cudaStream_t cuda_stream = reinterpret_cast<cudaStream_t>(config.stream); | ||
|
||
E *d_alloc_out = nullptr, *d_alloc_in = nullptr, *d_out; | ||
const E* d_in; | ||
if (!config.is_a_on_device) { | ||
CHK_IF_RETURN(cudaMallocAsync(&d_alloc_in, n * sizeof(E), cuda_stream)); | ||
CHK_IF_RETURN(cudaMemcpyAsync(d_alloc_in, input, n * sizeof(E), cudaMemcpyHostToDevice, cuda_stream)); | ||
d_in = d_alloc_in; | ||
} else { | ||
d_in = input; | ||
} | ||
|
||
if (!config.is_result_on_device) { | ||
CHK_IF_RETURN(cudaMallocAsync(&d_alloc_out, n * sizeof(E), cuda_stream)); | ||
d_out = d_alloc_out; | ||
} else { | ||
d_out = output; | ||
} | ||
|
||
int num_threads = MAX_THREADS_PER_BLOCK; | ||
int num_blocks = (n + num_threads - 1) / num_threads; | ||
MontgomeryKernel<E, is_into><<<num_blocks, num_threads, 0, cuda_stream>>>(d_in, n, d_out); | ||
|
||
if (d_alloc_in) { CHK_IF_RETURN(cudaFreeAsync(d_alloc_in, cuda_stream)); } | ||
if (d_alloc_out) { | ||
CHK_IF_RETURN(cudaMemcpyAsync(output, d_out, n * sizeof(E), cudaMemcpyDeviceToHost, cuda_stream)); | ||
CHK_IF_RETURN(cudaFreeAsync(d_out, cuda_stream)); | ||
} | ||
if (!config.is_async) return CHK_STICKY(cudaStreamSynchronize(cuda_stream)); | ||
|
||
return CHK_LAST(); | ||
} | ||
|
||
} // namespace montgomery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <cuda.h> | ||
#include <stdexcept> | ||
|
||
#include "icicle/errors.h" | ||
#include "icicle/curves/montgomery_conversion.h" | ||
#include "gpu-utils/error_handler.h" | ||
#include "error_translation.h" | ||
#include "cuda_mont.cuh" | ||
|
||
#include "icicle/curves/curve_config.h" | ||
using namespace curve_config; | ||
using namespace icicle; | ||
|
||
namespace icicle { | ||
|
||
template <typename T> | ||
eIcicleError | ||
cuda_convert_mont(const Device& device, const T* input, size_t n, bool is_into, const VecOpsConfig& config, T* output) | ||
{ | ||
cudaError_t err = is_into ? montgomery::ConvertMontgomery<T, true>(input, n, config, output) | ||
: montgomery::ConvertMontgomery<T, false>(input, n, config, output); | ||
return translateCudaError(err); | ||
} | ||
|
||
REGISTER_AFFINE_CONVERT_MONTGOMERY_BACKEND("CUDA", cuda_convert_mont<affine_t>); | ||
REGISTER_PROJECTIVE_CONVERT_MONTGOMERY_BACKEND("CUDA", cuda_convert_mont<projective_t>); | ||
|
||
#ifdef G2 | ||
REGISTER_AFFINE_G2_CONVERT_MONTGOMERY_BACKEND("CUDA", cuda_convert_mont<g2_affine_t>); | ||
REGISTER_PROJECTIVE_G2_CONVERT_MONTGOMERY_BACKEND("CUDA", cuda_convert_mont<g2_projective_t>); | ||
#endif // G2 | ||
|
||
} // namespace icicle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.