This project explains how to use the Fortran client (client.f90
) with the C wrapper (api_wrapper.c
) to interact with the Python Flask API (server.py
) in the High-Performance Computing (HPC) Cluster.
# Matrix Inversion API Client-Server Example
This repository demonstrates how to use a Fortran client to send a matrix to a Python Flask API for inversion. Since Fortran does not have native support for HTTP requests, a C wrapper is used to handle the HTTP communication.
## Prerequisites
- **Fortran Compiler**: `gfortran` (GNU Fortran)
- **C Compiler**: `gcc` (GNU C Compiler)
- **cURL Library**: Required for HTTP requests in the C wrapper.
- **Python 3**: Required to run the Flask server.
- **Flask**: Python web framework.
- **NumPy**: Python library for numerical computations.
## Setup
### 1. Clone the Repository
```bash
git clone https://github.com/yourusername/matrix-api-client-server.git
cd matrix-api-client-server
-
Create a virtual environment and activate it:
$ python3 -m venv venv $ source venv/bin/activate
-
Install the required Python packages:
$ pip install flask numpy
The C wrapper (api_wrapper.c
) uses the libcurl
library to handle HTTP requests. Compile it as follows:
$ module load curl
$ gcc -c api_wrapper.c -o api_wrapper.o -lcurl
The Fortran client (client.f90
) calls the C wrapper to send the matrix to the server. Compile it as follows:
$ module load curl
$ gfortran -g client.f90 api_wrapper.o -o client -lcurl
Run the Flask server (server.py
) to handle incoming requests:
$ python server.py
The server will start on http://0.0.0.0:5000
.
Once the server is running, execute the Fortran client:
$ ./client
The client will send a matrix to the server, receive the inverted matrix, and print both the original and inverted matrices.
Original matrix:
[[4.0, 7.0], [2.0, 6.0]]
Server response: {"inverted_matrix": [[0.6, -0.7], [-0.2, 0.4]]}
You can also test the API directly using cURL
:
curl -v -X POST http://172.20.11.21:5000/invert-matrix -H "Content-Type: application/json" -d '{"matrix": [[4.0, 7.0], [2.0, 6.0]]}'
To run the Flask server as a service, you can use the provided server.service
file. Follow these steps:
-
Copy the service file to the systemd directory:
$ sudo cp server.service /etc/systemd/server.service
-
Reload the systemd daemon:
$ sudo systemctl daemon-reload
-
Start and enable the service:
$ sudo systemctl start server.service $ sudo systemctl enable server.service
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.