-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gpu): add abs operation on gpu backend
- Loading branch information
1 parent
f9e8df4
commit 0aee4c5
Showing
11 changed files
with
470 additions
and
0 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,43 @@ | ||
#include "integer/abs.cuh" | ||
|
||
void scratch_cuda_integer_abs_inplace_radix_ciphertext_kb_64( | ||
void *const *streams, uint32_t const *gpu_indexes, uint32_t gpu_count, | ||
int8_t **mem_ptr, bool is_signed, uint32_t glwe_dimension, | ||
uint32_t polynomial_size, uint32_t big_lwe_dimension, | ||
uint32_t small_lwe_dimension, uint32_t ks_level, uint32_t ks_base_log, | ||
uint32_t pbs_level, uint32_t pbs_base_log, uint32_t grouping_factor, | ||
uint32_t num_blocks, uint32_t message_modulus, uint32_t carry_modulus, | ||
PBS_TYPE pbs_type, bool allocate_gpu_memory) { | ||
|
||
int_radix_params params(pbs_type, glwe_dimension, polynomial_size, | ||
big_lwe_dimension, small_lwe_dimension, ks_level, | ||
ks_base_log, pbs_level, pbs_base_log, grouping_factor, | ||
message_modulus, carry_modulus); | ||
|
||
scratch_cuda_integer_abs_kb<uint64_t>( | ||
(cudaStream_t *)(streams), gpu_indexes, gpu_count, | ||
(int_abs_buffer<uint64_t> **)mem_ptr, is_signed, num_blocks, params, | ||
allocate_gpu_memory); | ||
} | ||
|
||
void cuda_integer_abs_inplace_radix_ciphertext_kb_64( | ||
void *const *streams, uint32_t const *gpu_indexes, uint32_t gpu_count, | ||
void *ct, int8_t *mem_ptr, bool is_signed, void *const *bsks, | ||
void *const *ksks, uint32_t num_blocks) { | ||
|
||
auto mem = (int_abs_buffer<uint64_t> *)mem_ptr; | ||
|
||
host_integer_abs_kb<uint64_t>((cudaStream_t *)(streams), gpu_indexes, | ||
gpu_count, static_cast<uint64_t *>(ct), bsks, | ||
(uint64_t **)(ksks), mem, is_signed, | ||
num_blocks); | ||
} | ||
|
||
void cleanup_cuda_integer_abs_inplace(void *const *streams, | ||
uint32_t const *gpu_indexes, | ||
uint32_t gpu_count, | ||
int8_t **mem_ptr_void) { | ||
int_abs_buffer<uint64_t> *mem_ptr = | ||
(int_abs_buffer<uint64_t> *)(*mem_ptr_void); | ||
mem_ptr->release((cudaStream_t *)(streams), gpu_indexes, gpu_count); | ||
} |
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,69 @@ | ||
#ifndef TFHE_RS_ABS_CUH | ||
#define TFHE_RS_ABS_CUH | ||
|
||
#include "crypto/keyswitch.cuh" | ||
#include "device.h" | ||
#include "integer/bitwise_ops.cuh" | ||
#include "integer/comparison.cuh" | ||
#include "integer/integer.cuh" | ||
#include "integer/integer_utilities.h" | ||
#include "integer/negation.cuh" | ||
#include "integer/scalar_shifts.cuh" | ||
#include "linear_algebra.h" | ||
#include "pbs/programmable_bootstrap.h" | ||
#include "utils/helper.cuh" | ||
#include "utils/kernel_dimensions.cuh" | ||
#include <fstream> | ||
#include <iostream> | ||
#include <sstream> | ||
#include <string> | ||
#include <vector> | ||
|
||
template <typename Torus> | ||
__host__ void scratch_cuda_integer_abs_kb( | ||
cudaStream_t const *streams, uint32_t const *gpu_indexes, | ||
uint32_t gpu_count, int_abs_buffer<Torus> **mem_ptr, bool is_signed, | ||
uint32_t num_blocks, int_radix_params params, bool allocate_gpu_memory) { | ||
|
||
if (is_signed) | ||
*mem_ptr = | ||
new int_abs_buffer<Torus>(streams, gpu_indexes, gpu_count, params, | ||
num_blocks, allocate_gpu_memory); | ||
} | ||
|
||
template <typename Torus> | ||
__host__ void | ||
host_integer_abs_kb(cudaStream_t const *streams, uint32_t const *gpu_indexes, | ||
uint32_t gpu_count, Torus *ct, void *const *bsks, | ||
uint64_t *const *ksks, int_abs_buffer<uint64_t> *mem_ptr, | ||
bool is_signed, uint32_t num_blocks) { | ||
if (!is_signed) | ||
return; | ||
|
||
auto radix_params = mem_ptr->params; | ||
auto mask = mem_ptr->mask; | ||
|
||
auto big_lwe_dimension = radix_params.big_lwe_dimension; | ||
auto big_lwe_size = big_lwe_dimension + 1; | ||
auto big_lwe_size_bytes = big_lwe_size * sizeof(Torus); | ||
uint32_t num_bits_in_ciphertext = | ||
(31 - __builtin_clz(radix_params.message_modulus)) * num_blocks; | ||
|
||
cuda_memcpy_async_gpu_to_gpu(mask, ct, num_blocks * big_lwe_size_bytes, | ||
streams[0], gpu_indexes[0]); | ||
|
||
host_integer_radix_arithmetic_scalar_shift_kb_inplace( | ||
streams, gpu_indexes, gpu_count, mask, num_bits_in_ciphertext - 1, | ||
mem_ptr->arithmetic_scalar_shift_mem, bsks, ksks, num_blocks); | ||
host_addition<Torus>(streams[0], gpu_indexes[0], ct, mask, ct, | ||
radix_params.big_lwe_dimension, num_blocks); | ||
|
||
host_propagate_single_carry<Torus>(streams, gpu_indexes, gpu_count, ct, | ||
nullptr, nullptr, mem_ptr->scp_mem, bsks, | ||
ksks, num_blocks); | ||
|
||
host_integer_radix_bitop_kb(streams, gpu_indexes, gpu_count, ct, mask, ct, | ||
mem_ptr->bitxor_mem, bsks, ksks, num_blocks); | ||
} | ||
|
||
#endif // TFHE_RS_ABS_CUH |
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.