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
23bool verbose;
24
25/*
26* -----------------------------------------------------------------------------
27* This function allocates the data and sets up the data structures to be used
28* in the remainder.
29* -----------------------------------------------------------------------------
30*/
31void allocate_data (int active_threads,
32		    int64_t number_of_rows,
33		    int64_t number_of_columns,
34		    double ***A,
35		    double **b,
36		    double **c,
37		    double **ref,
38		    thread_data **thread_data_arguments,
39		    pthread_t **pthread_ids)
40{
41  if ((*b = (double *) malloc (number_of_columns * sizeof (double))) == NULL)
42    {
43      printf ("Error: allocation of vector b failed\n");
44      perror ("vector b");
45      exit (-1);
46    }
47  else
48    {
49      if (verbose) printf ("Vector b allocated\n");
50    }
51
52  if ((*c = (double *) malloc (number_of_rows * sizeof (double))) == NULL)
53    {
54      printf ("Error: allocation of vector c failed\n");
55      perror ("vector c");
56      exit (-1);
57    }
58  else
59    {
60      if (verbose) printf ("Vector c allocated\n");
61    }
62
63  if ((*ref = (double *) malloc (number_of_rows * sizeof (double))) == NULL)
64    {
65      printf ("Error: allocation of vector ref failed\n");
66      perror ("vector ref");
67      exit (-1);
68    }
69
70  if ((*A = (double **) malloc (number_of_rows * sizeof (double))) == NULL)
71    {
72      printf ("Error: allocation of matrix A failed\n");
73      perror ("matrix A");
74      exit (-1);
75    }
76  else
77    {
78      for (int64_t i=0; i<number_of_rows; i++)
79	{
80	  if (((*A)[i] = (double *) malloc (number_of_columns
81					* sizeof (double))) == NULL)
82	    {
83	      printf ("Error: allocation of matrix A columns failed\n");
84	      perror ("matrix A[i]");
85	      exit (-1);
86	    }
87	}
88      if (verbose) printf ("Matrix A allocated\n");
89    }
90
91
92  if ((*thread_data_arguments = (thread_data *) malloc ((active_threads)
93				* sizeof (thread_data))) == NULL)
94    {
95      perror ("malloc thread_data_arguments");
96      exit (-1);
97    }
98  else
99    {
100      if (verbose) printf ("Structure thread_data_arguments allocated\n");
101    }
102
103  if ((*pthread_ids = (pthread_t *) malloc ((active_threads)
104				* sizeof (pthread_t))) == NULL)
105    {
106      perror ("malloc pthread_ids");
107      exit (-1);
108    }
109  else
110    {
111      if (verbose) printf ("Structure pthread_ids allocated\n");
112    }
113}
114
115/*
116* -----------------------------------------------------------------------------
117* This function initializes the data.
118* -----------------------------------------------------------------------------
119*/
120void init_data (int64_t m,
121		int64_t n,
122		double **restrict A,
123		double *restrict b,
124		double *restrict c,
125		double *restrict ref)
126{
127
128  (void) srand48 (2020L);
129
130  for (int64_t j=0; j<n; j++)
131    b[j] = 1.0;
132
133  for (int64_t i=0; i<m; i++)
134    {
135      ref[i] = n*i;
136      c[i]   = -2022;
137      for (int64_t j=0; j<n; j++)
138	A[i][j] = drand48 ();
139    }
140
141  for (int64_t i=0; i<m; i++)
142    {
143      double row_sum = 0.0;
144      for (int64_t j=0; j<n; j++)
145	row_sum += A[i][j];
146      ref[i] = row_sum;
147    }
148}
149