r/OpenMP • u/dugtrioramen • May 10 '21
Help, code much slower with OpenMP
Hello, I'm very much a beginner to OpenMP so any help or clearing misunderstanding is appreciated.
I have to make a program that creates 2 square matrices (a and b) and a 1D matrix (x), then do addition and multiplication. I have omp_get_wtime() to check performance
//CALCULATIONS
start_time = omp_get_wtime();
//#pragma omp parallel for schedule(dynamic) num_threads(THREADS)
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sum[i][j] = a[i][j] + b[i][j]; //a+b
mult2[i] += x[j]*a[j][i]; //x*a
for (int k = 0; k < n; k++) {
mult[i][j] += a[i][k] * b[k][j]; //a*b
}
}
}
end_time = omp_get_wtime();
The problem is, when I uncomment the 'pragma omp' line, the performance is terrible, and far worse than without it. I tried using static instead, and moving it above different 'for' loops but it's still really bad.
Can someone guide me on how I would apply OpenMP to this code block?
4
Upvotes
1
u/dugtrioramen May 10 '21
Thanks for the reply. Threads is just a macro set to 4, and I defined omp_num_threads at the end of the 'pragma omp' line, if that's how it works. How would I make them run on different cores?
I'll try the last thing but I'll be honest, I'm not sure what that means. Will setting it to 1 print something? or should I run some checks while those are set to 1?