我正在尝试链接 cuda 和 SEAL。首先,我设置了 SEAL 3.6.6,它运行良好。之后我尝试通过 SEAL 使用我的 cuda 代码。但是,我遇到了一些错误,我无法解决。谁能帮帮我:) 我的工作环境如下;GPU:RTX 3070,操作系统:POP OS,nvcc 版本:11.4,gcc 版本:10.3,cmake 版本:3.21.3
这是我的 CMakeLists.txt
cmake_minimum_required(VERSION 3.17)
set(CUDA_INCLUDE_DIRS "/home/ret/Documents/SEAL-3.6.6/native/src/seal/seal.h")
set(CMAKE_CUDA_COMPILER /usr/local/cuda-11.4/bin/nvcc)
set(CMAKE_CXX_STANDARD 14)
project(demo CXX CUDA)
find_package(CUDA REQUIRED)
enable_language(CUDA)
find_package(SEAL 3.6.6 REQUIRED)
SET(GPU_ARCH "86" CACHE STRING "Cuda architecture number, one of ('20', '30', '32', '35', '37', '50', '52', '53', '72', '86', '87')")
SET(GCC_CUDA_VERSION "gcc-14" CACHE STRING "GCC compiler to be used with cuda")
option(USE_CUDA "Use CUDA" ON)
add_executable(demo test.cu hostadd.cuh )
target_link_libraries(demo SEAL::seal)
set_target_properties(demo PROPERTIES CUDA_SEPERABLE_COMPULATION ON)
target_include_directories(demo PUBLIC ${/usr/local/include/SEAL-3.6/seal/})
我按如下方式运行 CMakeLists.txt 文件:
cmake -DCMAKE_CUDA_ARCHITECTURES=86 .
make all
然后我收到以下错误:
[ 50%] Building CUDA object CMakeFiles/demo.dir/test.cu.o
/home/ret/Documents/link3/hostadd.cuh(86): warning: expression has no effect
/home/ret/Documents/link3/hostadd.cuh(86): warning: expression has no effect
/usr/local/include/SEAL-3.6/seal/dynarray.h: In instantiation of ‘void seal::DynArray<T>::reserve(std::size_t) [with T = long unsigned int; std::size_t = long unsigned int]’:
/usr/local/include/SEAL-3.6/seal/plaintext.h:206:25: required from here
/usr/local/include/SEAL-3.6/seal/dynarray.h:430:20: error: ‘__T0’ was not declared in this scope; did you mean ‘__y0’?
430 | auto new_data(util::allocate<T>(capacity, pool_));
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| __y0
/usr/local/include/SEAL-3.6/seal/dynarray.h: In instantiation of ‘void seal::DynArray<T>::resize(std::size_t, bool) [with T = long unsigned int; std::size_t = long unsigned int]’:
/usr/local/include/SEAL-3.6/seal/plaintext.h:248:27: required from here
/usr/local/include/SEAL-3.6/seal/dynarray.h:476:20: error: ‘__T1’ was not declared in this scope; did you mean ‘__y1’?
476 | auto new_data(util::allocate<T>(size, pool_));
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| __y1
/usr/local/include/SEAL-3.6/seal/dynarray.h: In instantiation of ‘void seal::DynArray<T>::resize(std::size_t, bool) [with T = seal::seal_byte; std::size_t = long unsigned int]’:
/usr/local/include/SEAL-3.6/seal/dynarray.h:87:1: required from ‘seal::DynArray<T>::DynArray(std::size_t, seal::MemoryPoolHandle) [with T = seal::seal_byte; std::size_t = long unsigned int]’
/usr/local/include/SEAL-3.6/seal/randomgen.h:330:181: required from here
/usr/local/include/SEAL-3.6/seal/dynarray.h:476:20: error: ‘__T1’ was not declared in this scope; did you mean ‘__y1’?
476 | auto new_data(util::allocate<T>(size, pool_));
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| __y1
make[2]: *** [CMakeFiles/demo.dir/build.make:76: CMakeFiles/demo.dir/test.cu.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/demo.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
这是我的 test.cu 文件:
#include <string>
#include <iostream>
#include "hostadd.cuh"
#include "seal/seal.h"
#include "examples.h"
using namespace seal;
using namespace std;
int main()
{
EncryptionParameters parms(scheme_type::bfv);
size_t poly_modulus_degree = 8192; // n
parms.set_poly_modulus_degree(poly_modulus_degree);
//parms.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
parms.set_coeff_modulus({ 2147352577, 2147205121, 2147074049, 2146959361, 2146713601, 2146418689, 2146336769 });
parms.set_plain_modulus(PlainModulus::Batching(poly_modulus_degree, 17));
// cout << "Debug-pt 0" << endl;
auto context = SEALContext(parms, true, sec_level_type::none);
print_parameters(context);// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
KeyGenerator keygen(context);
SecretKey secret_key = keygen.secret_key();
PublicKey public_key;
keygen.create_public_key(public_key);
Encryptor encryptor(context, public_key);
Evaluator evaluator(context);
Decryptor decryptor(context, secret_key);
BatchEncoder batch_encoder(context);
size_t slot_count = batch_encoder.slot_count();
size_t row_size = slot_count / 2;
vector<uint64_t> pod_matrix(slot_count, 0ULL);
pod_matrix[0] = 0ULL;
pod_matrix[1] = 12ULL;
pod_matrix[2] = 23ULL;
pod_matrix[3] = 31ULL;
pod_matrix[row_size] = 41ULL;
pod_matrix[row_size + 1] = 54ULL;
pod_matrix[row_size + 2] = 6ULL;
pod_matrix[row_size + 3] = 100ULL;
Plaintext plain_matrix;
batch_encoder.encode(pod_matrix, plain_matrix);
vector<uint64_t> pod_result;
batch_encoder.decode(plain_matrix, pod_result);
Ciphertext encrypted_matrix;
encryptor.encrypt(plain_matrix, encrypted_matrix);
vector<uint64_t> pod_matrix2;
for (size_t i = 0; i < slot_count; i++)
{
pod_matrix2.push_back((i % 2) + 1);
}
Plaintext plain_matrix2;
batch_encoder.encode(pod_matrix2, plain_matrix2);
Ciphertext encrypted_matrix2;
encryptor.encrypt(plain_matrix2, encrypted_matrix2);
auto context_data_ptr = context.get_context_data(encrypted_matrix.parms_id());
auto parameter = context_data_ptr.get()->parms();
size_t coeff_count = parameter.poly_modulus_degree();
size_t decomp_modulus_size = parameter.coeff_modulus().size();
unsigned long q[] = { 2147352577, 2147205121, 2147074049, 2146959361, 2146713601, 2146418689 , 2146336769 };
unsigned long mu[] = { 2147614727, 2147762211, 2147893325, 2148008063, 2148253971, 2148549135 };
int q_bit[] = { 31, 31, 31, 31, 31, 31 };
const int q_Size = sizeof(q) / sizeof(q[0]);
const int decomp_mod_count = q_Size - 1;
const int n = 8192;
const int array_size = n * decomp_mod_count;
unsigned long encrypted_1[2 * array_size];
unsigned long encrypted_2[2 * array_size];
for (int loop1 = 0; loop1 < 2; loop1++) {
for (int loop2 = 0; loop2 < decomp_modulus_size * coeff_count; loop2++) {
encrypted_1[decomp_modulus_size * coeff_count * loop1 + loop2] = encrypted_matrix.data(loop1)[loop2];
encrypted_2[decomp_modulus_size * coeff_count * loop1 + loop2] = encrypted_matrix2.data(loop1)[loop2];
}
}
unsigned long result[2 * array_size];
unsigned long* result_device;
cudaMalloc(&result_device, 2 * array_size * sizeof(unsigned long));
GPU_ADD_INPLACE(encrypted_1, encrypted_2, q, mu, q_bit, n, q_Size, result_device);
cudaMemcpy(result, result_device, 2 * array_size * sizeof(unsigned long), cudaMemcpyDeviceToHost);
evaluator.add_inplace(encrypted_matrix, encrypted_matrix2);
for (int loopx = 0; loopx < 2; loopx++) {
for (int loopy = 0; loopy < decomp_mod_count; loopy++) {
cout << "-------" << loopx << " - " << loopy << "-------" << endl;
for (int loop = 0; loop < 10; loop++) {
cout << result[loop + loopy * n + (loopx * n * decomp_mod_count)] << " --- " << encrypted_matrix.data(loopx)[loop + loopy * coeff_count] << endl;
}
}
}
cudaFree(result_device);
Plaintext plain_result;
decryptor.decrypt(encrypted_matrix, plain_result);
batch_encoder.decode(plain_result, pod_result);
return 0;
}
CMakeError.log
Performing C++ SOURCE FILE Test CMAKE_HAVE_LIBC_PTHREAD failed with the following output:
Change Dir: /home/ret/Documents/link3/CMakeFiles/CMakeTmp
Run Build Command(s):/usr/bin/gmake -f Makefile cmTC_e26ea/fast && /usr/bin/gmake -f CMakeFiles/cmTC_e26ea.dir/build.make CMakeFiles/cmTC_e26ea.dir/build
gmake[1]: Entering directory '/home/ret/Documents/link3/CMakeFiles/CMakeTmp'
Building CXX object CMakeFiles/cmTC_e26ea.dir/src.cxx.o
/usr/bin/c++ -DCMAKE_HAVE_LIBC_PTHREAD -std=gnu++14 -o CMakeFiles/cmTC_e26ea.dir/src.cxx.o -c /home/ret/Documents/link3/CMakeFiles/CMakeTmp/src.cxx
Linking CXX executable cmTC_e26ea
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/cmTC_e26ea.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/cmTC_e26ea.dir/src.cxx.o -o cmTC_e26ea
/usr/bin/ld: CMakeFiles/cmTC_e26ea.dir/src.cxx.o: in function `main':
src.cxx:(.text+0x46): undefined reference to `pthread_create'
/usr/bin/ld: src.cxx:(.text+0x52): undefined reference to `pthread_detach'
/usr/bin/ld: src.cxx:(.text+0x5e): undefined reference to `pthread_cancel'
/usr/bin/ld: src.cxx:(.text+0x6f): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
gmake[1]: *** [CMakeFiles/cmTC_e26ea.dir/build.make:99: cmTC_e26ea] Error 1
gmake[1]: Leaving directory '/home/ret/Documents/link3/CMakeFiles/CMakeTmp'
gmake: *** [Makefile:127: cmTC_e26ea/fast] Error 2
Source file was:
#include <pthread.h>
static void* test_func(void* data)
{
return data;
}
int main(void)
{
pthread_t thread;
pthread_create(&thread, NULL, test_func, NULL);
pthread_detach(thread);
pthread_cancel(thread);
pthread_join(thread, NULL);
pthread_atfork(NULL, NULL, NULL);
pthread_exit(NULL);
return 0;
}
Determining if the function pthread_create exists in the pthreads failed with the following output:
Change Dir: /home/ret/Documents/link3/CMakeFiles/CMakeTmp
Run Build Command(s):/usr/bin/gmake -f Makefile cmTC_4cab0/fast && /usr/bin/gmake -f CMakeFiles/cmTC_4cab0.dir/build.make CMakeFiles/cmTC_4cab0.dir/build
gmake[1]: Entering directory '/home/ret/Documents/link3/CMakeFiles/CMakeTmp'
Building CXX object CMakeFiles/cmTC_4cab0.dir/CheckFunctionExists.cxx.o
/usr/bin/c++ -DCHECK_FUNCTION_EXISTS=pthread_create -std=gnu++14 -o CMakeFiles/cmTC_4cab0.dir/CheckFunctionExists.cxx.o -c /home/ret/Documents/link3/CMakeFiles/CheckLibraryExists/CheckFunctionExists.cxx
Linking CXX executable cmTC_4cab0
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/cmTC_4cab0.dir/link.txt --verbose=1
/usr/bin/c++ -DCHECK_FUNCTION_EXISTS=pthread_create CMakeFiles/cmTC_4cab0.dir/CheckFunctionExists.cxx.o -o cmTC_4cab0 -lpthreads
/usr/bin/ld: cannot find -lpthreads
collect2: error: ld returned 1 exit status
gmake[1]: *** [CMakeFiles/cmTC_4cab0.dir/build.make:99: cmTC_4cab0] Error 1
gmake[1]: Leaving directory '/home/ret/Documents/link3/CMakeFiles/CMakeTmp'
gmake: *** [Makefile:127: cmTC_4cab0/fast] Error 2