1 /*********************************************************************/
2 /* Copyright 2009, 2010 The University of Texas at Austin.           */
3 /* All rights reserved.                                              */
4 /*                                                                   */
5 /* Redistribution and use in source and binary forms, with or        */
6 /* without modification, are permitted provided that the following   */
7 /* conditions are met:                                               */
8 /*                                                                   */
9 /*   1. Redistributions of source code must retain the above         */
10 /*      copyright notice, this list of conditions and the following  */
11 /*      disclaimer.                                                  */
12 /*                                                                   */
13 /*   2. Redistributions in binary form must reproduce the above      */
14 /*      copyright notice, this list of conditions and the following  */
15 /*      disclaimer in the documentation and/or other materials       */
16 /*      provided with the distribution.                              */
17 /*                                                                   */
18 /*    THIS  SOFTWARE IS PROVIDED  BY THE  UNIVERSITY OF  TEXAS AT    */
19 /*    AUSTIN  ``AS IS''  AND ANY  EXPRESS OR  IMPLIED WARRANTIES,    */
20 /*    INCLUDING, BUT  NOT LIMITED  TO, THE IMPLIED  WARRANTIES OF    */
21 /*    MERCHANTABILITY  AND FITNESS FOR  A PARTICULAR  PURPOSE ARE    */
22 /*    DISCLAIMED.  IN  NO EVENT SHALL THE UNIVERSITY  OF TEXAS AT    */
23 /*    AUSTIN OR CONTRIBUTORS BE  LIABLE FOR ANY DIRECT, INDIRECT,    */
24 /*    INCIDENTAL,  SPECIAL, EXEMPLARY,  OR  CONSEQUENTIAL DAMAGES    */
25 /*    (INCLUDING, BUT  NOT LIMITED TO,  PROCUREMENT OF SUBSTITUTE    */
26 /*    GOODS  OR  SERVICES; LOSS  OF  USE,  DATA,  OR PROFITS;  OR    */
27 /*    BUSINESS INTERRUPTION) HOWEVER CAUSED  AND ON ANY THEORY OF    */
28 /*    LIABILITY, WHETHER  IN CONTRACT, STRICT  LIABILITY, OR TORT    */
29 /*    (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY WAY OUT    */
30 /*    OF  THE  USE OF  THIS  SOFTWARE,  EVEN  IF ADVISED  OF  THE    */
31 /*    POSSIBILITY OF SUCH DAMAGE.                                    */
32 /*                                                                   */
33 /* The views and conclusions contained in the software and           */
34 /* documentation are those of the authors and should not be          */
35 /* interpreted as representing official policies, either expressed   */
36 /* or implied, of The University of Texas at Austin.                 */
37 /*********************************************************************/
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include "common.h"
42 
43 static const int divide_rule[][2] =
44   {{ 0,  0},
45    { 1,  1}, { 1,  2}, { 1,  3}, { 2,  2},
46    { 1,  5}, { 2,  3}, { 1,  7}, { 2,  4},
47    { 3,  3}, { 2,  5}, { 1, 11}, { 2,  6},
48    { 1, 13}, { 2,  7}, { 3,  5}, { 4,  4},
49    { 1, 17}, { 3,  6}, { 1, 19}, { 4,  5},
50    { 3,  7}, { 2, 11}, { 1, 23}, { 4,  6},
51    { 5,  5}, { 2, 13}, { 3,  9}, { 4,  7},
52    { 1, 29}, { 5,  6}, { 1, 31}, { 4,  8},
53    { 3, 11}, { 2, 17}, { 5,  7}, { 6,  6},
54    { 1, 37}, { 2, 19}, { 3, 13}, { 5,  8},
55    { 1, 41}, { 6,  7}, { 1, 43}, { 4, 11},
56    { 5,  9}, { 2, 23}, { 1, 47}, { 6,  8},
57    { 7,  7}, { 5, 10}, { 3, 17}, { 4, 13},
58    { 1, 53}, { 6,  9}, { 5, 11}, { 7,  8},
59    { 3, 19}, { 2, 29}, { 1, 59}, { 6, 10},
60    { 1, 61}, { 2, 31}, { 7,  9}, { 8,  8},
61 };
62 
CNAME(int mode,blas_arg_t * arg,BLASLONG * range_m,BLASLONG * range_n,int (* function)(),void * sa,void * sb,BLASLONG nthreads)63 int CNAME(int mode, blas_arg_t *arg, BLASLONG *range_m, BLASLONG *range_n, int (*function)(), void *sa, void *sb, BLASLONG nthreads) {
64 
65   blas_queue_t queue[MAX_CPU_NUMBER];
66 
67   BLASLONG range_M[MAX_CPU_NUMBER + 1], range_N[MAX_CPU_NUMBER + 1];
68   BLASLONG procs, total_procs, num_cpu_m, num_cpu_n;
69 
70   BLASLONG width, i, j;
71   BLASLONG divM, divN;
72 
73   divM = divide_rule[nthreads][0];
74   divN = divide_rule[nthreads][1];
75 
76   if (!range_m) {
77     range_M[0] = 0;
78     i          = arg -> m;
79   } else {
80     range_M[0] = range_M[0];
81     i          = range_M[1] - range_M[0];
82   }
83 
84   num_cpu_m  = 0;
85 
86   while (i > 0){
87 
88     width  = blas_quickdivide(i + divM - num_cpu_m - 1, divM - num_cpu_m);
89 
90     i -= width;
91     if (i < 0) width = width + i;
92 
93     range_M[num_cpu_m + 1] = range_M[num_cpu_m] + width;
94 
95     num_cpu_m ++;
96   }
97 
98   if (!range_n) {
99     range_N[0] = 0;
100     i          = arg -> n;
101   } else {
102     range_N[0] = range_n[0];
103     i          = range_n[1] - range_n[0];
104   }
105 
106   num_cpu_n  = 0;
107 
108   while (i > 0){
109 
110     width  = blas_quickdivide(i + divN - num_cpu_n - 1, divN - num_cpu_n);
111 
112     i -= width;
113     if (i < 0) width = width + i;
114 
115     range_N[num_cpu_n + 1] = range_N[num_cpu_n] + width;
116 
117     num_cpu_n ++;
118   }
119 
120   procs = 0;
121 
122   for (j = 0; j < num_cpu_n; j++) {
123     for (i = 0; i < num_cpu_m; i++) {
124 
125     queue[procs].mode    = mode;
126     queue[procs].routine = function;
127     queue[procs].args    = arg;
128     queue[procs].range_m = &range_M[i];
129     queue[procs].range_n = &range_N[j];
130     queue[procs].sa      = NULL;
131     queue[procs].sb      = NULL;
132     queue[procs].next    = &queue[procs + 1];
133 
134     procs ++;
135     }
136   }
137 
138   if (procs) {
139     queue[0].sa = sa;
140     queue[0].sb = sb;
141 
142     queue[procs - 1].next = NULL;
143 
144     exec_blas(procs, queue);
145   }
146 
147   return 0;
148 }
149