csvdcula

culagesvd() in CULA


I want to use the culadgesvd() function to compute SVD for a matrix. It is not clear for me how to actually use it in C/C++ with the info in the documentation. Could anyone give me a complete small C program, a template, to show how basically the function is used? Just a couple of lines (with stuff like culaInitialize() and culaShutdown()) will do, I just need to see how this function can run without error.


Solution

  • I thought that SVD is not included in the free version (it is though). Below is an example for a QR decomposition. Put this in a .cpp file:

    #include<cula.h>
    #include<iostream>
    
    int main()
    {       
        float x[] = {1, 2, 3, 4};
        int n_rows = 2, n_cols = 2;
        float scale[n_cols];
    
        culaInitialize();   
        culaSgeqrf(n_rows, n_cols, &(x[0]), n_rows, &(scale[0]));
        culaShutdown();
    
        for(int ii = 1; ii < n_rows; ii++)
        {
            for(int jj = 0; jj < n_cols; jj++)
            {
                if(ii > jj) { x[ii + jj * n_rows] *= scale[jj]; }
            }
        }
    
        for(int ii = 0; ii < n_rows * n_cols; ii++)
        {
            std::cout << x[ii] << std::endl;
        }
    
        return 0;
    }
    

    and compile it using:

    g++ -fPIC -c -I/usr/local/cula/include -Wl,-rpath,/usr/local/cula/lib64 -L/usr/local/cula/lib64 -lcula_lapack_basic gpuQR.cpp
    g++ -o gpuQR gpuQR.o -Wl,-rpath,/usr/local/cula/lib64 -L/usr/local/cula/lib64 -lcula_lapack_basic
    

    Then call the program using:

    ./gpuQR
    -2.23607
    0.894427
    -4.91935
    -0.894427
    

    Check my post about CULA and Rcpp here.