1

I am new to OpenMP. I try to create 1000x1000 matrix multiplication and try to speed up with open mp. But when I execute my code, it passes without any error occur. But when I run the execution code it does not return any of the results. I check with change the dimension to 10x10, then it immediately works! I have no idea what is wrong with my code.

#define n 1000 // Dimension of metrix

'''

int i, j, k, th_id, num_thread;
int max = 100;
double a[n][n], b[n][n], mul[n][n];
double norm;

srand(time(0));

double start = omp_get_wtime();
omp_set_num_threads(6);
#pragma omp parallel shared(a, b, mul, norm, num_thread) private(th_id, i, j, k)
{
    th_id = omp_get_thread_num();
    #pragma omp for
    for (i=0; i<n; i++){
        for (j=0; j<n; j++){
            a[i][j] = double(rand()%max)/100;
        }
    }
    #pragma omp for
    for (i=0; i<n; i++){
        for (j=0; j<n; j++){
            b[i][j] = double(rand()%max)/100;
        }
    }
    #pragma omp for
    for (i=0; i<n; i++){
        for (j=0; j<n; j++){
            mul[i][j] = 0;
        }
    }
    
    printf("Thread %d starting matrix multiply...\n", th_id);
    #pragma omp for
    for (i=0; i<n; i++) {
        printf("Thread = %d did row = %d\n", th_id, i);
        for (j=0; j<n; j++) {
            for (k=0; k<n; k++){
                mul[i][j] += a[i][k]*b[k][j]; 
            } 
        }
    }

    #pragma omp for reduction(+:norm)
    // Calculate Frobenius norm
    for (i=0; i<n; i++) {
        for (j=0; j<n; j++){
            norm += mul[i][j]*mul[i][j];
        }
    }
}
double end = omp_get_wtime();
printf("Frobenuis norm of parallel node is %lf\n", sqrt(norm));
printf("Elapsed time = %lf s\n", end-start);
printf("Precision = %lf s\n", omp_get_wtick());




printf("End Program");
return 0;

'''

P.Chian
  • 65
  • 1
  • 7
  • 2
    You got stack overflow. `1000 * 1000 * sizeof(double)` (for `a` array) is too much to be allocated onto stack. Make arrays as global, or allocate them in dynamic way, – rafix07 Sep 21 '21 at 04:17

1 Answers1

0

You need to change the definition of the arrays:

1- You can use the vector of vector as :

#include <cstdlib>
#include <vector>

vector<vector<double>> a, b, mul; 

2- Using Dynamic Allocation of Arrays as mentioned in comments.

auto a = new double[n][n]; = new double[n][n];
auto b = new double[n][n];
auto mult = new double[n][n];

Then delete

delete[] a;
delete[] b;
delete[] mul;

3- For doing matrix arithmetic in C++, it is always a good idea to map a 2d array on a 1d arrayhere is a discussion.

double* a = new double[n*n];
double* b = new double[n*n];
double* mul = new double[n*n];
Raha Moosavi
  • 527
  • 4
  • 19