xref: /netbsd-src/external/gpl3/binutils/dist/gprofng/examples/mxv-pthreads/src/mxv.c (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1 /* Copyright (C) 2021-2023 Free Software Foundation, Inc.
2    Contributed by Oracle.
3 
4    This file is part of GNU Binutils.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20 
21 #include "mydefs.h"
22 
23 /*
24 * -----------------------------------------------------------------------------
25 * Driver for the core computational part.
26 * -----------------------------------------------------------------------------
27 */
driver_mxv(void * thread_arguments)28 void *driver_mxv (void *thread_arguments)
29 {
30   thread_data *local_data;
31 
32   local_data = (thread_data *) thread_arguments;
33 
34   bool     do_work	  = local_data->do_work;
35   int64_t  repeat_count   = local_data->repeat_count;
36   int64_t row_index_start = local_data->row_index_start;
37   int64_t row_index_end   = local_data->row_index_end;
38   int64_t m		  = local_data->m;
39   int64_t n		  = local_data->n;
40   double  *b		  = local_data->b;
41   double  *c		  = local_data->c;
42   double  **A		  = local_data->A;
43 
44   if (do_work)
45     {
46       for (int64_t r=0; r<repeat_count; r++)
47 	{
48 	  (void)  mxv_core (row_index_start, row_index_end, m, n, A, b, c);
49 	}
50     }
51 
52   return (0);
53 }
54 
55 /*
56 * -----------------------------------------------------------------------------
57 * Computational heart of the algorithm.
58 *
59 * Disable inlining to avoid the repeat count loop is removed by the compiler.
60 * This is only done to make for a more interesting call tree.
61 * -----------------------------------------------------------------------------
62 */
mxv_core(int64_t row_index_start,int64_t row_index_end,int64_t m,int64_t n,double ** restrict A,double * restrict b,double * restrict c)63 void __attribute__ ((noinline)) mxv_core (int64_t row_index_start,
64 					  int64_t row_index_end,
65 					  int64_t m,
66 					  int64_t n,
67 					  double **restrict A,
68 					  double *restrict b,
69 					  double *restrict c)
70 {
71   for (int64_t i=row_index_start; i<=row_index_end; i++)
72     {
73       double row_sum = 0.0;
74       for (int64_t j=0; j<n; j++)
75 	row_sum += A[i][j] * b[j];
76       c[i] = row_sum;
77     }
78 }
79