OMEGOSH01
OMEGOSH01

Reputation: 53

GPU array addition using OpenMP

I am trying out OpenMP offloading with an nvidia GPU and I am trying to do some array calculations with it in C++.

Right now my output is not desirable, as I am new with offloading calculations with OpenMP. Would appreciate if someone can point me to the correct direction.

Code Snippet:

#include <omp.h>
#include <iostream>

using namespace std;

int main(){

        int totalSum, ompSum;
        const int N = 1000;
        int array[N];
        for (int i=0; i<N; i++){
                array[i]=i;
        }
        #pragma omp target
        {
                #pragma omp parallal private(ompSum) shared(totalSum)
                {
                        ompSum=0;
                        #pragma omp parallel for
                        for (int i=0; i<N; i++){
                                ompSum += array[i];
                        }

                        #pragma omp critical
                        totalSum += ompSum;

                }


                printf ( "Caculated sum should be %d but is %d\n", N*(N-1)/2, totalSum );
        }

        return 0;


}

Right now, I know that the sum should calculate to a number 499500, but my machine is outputting extremely big numbers that are also negative.

Upvotes: 3

Views: 379

Answers (1)

dreamcrash
dreamcrash

Reputation: 51393

You have some typos on the OpenMP constructors, namely:

  1. #pragma omp parallal -> #pragma omp parallel;
  2. #pragma omp parallel for -> #pragma omp for

Regarding 2. you do not need the parallel because you are already inside a parallel region.

Try the following:

using namespace std;

int main(){

        int totalSum = 0, ompSum = 0;
        const int N = 1000;
        int array[N];
        for (int i=0; i<N; i++){
                array[i]=i;
        }
        #pragma omp target
        {
                #pragma omp parallel private(ompSum) shared(totalSum)
                {
                        ompSum=0;
                        #pragma omp for
                        for (int i=0; i<N; i++){
                                ompSum += array[i];
                        }

                        #pragma omp critical
                        totalSum += ompSum;
                }


                printf ( "Caculated sum should be %d but is %d\n", N*(N-1)/2, totalSum );
        }

        return 0;
}

Upvotes: 5

Related Questions