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* This function determines the number of rows each thread will be working on
26* and also how many threads will be active.
27* -----------------------------------------------------------------------------
28*/
29void get_workload_stats (int64_t number_of_threads,
30			 int64_t number_of_rows,
31			 int64_t number_of_columns,
32			 int64_t *rows_per_thread,
33			 int64_t *remainder_rows,
34			 int64_t *active_threads)
35{
36  if (number_of_threads <= number_of_rows)
37    {
38      *remainder_rows  = number_of_rows%number_of_threads;
39      *rows_per_thread = (number_of_rows - (*remainder_rows))/number_of_threads;
40    }
41  else
42    {
43      *remainder_rows  = 0;
44      *rows_per_thread = 1;
45    }
46
47  *active_threads = number_of_threads < number_of_rows
48		? number_of_threads : number_of_rows;
49
50  if (verbose)
51    {
52      printf ("Rows per thread = %ld remainder = %ld\n",
53		*rows_per_thread, *remainder_rows);
54      printf ("Number of active threads = %ld\n", *active_threads);
55    }
56}
57
58/*
59* -----------------------------------------------------------------------------
60* This function determines which rows each thread will be working on.
61* -----------------------------------------------------------------------------
62*/
63void determine_work_per_thread (int64_t TID, int64_t rows_per_thread,
64				int64_t remainder_rows,
65				int64_t *row_index_start,
66				int64_t *row_index_end)
67{
68  int64_t chunk_per_thread;
69
70  if (TID < remainder_rows)
71    {
72      chunk_per_thread = rows_per_thread + 1;
73      *row_index_start = TID * chunk_per_thread;
74      *row_index_end   = (TID + 1) * chunk_per_thread - 1;
75    }
76  else
77    {
78      chunk_per_thread = rows_per_thread;
79      *row_index_start = remainder_rows * (rows_per_thread + 1)
80			 + (TID - remainder_rows) * chunk_per_thread;
81      *row_index_end   = remainder_rows * (rows_per_thread + 1)
82			 + (TID - remainder_rows) * chunk_per_thread
83			 + chunk_per_thread - 1;
84    }
85
86  if (verbose)
87    {
88      printf ("TID = %ld row_index_start = %ld row_index_end = %ld\n",
89		TID, *row_index_start, *row_index_end);
90    }
91}
92