3

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.

1 Answer 1

5

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

printf("hope you're doing well")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.