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 <math.h>
42 #include "common.h"
43 
CNAME(int mode,blas_arg_t * arg,BLASLONG * range_m,BLASLONG * range_n,int (* function)(),void * sa,void * sb,BLASLONG nthreads)44 int CNAME(int mode, blas_arg_t *arg, BLASLONG *range_m, BLASLONG *range_n, int (*function)(), void *sa, void *sb, BLASLONG nthreads) {
45 
46   blas_queue_t queue[MAX_CPU_NUMBER];
47   BLASLONG range[MAX_CPU_NUMBER + 1];
48 
49   BLASLONG width, i;
50   BLASLONG n_from, n_to;
51   double dnum, nf, nt, di;
52 
53   int num_cpu;
54   int mask = 0;
55 
56   if (!(mode & BLAS_COMPLEX)) {
57 
58     switch (mode & BLAS_PREC) {
59     case BLAS_SINGLE:
60       mask = MAX(SGEMM_UNROLL_M, SGEMM_UNROLL_N) - 1;
61       break;
62     case BLAS_DOUBLE:
63       mask = MAX(DGEMM_UNROLL_M, DGEMM_UNROLL_N) - 1;
64       break;
65 #ifdef EXPRECISION
66     case BLAS_XDOUBLE:
67       mask = MAX(QGEMM_UNROLL_M, QGEMM_UNROLL_N) - 1;
68       break;
69 #endif
70     }
71   } else {
72     switch (mode & BLAS_PREC) {
73     case BLAS_SINGLE:
74       mask = MAX(CGEMM_UNROLL_M, CGEMM_UNROLL_N) - 1;
75       break;
76     case BLAS_DOUBLE:
77       mask = MAX(ZGEMM_UNROLL_M, ZGEMM_UNROLL_N) - 1;
78       break;
79 #ifdef EXPRECISION
80     case BLAS_XDOUBLE:
81       mask = MAX(XGEMM_UNROLL_M, XGEMM_UNROLL_N) - 1;
82       break;
83 #endif
84     }
85   }
86 
87   n_from = 0;
88   n_to   = arg -> n;
89 
90   if (range_n) {
91     n_from = *(range_n + 0);
92     n_to   = *(range_n + 1);
93   }
94 
95   if (!(mode & BLAS_UPLO)) {
96 
97     nf = (double)(n_from);
98     nt = (double)(n_to);
99 
100     dnum = (nt * nt - nf * nf) / (double)nthreads;
101 
102     num_cpu  = 0;
103 
104     range[0] = n_from;
105     i        = n_from;
106 
107     while (i < n_to){
108 
109       if (nthreads - num_cpu > 1) {
110 
111 	di = (double)i;
112 	width = ((BLASLONG)( sqrt(di * di + dnum) - di) + mask) & ~mask;
113 
114 	if ((width <= 0) || (width > n_to - i)) width = n_to - i;
115 
116       } else {
117 	width = n_to - i;
118       }
119 
120       range[num_cpu + 1] = range[num_cpu] + width;
121 
122       queue[num_cpu].mode    = mode;
123       queue[num_cpu].routine = function;
124       queue[num_cpu].args    = arg;
125       queue[num_cpu].range_m = range_m;
126       queue[num_cpu].range_n = &range[num_cpu];
127       queue[num_cpu].sa      = NULL;
128       queue[num_cpu].sb      = NULL;
129       queue[num_cpu].next    = &queue[num_cpu + 1];
130 
131       num_cpu ++;
132       i += width;
133     }
134 
135   } else {
136 
137     nf = (double)(arg -> n - n_from);
138     nt = (double)(arg -> n - n_to);
139 
140     dnum = (nt * nt - nf * nf) / (double)nthreads;
141 
142     num_cpu  = 0;
143 
144     range[0] = n_from;
145     i        = n_from;
146 
147     while (i < n_to){
148 
149       if (nthreads - num_cpu > 1) {
150 
151 	di = (double)(arg -> n - i);
152 	width = ((BLASLONG)(-sqrt(di * di + dnum) + di) + mask) & ~mask;
153 
154 	if ((width <= 0) || (width > n_to - i)) width = n_to - i;
155 
156       } else {
157 	width = n_to - i;
158       }
159 
160       range[num_cpu + 1] = range[num_cpu] + width;
161 
162       queue[num_cpu].mode    = mode;
163       queue[num_cpu].routine = function;
164       queue[num_cpu].args    = arg;
165       queue[num_cpu].range_m = range_m;
166       queue[num_cpu].range_n = &range[num_cpu];
167       queue[num_cpu].sa      = NULL;
168       queue[num_cpu].sb      = NULL;
169       queue[num_cpu].next    = &queue[num_cpu + 1];
170 
171       num_cpu ++;
172       i += width;
173     }
174 
175   }
176 
177   if (num_cpu) {
178     queue[0].sa = sa;
179     queue[0].sb = sb;
180     queue[num_cpu - 1].next = NULL;
181 
182     exec_blas(num_cpu, queue);
183   }
184 
185   return 0;
186 }
187