Mixed C++/Fortran Problems

General OpenMP discussion

Mixed C++/Fortran Problems

Postby ozzimandias » Mon Oct 31, 2011 4:38 am

Hello,

I am using openMP in C++ to improve execution inside a loop, when calling a Fortran90 subroutine.
At the moment, I am just testing the following : I generate an array, pass it by reference to f90 subroutine, I erase it inside f90 subroutine, then the value passed to c++ is written to a matrix that is shared during execution.
Matrix is of size size(array passed to f90) * (number of iterations).
Here is a snipped of code:
Code: Select all
#pragma omp parallel private(i)
    {
      #pragma omp  for 
      for (i=0;i<100;i++)
      {
        for  (j=0;j<7;j++)
          p_tdc[j]=i;

       MY_SUB(p_tdc);

       for(j=0;j<7;j++)
          matrix2[i][j]=p_tdc[j];
      }
    }


The problem I encounter is the following: in some cases, the p_tdc array is updated inside MY_SUB, while in others is not. The matrix should be filled with zeros but instead it contains in certain places values from the first initialization.( If i replace MY_SUB with a loop where p_tdc is erased, it all works well; everything else is problem free )
Code: Select all
Output example
...
7 7 7 7 7 7 7
0 0 0 0 0 0 0
0 0 0 0 0 0 0
1 1 1 1 1 1 1
2 2 2 2 2 2 2
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
...

What am I doing wrong? any suggestion would be appreciated .
ozzimandias
 
Posts: 4
Joined: Mon Oct 31, 2011 4:04 am

Re: Mixed C++/Fortran Problems

Postby ftinetti » Mon Oct 31, 2011 10:51 am

Hi,

First, I suggest to make this section of code just C++ code (without Fortran interop. in a first step) in order to avoid non-C OpenMP possible problems.

It would be better if you post a little bit more of information, e.g., p_tdc declaration.

I suggest declaring explicitly every variable in the parallel section as private, shared, ... I know this is rather long and there are several default rules, but I think it is a good idea at least in the initial OpenMP steps.

Specifically on p_tdc, I think there are race conditions on p_tdc[j], since every thread assigns values to p_tdc[j], j= 0, ... 6.

HTH.
ftinetti
 
Posts: 582
Joined: Wed Feb 10, 2010 2:44 pm

Re: Mixed C++/Fortran Problems

Postby ozzimandias » Tue Nov 01, 2011 3:13 am

First, I suggest to make this section of code just C++ code (without Fortran interop. in a first step) in order to avoid non-C OpenMP possible problems.

Yes, I did that, and it works. Errors are encountered only when calling Fortran, so that is where I don't understand what happens.
It would be better if you post a little bit more of information, e.g., p_tdc declaration.

Code: Select all
double * p_tdc;
int p_ns=7;
p_tdc=new double[p_ns]

The c++ project uses a *.dll and *.lib file generated from the fortran subroutine.
In the fortran file , p_tdc has the following declarations:
Code: Select all
!DEC$ ATTRIBUTES reference ::  p_tdc
real*8, dimension(p_ns)       ::  p_tdc

It really gives me a headache the mechanism involved in this combination. I mean, threads are created and for each value of "i" the code inside is executed sequentially...
ozzimandias
 
Posts: 4
Joined: Mon Oct 31, 2011 4:04 am

Re: Mixed C++/Fortran Problems

Postby ftinetti » Tue Nov 01, 2011 3:26 am

I see...

Are you changing your code in order to avoid the race conditions I mentioned in my previous post?

Again, it would be good to have a working version of the code which reproduces the problem to play around with... but now I re-read your code, I see it is possible that p_tdc is used only for assigning values to matrix2.... if this is the case, you can make p_tdc private to each thread (and malloc inside the parallel region).

I've seen !DEC$ and I'm curious about:
Computer, RAM, CPUs/cores, OS:
Fortran compiler and compiler options:
C++ compiler and compiler options:

HTH.
ftinetti
 
Posts: 582
Joined: Wed Feb 10, 2010 2:44 pm

Re: Mixed C++/Fortran Problems

Postby ozzimandias » Tue Nov 01, 2011 3:27 am

Ok, problem solved.

I had race conditions on p_tdc. The errors appeared due to the fact that p_tdc was dynamically allocated. So... even if I declared p_tdc as private, it was only the pointer that was duplicated in each thread, not necessary the memory area. So sometimes the areas were overlapping, thus the randomness involved in writing different values for p_tdc.

Solution : use same declaration, but allocate p_tdc in the parralel region. Thus a whole memory area is becoming private, not just the pointer.
Here is how one can do this:
Code: Select all
#pragma omp parallel  private(i,p_tdc)
{
      //allocate area
      p_tdc=(double *)calloc(p_ns,sizeof(double));
   
#pragma omp  for
      for (i=0;i<100;i++)
      {
           // my work is done here
           }
          //free area
          free(p_tdc);
}


I hope it helps others facing this problem ^^
ozzimandias
 
Posts: 4
Joined: Mon Oct 31, 2011 4:04 am

Re: Mixed C++/Fortran Problems

Postby ozzimandias » Tue Nov 01, 2011 3:30 am

Thank you for your advice, @ftinetti :)
ozzimandias
 
Posts: 4
Joined: Mon Oct 31, 2011 4:04 am


Return to Using OpenMP

Who is online

Users browsing this forum: Yahoo [Bot] and 9 guests

cron