#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <sys/time.h>
#define N 10000
int A[N][N];
int B[N][N];
int C[N][N];
int main(){
int i,j,k;
struct timeval tv1, tv2;
struct timezone tz;
double elapsed;
for (i= 0; i< N; i++){
for (j= 0; j< N; j++)
{
A[i][j] = 3;
B[i][j] = 3;
}
}
gettimeofday(&tv1, &tz);
omp_set_num_threads(4);
#pragma omp parallel default (private) shared (A,B,C,N) num_threads(4)
#pragma omp parallel for schedule(static)
for (i = 0; i < N; ++i){
for (j = 0; j < N; ++j){
C[i][j]=0;
for (k = 0; k < N; ++k){
C[i][j] += A[i][k] * B[k][j];
}
}
}
gettimeofday(&tv2, &tz);
elapsed = (double) (tv2.tv_sec-tv1.tv_sec) + (double) (tv2.tv_usec-tv1.tv_usec) * 1.e-6;
printf("elapsed time = %f seconds.\n", elapsed);
for (i= 0; i< N; i++){
for (j= 0; j< N; j++) {
printf("%d \t",C[i][j]);
}
printf("\n");
}
}
this code is not working , although I make sure that every "{" is put right ! what's wrong with this code? It is a matrix multiplication using OpenMp library , and I am using eclipse c++ Any idea of what's wrong with the code? I get "expected declaration or statement at end of input } " and it glows under printf("\n"); I tried to delete the whole thing that prints the matrix but it didn't work
I got following errors on Wandbox:
prog.c: In function 'main':
prog.c:29:35: error: expected 'none' or 'shared' before 'private'
29 | #pragma omp parallel default (private) shared (A,B,C,N) num_threads(4)
| ^~~~~~~
prog.c:29:58: error: expected ')' before numeric constant
29 | #pragma omp parallel default (private) shared (A,B,C,N) num_threads(4)
| ~ ^
| )
Firstly, OpenMP default
clause doesn't have option private
.
You should remove the default
clause and mark the inner loop variables j
and k
private via private
clause.
Secondly, N
is defined as macro and is expanded to 10000
.
It is not a variable and therefore it cannot be specified in OpenMP clause that expect variables.