# Tutorial for Matrix and Vector Operations

This tutorial shows how to use matrices and vectors within NetSolve by developing a little program which either invokes a custom function to perform the multiplication or uses the BLAS routine dgemm. This includes the following steps:

1. Building the library

2. Installing the problem and adding BLAS to the NetSolve repository

3. Invoking the function via NetSolve

Requirements, the following files are needed (they should be in the current directory):

• libmatmul.c - Contains the function which should perform the calculation

• matmul.idl - A file which describes how the problem should be included into the NetSolve repository

• myprog.c - An example program which invokes the NetSolve function

Instructions

1. Building the Library NetSolve provides all functions through statically linked libraries. Execute the following steps on the server to build libmatmul.a
 ```UNIX> gcc -c libmatmul.c UNIX> ar rc libmatmul.a libmatmul.o```

2. Installing the Problem and Adding BLAS to the NetSolve Repository

To invoke a new function from the NetSolve client you have to add the function to the problems list in a NetSolve server and recompile the server. Perform the following steps to include 'matmul':

• Create a problem description file with:  `UNIX> \$NETSOLVE_ROOT/bin/\$NETSOLVE_ARCH/idltopdf matmul.idl`
Which generates the file matmul.pdf.

• Copy the file matmul.pdf to the \$NETSOLVE_ROOT/problems directory.

• Edit the file \$NETSOLVE_ROOT/server_config in your NetSolve directory and add the following line in the @PROBLEMS: section:  ``` ./problems/matmul.pdf```

• To rebuild the server, you have to set an environment variable which points to the directory of the previously created library (required by the problems definition file); Depending on you shell execute:  `UNIX> export NSMATMUL_LIB=/path/to/libmatmul`
or  `UNIX> setenv NSMATMUL_LIB /path/to/libmatmul`

• afterwards rebuild the server with the command  `UNIX> make server`

To include the function 'dgemm' provided by the BLAS package you only have to uncomment the line
 ``` #./problems/blas_subset```
In the file \$NETSOLVE_ROOT/server_config. NetSolve provides a subset of the BLAS package in the NetSolve directory; \$NETSOLVE_ROOT/src/SampleNumericalSoftware/BLAS.

Hint:

NetSolve also provides a subset of LaPack. If you want to include the complete LaPack uncomment:
 ``` #./problems/lapack" ```
In \$NETSOLVE_ROOT/server_config and rerun configure with the following additional options;
 `--with-blaslib=/path/to/libblas.a --with-lapack-lib=/path/to/liblapack.a`

Note: All these steps have to be performed at the machine where your server is running. Also make sure to kill the server if it is currently running.

3. Invoking the Function Via NetSolve

The file myprog.c demonstrates how to invoke the function calc via NetSolve in C. When you compile it you have to provide the include and library directory of NetSolve. If you have set the NETSOLVE_ROOT environment variable to your NetSolve directory and running this tutorial on x86 under Linux the command would be:
 ```UNIX> gcc myprog.c -o myprog -I\$NETSOLVE_ROOT/include -lnetsolve \ -L\$NETSOLVE_ROOT/lib/i686_pc_linux_gnu```

Invoking myprog uses 3 arguments:

• matrix size: integer with the dimension of the used matrix

• mode:

1. blocking call

2. non-blocking call