clapack package example

Pages: 12
I download the CLAPACK example from here https://icl.cs.utk.edu/lapack-for-windows/clapack/CLAPACK-EXAMPLE.zip and also download the prebuilt libraries in release mode from the same webpage https://icl.cs.utk.edu/lapack-for-windows/libraries/CLAPACK/blas.lib , https://icl.cs.utk.edu/lapack-for-windows/libraries/CLAPACK/libf2c.lib , https://icl.cs.utk.edu/lapack-for-windows/libraries/CLAPACK/lapack.lib and just copied these libraries into lib folder inside the project folder. and also added the blas.lib, lapack.lib and libf2c.lib as input in the linker. the I run the example it give an error similar to what I get before.
here is the error:
synatx error: '(' .
and it refers to this line
Check_return int __cdecl abs(In int _X);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "f2c.h"
#include "clapack.h"
#include <math.h>

#define NDIM 4 

int main() {

    int i, j, info2;
    int N, NRHS, LDA, LDB;
    double* A;
    double* B;
    static int IPIV[NDIM], INFO;

    A = (double*)malloc(NDIM * NDIM * sizeof(double));
    B = (double*)malloc(NDIM * sizeof(double));

    N = NDIM; NRHS = 1; LDA = NDIM; LDB = NDIM;

    A[0] = 1.0;
    A[4] = -1.0;
    A[8] = 2.0;
    A[12] = -1.0;
    A[1] = 2.0;
    A[5] = -2.0;
    A[9] = 3.0;
    A[13] = -3.0;
    A[2] = 1.0;
    A[6] = 1.0;
    A[10] = 1.0;
    A[14] = 0.0;
    A[3] = 1.0;
    A[7] = -1.0;
    A[11] = 4.0;
    A[15] = 3.0;

    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            printf("   %f  \n", A[i + N * j]);
        }
    }

    B[0] = -8.0;
    B[1] = -20.0;
    B[2] = -2.0;
    B[3] = 4.0;


    dgesv_(&N, &NRHS, A, &LDA, &IPIV, B, &LDB, &INFO);

    printf("info %d \n", INFO);

    for (i = 0; i < N; i++)
        printf("   %f \n", B[i]);
}
Last edited on
#include "f2c.h"
Oooh! Best solution: use Fortran! That's what lapack is written in.


Alternatively: put
#include <math.h>
BEFORE the other two headers. Actually, I think you need so stdio.h and stdlib.h as well, so your headers (in order) should look like
1
2
3
4
5
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "f2c.h"
#include "clapack.h" 



Depending on your compiler, you might have to adjust one typedef in f2c.h. Change
typedef long int integer;
to
typedef int integer;
Things can get VERY fussy when calling Fortran routines from C.


Your call to dgesv_ appears to be wrong, since IPIV is already an array (and passed as a pointer), just like A and B. Are you sure that it shouldn't be
dgesv_(&N, &NRHS, A, &LDA, IPIV, B, &LDB, &INFO); // no & before IPIV


What compiler are you using? I can't get the gnu compiler (gcc) to link those libraries correctly in windows. Are they for the MSVC compiler (cl.exe)? Are they for x86 rather than x64?
Last edited on
@lastchance I just followed the instruction and example provided in this link https://icl.cs.utk.edu/lapack-for-windows/clapack/index.html. I used prebuilt library and headers provided there. I am using visual studio 2022. using Fortran is not an option for me.
@lastchance if your are trying to build libraries by yourself you can build the library like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cd % LOCALAPPDATA% 
del clapack-3.2.1-CMAKE.tgz
del clapack-3.2.1-CMAKE.tar
curl -O https://icl.cs.utk.edu/lapack-for-windows/clapack/clapack-3.2.1-CMAKE.tgz
"c:\Program Files\7-Zip\7z.exe" x clapack-3.2.1-CMAKE.tgz
rmdir /s /q clapack-3.2.1-CMAKE
" c:\Program Files\7-Zip\7z.exe" x clapack-3.2.1-CMAKE.tar

cd % LOCALAPPDATA% 
cd clapack-3.2.1-CMAKE
rmdir /s /q build
mkdir build
cd build

del CMakeCache.txt
cmake -G "Visual Studio 17 2022" -T host=x64 ^
    -DUSE_BLAS_WRAP="yes" ^
    -DCMAKE_INSTALL_PREFIX=" c:\clapack-3.2.1-CMAKE" ^
..
cmake --build . --config RELEASE 
cmake --build . --config RELEASE --target INSTALL
dir "c:\clapack-3.2.1-CMAKE"


you also can use MSYS2 to build the library.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
pacman -Sy make
pacman -Sy cmake

mkdir c: \ tools 
cd c: \ tools 
rmdir /s /q clapack-3.2.1-CMAKE
cd c:\tools 
del clapack-3.2.1-CMAKE.tgz
del clapack-3.2.1-CMAKE.tar
wget https://icl.cs.utk.edu/lapack-for-windows/clapack/clapack-3.2.1-CMAKE.tgz
 x clapack-3.2.1-CMAKE.tgz
rmdir /s /q clapack-3.2.1-CMAKE
 x clapack-3.2.1-CMAKE.tar
cd c:\tools 
cd clapack-3.2.1-CMAKE
rmdir /s /q build
mkdir build
cd build
del CMakeCache.txt
c:\tools\msys64\mingw64\bin\cmake -G "MSYS Makefiles" ^
    -DCMAKE_INSTALL_PREFIX="c:\tools\clapack-3.2.1-CMAKE " ^
    ..
make -j8 all
make install
c:\tools\clapack-3.2.1-CMAKE\lib
Last edited on
OK, that web page says
Those libraries were built under Visual Studio 2008.


So, anyone like to advise me how to link those libraries on a 64-bit machine from the Windows command line (NOT Visual Studio) using cl.exe ? I'm much more used to using g++

I am NOT trying to build those libraries myself!
Last edited on
If the libraries are built under VS 2008 they can not be used in a project which is written in VS 2022?
I am using visual studio 2022. using Fortran is not an option for me.


There is a free Fortran for VS2022 from Intel
https://www.intel.com/content/www/us/en/developer/articles/news/free-intel-software-developer-tools.html

You'll need an Intel account.

Then Get The Base Kit
Choose option Windows/Web/Local and download

Then Get The HPC Kit

As part of the installation, make sure the box is ticked to integrate with VS.

How to use Fortran with VS (and Python, Arduino, Web and C++) is detailed in
https://www.amazon.co.uk/gp/product/1727581539/ref=ppx_yo_dt_b_search_asin_title
All this frustrating effort is just to make the clapack libraries and use it for Trilinos. I build the Trilinos but I noticed clapack libraries are not being built properly. So are you suggesting to use lapack instead of clapack and then the libraries generated will work with Trilinos packages?

I am just posting some warnings of cmake just might help.
There are lots of warning like call stack as follows

  The LOCATION property should not be read from target "xeigtstz".  Use the
  target name directly with add_custom_command, or use the generator
  expression $<TARGET_FILE>, as appropriate.

Call Stack (most recent call first):
  TESTING/CMakeLists.txt:287 (add_lapack_test)
This warning is for project developers.  Use -Wno-dev to suppress it.



Last edited on
The nearest I could get was:

changing f2c.h slightly:
typedef int integer; // not long int

Then test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <math.h>            // NOTE ORDER
#include <stdio.h>           // added
#include <stdlib.h>          // added
#include "f2c.h"
#include "clapack.h"

#define NDIM 4 

int main() {

    int i, j, info2;
    int N, NRHS, LDA, LDB;
    double* A;
    double* B;
    static int IPIV[NDIM], INFO;

    A = (double*)malloc(NDIM * NDIM * sizeof(double));
    B = (double*)malloc(NDIM * sizeof(double));

    N = NDIM; NRHS = 1; LDA = NDIM; LDB = NDIM;

    A[0] = 1.0;
    A[4] = -1.0;
    A[8] = 2.0;
    A[12] = -1.0;
    A[1] = 2.0;
    A[5] = -2.0;
    A[9] = 3.0;
    A[13] = -3.0;
    A[2] = 1.0;
    A[6] = 1.0;
    A[10] = 1.0;
    A[14] = 0.0;
    A[3] = 1.0;
    A[7] = -1.0;
    A[11] = 4.0;
    A[15] = 3.0;

    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            printf("   %f  \n", A[i + N * j]);
        }
    }

    B[0] = -8.0;
    B[1] = -20.0;
    B[2] = -2.0;
    B[3] = 4.0;


    dgesv_(&N, &NRHS, A, &LDA, IPIV, B, &LDB, &INFO);  // <==== no & before IPIV

    printf("info %d \n", INFO);

    for (i = 0; i < N; i++)
        printf("   %f \n", B[i]);
}


It clearly does know about dgesv_ (because if I use a wrong set of arguments it indentifies it and complains). However,
cl.exe test.c blas.lib lapack.lib libf2c.lib
produces
test.c
C:\Work\f2c.h(159): warning C4005: 'min': macro redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt\stdlib.h(1283): note: see previous definition of 'min'
C:\Work\f2c.h(160): warning C4005: 'max': macro redefinition
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt\stdlib.h(1282): note: see previous definition of 'max'
Microsoft (R) Incremental Linker Version 14.29.30133.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe 
test.obj 
blas.lib 
lapack.lib 
libf2c.lib 
test.obj : error LNK2019: unresolved external symbol dgesv_ referenced in function main
blas.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
lapack.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
libf2c.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
test.exe : fatal error LNK1120: 1 unresolved externals


I don't care about the warnings (for now), but dgesv_ is definitely defined in the clapack.h header (and was picked up by the compiler before correcting arguments), so I'm not sure why the linker can't find it.
Last edited on
I get the same error as well.
As the libraries were downloaded prebuilt, you need to specify the .lib files in either the solution properties or as a #pragma in the code.

https://icl.cs.utk.edu/lapack-for-windows/clapack/index.html

details the names of the .libs to use.
Same problem with g++
g++ test.c blas.lib lapack.lib libf2c.lib
produces
C:\... ... \ccaaNaqb.o:test.c:(.text+0x274): undefined reference to `dgesv_'
collect2.exe: error: ld returned 1 exit status



I'm not convinced about those precompiled libraries.


specify the .lib files ... as a #pragma in the code.

Can you explain?
Last edited on
@seeplus I already specified the .lib files in the solution properties but it didn't work.
specify the .lib files ... as a #pragma in the code.

Can you explain?


See https://docs.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp?view=msvc-170

Rather than specifying the .lib files in the solution properties, you can specify them as part of a #pragma. eg

 
#pragma comment(lib, "blas") 


Where are the .lib files located - and have you told VS where to find them?
properties "Linker > General > Additional Library Directory"
Last edited on
Yes I specified the library's path. Let forget about precompiled libraries. I built the library by myself and still I faced the same problem as I used precompiled ones. I think the problem is another thing which I don't know at the time being what's that exactly.
Last edited on
There are later versions of the lapack library at
http://icl.cs.utk.edu/lapack-for-windows/lapack/
(note the one-letter difference in that url - it's not the same as above)

You may have more luck with those.
Thanks for your answers.
If you're built the library, then you have the source to it. What is the definition of dgesv_ ? Is the definition the same as that stated in the header file? is the definition exported?

A shot in the dark - but have you tried replacing dgesv_ with just dgesv ??

Have you used DUMPBIN (from a VS command prompt) on the .lib file(s) to see what export names they contain?

eg
dumpbin /exports blas.lib

clapack v3.2.1 is available as a library with vcpkg, a utility that "hooks" into Visual Studio and makes it easy to download, install and set the needed library paths for a VS project.

Using vcpkg makes using 3rd party libraries in VS, if they are available, almost ridiculously easy.

https://vcpkg.io/en/getting-started.html

It ain't just for VS and Windows, it can be used on *nix and MacOS using g++6 or later.

I don't know for sure but I'd bet using it would make it easy-as-pie for g++ to use 3rd party libraries.

Using vcpkg to install a library, clapack for example, is as easy as vcpkg install clapack in a command-line console.

https://vcpkg.io/en/packages.html
Last edited on
@seeplus yes I had tried desde before posting the question, Yes the definition is the same as stated in the header file. I haven't tried DUMPBIN, I even don't know what that is.
Pages: 12