MakePackedPlaintext hangs in loop parallelized with OMP

I am running the following code but neither thread can complete the first iteration, they seem to be stuck in some critical section within MakePackedPlaintext. The program runs fine if I set num_threads(1).

#include <omp.h>

#include "openfhe.h"

using namespace lbcrypto;

int main()
{
    CCParams<CryptoContextBGVRNS> params;
    params.SetPlaintextModulus(65537);

    CryptoContext<DCRTPoly> cc = GenCryptoContext(params);

    omp_set_num_threads(2);

#pragma omp parallel for
    for (int i = 0; i < 10; i++)
    {
        std::cout << omp_get_thread_num() << ": " << i << std::endl;
        std::vector<int64_t> vec(10, 1L);
        Plaintext plain = cc->MakePackedPlaintext(vec);
        std::cout << omp_get_thread_num() << ": " << i << " done" << std::endl;
    }
}

@faden
OpenFHE is not guaranteed to be thread-safe, and this behavior might be caused by a deadlock. In your case, you might be able to avoid the issue by adding the following lines before “#pragma omp parallel”:

        std::vector<int64_t> vec(10, 1L);
        Plaintext plain = cc->MakePackedPlaintext(vec);

This initializes some internal data before running multiple threads

1 Like