OpenMP and Fortran90/C++ problems

  • Context: C/C++ 
  • Thread starter Thread starter ozzimandias
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the integration of Fortran and C++ using OpenMP for parallel execution in a simulation program. Participants explore performance issues related to execution speed and the correct method for passing data structures, specifically matrices, between C++ and Fortran.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant notes a low speedup (20%) when using OpenMP, questioning the efficiency of hyper-threading and its impact on performance.
  • Another participant suggests that memory bandwidth limitations and random memory access could hinder parallel processing benefits.
  • There is a discussion on the efficiency of thread creation in OpenMP, with a participant raising concerns about the number of threads versus workload distribution.
  • One participant confirms that passing a matrix to Fortran is similar to passing an array, emphasizing the need for consistent data representation across languages.
  • Another participant highlights the importance of understanding memory layout when passing matrices, suggesting printing memory addresses to clarify structure.
  • Concerns are raised about differences in matrix storage between C++ and Fortran, which could complicate data passing.

Areas of Agreement / Disagreement

Participants express differing views on the performance implications of hyper-threading and the effectiveness of parallel execution. The discussion about passing matrices remains unresolved, with multiple perspectives on the challenges involved.

Contextual Notes

Participants mention potential issues with data representation and memory layout when interfacing between C++ and Fortran, but do not resolve these concerns. The discussion also reflects uncertainty regarding the efficiency of hyper-threading in specific computational contexts.

ozzimandias
Messages
5
Reaction score
0
Hello,

I need to call a Fortran DLL from C++. My program simulates difussion of some substances, I won't go into details.
I generate my data in C++ and I pass it to the Fortran code which does all the computation part. I repeat this a number of times and try to implement parallel execution using OpenMP.
Everything runs well, I actually try to test the execution speed for the fortran subroutine.
My code is run on an Intel P4. 1 physical core, 2 virtual cores available for multithreading.
Here is some c++ code:
Code:
#pragma omp parallel
    {
	   #pragma omp  for  
	   for (i=1;i<=1000;i++)
	   {
		 ///here i generate some random data  : x,y,z...
		 ///here i call fortran with that data:     fortran_subroutine(x,y,z...)
	   }
    }

I would like to get an answer for the following questions:
1. The execution without openMP takes ~15s and with openMP enabled ~12s. Why is the speedup so low (20% improvement in execution speed)? I mean, I know it shouldn't it be around twice, but still... I don't have I/O operations inside the Fortran subroutine.

2. To pass an array by reference from c++->fortran you do the following:
Code:
int *array;
array=new double[some_size];
//initialize array;
//write array[i] before 
call_fortran(array);
//write array[i] after
The array will be modified inside fortran, then passed to c++.
Now my question is: How do you do the same thing with a matrix ?(i.e. declaration, passing it by reference to fortran)
 
Technology news on Phys.org
I"m not sure how efficient hyperthreading is, depending on the type of instructions being peformed, such as integer versus floating point operations. If the program is memory bandwidth limited, then parallel processing won't help much. A lot of random accessing of memory can be an issue also, since caching won't be as effective.
 
I'm not familar with the syntax of using OMP, but depending how much work is done for each value of i, it could make a big difference if you are creating 1000 separate threads, one for each value of i, or just 2 threads with 500 values of i processed by each thread.
 
ozzimandias said:
Hello,
Now my question is: How do you do the same thing with a matrix ?(i.e. declaration, passing it by reference to fortran)

Its the same thing. You just pass a pointer (machine word containing a memory address) to the routine.

The only thing you have to be careful about, especially in mixed language/platform environments, is that both implementations treat the data the exact same way. If this is not the case then you will have problems.

If the two platforms have different definitions of a float, or if for some reason one compiler added baggage data or optimized away something that it should have, this is what you need to look out for.

Otherwise, it should work perfectly. Obviously if your matrix is not static, then you need to tell the DLL about these parameters, but that is another issue.

Just to help us out, how are you declaring the matrix in C++?
 
I got the answer for one of my questions.
It seems that Intel's hyper-threading technology offers an improvement of up to 30% in execution times for multi-threaded applications. So I think that's the reason I don't get such a performance boost. I tested the application on a multicore CPU and the improvement is obvious something around twice as fast.
As for the problem regarding how I pass the matrix, it's still unsolved. Fortran stores differently matrices so that might be a problem but that's to be taken into account only after I successfully pass the matrix, which I don't (yet).
Just to help us out, how are you declaring the matrix in C++?
Code:
double **my_matrix
Obviously if your matrix is not static, then you need to tell the DLL about these parameters
That's taken care of.
Code:
!DEC$ ATTRIBUTES reference :: my_matrix
 
ozzimandias said:
I got the answer for one of my questions.
It seems that Intel's hyper-threading technology offers an improvement of up to 30% in execution times for multi-threaded applications. So I think that's the reason I don't get such a performance boost. I tested the application on a multicore CPU and the improvement is obvious something around twice as fast.
As for the problem regarding how I pass the matrix, it's still unsolved. Fortran stores differently matrices so that might be a problem but that's to be taken into account only after I successfully pass the matrix, which I don't (yet).

Code:
double **my_matrix

That's taken care of.
Code:
!DEC$ ATTRIBUTES reference :: my_matrix

Hey ozzimandias.

It's been a while since I've done any serious programming, but what I would recommend you do is to do a simple loop that prints out the memory address of each cell.

This should help you figure out the exact layout in memory of each cell, so you can gaurantee the structure that is fed into your fortran dll.

What I think is that the way it is stored, it stores each row continguosly in memory but I could be wrong. The best way to clarify this is to print the actual memory address of each cell: I don't want to generalize my experience because I could be wrong.

Also chances are the the float data structure is the same because most compilers use the native CPU commands and regardless of whether it was compiled from C or Fortran, the data structure for floats and doubles will be the same. Chances are if the size of the float is the same between platforms, the structure and the representation itself will also be the same.

This isn't gauranteed of course, but its common simply for the fact that compilers would rather use native CPU instructions than something that is say intermediate and slower.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 11 ·
Replies
11
Views
3K
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
8K
Replies
11
Views
16K
  • · Replies 5 ·
Replies
5
Views
2K