1

Sometimes my encrypt code is going well, but sometimes it got buffer overflow or did'nt get the right answer when I try to parallel it. I don't know why. This is my code:

#ifdef _OPENMP #pragma omp parallel shared(result) //#pragma omp parallel for shared(M,C) private(x) schedule(static) default(shared) #endif do { mpz_powm(MN, M, N, pub.n); /* MN = M^N mod n */ mpz_mul(result, result, MN); /* result = result * MN */ mpz_mod(result, result, pub.n); /* result = result mod n */ } while(!tmp); unsigned int i, j; unsigned int core = (unsigned int) omp_get_num_procs(); for(i = 1; i < core; i++) { if(mpz_get_ui(pub.e) % core == i) { for(j = 1; j <= i; j++) { mpz_mul(result, result, M); /* result = result * M */ mpz_mod(result, result, pub.n); /* result = result mod n */ } } } mpz_mod(C, result, pub.n); /* C = result mod n */ 
7
  • You have can run mpz_powm in parallel, but the following mpz_mul writes result in which case you have to synchronize the processes on write. Commented Dec 3, 2015 at 15:15
  • how to do that in openmp? Commented Dec 4, 2015 at 3:16
  • Oh I got it with #pragma omp critical. Commented Dec 4, 2015 at 3:54
  • 1
    Well, it would be better to have a temporary variable, so you can compute the multiplication in parallel and then simply copy the multiplication result into the shared variable. I think you will still need to synchronize the processes a second time after the modulo operation. If you have fully working code, please don't forget to answer your own question with it. Commented Dec 4, 2015 at 10:51
  • there are a few problems with the posted code: 1) tmp not defined and certainly not updated in the do...while(!tmp); loop 2) in a (for instance) 4 core CPU, this line: for(i = 1; i < core; i++) will only cover 3 of the cores Commented Dec 4, 2015 at 20:39

1 Answer 1

1

It's okay with modify that code to be like this. I can't get the error again. Thank you.

#pragma omp parallel shared(result) do { #pragma omp critical { mpz_powm(MN, M, N, pub.n); mpz_mul(result, result, MN); mpz_mod(result, result, pub.n); } } while(!tmp); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.