rt_benchs/communication_techniques/src/calculation/calc_mat.c

83 lines
2.2 KiB
C

/*
* Copyright (C) 2009, 2012 Thomas Preud'homme <thomas.preud-homme@lip6.fr>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
static int *mat, *vect;
static int li;
static int n, m; /* Size of the matrice: n lines, m columns */
static int *mat_cell_ptr;
int init_calc(int size)
{
int i;
n = size;
m = size;
srand(42);
mat = (int *) malloc(n * m * sizeof(int));
if (mat == NULL)
{
fprintf(stderr, "calc_mat: Unable to allocate memory for matrice calculation\n");
return -1;
}
vect = (int *) malloc(m * sizeof(int));
if (vect == NULL)
{
free(mat);
fprintf(stderr, "calc_mat: Unable to allocate memory for matrice calculation\n");
return -1;
}
for (i = 0; i < n * m; i++)
mat[i] = rand();
for (i = 0; i < m; i++)
vect[i] = rand();
li = 0;
return 0;
}
void **do_calc(void)
{
int co, p = 0;
for (co = 0; co < m; co++)
p += mat[li * m + co] * vect[li];
mat[li * m] = p;
if (unlikely(++li >= n))
li = 0;
mat_cell_ptr = &mat[li * m];
return (void **) &mat_cell_ptr;
}
int end_calc(void)
{
free(mat);
free(vect);
return 0;
}