# PRIMA: CMake building system and C interface available

**URL:** <https://fortran-lang.discourse.group/t/prima-cmake-building-system-and-c-interface-available/6499>\
**Category:** Uncategorized\
**Created:** [September 11, 2023, 6:29pm UTC](https://fortran-lang.discourse.group/t/prima-cmake-building-system-and-c-interface-available/6499 "2023-09-11T18:29:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [September 11, 2023, 6:29pm UTC](https://fortran-lang.discourse.group/t/prima-cmake-building-system-and-c-interface-available/6499/1 "2023-09-11T18:29:23Z")

</div>

[PRIMA](http://www.libprima.net) is a package for solving general nonlinear optimization problems **without using derivatives**. It provides the reference implementation for the [late Professor Powell](https://github.com/libprima/prima#who-was-powell)’s renowned derivative-free optimization methods, i.e., COBYLA, UOBYQA, NEWUOA, BOBYQA, and LINCOA. PRIMA means “Reference Implementation for Powell’s methods with Modernization and Amelioration”, “P” for Powell.

After almost **three** years of intensive coding, **the [modern Fortran version](https://github.com/libprima/prima/blob/main/fortran) of PRIMA has been finished by December 2022.**

Thanks to the great contribution of [Julien Schueller](https://github.com/jschueller), [PRIMA](http://www.libprima.net) now has a CMake building system and a C interface.

### Fortran

To compile the Fortran library, do the following.

```bash
git clone --depth 1 https://github.com/libprima/prima.git
cd prima
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=install -DBUILD_SHARED_LIBS=OFF
cmake --build build --target install

```

This should create the **`primaf`** library for Fortran usage, located in the `install/lib` directory  
to be used with the module files in `install/include/prima/mod`.

Examples of how to use the library from an external code are available in [`fortran/examples`](https://github.com/libprima/prima/tree/main/fortran/examples).  
Below is an illustration with COBYLA.

```auto
cd fortran/examples/cobyla
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=install -DPRIMA_DIR=$PWD/../../../install/lib/cmake/prima/
cmake --build build --target install
./install/bin/cobyla_example

```

### C

The compilation of the Fortran library also produces the **`primac`** library for C usage, located in `install/lib` to be used with the **`prima.h`** header in `install/include/prima`.

Examples on how to use the library from an external code are available in [`c/examples`](https://github.com/libprima/prima/tree/main/c/examples).  
Below is an illustration with COBYLA.

```bash
cd c/examples/cobyla
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=install -DPRIMA_DIR=$PWD/../../../install/lib/cmake/prima/
cmake --build build --target install
./install/bin/cobyla_example

```

* * *

### Who was Powell?

[Michael James David Powell FRS](https://en.wikipedia.org/wiki/Michael_J._D._Powell) was [“a British numerical analyst who was among the pioneers of computational mathematics”](https://royalsocietypublishing.org/doi/full/10.1098/rsbm.2017.0023). He was the inventor/early contributor of [quasi-Newton method](https://en.wikipedia.org/wiki/Quasi-Newton_method), [trust region method](https://en.wikipedia.org/wiki/Trust_region), [augmented Lagrangian method](https://en.wikipedia.org/wiki/Augmented_Lagrangian_method), and [SQP method](https://en.wikipedia.org/wiki/Sequential_quadratic_programming). Each of them is a pillar of modern numerical optimization. He also made significant contributions to [approximation theory and methods](https://www.cambridge.org/highereducation/books/approximation-theory-and-methods/66FD8CD6F18FE1ED499A8CA9A05F2A5A#overview).

Among numerous honors, Powell was one of the first two recipients of the [Dantzig Prize](https://en.wikipedia.org/wiki/Dantzig_Prize) from the Mathematical Programming Society (MOS) and Society for Industrial and Applied Mathematics (SIAM). This is considered the highest award in optimization.

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [September 11, 2023, 6:32pm UTC](https://fortran-lang.discourse.group/t/prima-cmake-building-system-and-c-interface-available/6499/2 "2023-09-11T18:32:34Z")

</div>

The next low-hanging apple would be to [make PRIMA available under `fpm`](https://github.com/libprima/prima/issues/43).

---

<div class="post-metadata">

**Author:** ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)\
**Post date:** [September 12, 2023, 2:42am UTC](https://fortran-lang.discourse.group/t/prima-cmake-building-system-and-c-interface-available/6499/3 "2023-09-12T02:42:57Z")

</div>

Excellent work. The organization and code/documentation quality is remarkable. I just built the library in WSL. CMake had difficulty finding the Fortran compiler with `-- The Fortran compiler identification is unknown`, despite having 5 Fortran compilers in the OS `bin` directory. I could readily solve it by specifying `-DCMAKE_Fortran_COMPILER=/usr/bin/gfortran-13`. This may be an exception, but it might be worth having a brief troubleshooting discussion in the installation guidelines just in case people unfamiliar with Fortran or CMake face the same problem. I have used Powell methods in the past and look forward to using the modernized version in the coming months.

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [September 12, 2023, 4:43am UTC](https://fortran-lang.discourse.group/t/prima-cmake-building-system-and-c-interface-available/6499/4 "2023-09-12T04:43:36Z")

</div>

Thank you @shahmoradi for the encouraging words.

> The organization and code/documentation quality is remarkable.

PRIMA still has a long way to go regarding documentation.  
Currently, the [README](https://github.com/libprima/prima#prima-reference-implementation-for-powells-methods-with-modernization-and-amelioration) is more like a presentation of the project rather than a user-oriented documentation, although I wrote it in that way on purpose. I hope the abundant comments in the Fortran code could help those who are interested in the methods, but we really need to [generate better documentation using tools like FORD](https://github.com/libprima/prima/issues/44).

> CMake had difficulty finding the Fortran compiler with `-- The Fortran compiler identification is unknown` , despite having 5 Fortran compilers in the OS `bin` directory. I could readily solve it by specifying `-DCMAKE_Fortran_COMPILER=/usr/bin/gfortran-13` . This may be an exception, but it might be worth having a brief troubleshooting discussion in the installation guidelines just in case people unfamiliar with Fortran or CMake face the same problem.

I have opened a [GitHub issue](https://github.com/libprima/prima/issues/53) about the problem you mentioned. Probably Julien will fix it.

> I have used Powell methods in the past and look forward to using the modernized version in the coming months

Great! I look forward to more feedback from you.

Thank you.

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [September 12, 2023, 6:24am UTC](https://fortran-lang.discourse.group/t/prima-cmake-building-system-and-c-interface-available/6499/5 "2023-09-12T06:24:33Z")

</div>

> [@zaikunzhang](#):
>
> I have opened a [GitHub issue](https://github.com/libprima/prima/issues/53) about the problem you mentioned. Probably Julien will fix it.

Response on GitHub from Julien Schueller:

> cmake will only find compilers in the PATH with default names, I think it will be fine if there was a gfortran symlink

Does this explain the problem you encountered? You are invited to give comments under the [GitHub issue](https://github.com/libprima/prima/issues/53).

Anyway, since you have five compilers installed, I guess at least one of them is in the PATH with default names, right?

---

<div class="post-metadata">

**Author:** ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)\
**Post date:** [September 12, 2023, 4:36pm UTC](https://fortran-lang.discourse.group/t/prima-cmake-building-system-and-c-interface-available/6499/6 "2023-09-12T16:36:57Z")

</div>

Forcing CMake to recognize all possible variants of Fortran compiler names is likely a long shot. Adding a troubleshooting section in the README files to discuss this potential pitfall and how to fix it if it happens should be more than enough.

What I do to bypass CMake inadequacies is to add a wrapper script around CMake, which attempts to identify the Fortran compiler first following CMake’s approach and, if failed, through an aggressive search in the system bin directory. Then, I define the macro `CMake_Fortran_COMPILER` with one of the identified compilers on the CMake command line. If there is no compiler, I ask the user’s permission to install a Fortran compiler. This can never be foolproof. But I confess setting up such a pipeline has been a project for me.

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [September 12, 2023, 5:00pm UTC](https://fortran-lang.discourse.group/t/prima-cmake-building-system-and-c-interface-available/6499/7 "2023-09-12T17:00:03Z")

</div>

Thank you for the suggestion @shahmoradi . What about something like “In case CMake fails to find your Fortran compiler, you can indicate it by specifying `-DCMAKE_Fortran_COMPILER=/path/to/your/Fortran/compiler`” in the README?

---

<div class="post-metadata">

**Author:** ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)\
**Post date:** [September 12, 2023, 5:08pm UTC](https://fortran-lang.discourse.group/t/prima-cmake-building-system-and-c-interface-available/6499/8 "2023-09-12T17:08:32Z")

</div>

That’s what I originally meant to suggest.

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [September 12, 2023, 5:24pm UTC](https://fortran-lang.discourse.group/t/prima-cmake-building-system-and-c-interface-available/6499/9 "2023-09-12T17:24:56Z")

</div>

> [@zaikunzhang](#):
>
> In case CMake fails to find your Fortran compiler, you can indicate it by specifying `-DCMAKE_Fortran_COMPILER=/path/to/your/Fortran/compiler`

Done. See [CMake had difficulty finding the Fortran compiler · Issue #53 · libprima/prima · GitHub](https://github.com/libprima/prima/issues/53#issuecomment-1716139175)  
Thank you for your suggestion @shahmoradi .

---

<div class="post-metadata">

**Author:** ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)\
**Post date:** [September 12, 2023, 5:44pm UTC](https://fortran-lang.discourse.group/t/prima-cmake-building-system-and-c-interface-available/6499/10 "2023-09-12T17:44:06Z")

</div>

Excellent, thank you. Again as an end user, I am very much impressed by the within-code documentation quality and code clarity.
