Multi-threading ciphertext multiplication and bootstrapping

Hi. Are there any precautions to take when multithreading ciphertext multiplication, addition, and bootstrapping? I’m using CKKS to evaluate a circuit that takes a vector of starting ciphertexts and, for each one, applies a multiplication, a bootstrap, and an evalpoly.

I used C++ threading from the C++ standard library, so it’s pthreads underneath. The code runs well on a single thread, but, when there are two or more threads, it receives a SIGABORT signal during bootstrapping in a method of dcrtpoly.cpp called operator+=(), where it adds rhs.m_vectors[I].

I’ve compiled the library with OpenMP ON but am running it with OMP_NUM_THREADS=1. The multithreading is coming from pthreads. All threads share the same CryptoContext and keys.

Thank You,
Drew

OMP does not always work well with pthreads. I suggest turning off OMP by compiling it with the CMAKE flag WITH_OPENMP=OFF. Also when using multithreading, make sure the code is written in a way where there are no shared variables/objects being updated at the same time.

Hi Yuriy. Thanks for responding. May I ask this question another way? I see in the code that there are many OpenMP critical sections and there is thread-local storage for the random number generator, so it’s expecting to be parallel within OpenFHE.

Have you seen any code that runs OpenFHE, calling it from an OpenMP for-loop. Like this:

#pragma omp parallel for
    for (int pixel_idx=0; pixel_idx < pixel_cnt; pixel_idx++) {
        CT bootstrapped_ct = context->EvalBootstrap(input_pixel[pixel_idx];
        CT relu_ct = context->EvalPoly(bootstrapped_ct, coefficients);
        output[pixel_idx] = relu_ct;
    }

I’m asking because I switched the code to use OpenMP, and I’m seeing a double-free in the destructor of a unique_ptr at the end of DropLastElementAndScale, which I’ll track down. It could be that I need to add a critical section somewhere to support calling OpenFHE from OMP threads instead of just calling OpenMP from one thread and asking it to use multiple threads inside.
Thank you - Drew