1

Hello OpenACC experts,

I'm facing a problem with writing array elements to a file using OpenACC. Here's the relevant code snippet:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream THeOutfile;
    THeOutfile.open("TheIndianNumbers.txt", ios::app);

    const int arraySize = 1000001;
    long long int* thenumbersarray = new long long int[arraySize];

    for (int m = 0; m < 4000; m++)
    {
        #pragma acc parallel loop
        for (int i = 0; i < arraySize - 1; i++)
        {
            thenumbersarray[i] = 6000000000LL + i;
            cout << "Calculation Done " << i << endl;
        }

        for (int x = 0; x < arraySize - 1; x++)
        {
            THeOutfile << thenumbersarray[x] << endl;
            cout << "Writing Done " << x << endl;
        }
    }

    delete[] thenumbersarray;
    THeOutfile.close();

    return 0;
}

The issue lies in the loop responsible for writing the array elements to the file. It's not functioning correctly, and I need your expertise to fix it.

I'm seeking suggestions or code snippets to properly parallelize the loop using OpenACC directives and ensure successful writing of all array elements to the file.

Thank you for your help in advance!

  • How many times `Calculation Done` and `Writing Done` appeared on the standard output? – n. m. could be an AI Jun 03 '23 at 09:44
  • 1
    The writing loop is not decorated with `#pragma acc parallel loop` so it has nothing to do with OpenACC. Please post a [mcve] that demonstrates the problem. A program that prints 8000008000 messages is hardly minimal. If you think that OpenACC is to blame, show how removing OpenACC changes the outcome. – n. m. could be an AI Jun 03 '23 at 09:47
  • @n.m. the Writing Done has not appered once. – yourmaster12321-at-hyphens Jun 04 '23 at 14:51
  • How many times `Calculation Done` appeared? How many times `Writing Done` should have appeared if everything went according to the plan? – n. m. could be an AI Jun 04 '23 at 16:07
  • According to the plan, `Writing Done` should appear right after every `Calculation Done`. – yourmaster12321-at-hyphens Jun 05 '23 at 07:09
  • Perhaps you should put *calculation* and *writing* in the same loop and not in two separate ones. But you still have not answered a simple question of *how many times*. – n. m. could be an AI Jun 05 '23 at 07:11
  • I didnt run the code completely because it iterates about a `400000000` times. so i stopped when I didnt see any `Writing Done` – yourmaster12321-at-hyphens Jun 05 '23 at 08:15
  • It looks like you expect, for some bizarre reason, that each individual single calculation is followed by a write. The C language doesn't work this way. You have a loop followed by another loop. The first loop must run to completion, then the second loop may start. Whether loops are parallelised or not does not change this simple fact. – n. m. could be an AI Jun 05 '23 at 09:27
  • Even without the line `#pragma acc parallel loop` meaning no parallelization the line `Writing Done` is not being printed. – yourmaster12321-at-hyphens Jun 05 '23 at 15:58
  • I just read the code once again `Writing Done` will be show once every 1000000 iterations. surprised that neither of us got that. Sorry. – yourmaster12321-at-hyphens Jun 05 '23 at 16:09
  • Reduce the numbers to something reasonable and try again. – n. m. could be an AI Jun 05 '23 at 16:56
  • What compiler are you using? If using nvc++, are you targeting multicore? Since I/O is limited on NVIDIA GPUs, you can't call "cout" from device code and you should be getting a compilation error. If I remove the "cout" call in the parallel loop and reduce the loop trip counts to a manageable number, the resulting text file looks fine. – Mat Colgrove Jun 05 '23 at 18:29
  • @MatColgrove The Problem is fixed. The Problem was there was no problem. But I am running it with `pgc++` – yourmaster12321-at-hyphens Jun 06 '23 at 08:14

0 Answers0