Troubleshooting a Fortran 77 Program with OpenMP

In summary, I'm trying to get a fortran 77 program to run faster on a computer with a dual-core processor, so I'm trying to learn how OpenMP works. Unfortunately, I can't get the "hello world" file on the wikipedia page to compile. I'm using G77 as a compiler. I think I'm supposed to compile it as
  • #1
JoAuSc
198
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
  • #2
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).
 
  • #3
What version of GCC are you using? (i.e. g77 -v)
 
  • #4
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:
  • #5
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.
 
  • #6
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.
 
  • #7
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.
 
  • #8
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?
 
  • #9
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
 

1. How do I enable OpenMP in my Fortran 77 program?

To enable OpenMP in your Fortran 77 program, you need to add the !$OMP PARALLEL directive before the code block that you want to parallelize. You also need to compile your program with the -fopenmp flag.

2. Why is my Fortran 77 program not showing any speedup with OpenMP?

There could be several reasons for this. One common mistake is not using the !$OMP PARALLEL directive properly. Make sure that the code block you want to parallelize is placed inside this directive. Another reason could be that the workload is not large enough to see a significant speedup. Also, check if your compiler supports OpenMP and if it is properly configured.

3. Can I use OpenMP in a Fortran 77 program with multiple subroutines?

Yes, you can use OpenMP in a Fortran 77 program with multiple subroutines. However, you need to make sure that each subroutine is called within its own !$OMP PARALLEL directive. Otherwise, all subroutines will run in parallel, which may not be desired.

4. How do I control the number of threads in my Fortran 77 program using OpenMP?

You can control the number of threads in your Fortran 77 program using the OMP_NUM_THREADS environment variable. Set this variable to the desired number of threads before running your program. Alternatively, you can also use the OMP_SET_NUM_THREADS subroutine to set the number of threads within your code.

5. Is it possible to debug a Fortran 77 program with OpenMP?

Yes, it is possible to debug a Fortran 77 program with OpenMP. However, debugging parallel programs can be challenging. You can use tools like gdb or totalview to debug your program. You may also need to add additional debug statements or use parallel debugging techniques to identify and fix errors in your code.

Similar threads

  • Programming and Computer Science
2
Replies
37
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
2
Views
5K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
4
Views
616
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
Back
Top