1

I'm trying to build a BLAS shared library for use with ghostjat/np cannot get make to run successfully on the CBLAS source code. I performed these exact steps on an Ubuntu 20 workstation:

# create new directory
mkdir ~/blas
cd ~/blas
# fetch and extract the CBLAS source code linked from the BLAS page
wget http://www.netlib.org/blas/blast-forum/cblas.tgz
tar -xvzf cblas.tgz

#cd into the CBLAS dir
cd CBLAS

#get appropriate make file according to README:
rm Makefile.in
ln -s Makefile.LINUX Makefile.in

#then we try make
make

This results in an error because gfortran was not installed:

gfortran -O3   -c sdotsub.f
make[1]: gfortran: Command not found
make[1]: *** [Makefile:247: sdotsub.o] Error 127
make[1]: Leaving directory '/home/foo/biz/machine-learning/blas/CBLAS/src'
make: *** [Makefile:147: allprecision] Error 2

So I install gfortran

sudo apt install gfortran
# answer YES to prompts

I am then able to make most of the project, but it croaks with an error:

make[1]: Entering directory '/home/foo/biz/machine-learning/blas/CBLAS/testing'
gcc -I../include -O3 -DADD_ -c c_sblas1.c
gfortran -O3   -c c_sblat1.f
c_sblat1.f:214:48:

  214 |                CALL STEST1(SNRM2TEST(N,SX,INCX),STEMP,STEMP,SFAC)
      |                                                1
Warning: Rank mismatch in argument ‘strue1’ at (1) (scalar and rank-1) [-Wargument-mismatch]
c_sblat1.f:218:48:

  218 |                CALL STEST1(SASUMTEST(N,SX,INCX),STEMP,STEMP,SFAC)
      |                                                1
Warning: Rank mismatch in argument ‘strue1’ at (1) (scalar and rank-1) [-Wargument-mismatch]
gfortran  -o xscblat1 c_sblat1.o c_sblas1.o ../lib/cblas_LINUX.a libblas.a 
gfortran: error: libblas.a: No such file or directory
make[1]: *** [Makefile:72: xscblat1] Error 1
make[1]: Leaving directory '/home/foo/biz/machine-learning/blas/CBLAS/testing'
make: *** [Makefile:180: alltst] Error 2

What is the problem here? This is mostly greek to me, but it looks like it compiles successfully all the CBLAS source code except it seems to barf when it gets to the testing, complaining that it cannot find a file, libblas.a. Can someone help me make sure this make operation completes?

Also, I was expecting this compilation step to produce a shared library, perhaps cblas.so or something. I am hoping this process will yield a viable BLAS library that I can use with ghostjat/np to perform fast matrix operations from a PHP script. However, there are no files in this directory ending in .so. Should I be looking for some other file?

EDIT: the comments have suggested that perhaps I should 'install BLAS' or 'install the libopenblas-dev package' on this machine. Let me first say that my goal is to obtain a library that I might distribute with some PHP source code. I am under the impression that building/making CBLAS will provide this library.

EDIT 2: After attempting a lot of trial and error, I think (but am not sure) that CBLAS is not a full-blown implementation of the BLAS functionality, but just a C wrapper around the BLAS functions, which are written in FORTRAN. It would appear that the makefile in CBLAS must be changed to point to a BLAS static library. I've been able to build the BLAS 3.11.0 library like so:

cd ~/blas
curl https://netlib.org/blas/blas-3.11.0.tgz > blas-3.11.0.tgz
tar -xvzf blas-3.11.0.tgz
cd BLAS-3.11.0
make

this runs for about a minute or so and yields a static lib, blas_LINUX.a. I take note of this file's location: /Users/foo/Desktop/biz/machine-learning/blas2/BLAS-3.11.0/blas_LINUX.a.

I then return to my previously downloaded/extracted CBLAS folder:

cd ~/blas/CBLAS

and note this information in the README file:

BLLIB is your Legacy BLAS library

I edit this line in Makefile.in:

BLLIB = libblas.a

so that it refers instead to the static blas_LINUX. I just compiled above:

BLLIB = /Users/foo/Desktop/biz/machine-learning/blas2/BLAS-3.11.0/blas_LINUX.a

I save the make file and then make CBLAS:

make clean all

This runs for awhile, but fails in the testing phase with a certain gfortrain complaint:

( cd testing && make all )
gcc -I../include -O3 -DADD_ -c c_sblas1.c
gfortran -O3   -c c_sblat1.f
c_sblat1.f:214:48:

  214 |                CALL STEST1(SNRM2TEST(N,SX,INCX),STEMP,STEMP,SFAC)
      |                                                1
Error: Rank mismatch in argument 'strue1' at (1) (scalar and rank-1)
c_sblat1.f:218:48:

  218 |                CALL STEST1(SASUMTEST(N,SX,INCX),STEMP,STEMP,SFAC)
      |                                                1
Error: Rank mismatch in argument 'strue1' at (1) (scalar and rank-1)
make[1]: *** [c_sblat1.o] Error 1
make: *** [alltst] Error 2
S. Imp
  • 2,833
  • 11
  • 24
  • Did you actually install BLAS? Do you actually have libblas.a or libblas.so? – Vladimir F Героям слава Dec 06 '22 at 06:47
  • @VladimirFГероямслава can you elaborate? I'm compiling CBLAS from source. The source includes numerous files that appear to be FORTRAN, e.g. **sdsdotsub.f**. I would add that the ubuntu machine I attempted this on has octave installed, which put a blas library at `/usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so` – S. Imp Dec 06 '22 at 18:58
  • libooenblas is not libblas, your makefile is looking for libblas. Either install BLAS with libblas or make your Makefile toblink with libopenblas. But you should be able to install CBLAS from your repositories as well. You normally do not have to compile it from source. It might even be already installed somewhere. – Vladimir F Героям слава Dec 07 '22 at 05:42
  • And do you actually even need CBLAS? In your first sentence you write you are trying to compile BLAS. – Vladimir F Героям слава Dec 07 '22 at 05:47
  • @VladimirFГероямслава i'm confused that cblas would require one to have already installed blas. I was under the impression, perhaps wrong, that making cblas would install blas? Also, I have edited my post, and you can see that the machine already has a libblas3 package installed. I'm not sure I need CBLAS or not. As I described in my post, I'm trying to get access to fast matrix operations in PHP by using [FFI](https://www.php.net/manual/en/class.ffi.php). I *think* FFI does need C functions to work. – S. Imp Dec 07 '22 at 12:36
  • @VladimirFГероямслава I think I understand now that CBLAS does not itself contain the BLAS functions I want, and I must separately acquire the [BLAS library](http://www.netlib.org/blas/blas-3.11.0.tgz). I have downloaded and made/built that BLAS lib and modified the Makefile in my CBLAS folder to point to that library and this helps, but the CBLAS code still halts in testing with a gfortran complaint. I have modified my original post with the additional detail. – S. Imp Dec 09 '22 at 12:09
  • Pragmatically, you would probably be better off by just using the binary Openblas package that contains both BLAS and CBLAS. To understand the latest error, we need the relevant source code. At the very least we need what the symbols in the error message are. – Vladimir F Героям слава Dec 09 '22 at 12:14

2 Answers2

0

Not a direct answer, but since you're on Ubuntu you can just install the libopenblas-dev package which contains the cblas headers and will pull in a high performance BLAS library as a dependency.

janneb
  • 36,249
  • 2
  • 81
  • 97
  • 1
    I wonder whether it will actually fix this error in compulation of this software or whether one will have to manually change -lblas and -llapack to a single -lopenblas somewhere in the Makefile or equivalent. When I use OpenBLAS, I usually have to do the latter. – Vladimir F Героям слава Dec 06 '22 at 13:07
  • Might not get all the way to the goal OP has, but at least a step closer. – janneb Dec 06 '22 at 14:47
  • @janneb the ubuntu workstation where i'm trying to build CBLAS already has some blas libs installed. I've edited my original post to provide more detail. If I need to make changes to the makefile or something, I'll need some pretty specific advice. – S. Imp Dec 07 '22 at 00:14
  • @S.Imp Based on your edit, you have libopenblas installed which is good. But you also need the libopenblas-dev package to provide the cblas.h header file (and the static library in case you want to distribute some kind of self-contained package). – janneb Dec 07 '22 at 05:44
  • @janneb it is completely confusing to me why I'd need libopenblas when I'm attempting to compile CBLAS from source. Are they not basically the same thing? – S. Imp Dec 07 '22 at 12:37
  • 1
    @S.Imp: libopenblas provides both the BLAS and CBLAS API's, so I'm trying to help you install libopenblas-dev, in which case you would no longer need to care about compiling CBLAS and BLAS yourself. – janneb Dec 07 '22 at 13:56
  • @janneb I have separately downloaded [OpenBlas](https://github.com/xianyi/OpenBLAS) and that project builds successfully. However, it's massive and overkill, i think, for my purposes. I'd like to try and get CBLAS built from source. I've made additional progress by downloading and building BLAS, but I'm still encountering a problem. I've modified my OP with additional detail. – S. Imp Dec 09 '22 at 12:12
0

I stumbled across these directions which have worked for me, at least on Ubuntu 20.04. A couple of things changed so I list the exact steps I took here on my Ubuntu 20.04 workstation. The basic solution is to first compile BLAS (this appears to be FORTRAIN code) into a static library, blas_LINUX.a, and then modify the CBLAS files to point to that static library. There are tgz archives for both on the BLAS homepage.

# make a working dir
mkdir ~/cblas
cd ~/cblas
# fetch the BLAS library (not CBLAS, just BLA)S
wget http://www.netlib.org/blas/blas-3.11.0.tgz
tar -xvzf blas-3.11.0.tgz
cd BLAS-3.11.0
make

This will produce a file, blas_LINUX.a. Take note of its location here, you'll need to refer to it in the CBLAS make file. Next, fetch CBLAS and compile.

# get out of this directory back to our working dir
cd ..
# fetch CBLAS tgz, linked from netlib page
wget http://www.netlib.org/blas/blast-forum/cblas.tgz
# extract it
tar -cvzf cblas.tgz
# cd into CBLAS dir for edits
cd CBLAS
# remove default makefile
rm Makefile.in
# copy LINUX make file to Makefile.in
ln -s Makefile.LINUX Makefile.in
# edit Makefile.in
nano Makefile.in

Make the following changes:

# path to just-compiled static lib
# NOTE your path will be different
BLLIB = /home/sneakyimp/cblas/BLAS-3.11.0/blas_LINUX.a
CBLIB = ../lib/cblas_$(PLAT).so

CFLAGS = -O3 -DADD_ -fPIC

FFLAGS = -O3 -fPIC

ARCH = gcc

ARCHFLAGS = -shared -o

Save & close Makefile.in and then make CBLAS

# this takes a bit of time
make
# ls -al lib

You should now have your so file in lib/cblas_LINUX.so

I was able edit the blas.h file in a composer-installed ghostjat/np to point to this cblas_LINUX.so file, and eventually get it working, but you'll have complaints about various functions in the that blas.h file which are not defined in CBLAS. If you remove each of the functions it complains about you may get it to work or not. I was able to get it working on Ubuntu 20 running php 8.2, but have had trouble on other machines. My attempt to run cblas_dgemm on a matrix i defined resulted in this error:

php: symbol lookup error: /home/sneakyimp/cblas/CBLAS/lib/cblas_LINUX.so: undefined symbol: dgemm_
S. Imp
  • 2,833
  • 11
  • 24