Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active February 11, 2025 19:08
Show Gist options
  • Save scivision/1ea2d19011c165b39b15ccb95d54f451 to your computer and use it in GitHub Desktop.
Save scivision/1ea2d19011c165b39b15ccb95d54f451 to your computer and use it in GitHub Desktop.
MUMPS example via find_packge(MUMPS CONFIG)

MUMPS CMake find_package(MUMPS CONFIG)

Another option for MUMPS is via CMake FetchContent.

This assumes you first build and install MUMPS

git clone https://github.com/scivision/mumps

cmake -S mumps -B mumps/build --install-prefix $HOME/mumps

cmake --build mumps/build

cmake --install mumps/build

Then use this example project

cmake -Bbuild -DMUMPS_ROOT=$HOME/mumps

cmake --build build

ctest --test-dir build
cmake_minimum_required(VERSION 3.19)
project(MUMPS_example
LANGUAGES C Fortran
)
enable_testing()
file(GENERATE OUTPUT ${PROJECT_BINARY_DIR}/.gitignore CONTENT "*")
find_package(MUMPS CONFIG REQUIRED)
add_executable(c_mumps d_simple.c)
target_link_libraries(c_mumps PRIVATE MUMPS::MUMPS MPI::MPI_C)
add_test(NAME MUMPS_C
COMMAND ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} 2 $<TARGET_FILE:c_mumps>
)
/*
*
* This file is part of MUMPS 5.2.1, released
* on Fri Jun 14 14:46:05 UTC 2019
*
*/
/* Example program using the C interface to the
* double real arithmetic version of MUMPS, dmumps_c.
* We solve the system A x = RHS with
* A = diag(1 2) and RHS = [1 4]^T
* Solution is [1 2]^T */
#include <stdio.h>
#include <string.h>
#include "mpi.h"
#include "dmumps_c.h"
#define JOB_INIT -1
#define JOB_END -2
#define USE_COMM_WORLD -987654
int main(int argc, char ** argv)
{
DMUMPS_STRUC_C id;
MUMPS_INT n = 2;
MUMPS_INT8 nnz = 2;
MUMPS_INT irn[] = {1,2};
MUMPS_INT jcn[] = {1,2};
double a[2];
double rhs[2];
MUMPS_INT myid, ierr;
ierr = MPI_Init(&argc, &argv);
if (ierr != 0) {
fprintf(stderr, "failed to init MPI");
return 1;
}
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &myid);
/* Define A and rhs */
rhs[0]=1.0;rhs[1]=4.0;
a[0]=1.0;a[1]=2.0;
/* Initialize a MUMPS instance. Use MPI_COMM_WORLD */
id.comm_fortran=USE_COMM_WORLD;
id.par=1; id.sym=0;
id.job=JOB_INIT;
dmumps_c(&id);
/* Define the problem on the host */
if (myid == 0) {
id.n = n; id.nnz =nnz; id.irn=irn; id.jcn=jcn;
id.a = a; id.rhs = rhs;
}
#define ICNTL(I) icntl[(I)-1] /* macro s.t. indices match documentation */
/* No outputs */
id.ICNTL(1)=-1; id.ICNTL(2)=-1; id.ICNTL(3)=-1; id.ICNTL(4)=0;
/* Call the MUMPS package (analyse, factorization and solve). */
id.job=6;
dmumps_c(&id);
if (id.infog[0]<0) {
fprintf(stderr," (PROC %d) ERROR RETURN: \tINFOG(1)= %d\n\t\t\t\tINFOG(2)= %d\n",
myid, id.infog[0], id.infog[1]);
return 1;
}
/* Terminate instance. */
id.job=JOB_END;
dmumps_c(&id);
if (myid == 0) printf("Solution is : (%8.2f %8.2f)\n", rhs[0],rhs[1]);
ierr = MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment