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 
CNAME(int mode,blas_arg_t * arg,BLASLONG * range_m,BLASLONG * range_n,int (* function)(),void * sa,void * sb,BLASLONG divM,BLASLONG divN)43 int CNAME(int mode,
44 	  blas_arg_t *arg, BLASLONG *range_m, BLASLONG *range_n,
45 	  int (*function)(), void *sa, void *sb, BLASLONG divM, BLASLONG divN) {
46 
47   blas_queue_t queue[MAX_CPU_NUMBER];
48 
49   BLASLONG range_M[MAX_CPU_NUMBER + 1], range_N[MAX_CPU_NUMBER + 1];
50   BLASLONG procs, num_cpu_m, num_cpu_n;
51 
52   BLASLONG width, i, j;
53 
54   if (!range_m) {
55     range_M[0] = 0;
56     i          = arg -> m;
57   } else {
58     range_M[0] = range_M[0];
59     i          = range_M[1] - range_M[0];
60   }
61 
62   num_cpu_m  = 0;
63 
64   while (i > 0){
65 
66     width  = blas_quickdivide(i + divM - num_cpu_m - 1, divM - num_cpu_m);
67 
68     i -= width;
69     if (i < 0) width = width + i;
70 
71     range_M[num_cpu_m + 1] = range_M[num_cpu_m] + width;
72 
73     num_cpu_m ++;
74   }
75 
76   if (!range_n) {
77     range_N[0] = 0;
78     i          = arg -> n;
79   } else {
80     range_N[0] = range_n[0];
81     i          = range_n[1] - range_n[0];
82   }
83 
84   num_cpu_n  = 0;
85 
86   while (i > 0){
87 
88     width  = blas_quickdivide(i + divN - num_cpu_n - 1, divN - num_cpu_n);
89 
90     i -= width;
91     if (i < 0) width = width + i;
92 
93     range_N[num_cpu_n + 1] = range_N[num_cpu_n] + width;
94 
95     num_cpu_n ++;
96   }
97 
98   procs = 0;
99 
100   for (j = 0; j < num_cpu_n; j++) {
101     for (i = 0; i < num_cpu_m; i++) {
102 
103     queue[procs].mode    = mode;
104     queue[procs].routine = function;
105     queue[procs].args    = arg;
106     queue[procs].range_m = &range_M[i];
107     queue[procs].range_n = &range_N[j];
108     queue[procs].sa      = NULL;
109     queue[procs].sb      = NULL;
110     queue[procs].next    = &queue[procs + 1];
111 
112     procs ++;
113     }
114   }
115 
116   if (procs) {
117     queue[0].sa = sa;
118     queue[0].sb = sb;
119 
120     queue[procs - 1].next = NULL;
121 
122     exec_blas(procs, queue);
123   }
124 
125   return 0;
126 }
127 
128