Skip to content

Instantly share code, notes, and snippets.

@TysonRayJones
Last active October 15, 2024 11:43
Show Gist options
  • Select an option

  • Save TysonRayJones/af7bedcdb8dc59868c7966232b4da903 to your computer and use it in GitHub Desktop.

Select an option

Save TysonRayJones/af7bedcdb8dc59868c7966232b4da903 to your computer and use it in GitHub Desktop.

Revisions

  1. TysonRayJones revised this gist May 7, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gsl_on_osx_and_arcus_guide.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    *this guide may be outdated for MacOS versions beyond 10.14 Mojave (and implied Xcode versions)*

    GSL
    ===
  2. TysonRayJones revised this gist Apr 3, 2019. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion gsl_on_osx_and_arcus_guide.md
    Original file line number Diff line number Diff line change
    @@ -78,7 +78,14 @@ then open the new folder e.g.
    cd gsl-2.4
    ```

    To install without admin permissions, we'll keep the GSL files in a folder called `gsl` . Replace `YOURUSERNAME` below with your ARCUS-B username (e.g. `abcd1234`) and run
    To install without admin permissions, we'll keep the GSL files in a folder called `gsl` .

    Note some newer versions off gcc (e.g. 8) seem incompatible. Use an older version (e.g. `gcc/5.3.0`) and first call
    ```bash
    module load binutils libtool
    ```

    Replace `YOURUSERNAME` below with your ARCUS-B username (e.g. `abcd1234`) and run
    ```
    export gsdir=/data/oums-quantopo/YOURUSERNAME/gsl
  3. TysonRayJones revised this gist Feb 20, 2019. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions gsl_on_osx_and_arcus_guide.md
    Original file line number Diff line number Diff line change
    @@ -5,9 +5,11 @@ GSL
    **Table of Contents**
    - [Installation](#installation)
    - [OSX](#osx)
    - [Ubuntu](#ubuntu)
    - [ARCUS-B](#arcus-b)
    - [Compilation](#compilation)
    - [OSX](#osx)
    - [Ubuntu](#ubuntu)
    - [ARCUS-B](#arcus-b)
    - [Usage](#usage)
    - [import](#import)
    @@ -46,6 +48,14 @@ make install

    You'll now be able to include GSL into your code from anywhere.

    ## Ubuntu

    ```bash
    sudo apt-get install libgsl-dev
    ```

    You'll now be able to include GSL into your code from anywhere.

    ## ARCUS-B

    > Oh wait, explore the use of...
    @@ -93,6 +103,10 @@ For example, we'd adapt the [QuEST makefile](https://github.com/aniabrown/QuEST/
    LIBS = -lm -lgsl -lgslcblas
    ```

    ### Ubuntu

    Exactly as above for OSX.

    ## ARCUS-B

    Before compiling on ARCUS-B, we must extend `LD_LIBRARY` to include our gsl folder (replace `YOURUSERNAME` below)
  4. TysonRayJones revised this gist Jul 11, 2018. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions gsl_on_osx_and_arcus_guide.md
    Original file line number Diff line number Diff line change
    @@ -48,6 +48,13 @@ You'll now be able to include GSL into your code from anywhere.

    ## ARCUS-B

    > Oh wait, explore the use of...
    > ```bash
    > module load gsl/1.15
    > module load gsl/1.16
    > module load gsl/2.1
    > ```
    In the ARCUS terminal, download GSL anywhere
    ```bash
    wget "ftp://ftp.gnu.org/gnu/gsl/gsl-latest.tar.gz"
  5. TysonRayJones revised this gist Jul 4, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gsl_on_osx_and_arcus_guide.md
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ The GNU Scientific library ([GSL](https://www.gnu.org/software/gsl/)) is a power

    # Installation

    This guide assumes you already have a working `C`/`C++` compiler.
    This guide assumes you already have a working `C`/`C++` compiler - check by entering `gcc --version` in Terminal. OSX's default `Clang` compiler is fine, otherwise [this page](https://quest.qtechtheory.org/download/) describes how to install `gcc 4.9`.

    ## OSX

  6. TysonRayJones revised this gist Jul 4, 2018. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gsl_on_osx_and_arcus_guide.md
    Original file line number Diff line number Diff line change
    @@ -18,10 +18,12 @@ GSL
    - [numerical errors](#numerical-errors)


    The GNU Scientific library ([GSL](https://www.gnu.org/software/gsl/)) is a powerful C/C++ numerical library.
    The GNU Scientific library ([GSL](https://www.gnu.org/software/gsl/)) is a powerful `C`/`C++` numerical library.

    # Installation

    This guide assumes you already have a working `C`/`C++` compiler.

    ## OSX

    Apparently GSL can be installed through [Homebrew](https://brew.sh/) via
  7. TysonRayJones revised this gist Jul 3, 2018. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions gsl_on_osx_and_arcus_guide.md
    Original file line number Diff line number Diff line change
    @@ -34,6 +34,14 @@ though installing it manually is just as simple, which we now describe.
    - Open the unzipped `gsl` folder in Terminal (e.g. `cd ~/Downloads/gsl-2.4`
    - Run `sudo ./configure && make && make install`

    If the above gives a "permission denied" error, instead try
    ```bash
    sudo make clean
    sudo chown -R $USER .
    ./configure && make
    make install
    ```

    You'll now be able to include GSL into your code from anywhere.

    ## ARCUS-B
  8. TysonRayJones revised this gist Apr 17, 2018. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gsl_on_osx_and_arcus_guide.md
    Original file line number Diff line number Diff line change
    @@ -146,7 +146,7 @@ Sometimes GSL is so powerful, it's confusing to figure out how to do some simple

    ### construct matrices and vectors

    Matrices are dynamic objects, indexed from 0.
    Matrices are dynamic objects, indexed from 0, which must be allocated before, and freed after, their use.

    ```C
    #include <gsl/gsl_matrix.h>
    @@ -160,6 +160,8 @@ for (int i=0; i < 10; i++)
    for (int j=0; j < 20; j++)
    gsl_matrix_set(myMatr, i, j, 8);

    int elem = gsl_matrix_get(myMatr, 3, 4);

    gsl_matrix_free(myMatr);
    ```
  9. TysonRayJones revised this gist Apr 17, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gsl_on_osx_and_arcus_guide.md
    Original file line number Diff line number Diff line change
    @@ -53,7 +53,7 @@ cd gsl-2.4

    To install without admin permissions, we'll keep the GSL files in a folder called `gsl` . Replace `YOURUSERNAME` below with your ARCUS-B username (e.g. `abcd1234`) and run
    ```
    export gsdir=data/oums-quantopo/YOURUSERNAME/gsl
    export gsdir=/data/oums-quantopo/YOURUSERNAME/gsl
    mkdir $gsdir
  10. TysonRayJones renamed this gist Apr 13, 2018. 1 changed file with 0 additions and 0 deletions.
  11. TysonRayJones revised this gist Apr 13, 2018. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions gsl_on_mac_and_arcus_guide.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,22 @@
    GSL
    ===

    **Table of Contents**
    - [Installation](#installation)
    - [OSX](#osx)
    - [ARCUS-B](#arcus-b)
    - [Compilation](#compilation)
    - [OSX](#osx)
    - [ARCUS-B](#arcus-b)
    - [Usage](#usage)
    - [import](#import)
    - [functions](#functions)
    - [construct matrices and vectors](#construct-matrices-and-vectors)
    - [multiply matrices and vectors](#multiply-matrices-and-vectors)
    - [solve linear equations](#solve-linear-equations)
    - [numerical errors](#numerical-errors)


    The GNU Scientific library ([GSL](https://www.gnu.org/software/gsl/)) is a powerful C/C++ numerical library.

    # Installation
  12. TysonRayJones revised this gist Apr 13, 2018. 1 changed file with 85 additions and 1 deletion.
    86 changes: 85 additions & 1 deletion gsl_on_mac_and_arcus_guide.md
    Original file line number Diff line number Diff line change
    @@ -113,4 +113,88 @@ See a complete example of my QuEST & gsl makefile [here](https://github.com/Tyso

    ## import

    ## use
    The many different GSL functions are available in different source files.

    For example, to use the matrix, linear equation solver, and matrix multiplication functionalities, respectively include
    ```C
    #include <gsl/gsl_matrix.h>
    #include <gsl/gsl_linalg.h>
    #include <gsl/gsl_blas.h>
    ```

    View available functions and their necessary header files can be found [here](https://www.gnu.org/software/gsl/doc/html/intro.html).

    ## functions

    Sometimes GSL is so powerful, it's confusing to figure out how to do some simple things. So here are some examples

    ### construct matrices and vectors

    Matrices are dynamic objects, indexed from 0.

    ```C
    #include <gsl/gsl_matrix.h>
    ```

    ```C
    gsl_matrix* myMatr = gsl_matrix_alloc(10,20);

    // matrix of 8s
    for (int i=0; i < 10; i++)
    for (int j=0; j < 20; j++)
    gsl_matrix_set(myMatr, i, j, 8);

    gsl_matrix_free(myMatr);
    ```
    For a vector, replace `matrix` above with `vector`.
    ### multiply matrices and vectors
    GSL's matrix operations come under 'Basic Linear Algebra Subprograms' (BLAS), listed [here](https://www.gnu.org/software/gsl/doc/html/blas.html)
    ```C
    #include <gsl/gsl_blas.h>
    ```

    To multiply matrices `matrA` and `matrB` and store the result in `matrC` (all of which must be previously allocated by `gsl_matrix_alloc`), call:
    ```C
    gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, matrA, matrB, 0.0, matrC);
    ```
    In the name `gsl_blas_dgemm`, the `d` refers to double-precision matrices, and the final `m` refers to matrix. The other arguments are explained [here](https://www.gnu.org/software/gsl/doc/html/blas.html#c.gsl_blas_dgemm).
    To multiply a matrix `matrA` and a vector `vecB` and store the result in `vecC` (previously allocated by `gsl_matrix_alloc` and `gsl_vector_alloc`), call:
    ```C
    gsl_blas_dgemv(CblasNoTrans, 1.0, matrA, vecB, 0.0, vecC);
    ```

    ### solve linear equations

    [Linear algebra operations](https://www.gnu.org/software/gsl/doc/html/linalg.html) are provided by
    ```C
    #include <gsl/gsl_linalg.h>
    ```

    Here's how to solve **A x = b** (or *matrA vecX = vecB*) for **x** by LU decomposition, which involves creating an intermediate `permutation` structure
    ```C
    gsl_permutation* perm = gsl_permutation_alloc(vecB->size);

    int swaps;
    gsl_linalg_LU_decomp(matrA, perm, &swaps);
    gsl_linalg_LU_solve(matrA, perm, vecB, vecX);

    gsl_permutation_free(perm);
    ```
    ### numerical errors
    By default, a numerical error (e.g. attempting to invert a non-invertible matrix) will cause an error message and execution to stop.
    To instead detect and handle numerical errors, include
    ```C
    #include <gsl/gsl_errno.h>
    ```
    and call
    ```C
    gsl_set_error_handler_off();
    ```
    before calling any numerical functions. Then functions (e.g. `gsl_linalg_LU_solve`) will return an integer `0` for success, otherwise an errorcode of those listed [here](https://www.gnu.org/software/gsl/doc/html/err.html) (e.g. `GSL_ERANGE`, `GSL_EIVANL`).
  13. TysonRayJones renamed this gist Apr 13, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  14. TysonRayJones created this gist Apr 13, 2018.
    116 changes: 116 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,116 @@

    GSL
    ===

    The GNU Scientific library ([GSL](https://www.gnu.org/software/gsl/)) is a powerful C/C++ numerical library.

    # Installation

    ## OSX

    Apparently GSL can be installed through [Homebrew](https://brew.sh/) via
    ```bash
    brew install gsl
    ```
    though installing it manually is just as simple, which we now describe.

    - Download [gsl-latest.tar.gz](ftp://ftp.gnu.org/gnu/gsl/gsl-latest.tar.gz) from the [GSL ftp site](ftp://ftp.gnu.org/gnu/gsl/) and unzip it anywhere (e.g. /Downloads)
    - Open the unzipped `gsl` folder in Terminal (e.g. `cd ~/Downloads/gsl-2.4`
    - Run `sudo ./configure && make && make install`

    You'll now be able to include GSL into your code from anywhere.

    ## ARCUS-B

    In the ARCUS terminal, download GSL anywhere
    ```bash
    wget "ftp://ftp.gnu.org/gnu/gsl/gsl-latest.tar.gz"
    ```
    and unzip it
    ```bash
    tar -xf gsl-latest.tar.gz
    ```
    then open the new folder e.g.
    ```bash
    cd gsl-2.4
    ```

    To install without admin permissions, we'll keep the GSL files in a folder called `gsl` . Replace `YOURUSERNAME` below with your ARCUS-B username (e.g. `abcd1234`) and run
    ```
    export gsdir=data/oums-quantopo/YOURUSERNAME/gsl

    mkdir $gsdir

    ./configure --prefix=$gsdir && make && make install
    ```

    We'll need to remember this location when compiling

    # Compilation

    ## OSX

    Compiling GSL with our code is easy; we simply add library flags `-lm -lgsl -lgslcblas` to our calls to `gcc`.

    For example, we'd adapt the [QuEST makefile](https://github.com/aniabrown/QuEST/blob/master/examples/makefile) to include
    ```bash
    #
    # --- libraries
    #
    LIBS = -lm -lgsl -lgslcblas
    ```

    ## ARCUS-B

    Before compiling on ARCUS-B, we must extend `LD_LIBRARY` to include our gsl folder (replace `YOURUSERNAME` below)
    ```
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/data/oums-quantopo/YOURUSERNAME/gsl/lib
    ```
    This need only be done once each time we SSH into ARCUS.

    Our compilation must additionally include the ` -lm -lgsl -lgslcblas` libraries and flags `-I/data/oums-quantopo/YOURUSERNAME/gsl/include` and `-L/data/oums-quantopo/YOURUSERNAME/gsl/lib`.

    For example, we'd modify the [QuEST makefile](https://github.com/aniabrown/QuEST/blob/master/examples/makefile) to include
    ```bash
    #
    # --- libraries
    #
    LIBS = -lm -lgsl -lgslcblas
    ```
    and just after that, insert
    ```
    #
    # --- GSL link
    #
    GSL_INCLUDE = -I/data/oums-quantopo/YOURUSERNAME/gsl/include
    GSL_LINK = -L/data/oums-quantopo/YOURUSERNAME/gsl/lib
    ```
    and modify the rules list
    ```
    #
    # --- rules
    #
    %.o: %.c
    $(CC) $(CFLAGS) $(CFLAGS_OMP) $(GSL_INCLUDE) -c $<

    %.o: $(QUEST_DIR)/%.c
    $(CC) $(CFLAGS) $(CFLAGS_OMP) $(GSL_INCLUDE) -c $<
    ```
    and finally the build commands
    ```
    #
    # --- build
    #
    default: $(EXE)

    $(EXE): $(OBJ)
    $(CC) $(CFLAGS) $(CFLAGS_OMP) $(GSL_INCLUDE) -o $(EXE) $(OBJ) $(LIBS) $(GSL_LINK)
    ```

    See a complete example of my QuEST & gsl makefile [here](https://github.com/TysonRayJones/CTools/blob/master/gslquest/makefile).

    # Usage

    ## import

    ## use