3. Write database - eliyahudev/mannix GitHub Wiki

Create data base

introduction

Now that we know how to run a simulation, let's see how we create a database and read from it the data into our structures. Before we continue, let's understand how data is stored in memory.
In the example, when we use the command "creatMatrix_int8(4, 3, &fc_weight[0], (Allocator_int8*) al);" the allocator looked at the array he pointed to and sent a pointer to the first place that was available. In this case, because the assignment did not assign data earlier, the returned pointer pointed to the array head. In other words:
matrix->data = &data[0].

Memory alignment

So theoretically if we add another matrix, let's say size "2,2" it assumes to start at address &data[12]. **BUT** this is only true for such examples. For example, let's take the example of helloworld and change the size of the fc_weight matrix to be "3.3". The problem now is that we have to deal with aligned addresses. The processor accesses memory using a single memory word at a time. That is, the addresses can only start with 0, 4, 8 or c (more information [here](https://en.wikipedia.org/wiki/Data_structure_alignment)).

To avoid this we add to the mannix data struct bytes (if necessary) so that the next address is align (and it will be called padding). For example, let's take the helloworld program we changed and add another 2.2-size matrix:


fclose(fd);
Matrix_int8 cnn_weight[1];
creatMatrix_int8(2, 2, &cnn_weight[0], (Allocator_int8*) al);
fd = fopen("PATH TO matrix.csv","r");
getMatrix_int8(cnn_weight, fd, label,-1, 1);
For the first matrix the program will allocate 12 bytes instead of 9 and the next matrix will start at address &data[12].


Create database

Now the data array should looks like this:
----------------------------------
1 2 3 4 5 6 7 8 9 0 0 0 | 1 2 3 4
----------------------------------
Now lets dump this array into the database.

To do so, Go to software->riskv_simul->config->man_def.h and define the dump flag:
'#define MEM_DUMP_MODE'
Also define the name of the dump file
#define MODEL_PARAMS_FILE "../model_params_db/matrix.txt"
finally lets write the array data to matrix.txt file:
dump_model_params_mfdb(al,MODEL_PARAMS_FILE); // dump mannix format data base

now run the program again:

⚠️ **GitHub.com Fallback** ⚠️