OpenMP on Simple Factor

I am trying to implement OpenMP on the code that I have written below so that I can find the prime factor of very large numbers much faster than it currently is. I just read up a bit on OpenMP for my first time and this is my implementation, which does not seem to be working, it is just spewing out my results in no ordered fashion. Should I be placing the parallel statements at different locations inside the factor function?

Thank you for taking the time to read.

*Note: I am also utilizing GMP to handle large numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Directives
#include <cassert>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <gmpxx.h>
#include <omp.h>
using namespace std;


void findOneFactor(mpz_class n, mpz_class& output);
mpz_class gcd(mpz_class a, mpz_class b);
mpz_class func(mpz_class x, mpz_class n);

int main(int argc, char *argv[]){

    if(argc <= 1){
        cout << "Usage: ./factorize <number>" << endl;
        return 2;        
    }

    cout << "You entered:" << " " << argv[1] << endl;
	
    mpz_class in_val(argv[1]); //converts input string to mpz_class int
    mpz_class out_val = -1; //initializes an output value, will remain -1 if no factor is found

    #pragma omp parallel
    {
        findOneFactor(in_val, out_val);

        cout << "Factor is: " << out_val << endl;
    }
	return 0;
}

void findOneFactor(mpz_class n, mpz_class& output){
    mpz_class m = sqrt(n);
    cout << "sqrt of n = " << m << endl;

    mpz_class i = 3;
    mpz_class x = 3;
    mpz_class y = 3;
    mpz_class d = 1;

  
    if(n%2 == 0){
        output = 2;
        return;
    }

    if(n == 1){
        output = 1;
        return;
    }


    while(d == 1){
        x = func(x, n);

        y = func(func(y, -1) * func(y, -1) + 1, n);
        
        d = gcd(n,abs(x - y));
    }

    output = d;

    return;
}

mpz_class gcd(mpz_class a, mpz_class b){
    return (b == 0 ? a:gcd(b,a%b));
}

mpz_class func(mpz_class x, mpz_class n){
    if(n == -1){
        return (x * x + 1);
    }
    else{
        return (x * x + 1)%n;
    }
    
}
Topic archived. No new replies allowed.