Fortran Troubleshooting a Fortran 77 Program with OpenMP

AI Thread Summary
The discussion centers on difficulties encountered while trying to compile a Fortran 77 program using OpenMP for parallel processing on a dual-core processor. The user is attempting to run a simple "hello world" program but faces compilation errors related to undefined references for OpenMP functions. The primary issue appears to stem from using the g77 compiler, which may not fully support OpenMP, especially in version 3.4.6. Suggestions include ensuring the correct linking to the OpenMP library and using the appropriate compiler flags, such as -fopenmp, although this flag is not recognized by g77. There is also confusion regarding the use of gfortran, which is a more modern Fortran compiler that supports OpenMP. The user is advised that .exe files typically cannot be run directly in Unix without an emulator like WINE. Overall, the conversation highlights the challenges of using outdated compilers for modern programming standards and the need for proper library linking to utilize OpenMP effectively.
JoAuSc
Messages
197
Reaction score
1
I'm trying to get a fortran 77 program to run faster on a computer with a dual-core processer, so I'm trying to learn how OpenMP works. Unfortunately, I can't get the "hello world" file on the wikipedia page to compile.

(Here's the code:
Code:
      PROGRAM HELLO
      INTEGER ID, NTHRDS
      INTEGER OMP_GET_THREAD_NUM, OMP_GET_NUM_THREADS
!$OMP PARALLEL PRIVATE(ID)
      ID = OMP_GET_THREAD_NUM()
      PRINT *, 'HELLO WORLD FROM THREAD', ID
!$OMP BARRIER
      IF ( ID .EQ. 0 ) THEN
        NTHRDS = OMP_GET_NUM_THREADS()
        PRINT *, 'THERE ARE', NTHRDS, 'THREADS'
      END IF
!$OMP END PARALLEL
      END

I'm using G77 as a compiler. I think I'm supposed to compile it as

Code:
...> g77 HelloWorld.f -openmp

I get the following error:

Code:
/tmp/cc2XahMo.o(.text+0xe): In function 'MAIN__':
: undefined reference to 'omp_get_thread_num__'
/tmp/cc2XahMo.o(.text+0x75): In function 'MAIN__':
: undefined reference to 'omp_get_num_threads__'
collect2: ld returned 1 exit status

I don't think the problem's that g77 doesn't accept openmp, because if I have

Code:
...> g77 -openmp

without the filename, it has a different error message than replacing "-openmp" with "-gdfgs" or something, specifically,
Code:
g77: no input files; unwilling to write output files
rather than
Code:
g77: no input files

Could someone who knows something about g77 or openMP help me?
 
Technology news on Phys.org
Most likely the problem is that the openMP library is meant to link to C or C++ programs not Fortran.

The clue is the fact that the unresolved reference is omp_get_thread_num__ (with some underscores at the end) not omp_get_thread_num which is presumably the C function you are trying to call.

Either you need a Fortran interface library, or you need to tell your compiler these are C functions. I expect there's a way to do that, but I don't have g77.

FWIW the command g77 -openmp is probably trying to create an object file called penmp.o, or an executable called penmp or penmp.exe, independent of the name of your .f file - which is a legal command, but not what you wanted to do!

Also FWIW some compiler systems understand the option -mp (not -openmp).
 
What version of GCC are you using? (i.e. g77 -v)
 
You need to tell g77 to link to your OpenMP library file with the -l option. something like
Code:
g77 helloworld.f -o helloworld -l<openmp library name>
where you replace <openmp library name> with the name of the OpenMP library minus the leading 'lib' and the extension
i.e. if your openMP library is called libopenmp.so (or libopenmp.a), then you use libopenmp

you might also have to specify the path to your OpenMP library with -L if it's not in the compiler's library search path

It's been a while since I worked with g77, but since it's just a front-end to gcc now, it should work. I've also not worked with OpenMP before, so I'm not positive there's a library that goes with it but if there is this is what you'd need to do.
 
Last edited:
I was able to build using GCC 4.2. As far as I understand, GCC did not support openmp prior to 4.2 (although some people built it into 4.1 on a provisional basis, e.g. fedora core 5/6).

I built GCC 4.2, than ran

/my/gcc4.2dir/gfortran -fopenmp hello_f77.f

with GCC 4.2 directories (including gmp and mpfr) at the front of LD_LIBRARY_PATH.

Not sure g77 will do that for you, as g77 is an artifact of pre-4.0 (unless you renamed your GCC 4.2 gfortran g77).

Maybe simply linking against the openmp lib will do it, pls let us know if that works.
 
Aleph:
Yes, -openmp was apparently trying to create a file named penmp.out or something. I must've read this site, which has the tag as -fopenmp, and forgot the f. Unfortunately, here's what I got:
Code:
...> g77 HelloWorld.f -fopenmp
f771: error: unrecognized command line option "-fopenmp"

I also tried -mp and -MP, but neither worked.

FYI, I'm using g77 which was installed on my school's computers, and I'm using tera term (unix).


nmtim, here's what I got for g77 -v:
Code:
...> g77 -v
Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.6/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/u
sr/shar/info --enable-shared --enable-threads=posix --disable-checking --with-s
ystem-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-aw
t=gtk --host=x86_64-redhat-linux
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-8)
So, to answer your question, version 3.4.6.

imabug: I'd try that, but I'm not sure where the OpenMP library is.
 
After doing a little more reading, I get the impression that OpenMP is a set of compiler extensions that's usually built in, so there may not be a library. nmtim is probably correct and your compiler version may not support the OpenMP extensions.
 
I'm guessing nmtim is right about openMP not being included.


So, next question: A while back I installed gfortran on the computer in my directory. However, I can't figure out how to use it in unix. (The file is gfortran.exe, and I've used it in the DOS command prompt without problems.) Can you run .exe files in a unix terminal?
 
JoAuSc said:
I'm guessing nmtim is right about openMP not being included.


So, next question: A while back I installed gfortran on the computer in my directory. However, I can't figure out how to use it in unix. (The file is gfortran.exe, and I've used it in the DOS command prompt without problems.) Can you run .exe files in a unix terminal?

It might run through WINE, but generally, no
 

Similar threads

Replies
8
Views
4K
Replies
6
Views
3K
Replies
5
Views
3K
Replies
2
Views
3K
Replies
5
Views
10K
Replies
8
Views
4K
Back
Top