0

I am trying to calculate LCS length using OpenMP. I picked up a conference paper on it and started executing the algorithm. To be honest I didn't understand how the algorithm works to very detail, but understood that it was trying to calculate the values diagonally in parallel. The code I have written is this:

#include <omp.h>
#include <stdio.h>

#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))

static long num_steps = 100000;
double step;

void main(){

    int m,n;
    printf("Enter m: ");
    scanf("%d", &m);
    char X[m];
    printf("Enter X: ");
    scanf("%s", X);

    printf("Enter n: ");
    scanf("%d", &n);
    char Y[n];
    printf("Enter Y: ");
    scanf("%s", Y);

    int C[m+1][n+1];

    #pragma omp parallel for
    for(int i=0; i<=m; i++)
    {
        C[i][0] = 0;
    }

    #pragma omp parallel for
    for(int j=0; j<=n; j++)
    {
        C[0][j] = 0;
    }


    for(int a = 0; a<m; a++){
        printf("\n");
        for(int b=0;b<n;b++){
            printf("%d ", C[a][b]);
        }
    }
    printf("\n-------------\n");


    int i,j,diag;

    #pragma omp parallel default(none) shared(X,Y,C,m,n) private(i,j,diag) //num_threads(8)
    {
        for(diag = 2; diag<=m+n; diag++) {
            #pragma omp parallel for
            for(i=MIN(m, diag-1); i<=MAX(1, diag-1); i--){
                j = diag - 1;
                if(X[i-1]==Y[j-1]) {
                    C[i][j] = C[i-1][j-1] + 1;
                }
                else {
                    C[i][j] = MAX(C[i-1][j], C[i][j-1]);
                }
            }
        }
    }

    for(int a = 0; a<m; a++){
        printf("\n");
        for(int b=0;b<n;b++){
            printf("%d ", C[a][b]);
        }
    }

Can you tell me what is wrong with this? I also would like to know about a good resource to understand this algorithm because the paper I have picked does not explain very well.

Kushal Mondal
  • 93
  • 1
  • 8
  • 2
    You have read the paper and didn't understand the algorithm, and now you want somebody who haven't read the paper to understand the algorithm? I'm afraid it isn't gonna work well. – n. m. could be an AI Jun 18 '20 at 06:29
  • I understand my mistake. I would like to know what I am doing wrong in the code. Also if there is a good resource that explain the stuff. – Kushal Mondal Jun 18 '20 at 07:52
  • What is your goal ? If it is to develop your openMP skills, very good (but as n pronouns n states, understanding the paper is required). If your purpose is to run LCS fast, you should have a look at the threshold algorithm, it runs way faster than vanilla DP versions. – m.raynal Jun 18 '20 at 09:08
  • I changed the algorithm from another paper. Now I am getting some positive result. What I understood is that the growing phase of threads is fine, while shrinking it is causing problems. I am working on it. @ m.raynal, I dont necessarily need a faster one i need this one to run. I will definitely look into the threshold algorithm. Thanks for suggesting – Kushal Mondal Jun 18 '20 at 11:23

0 Answers0