Question

How do i parallelize this function in OpenMP for C

int zeroRow(int**A,int n) {
int i, j, sum, num = 0;
for(i= 0;i< n;i++) {
  sum = 0;
  for(j = 0; j < n; j++) {
    sum += A[i][j];
  }
 if(sum == 0) {
   num++;
  }
}
return num;
}

I did this check if this is the right procedure.

int zeroRow(int**A,int n) {
int  num = 0;
#pragma omp parallel for reduction(+:num);
for(int i= 0;i< n;i++) {
  int sum = 0;
  for(int j = 0; j < n; j++) {
   sum += A[i][j];
   }
  if(sum == 0) {
    num++;
  }
}
return num;
}

please tell me if what i have done is right or wring i have parallelized the outer loop using reduction and a separate num is given to each thread.

Was it helpful?

Solution

Looks correct parallelized.

The only thing you should add is a term specifying the use of A. You rely that the default case is shared. You should explicitly name the status with

#pragma omp parallel for reduction(+:num) default(shared)

or

#pragma omp parallel for reduction(+:num) shared(A)

also you do not need to write a semicolon (;) at the end of the pragma line (but writing it would be no error)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top