[R] Question about building library and BLAS
Douglas Bates
bates at stat.wisc.edu
Mon May 31 18:39:38 CEST 2004
Douglas Bates <bates at cs.wisc.edu> writes:
> Zhu Wang <zhuw at mail.smu.edu> writes:
>
> > I am trying to create a library which uses some Fortran source files
>
> Someone named Martin Maechler will shortly be sending you email
> regarding the distinction between 'library' and 'package' :-)
>
> (You are creating a package, not a library, despite the fact that
> you will later attach it using a function called 'library'.)
>
> > and Lapack and Blas subroutines. The Fortran source files from the
> > original author contain subroutines isamax.f, sgefa.f and sgesl.f,
> > which are part of BLAS subroutines on my Linux computer, but maybe
> > different (old) versions. So in addition to these subroutines, there
> > are other Lapack and Blas subroutines involved. There is no problem
> > to compile and run these files using g77, such as the following to
> > create car:
> >
> > g77 -mieee-fp -fPIC -O2 -g -pipe -march=i386 -mcpu=i686 car
> > isamax.f, sgefa.f sgesl.f foo1.f ... foo20.f -llapack -lblas
> >
> > By doing this, the procedure does not use subroutines isamax.f, sgefa.f sgesl.f in BLAS,
> > as expected. In fact, there are problems to use these subroutines in BLAS, for some reason.
> >
> > Now what I want is to build an R library. The Makefile is the following:
> >
> > LIBNAME=car
> >
> > PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
> >
> > OBJS=isamax.o saxpy.o sscal.o foo1.o ... foo20.o -llapack -lblas
> >
> > $(LIBNAME)$(SHLIB_EXT): $(OBJS)
> > $(SHLIB_LD) $(SHLIB_LDFLAGS) -o $@ $(OBJS) $(FLIBS)
> >
> > clean:
> > @rm -f *.o *.$(SHLIB_EXT)
> >
> > realclean: clean
> >
> > Some compiling outputs are the following:
> >
> > g77 -mieee-fp -fPIC -O2 -g -pipe -march=i386 -mcpu=i686 -c isamax.f
> > -o isamax.o
> > ...
> > g77 -mieee-fp -fPIC -O2 -g -pipe -march=i386 -mcpu=i686 -c foo1.f -o
> > foo1.o
> > ......
> > gcc -shared -o car.so isamax.o ...... foo20.o -llapack -lblas
> > -L/usr/local/lib -L/usr/lib/gcc-lib/i386-redhat-linux/3.3.2
> > -L/usr/lib/gcc-lib/i386-redhat-linux/3.3.2/../../.. -lfrtbegin -lg2c
> > -lm -lgcc_s
> >
> > However, it turns out that this process did not take into account these
> > three files isamax, sgefa.f and sgesl.f.
> > Instead, it used subroutines in Blas, but again for some reason, it provided
> > error.
>
> I think you are working too hard. Temporarily move the source files
> for the BLAS and Lapack routines to backup names, such as
> isamax.f.old, then do the same to the Makefile (i.e. move it to
> Makefile.old) then create a file called Makevars containing
>
> PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
I should have read your message more carefully. Those three Fortran
routines that you mention are single precision and it would be silly
to use them on modern computers. R only provides single precision
floating point for compatibility. All numerical linear algebra is
doing in double precision. Check where sgefa and sgesl are being
called and see if they really need to be in single precision. I'm
sure if you replace them by calls to dgefa and dgesl (and suitably
change the precision of the arguments) they will run as fast as
before. In fact you can probably use the Lapack routines dgetrf and
dgetrs instead and get a performance boost.
More information about the R-help
mailing list