// Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
// This software is distributed under the MIT License
//
// Author: Bob Robey, Bob.Robey@amd.com

#include <stdlib.h>
#include <stdio.h>

void initialize_constants(int isize);
void compute(int cindex, double *x);

int main(int argc, char *argv[]){
   int N=1000;
   int isize=10;
   initialize_constants(isize);

   double *x = (double *)malloc(N*sizeof(double));
#pragma omp target enter data map (alloc:x[0:N])
#pragma omp target teams distribute parallel for
   for (int k = 0; k < N; k++){
      x[k] = 0.0;
   }

#pragma omp target teams distribute parallel for
   for (int k = 0; k < N; k++){
      int cindex = k%isize;
      compute(cindex, &x[k]);
   }

   double sum = 0.0;
#pragma omp target teams distribute parallel for reduction(+:sum)
   for (int k = 0; k < N; k++){
      sum += x[k];
   }

   printf("Result: sum of x is %lf\n",sum);

#pragma omp target exit data map (release:x[0:N])
   free(x);
}
      
