- Notifications
You must be signed in to change notification settings - Fork0
Matrix Inversion API Client-Server Example to Fortran consume API
License
ricardojacomini/api_wrapper.c
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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 ExampleThis 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```bashgit clone https://github.com/yourusername/matrix-api-client-server.gitcd matrix-api-client-server
Create a virtual environment and activate it:
$ python3 -m venv venv$source venv/bin/activateInstall the required Python packages:
$ pip install flask numpy
The C wrapper (api_wrapper.c) uses thelibcurl 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 onhttp://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 usingcURL:
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 providedserver.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 systemctlenable server.service
This project is licensed under the MIT License. See theLICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
About
Matrix Inversion API Client-Server Example to Fortran consume API
Resources
License
Uh oh!
There was an error while loading.Please reload this page.