llvm-new-flang / workload-by-flang

The guide to build workloads using LLVM Flang
Apache License 2.0
0 stars 0 forks source link

Build BLAS and CBLAS using LLVM FLang #3

Open Wang-Zhewei opened 2 years ago

Wang-Zhewei commented 2 years ago

The source code for BLAS and CBLAS can be found from the following website: https://netlib.org/blas/

$ mkdir workload && cd workload && mkdir blas && cd blas
$ wget http://www.netlib.org/blas/blas-3.10.0.tgz
$ wget http://www.netlib.org/blas/blast-forum/cblas.tgz
$ tar zxvf blas-3.10.0.tgz && cd BLAS-3.10.0
$ flang-new -flang-experimental-exec -c -O3 *.f
$ flang-new -flang-experimental-exec -c -O3 *.f90
$ ar rv libblas.a *.o
$ cp libblas.a /usr/local/lib
$ cd ../ && tar zxvf cblas.tgz && cd CBLAS
$ cp ../BLAS-3.10.0/libblas.a testing/
$ cp Makefile.LINUX Makefile.in

To build BLAS with LLVM Flang, we need to modify the compilation options in the Makefile.in file.

$ vim Makefile.in
#
# Makefile.LINUX
#
#
# If you compile, change the name to Makefile.in.
#
#

#-----------------------------------------------------------------------------
# Shell
#-----------------------------------------------------------------------------

SHELL = /bin/sh

#-----------------------------------------------------------------------------
# Platform
#-----------------------------------------------------------------------------

PLAT = LINUX

#-----------------------------------------------------------------------------
# Libraries and includs
#-----------------------------------------------------------------------------

BLLIB = libblas.a
CBLIB = ../lib/cblas_$(PLAT).a

#-----------------------------------------------------------------------------
# Compilers
#-----------------------------------------------------------------------------

CC = clang
FC = flang-new -flang-experimental-exec
LOADER = $(FC)

#-----------------------------------------------------------------------------
# Flags for Compilers
#-----------------------------------------------------------------------------

CFLAGS = -O3 -DADD_
FFLAGS = -O3

#-----------------------------------------------------------------------------
# Archive programs and flags
#-----------------------------------------------------------------------------

ARCH = ar
ARCHFLAGS = r
RANLIB = echo

Save it and make.

$ make

Some errors were generated at this step of 'make'.

make[1]: Entering directory '/root/workload/blas/CBLAS/testing'
clang -I../include -O3 -DADD_ -c c_sblas1.c
flang-new -flang-experimental-exec -O3   -c c_sblat1.f
flang-new: warning: argument unused during compilation: '-flang-experimental-exec'
flang-new -flang-experimental-exec  -o xscblat1 c_sblat1.o c_sblas1.o ../lib/cblas_LINUX.a libblas.a 
../lib/cblas_LINUX.a(cblas_srotg.o): In function `cblas_srotg':
cblas_srotg.c:(.text+0x0): undefined reference to `srotg_'
../lib/cblas_LINUX.a(snrm2sub.o): In function `snrm2sub_':
/root/workload/blas/CBLAS/src/./snrm2sub.f:13: undefined reference to `snrm2_'
../lib/cblas_LINUX.a(scnrm2sub.o): In function `scnrm2sub_':
/root/workload/blas/CBLAS/src/./scnrm2sub.f:13: undefined reference to `scnrm2_'
flang-new: error: linker command failed with exit code 1 (use -v to see invocation)
Makefile:72: recipe for target 'xscblat1' failed
make[1]: *** [xscblat1] Error 1
make[1]: Leaving directory '/root/workload/blas/CBLAS/testing'
Makefile:180: recipe for target 'alltst' failed
make: *** [alltst] Error 2
Wang-Zhewei commented 2 years ago

@PeixinQiao I think I know what's wrong. The command flang-new -flang-experimental-exec -c -O3 *.f only compiles .f files, and scnrm2.f90 needs to be compiled separately with the command flang-new -flang-experimental-exec -c -O3 *.f90. Then libblas.a can be used to make CBLAS successfully.

PeixinQiao commented 2 years ago

@Wang-Zhewei Good job.

Wang-Zhewei commented 2 years ago

@PeixinQiao I did build blas and cblas and put the static link libraries libblas.a and libcblas.a in the /usr/local/lib folder, but when I compile in C it still doesn't pass. test_blas.c

#include <stdio.h>
#include <cblas.h>

int main()
{
        const int dim=2;
        double a[4]={1.0,1.0,1.0,1.0},b[4]={2.0,2.0,2.0,2.0},c[4];
        int m=dim,n=dim,k=dim,lda=dim,ldb=dim,ldc=dim;
        double al=1.0,be=0.0;
        cblas_dgemm(101,111,111,m,n,k,al,a,lda,b,ldb,be,c,ldc);
        printf("the matrix c is:%f,%f\n%f,%f\n",c[0],c[1],c[2],c[3]);
        return 0;
}

The command line I used is clang test_blas.c -o test /usr/local/lib/libcblas.a /usr/local/lib/libblas.a -lm -I /root/workload/blas/CBLAS/include/. The compilation result:

/usr/local/lib/libblas.a(lsame.o): In function `lsame_':
/root/workload/blas/BLAS-3.10.0/./lsame.f:73: undefined reference to `_FortranACharacterCompareScalar1'
/usr/local/lib/libblas.a(xerbla.o): In function `xerbla_':
/root/workload/blas/BLAS-3.10.0/./xerbla.f:77: undefined reference to `_FortranAioBeginExternalFormattedOutput'
/root/workload/blas/BLAS-3.10.0/./xerbla.f:77: undefined reference to `_FortranAioOutputAscii'
/root/workload/blas/BLAS-3.10.0/./xerbla.f:77: undefined reference to `_FortranAioOutputInteger32'
/root/workload/blas/BLAS-3.10.0/./xerbla.f:77: undefined reference to `_FortranAioEndIoStatement'
/root/workload/blas/BLAS-3.10.0/./xerbla.f:79: undefined reference to `_FortranAStopStatement'
clang-16: error: linker command failed with exit code 1 (use -v to see invocation)

I feel like I should add some flang-new compile options, but I don't know how to add it correctly.

PeixinQiao commented 2 years ago

@Wang-Zhewei You can learn how to solve this problem here.

Wang-Zhewei commented 2 years ago

@PeixinQiao It seems that clang does not link some dynamic or static libraries by itself. Meanwhile, although I have added the relevant environment variables in LD_LIBRARY_PATH, the -l command does not help it to find some libraries. So I try to link them manually and generate the corresponding executable file. This is the command line I used clang ~/test/test_blas.c -o test /usr/local/lib/libcblas.a /usr/local/lib/libblas.a ~/flang-install-f18/install/lib/*.so -lm. But when I run the "./test" command, it prints the following error:

: CommandLine Error: Option 'limited-coverage-experimental' registered more than once!
LLVM ERROR: inconsistency in registered CommandLine options
Aborted (core dumped)

And I found an issud about this: https://github.com/fireice-uk/xmr-stak/issues/1782 So I don't know how to solve it

PeixinQiao commented 2 years ago

Can you try the following?

clang ~/test/test_blas.c -o test /usr/local/lib/libcblas.a /usr/local/lib/libblas.a  -lm -I/path-to-f18-install/install/lib -lFortran_main -lFortranRuntime -lFortranDecimal

Don't link all the so files. It's one very bad idea.

Wang-Zhewei commented 2 years ago

@PeixinQiao I tried this command: clang ~/test/test_blas.c -o test /usr/local/lib/libcblas.a /usr/local/lib/libblas.a -lm -I/root/flang-install-f18/install/lib/ -lFortran_main -lFortranRuntime -lFortranDecimal But it reported some errors:

/usr/bin/ld: cannot find -lFortran_main
/usr/bin/ld: cannot find -lFortranRuntime
/usr/bin/ld: cannot find -lFortranDecimal
clang-16: error: linker command failed with exit code 1 (use -v to see invocation)